def test_path_to_module_name(self): """Test path_to_module_name.""" self.assertEqual('one.two.three', path_to_module_name('one/two/three.py')) self.assertEqual('one.two', path_to_module_name('one/two/__init__.py'))
def test_path_to_module_name(self): """Test path_to_module_name.""" self.assertEqual( 'one.two.three', path_to_module_name('one/two/three.py') ) self.assertEqual( 'one.two', path_to_module_name('one/two/__init__.py') )
def __str__(self): test_method = getattr(self, self._testMethodName) return "%s (%s: %s)" % ( test_method.scenario.name, path_to_module_name(self.feature.filename), self.feature.name, )
def _in_directory(directory): """ A context manager to run the payload in a specified directory. On exit, all modules that were imported from this directory are removed from sys.modules. """ directory = os.path.abspath(directory) last_wd = os.getcwd() os.chdir(directory) try: yield finally: os.chdir(last_wd) # Unload modules which are loaded from the directory unload_modules = [] unload_path_prefix = os.path.join(directory, '') for module_name, module in sys.modules.items(): # Find out where is the module loaded from try: path = module.__file__ except AttributeError: # Maybe a namespace package module? try: if module.__spec__.origin == 'namespace': # pylint:disable=protected-access path = module.__path__._path[0] # pylint:enable=protected-access else: continue except AttributeError: continue # Is it loaded from a file in the directory? if not path: continue path = os.path.abspath(path) if not path.startswith(unload_path_prefix): continue # Does its name match what would it be if the module was really # imported from here? Consider two directories, 'foo' and 'foo/bar' # on sys.path, foo/bar/baz.py might have been loaded from either. path = path[len(unload_path_prefix):] if module_name == path_to_module_name(path): unload_modules.append(module_name) for module in unload_modules: del sys.modules[module]
def __str__(self): return "%s (%s: %s)" % ( self._testMethodName, path_to_module_name(self.feature.described_at.file), self.feature.name, )