Skip to content

Commit

Permalink
feat(reactive): run charm build in verbose mode (#2021)
Browse files Browse the repository at this point in the history
While a user could previously have added --verbose to reactive-charm-build-arguments, this automatically adds --verbose if the user hasn't specified a log level.

The verbose text gets handled by craft-cli anyway, but this changes the default so the text gets added to the log.

Spread test failures are unrelated: #2023
  • Loading branch information
lengau authored Dec 11, 2024
2 parents 0215ac0 + 35414e4 commit 7129d57
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
28 changes: 24 additions & 4 deletions charmcraft/parts/plugins/_reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from craft_parts import plugins
from craft_parts.errors import PluginEnvironmentValidationError

VERBOSITY_PARAMS = frozenset({"-v", "--verbose", "--debug", "-l", "--log-level"})


class ReactivePluginProperties(plugins.PluginProperties, frozen=True):
"""Properties used to pack reactive charms using charm-tools."""
Expand Down Expand Up @@ -156,6 +158,27 @@ def run_charm_tool(args: list[str]):
)


def _get_charm_build_command(charm_build_arguments: list[str], build_dir: Path):
"""Get a charm build command based on arguments."""
cmd = ["charm", "build"]

# If the user doesn't pass a verbosity, we want to make it verbose.
if not VERBOSITY_PARAMS & set(charm_build_arguments):
# Check for things like -ldebug or --log-level=debug
for argument in charm_build_arguments:
if argument.startswith("-l") or argument.startswith("--log-level"):
break
else:
cmd.append("--verbose")

if charm_build_arguments:
cmd.extend(charm_build_arguments)

cmd.extend(["-o", str(build_dir)])

return cmd


def build(
*,
charm_name: str,
Expand Down Expand Up @@ -193,10 +216,7 @@ def build(
if not charm_build_dir.exists():
charm_build_dir.symlink_to(install_dir, target_is_directory=True)

cmd = ["charm", "build"]
if charm_build_arguments:
cmd.extend(charm_build_arguments)
cmd.extend(["-o", str(build_dir)])
cmd = _get_charm_build_command(charm_build_arguments, build_dir)

try:
run_charm_tool(cmd)
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/parts/plugins/test_reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,71 @@ def fake_run():
patcher.stop()


@pytest.mark.parametrize(
("arguments", "build_dir", "expected"),
[
pytest.param(
[],
pathlib.Path("some-install-dir"),
["charm", "build", "--verbose", "-o", "some-install-dir"],
id="default",
),
pytest.param(
["-c"],
pathlib.Path("some-install-dir"),
["charm", "build", "--verbose", "-c", "-o", "some-install-dir"],
id="unrelated-param",
),
pytest.param(
["--verbose"],
pathlib.Path("some-install-dir"),
["charm", "build", "--verbose", "-o", "some-install-dir"],
id="--verbose",
),
pytest.param(
["-v"],
pathlib.Path("some-install-dir"),
["charm", "build", "-v", "-o", "some-install-dir"],
id="-v",
),
pytest.param(
["--debug"],
pathlib.Path("some-install-dir"),
["charm", "build", "--debug", "-o", "some-install-dir"],
id="--debug",
),
pytest.param(
["-l", "debug"],
pathlib.Path("some-install-dir"),
["charm", "build", "-l", "debug", "-o", "some-install-dir"],
id="-l debug",
),
pytest.param(
["--log-level", "debug"],
pathlib.Path("some-install-dir"),
["charm", "build", "--log-level", "debug", "-o", "some-install-dir"],
id="--log-level debug",
),
pytest.param(
["-ldebug"],
pathlib.Path("some-install-dir"),
["charm", "build", "-ldebug", "-o", "some-install-dir"],
id="-ldebug",
),
pytest.param(
["--log-level=debug"],
pathlib.Path("some-install-dir"),
["charm", "build", "--log-level=debug", "-o", "some-install-dir"],
id="--log-level=debug",
),
],
)
def test_get_charm_build_command(
arguments: list[str], build_dir: pathlib.Path, expected: list[str]
):
assert _reactive._get_charm_build_command(arguments, build_dir) == expected


def test_build(build_dir, install_dir, fake_run):
fake_run.return_value = CompletedProcess(("charm", "build"), 0)
returncode = _reactive.build(
Expand All @@ -197,6 +262,7 @@ def test_build(build_dir, install_dir, fake_run):
[
"charm",
"build",
"--verbose",
"--charm-argument",
"--charm-argument-with",
"argument",
Expand Down Expand Up @@ -245,6 +311,7 @@ def test_build_charm_proof_raises_warning_messages_does_not_raise(
[
"charm",
"build",
"--verbose",
"--charm-argument",
"--charm-argument-with",
"argument",
Expand Down Expand Up @@ -285,6 +352,7 @@ def _run_generator():
[
"charm",
"build",
"--verbose",
"--charm-argument",
"--charm-argument-with",
"argument",
Expand Down Expand Up @@ -334,6 +402,7 @@ def _run_generator():
[
"charm",
"build",
"--verbose",
"--charm-argument",
"--charm-argument-with",
"argument",
Expand Down

0 comments on commit 7129d57

Please sign in to comment.