diff --git a/cookieplone/__init__.py b/cookieplone/__init__.py index ef7bd9c..620b94b 100644 --- a/cookieplone/__init__.py +++ b/cookieplone/__init__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2024-present Plone Foundation # # SPDX-License-Identifier: MIT -__version__ = "0.5.2" +__version__ = "0.5.3.dev0" diff --git a/cookieplone/cli.py b/cookieplone/cli.py index ff85616..03b349c 100644 --- a/cookieplone/cli.py +++ b/cookieplone/cli.py @@ -19,19 +19,24 @@ from cookieplone.utils import console, internal -def validate_extra_context(value: list[str] | None = None): +def validate_extra_context(value: list[str] | None = None) -> list[str]: """Validate extra content follows the correct pattern.""" if not value: - return {} - for string in value: - if "=" not in string: + return [] + for item in value: + if "=" not in item: raise typer.BadParameter( f"EXTRA_CONTEXT should contain items of the form key=value; " - f"'{string}' doesn't match that form" + f"'{item}' doesn't match that form" ) - # Convert list -- e.g.: ['program_name=foobar', 'startsecs=66'] - # to dict -- e.g.: {'program_name': 'foobar', 'startsecs': '66'} - return dict([s.split("=", 1) for s in value]) + return value + + +def parse_extra_content(value: list[str]) -> dict: + """Parse extra content and return a dictionary with options.""" + if not value: + return {} + return dict([s.split("=") for s in value]) def prompt_for_template(base_path: Path) -> str: @@ -132,7 +137,7 @@ def cli( output_dir = Path().cwd() configure_logger(stream_level="DEBUG" if verbose else "INFO", debug_file=debug_file) # Annotate extra_context - extra_context = extra_context if extra_context else {} + extra_context = parse_extra_content(extra_context) extra_context["__generator_signature"] = internal.signature_md(repo_path) # Run generator try: diff --git a/news/18.bugfix b/news/18.bugfix new file mode 100644 index 0000000..f0bb681 --- /dev/null +++ b/news/18.bugfix @@ -0,0 +1 @@ +Fix parsing of extra_context via cli [@ericof] diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..6aeda2d --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,45 @@ +import pytest +from click.exceptions import BadParameter + +from cookieplone import cli + + +@pytest.mark.parametrize( + "value,expected", + [ + ([], {}), + (["foo=bar", "bar=bar"], {"foo": "bar", "bar": "bar"}), + (["foo=1", "bar=2"], {"foo": "1", "bar": "2"}), + ], +) +def test_parse_extra_content(value: list[str], expected: dict): + func = cli.parse_extra_content + assert func(value) == expected + + +@pytest.mark.parametrize( + "value,expected", + [ + (None, []), + ([], []), + (["foo=bar", "bar=bar"], ["foo=bar", "bar=bar"]), + (["foo=1", "bar=2"], ["foo=1", "bar=2"]), + ], +) +def test_validate_extra_context_pass(value: list[str] | None, expected: list): + func = cli.validate_extra_context + assert func(value) == expected + + +@pytest.mark.parametrize( + "value,expected", + [ + (["foo=bar", "bar2"], "bar2"), + (["foo-1", "bar=2"], "foo-1"), + ], +) +def test_validate_extra_context_fail(value: list[str] | None, expected: str): + func = cli.validate_extra_context + with pytest.raises(BadParameter) as exc: + func(value) + assert expected in str(exc)