1. eduardo schettino
  2. pytest-incremental

Commits

eduardo schettino  committed 5e12851

handle intra package imports

  • Participants
  • Parent commits 6f7d8e7
  • Branches default

Comments (0)

Files changed (4)

File pytest_doit.py

View file
                 name_list.reverse()
                 return name_list
             current_path = os.path.basename(parent_path)
-            parent_path = os.path.dirname(current_path)
+            parent_path = os.path.dirname(parent_path)
 
     def add_import(self, module):
         """add another module as import
         """set imports for module"""
         module.imports = set()
         raw_imports = find_imports(module.path)
-        for x in raw_imports:
+        for import_entry in raw_imports:
+            try_names = []
             # join 'from' and 'import' part of import statement
-            full = ".".join(s for s in x[:2] if s is not None)
+            full = ".".join(s for s in import_entry[:2] if s)
 
-            # TODO: if levels
+            import_level = import_entry[3]
+            if import_level:
+                # intra package imports
+                intra = '.'.join(module.name[:-import_level] + [full])
+                try_names = (intra,)
+            else:
+                # deal with old-style relative imports
+                module_pkg = '.'.join(module.name[:-1])
+                full_relative = "%s.%s" % (module_pkg, full)
+                try_names = (full_relative, full,)
 
-            # deal with old-style relative imports
-            module_pkg = '.'.join(module.name[:-1])
-            full_relative = "%s.%s" % (module_pkg, full)
-
-            for imported_name in (full_relative, full):
+            for imported_name in try_names:
                 imported = self._get_imported_module(imported_name)
                 if imported:
                     module.add_import(imported)
                     break
 
-            # didnt find... must be out
+            # didnt find... must be out of tracked namespaces
 
 
 ######### start doit section

File tests/sample/subsample/__init__.py

Empty file added.

File tests/sample/subsample/sample_c.py

View file
+from .. import sample_a

File tests/test_import.py

View file
 SAMPLE_INIT = os.path.join(SAMPLE, '__init__.py')
 SAMPLE_A = os.path.join(SAMPLE, 'sample_a.py')
 SAMPLE_B = os.path.join(SAMPLE, 'sample_b.py')
-SAMPLE_C = os.path.join(SAMPLE, 'sample_c.py')
+SUBSAMPLE = os.path.join(SAMPLE, 'subsample')
+SAMPLE_C = os.path.join(SUBSAMPLE, 'sample_c.py')
 
 def test_find_imports():
     imports = find_imports(SAMPLE_A)
     assert 7 == len(imports)
 
 
-class Test_PyModule(object):
-    pass # TODO
-
-
 class Test_ModuleSet(object):
 
     def test_import_top(self, monkeypatch):
         assert ['sample','sample_a'] == modset.by_path[SAMPLE_A].name
         assert 1 == len(module.imports)
 
+    def test_intrapkg_import(self):
+        modset = ModuleSet([SAMPLE_INIT, SAMPLE_A])
+        module = _PyModule(SAMPLE_C)
+        modset.set_imports(module)
+        assert SAMPLE_A in module.imports
+        assert ['sample','sample_a'] == modset.by_path[SAMPLE_A].name
+        assert 1 == len(module.imports)
+