示例#1
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 io.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
        code = main(tool_args)

        self.assertEquals(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)
示例#2
0
    def test_load_snippets_html(self):

        # Need to be in the fixture directory
        # so the source path is displayed correctly
        old_cwd = os.getcwd()
        self.addCleanup(lambda: os.chdir(old_cwd))
        os.chdir(fixture_path(''))

        src_path = fixture_path('snippet_src.py')
        self._init_src_file(100, src_path)

        # One higher-level test to make sure
        # the snippets are being rendered correctly
        violations = [10, 12, 13, 50, 51, 54, 55, 57]
        snippets_html = '\n\n'.join(
            Snippet.load_snippets_html('snippet_src.py', violations)
        )

        # Load the fixture for the expected contents
        expected_path = fixture_path('snippet_list.html')
        with open(expected_path) as fixture_file:
            expected = fixture_file.read()

        # Check that we got what we expected
        assert_long_str_equal(expected, snippets_html, strip=True)
示例#3
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)
示例#4
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)
示例#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) 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
        code = main(tool_args)

        self.assertEquals(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)
示例#6
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 = 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
示例#7
0
    def _assert_format(self, src_tokens, src_filename, start_line, violation_lines, expected_fixture):

        snippet = Snippet(src_tokens, src_filename, start_line, violation_lines)
        result = snippet.html()

        expected_str = load_fixture(expected_fixture, encoding="utf-8")

        assert_long_str_equal(expected_str, result, strip=True)
        self.assertTrue(isinstance(result, six.text_type))
示例#8
0
    def _assert_format(self, src_tokens, src_filename, start_line,
                       violation_lines, expected_fixture):

        snippet = Snippet(src_tokens, src_filename, start_line,
                          violation_lines)
        result = snippet.html()

        expected_str = load_fixture(expected_fixture, encoding='utf-8')

        assert_long_str_equal(expected_str, result, strip=True)
        self.assertTrue(isinstance(result, six.text_type))
示例#9
0
    def assert_report(self, expected):
        """
        Generate a report and assert that it matches
        the string `expected`.
        """
        # Create a buffer for the output
        output = BytesIO()

        # Generate the report
        self.report.generate_report(output)

        # Get the output
        output_str = output.getvalue()
        output.close()

        # Verify that we got the expected string
        assert_long_str_equal(expected, output_str, strip=True)
示例#10
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
示例#11
0
    def assert_report(self, expected):
        """
        Generate a report and assert that it matches
        the string `expected`.
        """
        # Create a buffer for the output
        output = BytesIO()

        # Generate the report
        self.report.generate_report(output)

        # Get the output
        output_str = output.getvalue()
        output.close()

        # Verify that we got the expected string
        assert_long_str_equal(expected, output_str, strip=True)
示例#12
0
    def _assert_format(
        self,
        src_tokens,
        src_filename,
        start_line,
        last_line,
        violation_lines,
        expected_fixture,
    ):

        snippet = Snippet(src_tokens, src_filename, start_line, last_line,
                          violation_lines, None)
        result = snippet.html()

        expected_str = load_fixture(expected_fixture, encoding="utf-8")

        assert_long_str_equal(expected_str, result, strip=True)
        self.assertIsInstance(result, str)
示例#13
0
    def _compare_snippets_html_output(self, filename, violations, expected_out_filename):
        # Need to be in the fixture directory
        # so the source path is displayed correctly
        old_cwd = os.getcwd()
        self.addCleanup(lambda: os.chdir(old_cwd))
        os.chdir(fixture_path(""))

        # One higher-level test to make sure
        # the snippets are being rendered correctly
        snippets_html = "\n\n".join(Snippet.load_snippets_html(filename, violations))
        # Load the fixture for the expected contents
        expected_path = fixture_path(expected_out_filename)
        with open(expected_path) as fixture_file:
            expected = fixture_file.read()
            if isinstance(expected, six.binary_type):
                expected = expected.decode("utf-8")

        # Check that we got what we expected
        assert_long_str_equal(expected, snippets_html, strip=True)
示例#14
0
    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)
示例#15
0
    def _compare_snippets_html_output(self, filename, violations, expected_out_filename):
        # Need to be in the fixture directory
        # so the source path is displayed correctly
        old_cwd = os.getcwd()
        self.addCleanup(lambda: os.chdir(old_cwd))
        os.chdir(fixture_path(''))

        # One higher-level test to make sure
        # the snippets are being rendered correctly
        snippets_html = '\n\n'.join(
            Snippet.load_snippets_html(filename, violations)
        )
        # Load the fixture for the expected contents
        expected_path = fixture_path(expected_out_filename)
        with open(expected_path) as fixture_file:
            expected = fixture_file.read()
            if isinstance(expected, six.binary_type):
                expected = expected.decode('utf-8')

        # Check that we got what we expected
        assert_long_str_equal(expected, snippets_html, strip=True)
示例#16
0
    def _compare_snippets_output(self, format, filename, violations,
                                 expected_out_filename):
        # Need to be in the fixture directory
        # so the source path is displayed correctly
        old_cwd = os.getcwd()
        self.addCleanup(lambda: os.chdir(old_cwd))
        os.chdir(fixture_path(""))

        # One higher-level test to make sure
        # the snippets are being rendered correctly
        formatted_snippets = Snippet.load_formatted_snippets(
            filename, violations)
        snippets_selected = "\n\n".join(formatted_snippets[format])
        # Load the fixture for the expected contents
        expected_path = fixture_path(expected_out_filename)
        with open(expected_path, encoding="utf-8") as fixture_file:
            expected = fixture_file.read()
            if isinstance(expected, bytes):
                expected = expected.decode("utf-8")

        # Check that we got what we expected
        assert_long_str_equal(expected, snippets_selected, strip=True)
示例#17
0
    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()
            assert_long_str_equal(expected, report, strip=True)
 def assert_report(self, expected):
     output_report_string = self.get_report()
     assert_long_str_equal(expected, output_report_string, strip=True)