diff --git a/gcovr/formats/html/__init__.py b/gcovr/formats/html/__init__.py index c85d6af7d..090f39028 100644 --- a/gcovr/formats/html/__init__.py +++ b/gcovr/formats/html/__init__.py @@ -290,22 +290,31 @@ 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 ): @@ -313,11 +322,16 @@ def validate_options(self) -> None: "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 diff --git a/gcovr/formats/html/write.py b/gcovr/formats/html/write.py index 3df53be33..d6b28ddeb 100644 --- a/gcovr/formats/html/write.py +++ b/gcovr/formats/html/write.py @@ -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: @@ -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 diff --git a/tests/test_args.py b/tests/test_args.py index 77d0d0b20..5e66a685b 100644 --- a/tests/test_args.py +++ b/tests/test_args.py @@ -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] @@ -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] @@ -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] @@ -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", [