Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
akamat10 committed Oct 6, 2024
1 parent 0a8ebeb commit 0417a67
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,41 @@ def test_import_symlink_with_source_outside_of_path(self) -> None:
finally:
os.remove(linked_file_name)

def test_modpath_from_file_path_order(self) -> None:
"""Test for ordering of paths.
The test does the following:
1. Add a tmp directory to beginning of sys.path
2. Create a module file in sub directory of tmp directory
3. If the sub directory is passed as additional directory, module name
should be relative to the subdirectory since additional directory has
higher precedence."""
orig_path = sys.path.copy()
with tempfile.TemporaryDirectory() as tmp_dir:
try:
mod_name = "module"
sub_dirname = "subdir"
sub_dir = tmp_dir + "/" + sub_dirname
os.mkdir(sub_dir)
module_file = f"{sub_dir}/{mod_name}.py"

sys.path.insert(0, str(tmp_dir))
with open(module_file, "w+", encoding="utf-8"):
pass

# Without additional directory, return relative to tmp_dir
self.assertEqual(
modutils.modpath_from_file(module_file), [sub_dirname, mod_name]
)

# With sub directory as additional directory, return relative to
# sub directory
self.assertEqual(
modutils.modpath_from_file(f"{sub_dir}/{mod_name}.py", [sub_dir]),
[mod_name],
)
finally:
sys.path[:] = orig_path

def test_import_symlink_both_outside_of_path(self) -> None:
with tempfile.NamedTemporaryFile() as tmpfile:
linked_file_name = os.path.join(tempfile.gettempdir(), "symlinked_file.py")
Expand Down

0 comments on commit 0417a67

Please sign in to comment.