def test_git_diff_error(self): # Patch the output of `git diff` to return an error self._set_git_diff_output("", "fatal error", 1) # Expect an error with pytest.raises(CommandError): diff_cover_tool.main(["diff-cover", "coverage.xml"])
def _check_html_report( self, git_diff_path, expected_html_path, tool_args, expected_status=0, css_file=None, ): """ Verify that the tool produces the expected HTML report. `git_diff_path` is a path to a fixture containing the (patched) output of the call to `git diff`. `expected_console_path` is a path to the fixture containing the expected HTML output of the tool. `tool_args` is a list of command line arguments to pass to the tool. You should include the name of the tool as the first argument. """ # Patch the output of `git diff` with open(git_diff_path, encoding="utf-8") as git_diff_file: self._set_git_diff_output(git_diff_file.read(), "") # Create a temporary directory to hold the output HTML report # Add a cleanup to ensure the directory gets deleted temp_dir = self.tmp_path / "dummy" temp_dir.mkdir() html_report_path = os.path.join(temp_dir, "diff_coverage.html") args = tool_args + ["--html-report", html_report_path] if css_file: css_file = os.path.join(temp_dir, css_file) args += ["--external-css-file", css_file] # Execute the tool if "diff-cover" in args[0]: code = diff_cover_tool.main(args) else: code = diff_quality_tool.main(args) assert code == expected_status # Check the HTML report with open(expected_html_path, encoding="utf-8") as expected_file: with open(html_report_path, encoding="utf-8") as html_report: html = html_report.read() expected = expected_file.read() if css_file is None: html = self._clear_css(html) expected = self._clear_css(expected) assert expected.strip() == html.strip() return temp_dir
def pytest_unconfigure(config): if config.option.cov_source: print() cov_dir = Path(__file__).parent / "coverage" xml = str(cov_dir / "output.xml") html = str(cov_dir / "diff.html") exit = main(["", xml, "--html-report", html, "--fail-under", "100"]) print(f"\nHTML diff coverage showing missing lines written to {html}") if exit != 0: sys.exit(exit)
def _check_console_report(self, git_diff_path, expected_console_path, tool_args, expected_status=0): """ Verify that the tool produces the expected console report. `git_diff_path` is a path to a fixture containing the (patched) output of the call to `git diff`. `expected_console_path` is a path to the fixture containing the expected console output of the tool. `tool_args` is a list of command line arguments to pass to the tool. You should include the name of the tool as the first argument. """ # Patch the output of `git diff` with open(git_diff_path, encoding="utf-8") as git_diff_file: self._set_git_diff_output(git_diff_file.read(), "") # Capture stdout to a string buffer string_buffer = BytesIO() self._capture_stdout(string_buffer) # Execute the tool if "diff-cover" in tool_args[0]: code = diff_cover_tool.main(tool_args) else: code = diff_quality_tool.main(tool_args) assert code == expected_status # Check the console report with open(expected_console_path) as expected_file: report = string_buffer.getvalue() expected = expected_file.read() assert expected.strip() == report.strip().decode("utf-8")
from diff_cover.diff_cover_tool import main main()