コード例 #1
0
    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.process = mock.Mock()
        self.subprocess = mock.patch('diff_cover.command_runner.subprocess').start()
        self.subprocess.Popen.return_value = self.process
        # Create the git diff tool
        self.tool = GitDiffTool()
コード例 #2
0
    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.subprocess = mock.Mock()
        self.process = mock.Mock()
        self.subprocess.Popen = mock.Mock(return_value=self.process)
        self.process.communicate = mock.Mock()

        # Create the git diff tool
        self.tool = GitDiffTool(subprocess_mod=self.subprocess)
コード例 #3
0
class TestGitDiffTool(unittest.TestCase):

    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.subprocess = mock.Mock()
        self.process = mock.Mock()
        self.subprocess.Popen = mock.Mock(return_value=self.process)
        self.process.communicate = mock.Mock()

        # Create the git diff tool
        self.tool = GitDiffTool(subprocess_mod=self.subprocess)

    def test_diff_committed(self):

        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', 'origin/master...HEAD', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_unstaged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_unstaged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_staged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_staged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', '--cached', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_errors(self):
        self._set_git_diff_output('test output', 'fatal error')

        with self.assertRaises(GitDiffError):
            self.tool.diff_unstaged()

        with self.assertRaises(GitDiffError):
            self.tool.diff_staged()

        with self.assertRaises(GitDiffError):
            self.tool.diff_unstaged()

    def _set_git_diff_output(self, stdout, stderr):
        """
        Configure the `git diff` mock to output `stdout`
        and `stderr` to stdout and stderr, respectively.
        """
        self.process.communicate.return_value = (stdout, stderr)
コード例 #4
0
ファイル: test_git_diff.py プロジェクト: hugovk/diff-cover
class TestGitDiffTool(unittest.TestCase):

    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.process = mock.Mock()
        self.process.returncode = 0
        self.subprocess = mock.patch('diff_cover.command_runner.subprocess').start()
        self.subprocess.Popen.return_value = self.process
        # Create the git diff tool
        self.tool = GitDiffTool()

    def test_diff_committed(self):

        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', 'diff',
                    'origin/master...HEAD', '--no-color', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_unstaged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_unstaged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', 'diff',
                    '--no-color', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_staged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_staged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', 'diff', '--cached',
                    '--no-color', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_committed_compare_branch(self):

        # Override the default compare branch
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed(compare_branch='release')

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', 'diff',
                    'release...HEAD', '--no-color', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_errors(self):
        self._set_git_diff_output('test output', 'fatal error', 1)

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

        with self.assertRaises(CommandError):
            self.tool.diff_staged()

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

    def _set_git_diff_output(self, stdout, stderr, returncode=0):
        """
        Configure the `git diff` mock to output `stdout`
        and `stderr` to stdout and stderr, respectively.
        """
        self.process.communicate.return_value = (stdout, stderr)
        self.process.returncode = returncode
コード例 #5
0
def generate_coverage_report(
    coverage_xml,
    compare_branch,
    html_report=None,
    css_file=None,
    json_report=None,
    markdown_report=None,
    ignore_staged=False,
    ignore_unstaged=False,
    include_untracked=False,
    exclude=None,
    src_roots=None,
    diff_range_notation=None,
    ignore_whitespace=False,
    quiet=False,
    show_uncovered=False,
):
    """
    Generate the diff coverage report, using kwargs from `parse_args()`.
    """
    diff = GitDiffReporter(
        compare_branch,
        git_diff=GitDiffTool(diff_range_notation, ignore_whitespace),
        ignore_staged=ignore_staged,
        ignore_unstaged=ignore_unstaged,
        include_untracked=include_untracked,
        exclude=exclude,
    )

    xml_roots = [etree.parse(xml_root) for xml_root in coverage_xml]
    coverage = XmlCoverageReporter(xml_roots, src_roots)

    # Build a report generator
    if html_report is not None:
        css_url = css_file
        if css_url is not None:
            css_url = os.path.relpath(css_file, os.path.dirname(html_report))
        reporter = HtmlReportGenerator(coverage, diff, css_url=css_url)
        with open(html_report, "wb") as output_file:
            reporter.generate_report(output_file)
        if css_file is not None:
            with open(css_file, "wb") as output_file:
                reporter.generate_css(output_file)

    if json_report is not None:
        reporter = JsonReportGenerator(coverage, diff)
        with open(json_report, "wb") as output_file:
            reporter.generate_report(output_file)

    if markdown_report is not None:
        reporter = MarkdownReportGenerator(coverage, diff)
        with open(markdown_report, "wb") as output_file:
            reporter.generate_report(output_file)

    # Generate the report for stdout
    reporter = StringReportGenerator(coverage, diff, show_uncovered)
    output_file = io.BytesIO() if quiet else sys.stdout.buffer

    # Generate the report
    reporter.generate_report(output_file)
    return reporter.total_percent_covered()
