Skip to content

Commit

Permalink
Added a verbosity flag -v. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain MARIE committed May 17, 2021
1 parent 1f93ca9 commit 565398c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
26 changes: 18 additions & 8 deletions genbadge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

INFILE_XML_HELP = "An alternate test results XML file to read. '-' is supported and means <stdin>."
OUTFILE_BADGE_HELP = ("An alternate SVG badge file to write to. '-' is supported and means <stdout>. Note that in this "
"case no other message will be printed to <stdout>.")
"case no other message will be printed to <stdout>. In particular the verbose flag will have no "
"effect.")
SHIELDS_HELP = ("Indicates if badges should be generated using the shields.io HTTP API (default) or the local SVG file "
"template included.")
VERBOSE_HELP = ("Use this flag to print details to stdout during the badge generation process. Note that this flag has"
" no effect when '-' is used as output, since the badge is written to <stdout>.")


@click.group()
Expand All @@ -42,12 +45,13 @@ def genbadge():
help="An optional success percentage threshold to use. The command will fail with exit code 1 if the"
"actual success percentage is strictly less than the provided value.")
@click.option('-w/-l', '--webshields/--local', type=bool, default=True, help=SHIELDS_HELP)
# TODO -f --format
@click.option('-v', '--verbose', type=bool, default=False, is_flag=True, help=VERBOSE_HELP)
def gen_tests_badge(
input_file=None,
output_file=None,
threshold=None,
webshields=None
webshields=None,
verbose=None
):
"""
This command generates a badge for the test results, from an XML file in the
Expand All @@ -58,6 +62,9 @@ def gen_tests_badge(
the output file is `./tests-badge.svg`. You can change these settings with
the `-i/--input_file` and `-o/--output-file` options.
You can use the verbose flag `-v/--verbose` to display information on the
input file contents, for verification.
The resulting badge will by default look like this: [tests | 6/12]
where 6 is the number of tests that have run successfully, and 12 is the
total number of tests minus the number of skipped tests. You can change the
Expand All @@ -78,8 +85,7 @@ def gen_tests_badge(
except FileNotFoundError:
raise click.exceptions.FileError(input_file, hint="File not found")

# TODO if verbose
if not is_stdout:
if verbose and not is_stdout:
click.echo("""Test statistics parsed successfully from %r
- Nb tests: Total (%s) = Success (%s) + Skipped (%s) + Failed (%s) + Errors (%s)
- Success percentage: %.2f%% (%s / %s) (Skipped tests are excluded)
Expand Down Expand Up @@ -113,10 +119,12 @@ def gen_tests_badge(
@click.option('-i', '--input-file', type=click.File('rt'), help=INFILE_XML_HELP)
@click.option('-o', '--output-file', type=click.File('wt'), help=OUTFILE_BADGE_HELP)
@click.option('-w/-l', '--webshields/--local', type=bool, default=True, help=SHIELDS_HELP)
@click.option('-v', '--verbose', type=bool, default=False, is_flag=True, help=VERBOSE_HELP)
def gen_coverage_badge(
input_file=None,
output_file=None,
webshields=None
webshields=None,
verbose=None
):
"""
This command generates a badge for the coverage results, from an XML file in
Expand All @@ -127,6 +135,9 @@ def gen_coverage_badge(
and the output file is `./coverage-badge.svg`. You can change these settings
with the `-i/--input_file` and `-o/--output-file` options.
You can use the verbose flag `-v/--verbose` to display information on the
input file contents, for verification.
The resulting badge will by default look like this: [coverage | 98.1%] where
98.1 is the total coverage, obtained from the branch and line coverages
using the formula
Expand All @@ -145,8 +156,7 @@ def gen_coverage_badge(
except FileNotFoundError:
raise click.exceptions.FileError(input_file, hint="File not found")

# TODO if verbose
if not is_stdout:
if verbose and not is_stdout:
click.echo("""Coverage results parsed successfully from %(ifp)r
- Branch coverage: %(bcp).2f%% (%(bc)s/%(bv)s)
- Line coverage: %(lcp).2f%% (%(lc)s/%(lv)s)
Expand Down
50 changes: 39 additions & 11 deletions genbadge/tests/test_readme_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class CmdReference:
"""
Container class used to store our test reference for all commands
"""
def __init__(self, name, default_infile, default_outfile, example_input_file, example_output_msg, help_msg):
def __init__(self, name, default_infile, default_outfile, example_input_file, example_output_msg,
example_output_msg_long, help_msg):
self.name = name
self.default_infile = default_infile
self.default_outfile = default_outfile
self.example_input_file = example_input_file
self.example_output_msg = example_output_msg
self.example_output_msg_long = example_output_msg_long
self.help_msg = help_msg

def __str__(self):
Expand All @@ -40,7 +42,8 @@ def __str__(self):
default_infile="reports/junit/junit.xml",
default_outfile = "tests-badge.svg",
example_input_file=(TESTS_FOLDER / "reports" / "junit" / "junit.xml").as_posix(),
example_output_msg="""
example_output_msg="SUCCESS - Tests badge created: %r\n",
example_output_msg_long="""
Test statistics parsed successfully from %r
- Nb tests: Total (6) = Success (2) + Skipped (1) + Failed (2) + Errors (1)
- Success percentage: 40.00%% (2 / 5) (Skipped tests are excluded)
Expand All @@ -57,6 +60,9 @@ def __str__(self):
output file is `./tests-badge.svg`. You can change these settings with the
`-i/--input_file` and `-o/--output-file` options.
You can use the verbose flag `-v/--verbose` to display information on the
input file contents, for verification.
The resulting badge will by default look like this: [tests | 6/12] where 6 is
the number of tests that have run successfully, and 12 is the total number of
tests minus the number of skipped tests. You can change the appearance of the
Expand All @@ -73,14 +79,20 @@ def __str__(self):
-o, --output-file FILENAME An alternate SVG badge file to write to. '-'
is supported and means <stdout>. Note that in
this case no other message will be printed to
<stdout>.
<stdout>. In particular the verbose flag will
have no effect.
-t, --threshold FLOAT An optional success percentage threshold to
use. The command will fail with exit code 1 if
theactual success percentage is strictly less
than the provided value.
-w, --webshields / -l, --local Indicates if badges should be generated using
the shields.io HTTP API (default) or the local
SVG file template included.
-v, --verbose Use this flag to print details to stdout
during the badge generation process. Note that
this flag has no effect when '-' is used as
output, since the badge is written to
<stdout>.
--help Show this message and exit.
"""
)
Expand All @@ -89,7 +101,8 @@ def __str__(self):
default_infile="reports/coverage/coverage.xml",
default_outfile = "coverage-badge.svg",
example_input_file=(TESTS_FOLDER / "reports" / "coverage" / "coverage.xml").as_posix(),
example_output_msg="""
example_output_msg="SUCCESS - Coverage badge created: %r\n",
example_output_msg_long="""
Coverage results parsed successfully from %r
- Branch coverage: 5.56%% (1/18)
- Line coverage: 17.81%% (13/73)
Expand All @@ -107,6 +120,9 @@ def __str__(self):
and the output file is `./coverage-badge.svg`. You can change these settings
with the `-i/--input_file` and `-o/--output-file` options.
You can use the verbose flag `-v/--verbose` to display information on the
input file contents, for verification.
The resulting badge will by default look like this: [coverage | 98.1%] where
98.1 is the total coverage, obtained from the branch and line coverages using
the formula
Expand All @@ -121,10 +137,16 @@ def __str__(self):
-o, --output-file FILENAME An alternate SVG badge file to write to. '-'
is supported and means <stdout>. Note that in
this case no other message will be printed to
<stdout>.
<stdout>. In particular the verbose flag will
have no effect.
-w, --webshields / -l, --local Indicates if badges should be generated using
the shields.io HTTP API (default) or the local
SVG file template included.
-v, --verbose Use this flag to print details to stdout
during the badge generation process. Note that
this flag has no effect when '-' is used as
output, since the badge is written to
<stdout>.
--help Show this message and exit.
"""
)
Expand Down Expand Up @@ -214,9 +236,10 @@ def test_file_not_found(monkeypatch, tmpdir, cmd):


