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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
|
# pylint: disable=missing-function-docstring,protected-access
"""Test for diff_cover.diff_reporter"""
from pathlib import Path
from textwrap import dedent
import pytest
from diff_cover.diff_reporter import GitDiffReporter
from diff_cover.git_diff import GitDiffError, GitDiffTool
from diff_cover.util import to_unix_paths
from tests.helpers import git_diff_output, line_numbers
@pytest.fixture
def git_diff(mocker):
m = mocker.MagicMock(GitDiffTool)
m.range_notation = "..."
return m
@pytest.fixture
def diff(git_diff):
return GitDiffReporter(git_diff=git_diff)
def test_name(diff):
# Expect that diff report is named after its compare branch
assert diff.name() == "origin/main...HEAD, staged and unstaged changes"
def test_name_compare_branch(git_diff):
# Override the default branch
assert (
GitDiffReporter(git_diff=git_diff, compare_branch="release").name()
== "release...HEAD, staged and unstaged changes"
)
def test_name_ignore_staged(git_diff):
# Override the default branch
assert (
GitDiffReporter(git_diff=git_diff, ignore_staged=True).name()
== "origin/main...HEAD and unstaged changes"
)
def test_name_ignore_unstaged(git_diff):
# Override the default branch
assert (
GitDiffReporter(git_diff=git_diff, ignore_unstaged=True).name()
== "origin/main...HEAD and staged changes"
)
def test_name_ignore_staged_and_unstaged(git_diff):
# Override the default branch
assert (
GitDiffReporter(
git_diff=git_diff, ignore_staged=True, ignore_unstaged=True
).name()
== "origin/main...HEAD"
)
def test_name_include_untracked(git_diff):
# Override the default branch
assert (
GitDiffReporter(git_diff=git_diff, include_untracked=True).name()
== "origin/main...HEAD, staged, unstaged and untracked changes"
)
@pytest.mark.parametrize(
"include,exclude,expected",
[
# no include/exclude --> use all paths
(
[],
[],
to_unix_paths(
["file3.py", "README.md", "subdir1/file1.py", "subdir2/file2.py"]
),
),
# specified exclude without include
(
[],
["file1.py"],
to_unix_paths(["file3.py", "README.md", "subdir2/file2.py"]),
),
# specified include (folder) without exclude
(["subdir1/**"], [], to_unix_paths(["subdir1/file1.py"])),
# specified include (file) without exclude
(["subdir1/file1.py"], [], to_unix_paths(["subdir1/file1.py"])),
# specified include and exclude
(
["subdir1/**", "subdir2/**"],
["file1.py", "file3.py"],
to_unix_paths(["subdir2/file2.py"]),
),
],
)
def test_git_path_selection(
monkeypatch, tmp_path, diff, git_diff, include, exclude, expected
):
monkeypatch.chdir(tmp_path)
diff = GitDiffReporter(git_diff=git_diff, exclude=exclude, include=include)
main_dir = Path(tmp_path)
(main_dir / "file3.py").touch()
subdir1 = main_dir / "subdir1"
subdir1.mkdir()
(subdir1 / "file1.py").touch()
subdir2 = main_dir / "subdir2"
subdir2.mkdir()
(subdir2 / "file2.py").touch()
# Configure the git diff output
_set_git_diff_output(
diff,
git_diff,
git_diff_output(
{"subdir1/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
),
git_diff_output({"subdir2/file2.py": line_numbers(3, 10), "file3.py": [0]}),
git_diff_output({}, deleted_files=["README.md"]),
)
# Get the source paths in the diff
source_paths = diff.src_paths_changed()
# Validate the source paths
# They should be in alphabetical order
assert source_paths == expected
def test_git_source_paths(diff, git_diff):
# Configure the git diff output
_set_git_diff_output(
diff,
git_diff,
git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
),
git_diff_output({"subdir/file2.py": line_numbers(3, 10), "file3.py": [0]}),
git_diff_output({}, deleted_files=["README.md"]),
)
# Get the source paths in the diff
source_paths = diff.src_paths_changed()
# Validate the source paths
assert source_paths == to_unix_paths(
["file3.py", "README.md", "subdir/file1.py", "subdir/file2.py"]
)
def test_git_source_paths_with_space(diff, git_diff):
_set_git_diff_output(
diff,
git_diff,
git_diff_output({" weird.py": [0]}),
)
source_paths = diff.src_paths_changed()
assert source_paths == to_unix_paths([" weird.py"])
def test_duplicate_source_paths(diff, git_diff):
# Duplicate the output for committed, staged, and unstaged changes
diff_output = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(diff, git_diff, diff_output, diff_output, diff_output)
# Get the source paths in the diff
source_paths = diff.src_paths_changed()
# Should see only one copy of source files
assert source_paths == to_unix_paths(["subdir/file1.py"])
def test_git_source_paths_with_supported_extensions(diff, git_diff):
# Configure the git diff output
_set_git_diff_output(
diff,
git_diff,
git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
),
git_diff_output({"subdir/file2.py": line_numbers(3, 10), "file3.py": [0]}),
git_diff_output({"README.md": line_numbers(3, 10)}),
)
# Set supported extensions
diff._supported_extensions = ["py"]
# Get the source paths in the diff
source_paths = diff.src_paths_changed()
# Validate the source paths, README.md should be left out
assert source_paths == to_unix_paths(
["file3.py", "subdir/file1.py", "subdir/file2.py"]
)
def test_git_lines_changed(diff, git_diff):
# Configure the git diff output
_set_git_diff_output(
diff,
git_diff,
git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
),
git_diff_output({"subdir/file2.py": line_numbers(3, 10), "file3.py": [0]}),
git_diff_output({}, deleted_files=["README.md"]),
)
# Get the lines changed in the diff
lines_changed = diff.lines_changed("subdir/file1.py")
# Validate the lines changed
assert lines_changed == line_numbers(3, 10) + line_numbers(34, 47)
def test_ignore_lines_outside_src(diff, git_diff):
# Add some lines at the start of the diff, before any
# source files are specified
diff_output = git_diff_output({"subdir/file1.py": line_numbers(3, 10)})
main_diff = "\n".join(["- deleted line", "+ added line", diff_output])
# Configure the git diff output
_set_git_diff_output(diff, git_diff, main_diff, "", "")
# Get the lines changed in the diff
lines_changed = diff.lines_changed("subdir/file1.py")
# Validate the lines changed
assert lines_changed == line_numbers(3, 10)
def test_one_line_file(diff, git_diff):
# Files with only one line have a special format
# in which the "length" part of the hunk is not specified
diff_str = dedent(
"""
diff --git a/diff_cover/one_line.txt b/diff_cover/one_line.txt
index 0867e73..9daeafb 100644
--- a/diff_cover/one_line.txt
+++ b/diff_cover/one_line.txt
@@ -1,3 +1 @@
test
-test
-test
"""
).strip()
# Configure the git diff output
_set_git_diff_output(diff, git_diff, diff_str, "", "")
# Get the lines changed in the diff
lines_changed = diff.lines_changed("one_line.txt")
# Expect that no lines are changed
assert not lines_changed
def test_git_deleted_lines(diff, git_diff):
# Configure the git diff output
_set_git_diff_output(
diff,
git_diff,
git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
),
git_diff_output({"subdir/file2.py": line_numbers(3, 10), "file3.py": [0]}),
git_diff_output({}, deleted_files=["README.md"]),
)
# Get the lines changed in the diff
lines_changed = diff.lines_changed("README.md")
# Validate no lines changed
assert not lines_changed
def test_git_unicode_filename(diff, git_diff):
# Filenames with unicode characters have double quotes surrounding them
# in the git diff output.
diff_str = dedent(
"""
diff --git "a/unic\303\270\342\210\202e\314\201.txt" "b/unic\303\270\342\210\202e\314\201.txt"
new file mode 100644
index 0000000..248ebea
--- /dev/null
+++ "b/unic\303\270\342\210\202e\314\201.txt"
@@ -0,0 +1,13 @@
+μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος
+οὐλομένην, ἣ μυρί᾽ Ἀχαιοῖς ἄλγε᾽ ἔθηκε,
+πολλὰς δ᾽ ἰφθίμους ψυχὰς Ἄϊδι προΐαψεν
"""
).strip()
_set_git_diff_output(diff, git_diff, diff_str, "", "")
# Get the lines changed in the diff
lines_changed = diff.lines_changed("unic\303\270\342\210\202e\314\201.txt")
# Expect that three lines changed
assert lines_changed == [1, 2, 3]
def test_git_repeat_lines(diff, git_diff):
# Same committed, staged, and unstaged lines
diff_output = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(diff, git_diff, diff_output, diff_output, diff_output)
# Get the lines changed in the diff
lines_changed = diff.lines_changed("subdir/file1.py")
# Validate the lines changed
assert lines_changed == line_numbers(3, 10) + line_numbers(34, 47)
def test_git_overlapping_lines(diff, git_diff):
main_diff = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
# Overlap, extending the end of the hunk (lines 3 to 10)
overlap_1 = git_diff_output({"subdir/file1.py": line_numbers(5, 14)})
# Overlap, extending the beginning of the hunk (lines 34 to 47)
overlap_2 = git_diff_output({"subdir/file1.py": line_numbers(32, 37)})
# Lines in staged / unstaged overlap with lines in main
_set_git_diff_output(diff, git_diff, main_diff, overlap_1, overlap_2)
# Get the lines changed in the diff
lines_changed = diff.lines_changed("subdir/file1.py")
# Validate the lines changed
assert lines_changed == line_numbers(3, 14) + line_numbers(32, 47)
def test_git_line_within_hunk(diff, git_diff):
main_diff = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
# Surround hunk in main (lines 3 to 10)
surround = git_diff_output({"subdir/file1.py": line_numbers(2, 11)})
# Within hunk in main (lines 34 to 47)
within = git_diff_output({"subdir/file1.py": line_numbers(35, 46)})
# Lines in staged / unstaged overlap with hunks in main
_set_git_diff_output(diff, git_diff, main_diff, surround, within)
# Get the lines changed in the diff
lines_changed = diff.lines_changed("subdir/file1.py")
# Validate the lines changed
assert lines_changed == line_numbers(2, 11) + line_numbers(34, 47)
def test_inter_diff_conflict(diff, git_diff):
# Commit changes to lines 3 through 10
added_diff = git_diff_output({"file.py": line_numbers(3, 10)})
# Delete the lines we modified
deleted_lines = []
for line in added_diff.split("\n"):
# Any added line becomes a deleted line
if line.startswith("+"):
deleted_lines.append(line.replace("+", "-"))
# No need to include lines we already deleted
elif line.startswith("-"):
pass
# Keep any other line
else:
deleted_lines.append(line)
deleted_diff = "\n".join(deleted_lines)
# Try all combinations of diff conflicts
combinations = [
(added_diff, deleted_diff, ""),
(added_diff, "", deleted_diff),
("", added_diff, deleted_diff),
(added_diff, deleted_diff, deleted_diff),
]
for main_diff, staged_diff, unstaged_diff in combinations:
# Set up so we add lines, then delete them
_set_git_diff_output(diff, git_diff, main_diff, staged_diff, unstaged_diff)
assert diff.lines_changed("file.py") == []
def test_git_no_such_file(diff, git_diff):
diff_output = git_diff_output(
{"subdir/file1.py": [1], "subdir/file2.py": [2], "file3.py": [3]}
)
# Configure the git diff output
_set_git_diff_output(diff, git_diff, diff_output, "", "")
lines_changed = diff.lines_changed("no_such_file.txt")
assert not lines_changed
def test_no_diff(diff, git_diff):
# Configure the git diff output
_set_git_diff_output(diff, git_diff, "", "", "")
# Expect no files changed
source_paths = diff.src_paths_changed()
assert source_paths == []
def test_git_diff_error(
diff,
git_diff,
):
invalid_hunk_str = dedent(
"""
diff --git a/subdir/file1.py b/subdir/file1.py
@@ invalid @@ Text
"""
).strip()
no_src_line_str = "@@ -33,10 +34,13 @@ Text"
non_numeric_lines = dedent(
"""
diff --git a/subdir/file1.py b/subdir/file1.py
@@ -1,2 +a,b @@
"""
).strip()
missing_line_num = dedent(
"""
diff --git a/subdir/file1.py b/subdir/file1.py
@@ -1,2 + @@
"""
).strip()
missing_src_str = "diff --git "
# List of (stdout, stderr) git diff pairs that should cause
# a GitDiffError to be raised.
err_outputs = [
invalid_hunk_str,
no_src_line_str,
non_numeric_lines,
missing_line_num,
missing_src_str,
]
for diff_str in err_outputs:
# Configure the git diff output
_set_git_diff_output(diff, git_diff, diff_str, "", "")
# Expect that both methods that access git diff raise an error
with pytest.raises(GitDiffError):
diff.src_paths_changed()
with pytest.raises(GitDiffError):
diff.lines_changed("subdir/file1.py")
def test_plus_sign_in_hunk_bug(diff, git_diff):
# This was a bug that caused a parse error
diff_str = dedent(
"""
diff --git a/file.py b/file.py
@@ -16,16 +16,7 @@ 1 + 2
+ test
+ test
+ test
+ test
"""
)
_set_git_diff_output(diff, git_diff, diff_str, "", "")
lines_changed = diff.lines_changed("file.py")
assert lines_changed == [16, 17, 18, 19]
def test_terminating_chars_in_hunk(diff, git_diff):
# Check what happens when there's an @@ symbol after the
# first terminating @@ symbol
diff_str = dedent(
"""
diff --git a/file.py b/file.py
@@ -16,16 +16,7 @@ and another +23,2 @@ symbol
+ test
+ test
+ test
+ test
"""
)
_set_git_diff_output(diff, git_diff, diff_str, "", "")
lines_changed = diff.lines_changed("file.py")
assert lines_changed == [16, 17, 18, 19]
def test_merge_conflict_diff(diff, git_diff):
# Handle different git diff format when in the middle
# of a merge conflict
diff_str = dedent(
"""
diff --cc subdir/src.py
index d2034c0,e594d54..0000000
diff --cc subdir/src.py
index d2034c0,e594d54..0000000
--- a/subdir/src.py
+++ b/subdir/src.py
@@@ -16,88 -16,222 +16,7 @@@ text
+ test
++<<<<<< HEAD
+ test
++=======
"""
)
_set_git_diff_output(diff, git_diff, diff_str, "", "")
lines_changed = diff.lines_changed("subdir/src.py")
assert lines_changed == [16, 17, 18, 19]
def test_inclusion_list(diff, git_diff):
unstaged_input = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(diff, git_diff, "", "", unstaged_input)
assert diff._get_included_diff_results() == ["", "", unstaged_input]
def test_ignore_staged_inclusion(git_diff):
reporter = GitDiffReporter(git_diff=git_diff, ignore_staged=True)
staged_input = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(reporter, git_diff, "", staged_input, "")
assert reporter._get_included_diff_results() == ["", ""]
def test_ignore_unstaged_inclusion(git_diff):
reporter = GitDiffReporter(git_diff=git_diff, ignore_unstaged=True)
unstaged_input = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(reporter, git_diff, "", "", unstaged_input)
assert reporter._get_included_diff_results() == ["", ""]
def test_ignore_staged_and_unstaged_inclusion(git_diff):
reporter = GitDiffReporter(
git_diff=git_diff, ignore_staged=True, ignore_unstaged=True
)
staged_input = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
unstaged_input = git_diff_output(
{"subdir/file2.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(reporter, git_diff, "", staged_input, unstaged_input)
assert reporter._get_included_diff_results() == [""]
def test_fnmatch(diff):
"""Verify that our fnmatch wrapper works as expected."""
assert diff._fnmatch("foo.py", [])
assert not diff._fnmatch("foo.py", ["*.pyc"])
assert diff._fnmatch("foo.pyc", ["*.pyc"])
assert diff._fnmatch("foo.pyc", ["*.swp", "*.pyc", "*.py"])
def test_fnmatch_returns_the_default_with_empty_default(diff):
"""The default parameter should be returned when no patterns are given."""
sentinel = object()
assert diff._fnmatch("file.py", [], default=sentinel) is sentinel
def test_include_untracked(mocker, git_diff):
reporter = GitDiffReporter(git_diff=git_diff, include_untracked=True)
diff_output = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(
reporter,
git_diff,
staged_diff=diff_output,
untracked=["u1.py", " u2.py", "binary1.bin"],
)
base_open_mock = mocker.mock_open(read_data="1\n2\n3\n")
raise_count = 0
def open_side_effect(*args, **kwargs):
if args[0] == "binary1.bin":
nonlocal raise_count
raise_count += 1
raise UnicodeDecodeError("utf-8", b"", 0, 1, "invalid start byte")
return base_open_mock(*args, **kwargs)
mocker.patch("diff_cover.diff_reporter.open", open_side_effect)
changed = reporter.src_paths_changed()
assert sorted(changed) == [" u2.py", "binary1.bin", "subdir/file1.py", "u1.py"]
assert reporter.lines_changed("u1.py") == [1, 2, 3]
assert reporter.lines_changed(" u2.py") == [1, 2, 3]
assert reporter.lines_changed("binary1.bin") == []
assert raise_count == 1
@pytest.mark.parametrize(
"excluded, supported_extensions, path",
[
(["file.bin"], ["py"], "file.bin"),
([], ["py"], "file.bin"),
],
)
def test_include_untracked__not_valid_path__not_include_it(
git_diff, excluded, supported_extensions, path
):
reporter = GitDiffReporter(
git_diff=git_diff,
include_untracked=True,
supported_extensions=supported_extensions,
exclude=excluded,
)
diff_output = git_diff_output(
{"subdir/file1.py": line_numbers(3, 10) + line_numbers(34, 47)}
)
_set_git_diff_output(
reporter,
git_diff,
staged_diff=diff_output,
untracked=[path],
)
changed = reporter.src_paths_changed()
assert sorted(changed) == ["subdir/file1.py"]
def _set_git_diff_output(
reporter,
diff_tool,
committed_diff="",
staged_diff="",
unstaged_diff="",
untracked=None,
):
"""
Configure the git diff tool to return `committed_diff`,
`staged_diff`, and `unstaged_diff` as outputs from
`git diff`
"""
reporter.clear_cache()
diff_tool.diff_committed.return_value = committed_diff
diff_tool.diff_staged.return_value = staged_diff
diff_tool.diff_unstaged.return_value = unstaged_diff
diff_tool.untracked.return_value = untracked
def test_name_with_default_range(git_diff):
reporter = GitDiffReporter(git_diff=git_diff, ignore_staged=True)
assert reporter.name() == "origin/main...HEAD and unstaged changes"
def test_name_different_range(mocker):
diff_tool = mocker.MagicMock(GitDiffTool)
diff_tool.range_notation = ".."
reporter = GitDiffReporter(git_diff=diff_tool, ignore_staged=True)
assert reporter.name() == "origin/main..HEAD and unstaged changes"
|