Skip to content

Commit

Permalink
fix(app): get_arg_or_config gets environment correctly
Browse files Browse the repository at this point in the history
Previously, if a value was in the passed Namespace, it would return
that. The correct behaviour however is to check the environment for
the value and return that instead.
  • Loading branch information
lengau committed Sep 18, 2024
1 parent 77a3582 commit b7bae82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,10 @@ def get_arg_or_config(
:param item: the name of the namespace or config item.
:returns: the requested value.
"""
return getattr(parsed_args, item, self.services.config.get(item))
arg_value = getattr(parsed_args, item, None)
if arg_value is not None:
return arg_value
return self.services.config.get(item)

def _run_inner(self) -> int:
"""Actual run implementation."""
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,38 @@ def test_run_managed_empty_plan(app, fake_project):
app.run_managed(None, None)


@pytest.mark.parametrize(
("parsed_args", "environ", "item", "expected"),
[
(argparse.Namespace(), {}, "build_for", None),
(argparse.Namespace(build_for=None), {}, "build_for", None),
(
argparse.Namespace(build_for=None),
{"CRAFT_BUILD_FOR": "arm64"},
"build_for",
"arm64",
),
(
argparse.Namespace(build_for=None),
{"TESTCRAFT_BUILD_FOR": "arm64"},
"build_for",
"arm64",
),
(
argparse.Namespace(build_for="riscv64"),
{"TESTCRAFT_BUILD_FOR": "arm64"},
"build_for",
"riscv64",
),
],
)
def test_get_arg_or_config(monkeypatch, app, parsed_args, environ, item, expected):
for var, content in environ.items():
monkeypatch.setenv(var, content)

assert app.get_arg_or_config(parsed_args, item) == expected


@pytest.mark.parametrize(
("managed", "error", "exit_code", "message"),
[
Expand Down

0 comments on commit b7bae82

Please sign in to comment.