|
22 | 22 | from astroid import modutils
|
23 | 23 | from astroid.const import PY310_PLUS
|
24 | 24 | from astroid.interpreter._import import spec
|
| 25 | +from astroid.util import augmented_sys_path |
25 | 26 |
|
26 | 27 | from . import resources
|
27 | 28 |
|
@@ -175,6 +176,37 @@ def test_import_symlink_with_source_outside_of_path(self) -> None:
|
175 | 176 | finally:
|
176 | 177 | os.remove(linked_file_name)
|
177 | 178 |
|
| 179 | + def test_modpath_from_file_path_order(self) -> None: |
| 180 | + """Test for ordering of paths. |
| 181 | + The test does the following: |
| 182 | + 1. Add a tmp directory to beginning of sys.path via augmented_sys_path |
| 183 | + 2. Create a module file in sub directory of tmp directory |
| 184 | + 3. If the sub directory is passed as additional directory, module name |
| 185 | + should be relative to the subdirectory since additional directory has |
| 186 | + higher precedence.""" |
| 187 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 188 | + with augmented_sys_path([tmp_dir]): |
| 189 | + mod_name = "module" |
| 190 | + sub_dirname = "subdir" |
| 191 | + sub_dir = tmp_dir + "/" + sub_dirname |
| 192 | + os.mkdir(sub_dir) |
| 193 | + module_file = f"{sub_dir}/{mod_name}.py" |
| 194 | + |
| 195 | + with open(module_file, "w+", encoding="utf-8"): |
| 196 | + pass |
| 197 | + |
| 198 | + # Without additional directory, return relative to tmp_dir |
| 199 | + self.assertEqual( |
| 200 | + modutils.modpath_from_file(module_file), [sub_dirname, mod_name] |
| 201 | + ) |
| 202 | + |
| 203 | + # With sub directory as additional directory, return relative to |
| 204 | + # sub directory |
| 205 | + self.assertEqual( |
| 206 | + modutils.modpath_from_file(f"{sub_dir}/{mod_name}.py", [sub_dir]), |
| 207 | + [mod_name], |
| 208 | + ) |
| 209 | + |
178 | 210 | def test_import_symlink_both_outside_of_path(self) -> None:
|
179 | 211 | with tempfile.NamedTemporaryFile() as tmpfile:
|
180 | 212 | linked_file_name = os.path.join(tempfile.gettempdir(), "symlinked_file.py")
|
|
0 commit comments