def test_git_diff_error_diff_quality(self): # Patch the output of `git diff` to return an error self._set_git_diff_output("", "fatal error", 1) # Expect an error with self.assertRaises(CommandError): diff_quality_main(["diff-quality", "--violations", "pycodestyle"])
def test_git_diff_error_diff_quality(self): # Patch the output of `git diff` to return an error self._set_git_diff_output('', 'fatal error', 1) # Expect an error with self.assertRaises(CommandError): diff_quality_main(['diff-quality', '--violations', 'pycodestyle'])
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 = tempfile.mkdtemp() self.addCleanup(lambda: shutil.rmtree(temp_dir)) 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_main(args) else: code = diff_quality_main(args) self.assertEqual(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_long_str_equal(expected, html, strip=True) return temp_dir
def _call_quality_expecting_error( self, tool_name, expected_error, report_arg="pylint_report.txt" ): """ Makes calls to diff_quality that should fail to ensure we get back the correct failure. Takes in a string which is a tool to call and an string which is the error you expect to see """ with open("git_diff_add.txt", encoding="utf-8") as git_diff_file: self._set_git_diff_output(git_diff_file.read(), "") argv = ["diff-quality", f"--violations={tool_name}", report_arg] with patch("diff_cover.diff_quality_tool.LOGGER") as logger: exit_value = diff_quality_main(argv) logger.error.assert_called_with(expected_error) self.assertEqual(exit_value, 1)
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_main(tool_args) else: code = diff_quality_main(tool_args) self.assertEqual(code, expected_status) # Check the console report with open(expected_console_path) as expected_file: report = string_buffer.getvalue() expected = expected_file.read() assert_long_str_equal(expected, report, strip=True)