1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
# pylint: disable=missing-function-docstring
"""Test for diff_cover.git_diff"""
import pytest
from diff_cover.command_runner import CommandError
from diff_cover.git_diff import GitDiffTool
@pytest.fixture
def process(mocker):
process_ = mocker.Mock()
process_.returncode = 0
return process_
@pytest.fixture(autouse=True)
def subprocess(mocker, process):
subprocess_ = mocker.patch("diff_cover.command_runner.subprocess")
popen_mock = subprocess_.Popen
popen_instance = popen_mock.return_value
popen_instance.__enter__.return_value = process
popen_instance.__exit__.return_value = None
return subprocess_
@pytest.fixture
def tool():
return GitDiffTool(range_notation="...", ignore_whitespace=False)
@pytest.fixture
def set_git_diff_output(process):
def _inner(stdout, stderr, returncode=0):
process.communicate.return_value = (stdout, stderr)
process.returncode = returncode
return _inner
@pytest.fixture
def check_diff_committed(subprocess, set_git_diff_output):
def _inner(diff_range_notation, ignore_whitespace):
tool_ = GitDiffTool(
range_notation=diff_range_notation, ignore_whitespace=ignore_whitespace
)
set_git_diff_output("test output", "")
output = tool_.diff_committed()
# Expect that we get the correct output
assert 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/main{diff_range_notation}HEAD")
subprocess.Popen.assert_called_with(
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
return _inner
def test_diff_committed(check_diff_committed):
check_diff_committed("...", ignore_whitespace=False)
check_diff_committed("...", ignore_whitespace=True)
check_diff_committed("..", ignore_whitespace=False)
check_diff_committed("..", ignore_whitespace=True)
def test_diff_unstaged(set_git_diff_output, tool, subprocess):
set_git_diff_output("test output", "")
output = tool.diff_unstaged()
# Expect that we get the correct output
assert 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",
]
subprocess.Popen.assert_called_with(
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
def test_diff_staged(tool, subprocess, set_git_diff_output):
set_git_diff_output("test output", "")
output = tool.diff_staged()
# Expect that we get the correct output
assert 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",
]
subprocess.Popen.assert_called_with(
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
def test_diff_missing_branch_error(set_git_diff_output, tool, subprocess):
# Override the default compare branch
set_git_diff_output("test output", "fatal error", 1)
with pytest.raises(CommandError):
tool.diff_committed(compare_branch="release")
set_git_diff_output(
"test output",
"ambiguous argument 'origin/main...HEAD': "
"unknown revision or path not in the working tree.",
1,
)
with pytest.raises(ValueError):
tool.diff_committed(compare_branch="release")
def test_diff_committed_compare_branch(set_git_diff_output, tool, subprocess):
# Override the default compare branch
set_git_diff_output("test output", "")
output = tool.diff_committed(compare_branch="release")
# Expect that we get the correct output
assert 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",
]
subprocess.Popen.assert_called_with(
expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
def test_errors(set_git_diff_output, tool):
set_git_diff_output("test output", "fatal error", 1)
with pytest.raises(CommandError):
tool.diff_unstaged()
with pytest.raises(CommandError):
tool.diff_staged()
with pytest.raises(CommandError):
tool.diff_unstaged()
@pytest.mark.parametrize(
"output,expected",
[
("", []),
("\n", []),
("a.py\n", ["a.py"]),
("a.py\nb.py\n", ["a.py", "b.py"]),
],
)
def test_untracked(tool, set_git_diff_output, output, expected):
set_git_diff_output(output, b"")
assert tool.untracked() == expected
def test_git_diff_tool_untracked_cache(tool, set_git_diff_output):
set_git_diff_output("file.txt\nfile2.txt\n", "")
output = tool.untracked()
assert output == ["file.txt", "file2.txt"]
set_git_diff_output("file2.txt\n", "")
output = tool.untracked()
assert output == ["file.txt", "file2.txt"]
|