def _check_console_report(self, git_diff_path, expected_console_path,
                              tool_args):
        """
        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) as git_diff_file:
            self._set_git_diff_output(git_diff_file.read(), "")

        # Capture stdout to a string buffer
        string_buffer = StringIO()
        self._capture_stdout(string_buffer)

        # Patch sys.argv
        self._set_sys_args(tool_args)

        # Execute the tool
        main()

        # 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)
    def _check_console_report(self, git_diff_path, expected_console_path, tool_args):
        """
        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) 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)

        # Patch sys.argv
        self._set_sys_args(tool_args)

        # Execute the tool
        main()

        # 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)
    def _check_console_report(self, git_diff_path, coverage_xml_paths,
                              expected_console_path, quality_reporter='pep8'):
        """
        If `coverage_xml_paths` is not empty, assert that given `git_diff_path`
        and `coverage_xml_paths`, diff-cover generates the expected console report.

        If `coverage_xml_paths` is empty, asserts that given `git_diff_path` and
        `quality_reporter` generates the expected console report.
        """

        # Patch the output of `git diff`
        with open(git_diff_path) as git_diff_file:
            self._set_git_diff_output(git_diff_file.read(), "")

        # Capture stdout to a string buffer
        string_buffer = StringIO()
        self._capture_stdout(string_buffer)

        # Patch sys.argv
        if len(coverage_xml_paths) > 0:
            input_list = ['diff-cover'] + coverage_xml_paths
            self._set_sys_args(input_list)
        else:
            self._set_sys_args(['diff-quality',
                                '--violations', quality_reporter])

        # Run diff-cover or diff-quality, as specified
        main()

        # Check the console report
        with open(expected_console_path) as expected_file:
            report = string_buffer.getvalue()
            expected = expected_file.read()
            self.assertEqual(report, expected)
    def test_git_diff_error(self):

        # Patch the output of `git diff` to return an error
        self._set_git_diff_output('', 'fatal error')

        # Expect an error
        with self.assertRaises(CommandError):
            main(['diff-cover', 'coverage.xml'])
    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')

        # Expect an error
        with self.assertRaises(CommandError):
            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")

        # Expect an error
        with self.assertRaises(CommandError):
            main(["diff-quality", "--violations", "pep8"])
    def test_git_diff_error(self):

        # Patch the output of `git diff` to return an error
        self._set_git_diff_output('', 'fatal error')

        # Expect an error
        with self.assertRaises(GitDiffError):
            main(['diff-cover', 'coverage.xml'])
    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')

        # Expect an error
        with self.assertRaises(GitDiffError):
            main(['diff-quality', '--violations', 'pep8'])
