Skip to content

Commit

Permalink
Fix a fatal error in the commands to create and update an extension's…
Browse files Browse the repository at this point in the history
… translations
  • Loading branch information
bartfeenstra committed Dec 20, 2024
1 parent d13531c commit 5cc02f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions betty/cli/commands/extension_new_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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.locale.translation import assert_extension_has_assets_directory_path
from betty.plugin import ShorthandPluginBase
from betty.project import extension

Expand Down Expand Up @@ -53,7 +53,7 @@ async def click_command(self) -> click.Command:
"extension",
required=True,
callback=parameter_callback(
lambda extension_id: assert_extension_assets_directory_path(
lambda extension_id: assert_extension_has_assets_directory_path(
extension_id_to_type_map.get(extension_id)
)
),
Expand Down
4 changes: 2 additions & 2 deletions betty/cli/commands/extension_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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.locale.translation import assert_extension_has_assets_directory_path
from betty.plugin import ShorthandPluginBase
from betty.project import extension

Expand Down Expand Up @@ -59,7 +59,7 @@ async def click_command(self) -> click.Command:
"extension",
required=True,
callback=parameter_callback(
lambda extension_id: assert_extension_assets_directory_path(
lambda extension_id: assert_extension_has_assets_directory_path(
extension_id_to_type_mapping.get(extension_id)
)
),
Expand Down
17 changes: 15 additions & 2 deletions betty/locale/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, TypeVar

from aiofiles.os import makedirs
from aiofiles.ospath import exists
Expand All @@ -18,11 +18,14 @@
from betty.locale import get_data
from betty.locale.babel import run_babel
from betty.locale.localizable import _
from betty.project.extension import Extension

if TYPE_CHECKING:
from collections.abc import Iterable
from betty.project import Project
from betty.project.extension import Extension


ExtensionT = TypeVar("ExtensionT", bound=Extension)


def assert_extension_assets_directory_path(extension: type[Extension]) -> Path:
Expand All @@ -39,6 +42,16 @@ def assert_extension_assets_directory_path(extension: type[Extension]) -> Path:
return assets_directory_path


def assert_extension_has_assets_directory_path(
extension: type[ExtensionT],
) -> type[ExtensionT]:
"""
Check that the given extension has an assets directory, and return it.
"""
assert_extension_assets_directory_path(extension)
return extension


async def new_extension_translation(locale: str, extension: type[Extension]) -> None:
"""
Create a new translation for the given extension.
Expand Down
21 changes: 21 additions & 0 deletions betty/tests/locale/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from betty.locale.translation import (
update_dev_translations,
assert_extension_assets_directory_path,
assert_extension_has_assets_directory_path,
)
from betty.test_utils.locale import PotFileTestBase
from betty.test_utils.project.extension import DummyExtension
Expand All @@ -35,6 +36,26 @@ def test_with_assets_directory(self) -> None:
)


class TestAssertExtensionHasAssetsDirectoryPath:
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_has_assets_directory_path(DummyExtension)

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


class TestPotFile(PotFileTestBase):
@override
def assets_directory_path(self) -> Path:
Expand Down

0 comments on commit 5cc02f2

Please sign in to comment.