Fix renaming of namespace packages, fixes #1779

This commit is contained in:
Dave Halter
2023-07-27 03:06:27 +02:00
parent f2444b4be5
commit 5f19237a3e
4 changed files with 66 additions and 9 deletions

View File

@@ -76,7 +76,7 @@ from import_tree.pkg.mod1 import not_existant,
#? 22 ['mod1', 'base']
from import_tree.pkg. import mod1
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'references', 'rename1', 'rename2', 'classes', 'globals', 'recurse_class1', 'recurse_class2', 'invisible_pkg', 'flow_import']
from import_tree. import pkg
from import_tree. import new_pkg
#? 18 ['pkg']
from import_tree.p import pkg

View File

@@ -1,4 +1,5 @@
import os
import shutil
from textwrap import dedent
from pathlib import Path
import platform
@@ -6,6 +7,7 @@ import platform
import pytest
import jedi
from test.helpers import get_example_dir
@pytest.fixture()
@@ -52,6 +54,46 @@ def test_rename_mod(Script, dir_with_content):
''').format(dir=dir_with_content)
def test_namespace_package(Script, tmpdir, skip_pre_python38):
origin = get_example_dir('implicit_namespace_package')
shutil.copytree(origin, tmpdir.strpath, dirs_exist_ok=True)
sys_path = [
os.path.join(tmpdir.strpath, 'ns1'),
os.path.join(tmpdir.strpath, 'ns2')
]
script_path = os.path.join(tmpdir.strpath, 'script.py')
script = Script(
'import pkg\n',
path=script_path,
project=jedi.Project(os.path.join(tmpdir.strpath, 'does-not-exist'), sys_path=sys_path),
)
refactoring = script.rename(line=1, new_name='new_pkg')
refactoring.apply()
old1 = os.path.join(sys_path[0], "pkg")
new1 = os.path.join(sys_path[0], "new_pkg")
old2 = os.path.join(sys_path[1], "pkg")
new2 = os.path.join(sys_path[1], "new_pkg")
assert not os.path.exists(old1)
assert os.path.exists(new1)
assert not os.path.exists(old2)
assert os.path.exists(new2)
changed, = iter(refactoring.get_changed_files().values())
assert changed.get_new_code() == "import new_pkg\n"
assert refactoring.get_diff() == dedent(f'''\
rename from {old1}
rename to {new1}
rename from {old2}
rename to {new2}
--- {script_path}
+++ {script_path}
@@ -1,2 +1,2 @@
-import pkg
+import new_pkg
''').format(dir=dir_with_content)
def test_rename_none_path(Script):
refactoring = Script('foo', path=None).rename(new_name='bar')
with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):