@@ -175,6 +175,41 @@ def test_import_symlink_with_source_outside_of_path(self) -> None:
175
175
finally :
176
176
os .remove (linked_file_name )
177
177
178
+ def test_modpath_from_file_path_order (self ) -> None :
179
+ """Test for ordering of paths.
180
+ The test does the following:
181
+ 1. Add a tmp directory to beginning of sys.path
182
+ 2. Create a module file in sub directory of tmp directory
183
+ 3. If the sub directory is passed as additional directory, module name
184
+ should be relative to the subdirectory since additional directory has
185
+ higher precedence."""
186
+ orig_path = sys .path .copy ()
187
+ with tempfile .TemporaryDirectory () as tmp_dir :
188
+ try :
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
+ sys .path .insert (0 , str (tmp_dir ))
196
+ with open (module_file , "w+" , encoding = "utf-8" ):
197
+ pass
198
+
199
+ # Without additional directory, return relative to tmp_dir
200
+ self .assertEqual (
201
+ modutils .modpath_from_file (module_file ), [sub_dirname , mod_name ]
202
+ )
203
+
204
+ # With sub directory as additional directory, return relative to
205
+ # sub directory
206
+ self .assertEqual (
207
+ modutils .modpath_from_file (f"{ sub_dir } /{ mod_name } .py" , [sub_dir ]),
208
+ [mod_name ],
209
+ )
210
+ finally :
211
+ sys .path [:] = orig_path
212
+
178
213
def test_import_symlink_both_outside_of_path (self ) -> None :
179
214
with tempfile .NamedTemporaryFile () as tmpfile :
180
215
linked_file_name = os .path .join (tempfile .gettempdir (), "symlinked_file.py" )
0 commit comments