def _run_main(self, argv):
     gen_report_patch = patch(
         "diff_cover.diff_quality_tool.generate_quality_report",
         return_value=100)
     with gen_report_patch as p:
         main(argv)
         quality_reporter = p.call_args[0][0]
         assert quality_reporter.driver.name == "pylint"
         assert quality_reporter.options == "--foobar"
Exemplo n.º 2
0
    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 pytest.raises(CommandError):
            diff_quality_tool.main(
                ["diff-quality", "--violations", "pycodestyle"])
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    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}"]
        if report_arg:
            argv.append(report_arg)

        logger = self.mocker.patch("diff_cover.diff_quality_tool.LOGGER")
        exit_value = diff_quality_tool.main(argv)
        logger.error.assert_called_with(*expected_error)
        assert exit_value == 1
Exemplo n.º 5
0
    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")
Exemplo n.º 6
0
def _run_main(report, argv):
    main(argv)
    quality_reporter = report.call_args[0][0]
    assert quality_reporter.driver.name == "pylint"
    assert quality_reporter.options == "--foobar"