Skip to content

Commit

Permalink
Normalize paths from patch file
Browse files Browse the repository at this point in the history
os.path.join will use either "/" or "\" as path separator, depending on
running host system. Patch files use UNIX separators.
  • Loading branch information
FlTr committed Apr 4, 2024
1 parent d598ea7 commit d6764fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mutmut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ def read_patch_data(patch_file_path: str):
diffs = whatthepatch.parse_patch(f.read())

return {
diff.header.new_path: {change.new for change in diff.changes if change.old is None}
os.path.normpath(diff.header.new_path): {change.new for change in diff.changes if change.old is None}
for diff in diffs if diff.changes
}

Expand Down
13 changes: 13 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import os
from pathlib import Path
from time import sleep
from pytest import raises, fixture
Expand Down Expand Up @@ -140,6 +141,18 @@ def test_read_patch_data_edited_line_is_in_the_list(testpatches_path: Path):
assert file_name in file_changes
assert file_changes[file_name] == {2} # line is added between 2nd and 3rd

def test_read_patch_data_edited_line_in_subfolder_is_in_the_list(testpatches_path: Path):
# arrange
file_name = os.path.join("sub", "existing_file.txt") # unix will use "/", windows "\" to join
file_patch = testpatches_path / "edit_existing_line_in_subfolder.patch"

# act
file_changes = read_patch_data(file_patch)

# assert
assert file_name in file_changes
assert file_changes[file_name] == {2} # line is added between 2nd and 3rd

def test_read_patch_data_renamed_file_edited_line_is_in_the_list(testpatches_path: Path):
# arrange
original_file_name = "existing_file.txt"
Expand Down
10 changes: 10 additions & 0 deletions tests/testdata/test_patches/edit_existing_line_in_subfolder.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
diff --git a/sub/existing_file.txt b/sub/existing_file.txt
index 272b2e4..9767587 100644
--- a/sub/existing_file.txt
+++ b/sub/existing_file.txt
@@ -1,3 +1,3 @@
first line
-second line
+second line is edited
third line
\ No newline at end of file

0 comments on commit d6764fc

Please sign in to comment.