コード例 #6
0
class TestGitDiffTool(unittest.TestCase):
    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.process = mock.Mock()
        self.process.returncode = 0
        self.subprocess = mock.patch(
            "diff_cover.command_runner.subprocess").start()
        self.subprocess.Popen.return_value = self.process
        self.addCleanup(mock.patch.stopall)
        # Create the git diff tool
        self.tool = GitDiffTool(range_notation="...", ignore_whitespace=False)

    def check_diff_committed(self, diff_range_notation, ignore_whitespace):
        self.tool = GitDiffTool(range_notation=diff_range_notation,
                                ignore_whitespace=ignore_whitespace)
        self._set_git_diff_output("test output", "")
        output = self.tool.diff_committed()

        # Expect that we get the correct output
        self.assertEqual(output, "test output")

        # Expect that the correct command was executed
        expected = [
            "git",
            "-c",
            "diff.mnemonicprefix=no",
            "-c",
            "diff.noprefix=no",
            "diff",
            "--no-color",
            "--no-ext-diff",
            "-U0",
        ]
        if ignore_whitespace:
            expected.append("--ignore-all-space")
            expected.append("--ignore-blank-lines")
        expected.append(f"origin/master{diff_range_notation}HEAD")
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_commited(self):
        self.check_diff_committed("...", ignore_whitespace=False)
        self.check_diff_committed("...", ignore_whitespace=True)
        self.check_diff_committed("..", ignore_whitespace=False)
        self.check_diff_committed("..", ignore_whitespace=True)

    def test_diff_unstaged(self):
        self._set_git_diff_output("test output", "")
        output = self.tool.diff_unstaged()

        # Expect that we get the correct output
        self.assertEqual(output, "test output")

        # Expect that the correct command was executed
        expected = [
            "git",
            "-c",
            "diff.mnemonicprefix=no",
            "-c",
            "diff.noprefix=no",
            "diff",
            "--no-color",
            "--no-ext-diff",
            "-U0",
        ]
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_staged(self):
        self._set_git_diff_output("test output", "")
        output = self.tool.diff_staged()

        # Expect that we get the correct output
        self.assertEqual(output, "test output")

        # Expect that the correct command was executed
        expected = [
            "git",
            "-c",
            "diff.mnemonicprefix=no",
            "-c",
            "diff.noprefix=no",
            "diff",
            "--no-color",
            "--no-ext-diff",
            "-U0",
            "--cached",
        ]
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_committed_compare_branch(self):

        # Override the default compare branch
        self._set_git_diff_output("test output", "")
        output = self.tool.diff_committed(compare_branch="release")

        # Expect that we get the correct output
        self.assertEqual(output, "test output")

        # Expect that the correct command was executed

        expected = [
            "git",
            "-c",
            "diff.mnemonicprefix=no",
            "-c",
            "diff.noprefix=no",
            "diff",
            "--no-color",
            "--no-ext-diff",
            "-U0",
            "release...HEAD",
        ]
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_errors(self):
        self._set_git_diff_output("test output", "fatal error", 1)

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

        with self.assertRaises(CommandError):
            self.tool.diff_staged()

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

    def _set_git_diff_output(self, stdout, stderr, returncode=0):
        """
        Configure the `git diff` mock to output `stdout`
        and `stderr` to stdout and stderr, respectively.
        """
        self.process.communicate.return_value = (stdout, stderr)
        self.process.returncode = returncode
コード例 #7
0
ファイル: test_git_diff.py プロジェクト: ziafazal/diff_cover
class TestGitDiffTool(unittest.TestCase):

    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.process = mock.Mock()
        self.process.returncode = 0
        self.subprocess = mock.patch('diff_cover.command_runner.subprocess').start()
        self.subprocess.Popen.return_value = self.process
        self.addCleanup(mock.patch.stopall)
        # Create the git diff tool
        self.tool = GitDiffTool('...')

    def check_diff_committed(self, diff_range_notation):
        self.tool = GitDiffTool(diff_range_notation)
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', '-c',
                    'diff.noprefix=no', 'diff', 'origin/master{}HEAD'.format(diff_range_notation),
                    '--no-color', '--no-ext-diff', '-U0']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_commited(self):
        self.check_diff_committed('...')
        self.check_diff_committed('..')

    def test_diff_unstaged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_unstaged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', '-c',
                    'diff.noprefix=no', 'diff', '--no-color', '--no-ext-diff',
                    '-U0']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_staged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_staged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', '-c',
                    'diff.noprefix=no', 'diff', '--cached', '--no-color',
                    '--no-ext-diff', '-U0']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_diff_committed_compare_branch(self):

        # Override the default compare branch
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed(compare_branch='release')

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', '-c', 'diff.mnemonicprefix=no', '-c',
                    'diff.noprefix=no', 'diff', 'release...HEAD', '--no-color',
                    '--no-ext-diff', '-U0']
        self.subprocess.Popen.assert_called_with(
            expected, stdout=self.subprocess.PIPE, stderr=self.subprocess.PIPE
        )

    def test_errors(self):
        self._set_git_diff_output('test output', 'fatal error', 1)

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

        with self.assertRaises(CommandError):
            self.tool.diff_staged()

        with self.assertRaises(CommandError):
            self.tool.diff_unstaged()

    def _set_git_diff_output(self, stdout, stderr, returncode=0):
        """
        Configure the `git diff` mock to output `stdout`
        and `stderr` to stdout and stderr, respectively.
        """
        self.process.communicate.return_value = (stdout, stderr)
        self.process.returncode = returncode
