Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --verbose flag to pass -vv to spread from charmcraft test #1988

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions charmcraft/application/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@


class TestCommand(base.CharmcraftCommand):
"""Initialize a directory to be a charm project."""
"""Run charm tests with spread."""

name = "test"
help_msg = "Execute charm test suites"
help_msg = "Execute charm test suites with spread"
overview = _overview
common = True

Expand Down Expand Up @@ -67,6 +67,12 @@ def fill_parser(self, parser: argparse.ArgumentParser) -> None:
help="Just show the list of jobs that would run.",
)

parser.add_argument(
"--verbose",
action="store_true",
help="Run spread in verbose mode.",
)

parser.add_argument(
"spread_tasks",
metavar="tasks",
Expand All @@ -84,6 +90,9 @@ def run(self, parsed_args: argparse.Namespace):
else:
cmd = ["spread"]

if vars(parsed_args).get("verbose"):
Copy link
Collaborator

@lengau lengau Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if vars(parsed_args).get("verbose"):
if emit.get_mode().value >= EmitterMode.VERBOSE.value:

(You'll need to import EmitterMode from craft_cli above, but then you can remove the add_argument too)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add tests? This unit test should be fine in tests/unit/commands/test_test.py:

# Copyright 2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For further info, check https://github.com/canonical/charmcraft
"""Unit tests for the test command."""


import argparse
from craft_cli import EmitterMode, emit
import pytest

from charmcraft.application.commands.test import TestCommand
from charmcraft.application.main import APP_METADATA


@pytest.mark.parametrize("emitter_mode", [EmitterMode.VERBOSE, EmitterMode.DEBUG, EmitterMode.TRACE])
def test_spread_verbose(fp, capsys, service_factory, emitter_mode: EmitterMode) -> None:
    emit._mode = emitter_mode
    fp.register(["spread", "-vv", "wololo"])
    cmd = TestCommand({"app": APP_METADATA, "services": service_factory})
    args = argparse.Namespace(spread_tasks=["wololo"])

    cmd.run(args)


@pytest.mark.parametrize("emitter_mode", [EmitterMode.QUIET, EmitterMode.BRIEF])
def test_spread_not_verbose(fp, capsys, service_factory, emitter_mode: EmitterMode) -> None:
    emit._mode = emitter_mode
    fp.register(["spread", "wololo"])
    cmd = TestCommand({"app": APP_METADATA, "services": service_factory})
    args = argparse.Namespace(spread_tasks=["wololo"])

    cmd.run(args)

cmd.append("-vv")

for arg in ("shell", "shell-after", "debug", "list"):
if vars(parsed_args).get(arg):
cmd.append("-" + arg)
Expand Down
Loading