Skip to content

Commit

Permalink
Move CLI validation and check all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacetown committed Apr 16, 2024
1 parent d5a21f4 commit 8f5b81b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
32 changes: 23 additions & 9 deletions gcovr/formats/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,34 +290,48 @@ def validate_options(self) -> None:
"--html-details and --html-nested can not be used together."
)

potential_html_output = (
(self.options.html and self.options.html.value)
or (self.options.html_details and self.options.html_details.value)
or (self.options.html_nested and self.options.html_nested.value)
or (self.options.output and self.options.output.value)
)
if self.options.html_details and not potential_html_output:
html_output = None
if self.options.html and self.options.html.value:
html_output = self.options.html.value
elif self.options.html_details and self.options.html_details.value:
html_output = self.options.html_details.value
elif self.options.html_nested and self.options.html_nested.value:
html_output = self.options.html_nested.value
elif self.options.output and self.options.output.value:
html_output = self.options.output.value

if self.options.html_details and not html_output:
raise RuntimeError(
"a named output must be given, if the option --html-details is used."
)

if self.options.html_nested and not potential_html_output:
if self.options.html_nested and not html_output:
raise RuntimeError(
"a named output must be given, if the option --html-nested is used."
)

if html_output == "-" and not self.options.html_single_page and (
self.options.html_details or self.options.html_nested
):
raise RuntimeError("detailed reports can only be printed to STDOUT as --html-single-page.")

if self.options.html_single_page and not (
self.options.html_details or self.options.html_nested
):
raise RuntimeError(
"option --html-details or --html-nested is needed, if the option --html-single-page is used."
)

if self.options.html_self_contained is False and not potential_html_output:
if self.options.html_self_contained is False and not html_output:
raise RuntimeError(
"can only disable --html-self-contained when a named output is given."
)

if self.options.html_self_contained is False and html_output == "-" and not self.options.html_single_page:
raise RuntimeError(
"only self contained reports can be printed to STDOUT"
)

def write_report(self, covdata: CovData, output_file: str) -> None:
from .write import write_report

Expand Down
9 changes: 1 addition & 8 deletions gcovr/formats/html/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,6 @@ def write_report(covdata: CovData, output_file: str, options: Options) -> None:
not (options.html_details or options.html_nested)
or options.html_single_page
)
if output_file == "-":
if not self_contained:
raise ArgumentTypeError(
"Only self contained reports can be printed to STDOUT"
)
elif options.html_details or options.html_nested:
raise ArgumentTypeError("Detailed reports can not be printed to STDOUT")

if output_file.endswith(os.sep):
if options.html_single_page:
Expand Down Expand Up @@ -354,7 +347,7 @@ def write_report(covdata: CovData, output_file: str, options: Options) -> None:

if options.html_relative_anchors:
css_link = os.path.basename(css_output)
else:
else: # pragma: no cover Can't be checked because of the reference compare
css_link = css_output
data["css_link"] = css_link

Expand Down
41 changes: 41 additions & 0 deletions tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ def test_non_writable_directory_csv(capsys):
helper_test_non_writable_directory_output(capsys, "--csv")


def test_stdout_no_html_self_contained(caplog):
c = log_capture(caplog, ["--output", "-", "--no-html-self-contained"])
message = c.record_tuples[0]
assert message[1] == logging.ERROR
assert (
message[2]
== "only self contained reports can be printed to STDOUT"
)
assert c.exception.code != 0


def test_no_output_html_details(caplog):
c = log_capture(caplog, ["--html-details"])
message = c.record_tuples[0]
Expand All @@ -275,6 +286,17 @@ def test_no_output_html_details(caplog):
assert c.exception.code != 0


def test_stdout_html_details(caplog):
c = log_capture(caplog, ["--html-details", "-"])
message = c.record_tuples[0]
assert message[1] == logging.ERROR
assert (
message[2]
== "detailed reports can only be printed to STDOUT as --html-single-page."
)
assert c.exception.code != 0


def test_no_output_html_nested(caplog):
c = log_capture(caplog, ["--html-nested"])
message = c.record_tuples[0]
Expand All @@ -286,6 +308,17 @@ def test_no_output_html_nested(caplog):
assert c.exception.code != 0


def test_stdout_html_nested(caplog):
c = log_capture(caplog, ["--html-nested", "-"])
message = c.record_tuples[0]
assert message[1] == logging.ERROR
assert (
message[2]
== "detailed reports can only be printed to STDOUT as --html-single-page."
)
assert c.exception.code != 0


def test_html_details_and_html_nested(caplog):
c = log_capture(caplog, ["--output", "x", "--html-details", "--html-nested"])
message = c.record_tuples[0]
Expand All @@ -294,6 +327,14 @@ def test_html_details_and_html_nested(caplog):
assert c.exception.code != 0


def test_html_single_page_without_html_details_or_html_nested(caplog):
c = log_capture(caplog, ["--output", "x", "--html-single-page"])
message = c.record_tuples[0]
assert message[1] == logging.ERROR
assert message[2] == "option --html-details or --html-nested is needed, if the option --html-single-page is used."
assert c.exception.code != 0


@pytest.mark.parametrize(
"option",
[
Expand Down

0 comments on commit 8f5b81b

Please sign in to comment.