@pytest.mark.parametrize("outstream", [False, True], ids="outstream={}".format)
@pytest.mark.parametrize("verbose", [False, True], ids="verbose={}".format)
@pytest.mark.parametrize("variant", ["default", "custom", "custom_shortargs", "custom_absolute"])
@pytest.mark.parametrize("cmd", ALL_COMMANDS, ids=str)
def test_any_command(monkeypatch, cmd, tmpdir, variant, outstream):
def test_any_command(monkeypatch, cmd, tmpdir, variant, outstream, verbose):
"""Test that `genbadge <cmd>` works consistently concerning the ios and output messages"""

# from pytest path to pathlib path
Expand All @@ -227,6 +250,8 @@ def test_any_command(monkeypatch, cmd, tmpdir, variant, outstream):

# create the various arguments. Use local template for faster exec
args = [cmd.name, "-l"]
if verbose:
args.append("--verbose")
if variant == "default":
if outstream:
pytest.skip("this test does not make sense")
Expand Down Expand Up @@ -258,7 +283,10 @@ def test_any_command(monkeypatch, cmd, tmpdir, variant, outstream):

# verify the output message
if not outstream:
assert "\n" + result.output == cmd.example_output_msg % (infile_path_for_msg, outfile_path_for_msg)
if verbose:
assert "\n" + result.output == cmd.example_output_msg_long % (infile_path_for_msg, outfile_path_for_msg)
else:
assert result.output == cmd.example_output_msg % outfile_path_for_msg
assert outfile.exists()
else:
assert result.output.startswith('<svg xmlns="http://www.w3.org/2000/svg" '
Expand All @@ -274,7 +302,7 @@ def test_threshold(threshold, shortarg, tmpdir):
badge_path = destfolder / "tests-badge.svg"

# define cli args (explicit input so that we do not fall into the python 2 issue with CliRunner IOError
args = ["tests", "-i", str(TEST_CMD.example_input_file), "-o", "%s" % badge_path]
args = ["tests", "-v", "-i", str(TEST_CMD.example_input_file), "-o", "%s" % badge_path]
args += ["-t" if shortarg else "--threshold", str(threshold)]

# execute "genbadge tests" with the appropriate arguments
Expand All @@ -298,7 +326,7 @@ def test_threshold(threshold, shortarg, tmpdir):
assert result.exit_code == 0

# verify the output message
assert "\n" + result.output == TEST_CMD.example_output_msg % (str(TEST_CMD.example_input_file), str(badge_path.as_posix()))
assert "\n" + result.output == TEST_CMD.example_output_msg_long % (str(TEST_CMD.example_input_file), str(badge_path.as_posix()))

assert badge_path.exists()

Expand All @@ -316,7 +344,7 @@ def test_local_remote(use_shields, shortarg, tmpdir):
badge_path = destfolder / "tests-badge.svg"

# define cli args (explicit input so that we do not fall into the python 2 issue with CliRunner IOError
args = ["tests", "-i", str(TEST_CMD.example_input_file), "-o", "%s" % badge_path]
args = ["tests", "-v", "-i", str(TEST_CMD.example_input_file), "-o", "%s" % badge_path]
if use_shields is False:
args.append("-l" if shortarg else "--local")
if use_shields is True:
Expand All @@ -327,7 +355,7 @@ def test_local_remote(use_shields, shortarg, tmpdir):
assert result.exit_code == 0

# verify the output message
assert "\n" + result.output == TEST_CMD.example_output_msg % (str(TEST_CMD.example_input_file), str(badge_path.as_posix()))
assert "\n" + result.output == TEST_CMD.example_output_msg_long % (str(TEST_CMD.example_input_file), str(badge_path.as_posix()))

assert badge_path.exists()

Expand Down

0 comments on commit 565398c

Please sign in to comment.