コード例 #8
0
def generate_coverage_report(coverage_xml, compare_branch,
                             html_report=None, css_file=None,
                             json_report=None,
                             ignore_staged=False, ignore_unstaged=False,
                             exclude=None, src_roots=None, diff_range_notation=None,
                             target_dir=None, diff_json=None):
    """
    Generate the diff coverage report, using kwargs from `parse_args()`.
    """
    if target_dir:
        class FileDiffReporter(GitDiffReporter):
            def __init__(self, *args, **kwargs):
                self.target_dir = kwargs.pop("td")
                super(FileDiffReporter, self).__init__(*args, **kwargs)

            def _git_diff(self):
                def compare(left, right):
                    d = difile.Difile()
                    result = d.compare_dir(left, right)
                    diff_result = dict()

                    for each_file in result:
                        if not each_file:
                            continue
                        key = each_file[0].file_path.as_posix()
                        if key.startswith(str(self.target_dir)):
                            key = key.replace(str(self.target_dir) + "/", "")
                        if key not in diff_result:
                            diff_result[key] = []
                        for each_line in each_file:
                            diff_result[key].append(each_line.line_no)
                    return diff_result
                return compare(src_roots[0], self.target_dir)

        diff = FileDiffReporter(
            compare_branch, git_diff=GitDiffTool(diff_range_notation),
            ignore_staged=ignore_staged, ignore_unstaged=ignore_unstaged,
            exclude=exclude, td=target_dir)
    else:
        diff = GitDiffReporter(
            compare_branch, git_diff=GitDiffTool(diff_range_notation),
            ignore_staged=ignore_staged, ignore_unstaged=ignore_unstaged,
            exclude=exclude)

    xml_roots = [etree.parse(xml_root) for xml_root in coverage_xml]
    coverage = XmlCoverageReporter(xml_roots, src_roots)

    if diff_json:
        diff_dict = diff._git_diff()
        with open(diff_json, "w") as f:
            json.dump(diff_dict, f)

    # Build a report generator
    if html_report is not None:
        css_url = css_file
        if css_url is not None:
            css_url = os.path.relpath(css_file, os.path.dirname(html_report))
        reporter = HtmlReportGenerator(coverage, diff, css_url=css_url)
        with open(html_report, "wb") as output_file:
            reporter.generate_report(output_file)
        if css_file is not None:
            with open(css_file, "wb") as output_file:
                reporter.generate_css(output_file)

    elif json_report is not None:
        reporter = JsonReportGenerator(coverage, diff)
        with open(json_report, "wb") as output_file:
            reporter.generate_report(output_file)

    reporter = StringReportGenerator(coverage, diff)
    output_file = sys.stdout if six.PY2 else sys.stdout.buffer

    # Generate the report
    reporter.generate_report(output_file)
    return reporter.total_percent_covered()
コード例 #9
0
ファイル: test_git_diff.py プロジェクト: kasium/diff_cover
def tool():
    return GitDiffTool(range_notation="...", ignore_whitespace=False)
コード例 #10
0
class TestGitDiffTool(unittest.TestCase):
    def setUp(self):

        # Create mock subprocess to simulate `git diff`
        self.subprocess = mock.Mock()
        self.process = mock.Mock()
        self.subprocess.Popen = mock.Mock(return_value=self.process)
        self.process.communicate = mock.Mock()

        # Create the git diff tool
        self.tool = GitDiffTool(subprocess_mod=self.subprocess)

    def test_diff_committed(self):

        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', 'origin/master...HEAD', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_unstaged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_unstaged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_staged(self):
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_staged()

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', '--cached', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_diff_committed_compare_branch(self):

        # Override the default compare branch
        self._set_git_diff_output('test output', '')
        output = self.tool.diff_committed(compare_branch='release')

        # Expect that we get the correct output
        self.assertEqual(output, 'test output')

        # Expect that the correct command was executed
        expected = ['git', 'diff', 'release...HEAD', '--no-ext-diff']
        self.subprocess.Popen.assert_called_with(expected,
                                                 stdout=self.subprocess.PIPE,
                                                 stderr=self.subprocess.PIPE)

    def test_errors(self):
        self._set_git_diff_output('test output', 'fatal error')

        with self.assertRaises(GitDiffError):
            self.tool.diff_unstaged()

        with self.assertRaises(GitDiffError):
            self.tool.diff_staged()

        with self.assertRaises(GitDiffError):
            self.tool.diff_unstaged()

    def _set_git_diff_output(self, stdout, stderr):
        """
        Configure the `git diff` mock to output `stdout`
        and `stderr` to stdout and stderr, respectively.
        """
        self.process.communicate.return_value = (stdout, stderr)