Skip to content

Commit

Permalink
Ensure that the CLI exits with code 2 for bad invocations (#2138)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Oct 15, 2024
1 parent abaf43e commit 728536c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
7 changes: 6 additions & 1 deletion betty/cli/commands/extension_new_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from betty.cli.commands import command, parameter_callback, Command
from betty.locale import translation
from betty.locale.localizable import _
from betty.locale.translation import assert_extension_assets_directory_path
from betty.plugin import ShorthandPluginBase
from betty.project import extension

Expand Down Expand Up @@ -51,7 +52,11 @@ async def click_command(self) -> click.Command:
@click.argument(
"extension",
required=True,
callback=parameter_callback(extension_id_to_type_map.get),
callback=parameter_callback(
lambda extension_id: assert_extension_assets_directory_path(
extension_id_to_type_map.get(extension_id)
)
),
)
@click.argument(
"locale", required=True, callback=parameter_callback(assert_locale())
Expand Down
7 changes: 6 additions & 1 deletion betty/cli/commands/extension_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from betty.cli.commands import command, Command, parameter_callback
from betty.locale import translation
from betty.locale.localizable import _
from betty.locale.translation import assert_extension_assets_directory_path
from betty.plugin import ShorthandPluginBase
from betty.project import extension

Expand Down Expand Up @@ -57,7 +58,11 @@ async def click_command(self) -> click.Command:
@click.argument(
"extension",
required=True,
callback=parameter_callback(extension_id_to_type_map.get),
callback=parameter_callback(
lambda extension_id: assert_extension_assets_directory_path(
extension_id_to_type_map.get(extension_id)
)
),
)
@click.argument(
"source",
Expand Down
9 changes: 6 additions & 3 deletions betty/locale/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
from betty.project.extension import Extension


def _assert_extension_assets_directory_path(extension: type[Extension]) -> Path:
def assert_extension_assets_directory_path(extension: type[Extension]) -> Path:
"""
Check that the given extension has an assets directory, and return its path.
"""
assets_directory_path = extension.assets_directory_path()
if assets_directory_path is None:
raise UserFacingError(
Expand All @@ -40,7 +43,7 @@ async def new_extension_translation(locale: str, extension: type[Extension]) ->
"""
Create a new translation for the given extension.
"""
await _new_translation(locale, _assert_extension_assets_directory_path(extension))
await _new_translation(locale, assert_extension_assets_directory_path(extension))


async def new_project_translation(locale: str, project: Project) -> None:
Expand Down Expand Up @@ -105,7 +108,7 @@ async def update_extension_translations(
source_file_paths = set()
await _update_translations(
source_file_paths,
_assert_extension_assets_directory_path(extension),
assert_extension_assets_directory_path(extension),
_output_assets_directory_path_override,
)

Expand Down
2 changes: 1 addition & 1 deletion betty/tests/cli/commands/test_extension_new_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def test_click_command_with_extension_without_assets_directory(
"extension-new-translation",
"without-assets",
"nl-NL",
expected_exit_code=1,
expected_exit_code=2,
)

async def test_click_command_with_invalid_locale(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_click_command_with_extension_without_assets_directory(
"extension-update-translations",
"without-assets",
str(source),
expected_exit_code=1,
expected_exit_code=2,
)

async def test_click_command_with_invalid_source_directory(
Expand Down
34 changes: 29 additions & 5 deletions betty/tests/locale/test_translation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from pathlib import Path

import pytest
from typing_extensions import override

from betty.error import UserFacingError
from betty.fs import ASSETS_DIRECTORY_PATH
from betty.locale.translation import update_dev_translations
from betty.locale.translation import (
update_dev_translations,
assert_extension_assets_directory_path,
)
from betty.test_utils.locale import PotFileTestBase
from typing_extensions import override
from betty.test_utils.project.extension import DummyExtension


if TYPE_CHECKING:
from pathlib import Path
class TestAssertExtensionAssetsDirectoryPath:
class _DummyExtensionWithAssetsDirectory(DummyExtension):
@override
@classmethod
def assets_directory_path(cls) -> Path | None:
return Path(__file__)

def test_without_assets_directory(self) -> None:
with pytest.raises(UserFacingError):
assert_extension_assets_directory_path(DummyExtension)

def test_with_assets_directory(self) -> None:
assert (
assert_extension_assets_directory_path(
self._DummyExtensionWithAssetsDirectory
)
== self._DummyExtensionWithAssetsDirectory.assets_directory_path()
)


class TestPotFile(PotFileTestBase):
Expand Down

0 comments on commit 728536c

Please sign in to comment.