Fix an issue with creatin a diff, fixes #1757

This commit is contained in:
Dave Halter
2022-11-13 17:51:36 +01:00
parent 26f7878d97
commit 0ba48bbb9d
2 changed files with 24 additions and 2 deletions

View File

@@ -42,11 +42,17 @@ class ChangedFile:
if self._from_path is None: if self._from_path is None:
from_p = '' from_p = ''
else: else:
from_p = self._from_path.relative_to(project_path) try:
from_p = self._from_path.relative_to(project_path)
except ValueError: # Happens it the path is not on th project_path
from_p = self._from_path
if self._to_path is None: if self._to_path is None:
to_p = '' to_p = ''
else: else:
to_p = self._to_path.relative_to(project_path) try:
to_p = self._to_path.relative_to(project_path)
except ValueError:
to_p = self._to_path
diff = difflib.unified_diff( diff = difflib.unified_diff(
old_lines, new_lines, old_lines, new_lines,
fromfile=str(from_p), fromfile=str(from_p),

View File

@@ -70,3 +70,19 @@ def test_diff_without_ending_newline(Script):
-a -a
+c +c
''') ''')
def test_diff_path_outside_of_project(Script):
script = Script(
code='foo = 1',
path='/unknown_dir/file.py',
project=jedi.get_default_project()
)
diff = script.rename(line=1, column=0, new_name='bar').get_diff()
assert diff == dedent('''\
--- /unknown_dir/file.py
+++ /unknown_dir/file.py
@@ -1 +1 @@
-foo = 1
+bar = 1
''')