Skip to content

Commit

Permalink
Back to development: 0.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed May 15, 2024
1 parent 1451380 commit 3cd216d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cookieplone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Plone Foundation <[email protected]>
#
# SPDX-License-Identifier: MIT
__version__ = "0.5.2"
__version__ = "0.5.3.dev0"
23 changes: 14 additions & 9 deletions cookieplone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions news/18.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix parsing of extra_context via cli [@ericof]
45 changes: 45 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3cd216d

Please sign in to comment.