Exemple #9
0
 def _run_main(self, argv):
     gen_report_patch = patch("diff_cover.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'
Exemple #10
0
 def _run_main(self, argv):
     gen_report_patch = patch("diff_cover.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'
Exemple #11
0
    def test_git_diff_error(self):

        # Patch the output of `git diff` to return an error
        self._set_git_diff_output("", "fatal error")

        # Expect an error
        with self.assertRaises(CommandError):
            main(["diff-cover", "coverage.xml"])
Exemple #12
0
    def test_git_diff_error_diff_quality(self):

        # Patch sys.argv
        self._set_sys_args(['diff-quality', '--violations', 'pep8'])

        # Patch the output of `git diff` to return an error
        self._set_git_diff_output('', 'fatal error')

        # Expect an error
        with self.assertRaises(GitDiffError):
            main()
Exemple #13
0
    def test_git_diff_error(self):

        # Patch sys.argv
        self._set_sys_args(['diff-cover', 'coverage.xml'])

        # Patch the output of `git diff` to return an error
        self._set_git_diff_output('', 'fatal error')

        # Expect an error
        with self.assertRaises(GitDiffError):
            main()
Exemple #14
0
    def test_parse_options_without_quotes(self):
        argv = ["diff-quality", "--violations", "pylint", "--options=--foobar"]
        fake_pylint_reporter = Mock()
        reporter_patch = patch.dict("diff_cover.tool.QUALITY_REPORTERS", {"pylint": fake_pylint_reporter})
        gen_report_patch = patch("diff_cover.tool.generate_quality_report", return_value=100)
        with reporter_patch:
            with gen_report_patch:
                main(argv)

        self.assertTrue(fake_pylint_reporter.called)
        call = fake_pylint_reporter.call_args
        user_options = call[1]["user_options"]
        self.assertEqual(user_options, "--foobar")
Exemple #15
0
    def test_parse_prog_name(self):
        # Windows will give the full path to the tool.
        argv = [
            # Using forward slashes so the tests work everywhere.
            'C:/diff-cover/diff-quality-script.py',
            '--violations', 'pylint',
            '--options=--foobar',
        ]
        self._run_main(argv)

        # No silent failures if the tool name is unrecognized.
        with self.assertRaises(AssertionError):
            main(['diff-foobar'])
Exemple #16
0
    def _check_html_report(self, git_diff_path, expected_html_path, tool_args, expected_status=0):
        """
        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) 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')

        # Execute the tool
        code = main(tool_args + ['--html-report', html_report_path])
        self.assertEquals(code, expected_status)

        # Check the HTML report
        with open(expected_html_path) as expected_file:
            with open(html_report_path) as html_report:
                html = html_report.read()
                expected = expected_file.read()
                assert_long_str_equal(expected, html, strip=True)
    def _check_html_report(self, git_diff_path, expected_html_path, tool_args, expected_status=0):
        """
        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) 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')

        # Execute the tool
        code = main(tool_args + ['--html-report', html_report_path])
        self.assertEquals(code, expected_status)

        # Check the HTML report
        with open(expected_html_path) as expected_file:
            with open(html_report_path) as html_report:
                html = html_report.read()
                expected = expected_file.read()
                assert_long_str_equal(expected, html, strip=True)
Exemple #18
0
    def test_parse_options_without_quotes(self):
        argv = [
            "diff-quality",
            "--violations",
            "pylint",
            '--options=--foobar',
        ]
        fake_pylint_reporter = Mock()
        reporter_patch = patch.dict("diff_cover.tool.QUALITY_REPORTERS",
                                    {"pylint": fake_pylint_reporter})
        gen_report_patch = patch("diff_cover.tool.generate_quality_report",
                                 return_value=100)
        with reporter_patch:
            with gen_report_patch:
                main(argv)

        self.assertTrue(fake_pylint_reporter.called)
        call = fake_pylint_reporter.call_args
        user_options = call[1]["user_options"]
        self.assertEqual(user_options, "--foobar")
    def _check_html_report(self, git_diff_path, coverage_xml_paths,
                           expected_html_path, quality_reporter='pep8'):
        """
        If `coverage_xml_paths` is not empty,
        assert that given `git_diff_path`
        and `coverage_xml_paths`, diff-cover
        generates the expected HTML report.

        If `coverage_xml_paths` is empty,
        asserts that given `git_diff_path` and
        `quality_reporter` generates the expected HTML report.
        """

        # Patch the output of `git diff`
        with open(git_diff_path) 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')

        if len(coverage_xml_paths) > 0:
            input_list = ['diff-cover'] + coverage_xml_paths + ['--html-report', html_report_path]
            self._set_sys_args(input_list)

        else:
            self._set_sys_args(['diff-quality',
                                '--violations', quality_reporter,
                                '--html-report', html_report_path])

        # Run diff-cover or diff-quality, as specified
        main()

        # Check the HTML report
        with open(expected_html_path) as expected_file:
            with open(html_report_path) as html_report:
                html = html_report.read()
                expected = expected_file.read()
                assert_long_str_equal(expected, html, strip=True)
Exemple #20
0
    def _call_quality_expecting_error(self, tool_name, expected_error):
        """
        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
        """
        self._set_git_diff_output("does/not/matter", "")
        argv = ["diff-quality", "--violations={0}".format(tool_name), "pylint_report.txt"]

        with patch("diff_cover.tool.LOGGER") as logger:
            exit_value = main(argv)
            logger.error.assert_called_with(expected_error)
            self.assertEqual(exit_value, 1)
Exemple #21
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 io.open("git_diff_add.txt", encoding="utf-8") as git_diff_file:
            self._set_git_diff_output(git_diff_file.read(), "")
        argv = ["diff-quality", "--violations={0}".format(tool_name), report_arg]

        with patch("diff_cover.tool.LOGGER") as logger:
            exit_value = main(argv)
            logger.error.assert_called_with(expected_error)
            self.assertEqual(exit_value, 1)
Exemple #22
0
    def _call_quality_expecting_error(self, tool_name, expected_error):
        """
        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
        """
        self._set_git_diff_output("does/not/matter", "")
        argv = ['diff-quality',
                '--violations={0}'.format(tool_name),
                'pylint_report.txt']

        with patch('diff_cover.tool.LOGGER') as logger:
            exit_value = main(argv)
            logger.error.assert_called_with(expected_error)
            self.assertEqual(exit_value, 1)
Exemple #23
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 io.open('git_diff_add.txt', encoding='utf-8') as git_diff_file:
            self._set_git_diff_output(git_diff_file.read(), "")
        argv = ['diff-quality',
                '--violations={0}'.format(tool_name),
                report_arg]

        with patch('diff_cover.tool.LOGGER') as logger:
            exit_value = main(argv)
            logger.error.assert_called_with(expected_error)
            self.assertEqual(exit_value, 1)
Exemple #24
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 io.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
        code = main(args)
        self.assertEquals(code, expected_status)

        # Check the HTML report
        with io.open(expected_html_path, encoding="utf-8") as expected_file:
            with io.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