def load(self): if os.path.isfile(self.path): return emport.import_file(self.path) main = self.config.get('main', 'main') if not main.endswith('.py'): main += '.py' return emport.import_file(os.path.join(self.path, main))
def _test__importing_directory(self, init_py_directly): directory = os.path.join(self.root, "pkg") os.makedirs(directory) _, expected_value = self._create_module(directory, "__init__.py") if init_py_directly: module = emport.import_file(os.path.join(directory, "__init__.py")) else: module = emport.import_file(directory) self.assertRaises(module.__name__.endswith(".pkg")) self.assertEquals(module.value, expected_value)
def test_importing_directory(init_py_directly, tmpdir, module_file_factory): directory = str(tmpdir.join("pkg")) os.makedirs(directory) _, expected_value = module_file_factory(directory, "__init__.py") if init_py_directly: module = emport.import_file(os.path.join(directory, "__init__.py")) else: module = emport.import_file(directory) assert module.__name__.endswith(".pkg") assert module.value == expected_value
def test_importing_subdirectory_init_file(tmpdir, module_file_factory): directory = tmpdir.join('pkg') module_file_factory(directory, '__init__.py') subdir = directory.join('subpkg') filename, expected_value = module_file_factory(subdir, '__init__.py') module = emport.import_file(filename) assert module.value == expected_value
def test_importing_sub_package(tmpdir, module_file_factory): subpackage_dir = tmpdir.join('subpackage') subpackage_dir.join('__init__.py').ensure(file=True) full_filename, expected_value = module_file_factory( subpackage_dir, "module.py") module = emport.import_file(full_filename) assert module.__name__.endswith('.subpackage.module')
def _iter_paths(self, paths): paths = list(paths) for path in paths: if not os.path.exists(path): msg = "Path {!r} could not be found".format(path) with handling_exceptions(): raise CannotLoadTests(msg) for path in paths: for file_path in _walk(path): _logger.debug("Checking {0}", file_path) if not self._is_file_wanted(file_path): _logger.debug("{0} is not wanted. Skipping...", file_path) continue module = None try: with handling_exceptions(context="during import"): if not config.root.run.message_assertion_introspection: dessert.disable_message_introspection() with dessert.rewrite_assertions_context(): module = import_file(file_path) except Exception as e: tb_file, tb_lineno, _, _ = _extract_tb() raise mark_exception_handled( CannotLoadTests( "Could not load {0!r} ({1}:{2} - {3})".format(file_path, tb_file, tb_lineno, e))) if module is not None: self._duplicate_funcs |= check_duplicate_functions(file_path) with self._adding_local_fixtures(file_path, module): for runnable in self._iter_runnable_tests_in_module(file_path, module): if self._is_excluded(runnable): continue yield runnable
def _import_and_verify(filenames_and_expected_values): returned = [] for filename, expected_value in filenames_and_expected_values: module = emport.import_file(filename) assert module.value == expected_value returned.append(module) return returned
def test_traceback_line_numbers(tmpdir): filename = tmpdir.join('filename.py') with filename.open('w') as f: f.write('''from contextlib import contextmanager def f(): with context(): a = 1 b = 2 g() c = 3 d = 4 def g(): 1/0 @contextmanager def context(): yield ''') mod = emport.import_file(str(filename)) try: mod.f() except ZeroDivisionError: err = Error(exc_info=sys.exc_info()) else: assert False, 'did not fail' assert err.traceback.frames[-2].lineno == 6
def test__importing_sub_package(self): subpackage_dir = os.path.join(self.root, "subpackage") os.makedirs(subpackage_dir) _touch(subpackage_dir, "__init__.py") full_filename, expected_value = self._create_module(subpackage_dir, "module.py") module = emport.import_file(full_filename) self.assertTrue(module.__name__.endswith(".subpackage.module"))
def _import_and_verify(self, filenames_and_expected_values): returned = [] for filename, expected_value in filenames_and_expected_values: module = emport.import_file(filename) self.assertEquals(module.value, expected_value) returned.append(module) return returned
def _iter_paths(self, paths): paths = list(paths) for path in paths: if not os.path.exists(path): msg = "Path {0} could not be found".format(path) with handling_exceptions(): raise CannotLoadTests(msg) for path in paths: for file_path in _walk(path): _logger.debug("Checking {0}", file_path) if not self._is_file_wanted(file_path): _logger.debug("{0} is not wanted. Skipping...", file_path) continue module = None try: with handling_exceptions(context="during import"): with dessert.rewrite_assertions_context(): module = import_file(file_path) except Exception as e: tb_file, tb_lineno, _, _ = traceback.extract_tb(sys.exc_info()[2])[-1] raise CannotLoadTests( "Could not load {0!r} ({1}:{2} - {3})".format(file_path, tb_file, tb_lineno, e)) if module is not None: with self._adding_local_fixtures(file_path, module): for runnable in self._iter_runnable_tests_in_module(file_path, module): if self._is_excluded(runnable): continue yield runnable
def _iter_paths(self, paths): paths = list(paths) for path in paths: if not os.path.exists(path): msg = "Path {0} could not be found".format(path) add_error(msg) raise CannotLoadTests(msg) for path in paths: for file_path in _walk(path): _logger.debug("Checking {0}", file_path) if not self._is_file_wanted(file_path): _logger.debug("{0} is not wanted. Skipping...", file_path) continue module = None with self._handling_import_errors(file_path): try: with dessert.rewrite_assertions_context(): module = import_file(file_path) except Exception as e: raise CannotLoadTests( "Could not load {0!r} ({1})".format(file_path, e)) if module is not None: with self._adding_local_fixtures(file_path, module): for runnable in self._iter_runnable_tests_in_module(file_path, module): if self._is_excluded(runnable): continue yield runnable
def _iter_paths(self, paths): paths = list(paths) for path in paths: if not os.path.exists(path): msg = "Path {0} could not be found".format(path) with handling_exceptions(): raise CannotLoadTests(msg) for path in paths: for file_path in _walk(path): _logger.debug("Checking {0}", file_path) if not self._is_file_wanted(file_path): _logger.debug("{0} is not wanted. Skipping...", file_path) continue module = None try: with handling_exceptions(context="during import"): with dessert.rewrite_assertions_context(): module = import_file(file_path) except Exception as e: tb_file, tb_lineno, _, _ = traceback.extract_tb( sys.exc_info()[2])[-1] raise CannotLoadTests( "Could not load {0!r} ({1}:{2} - {3})".format( file_path, tb_file, tb_lineno, e)) if module is not None: with self._adding_local_fixtures(file_path, module): for runnable in self._iter_runnable_tests_in_module( file_path, module): if self._is_excluded(runnable): continue yield runnable
def test_warnings_from_rewrite(source_filename): tmp_dir = os.path.dirname(source_filename) full_path = os.path.join(tmp_dir, 'file_with_warnings.py') with open(full_path, "w") as f: f.write(r""" import warnings warnings.simplefilter('always') warnings.warn('Some import warning') def func(): assert True """) with dessert.rewrite_assertions_context(): with _disable_pytest_rewriting(): with pytest.warns(None) as caught: emport.import_file(full_path) [warning] = caught.list assert warning.filename == full_path
def test_import_set_name(tmpdir): package_name = '__pkg_{0}__'.format(str(uuid4()).replace('-', '_')) emport.set_package_name(tmpdir, package_name) with tmpdir.join('file.py').open('w') as f: f.write('value = 666') mod = emport.import_file(f.name) assert mod.__name__ == '{0}.file'.format(package_name)
def load_python_module_by_name(self, rel_filename): assert not os.path.isabs(rel_filename) rel_filename = os.path.join(self.path, rel_filename) if not os.path.isfile(rel_filename) and not rel_filename.endswith('.py'): rel_filename += '.py' if not os.path.isfile(rel_filename): raise RuntimeError(f'File does not exist: {rel_filename!r}') module = emport.import_file(rel_filename) return module
def module(request, source_filename): with dessert.rewrite_assertions_context(): with _disable_pytest_rewriting(): module = emport.import_file(source_filename) @request.addfinalizer def drop_from_sys_modules(): sys.modules.pop(module.__name__) return module
def module(request, source_filename, assertion_line): with dessert.rewrite_assertions_context(): with _disable_pytest_rewriting(): module = emport.import_file(source_filename) @request.addfinalizer def drop_from_sys_modules(): sys.modules.pop(module.__name__) return module
def _register_blueprints(self, flask_app): config = self._load_config() for blueprint_filename in self.workdir.join('blueprints').listdir(): if blueprint_filename.ext != '.py' or blueprint_filename.basename.startswith('_'): _logger.debug('Ignoring {}', blueprint_filename) continue _logger.debug('Registering blueprint {.basename}', blueprint_filename) mod = import_file(str(blueprint_filename)) flask_app.register_blueprint(mod.blueprint)
def _register_blueprints(self, flask_app): config = self._load_config() for blueprint_filename in self.workdir.join('blueprints').listdir(): if blueprint_filename.ext != '.py' or blueprint_filename.basename.startswith( '_'): _logger.debug('Ignoring {}', blueprint_filename) continue _logger.debug('Registering blueprint {.basename}', blueprint_filename) mod = import_file(str(blueprint_filename)) flask_app.register_blueprint(mod.blueprint)
def discover(self): """ Iterates over all search paths and loads plugins """ for search_path in config.root.plugins.search_paths: for path, _, filenames in os.walk(search_path): for filename in filenames: if not filename.endswith(".py"): continue module = import_file(os.path.join(path, filename)) install_func = getattr(module, "install_plugins", None) if install_func is None: continue install_func()
def iter_paths(self, paths): paths = list(paths) for path in paths: if not os.path.exists(path): raise CannotLoadTests("Path {0} could not be found".format(path)) for path in paths: for file_path in _walk(path): _logger.debug("Checking {0}", file_path) if not self._is_file_wanted(file_path): _logger.debug("{0} is not wanted. Skipping...", file_path) continue with self._handling_import_errors(): module = import_file(file_path) for runnable in self._iter_runnable_tests_in_module(module): yield runnable
def test_dessert_interop(tmpdir): path = tmpdir.join('testme.py') with path.open('w') as f: f.write("""def f(): a = 1 b = 2 assert a == b """) with _disable_pytest_rewriting(): with dessert.rewrite_assertions_context(): mod = import_file(str(path)) with pytest.raises(AssertionError) as caught: mod.f() assert '1 == 2' in str(caught.value)
def _build_config(self, path): confstack = [] for dir_path in self._traverse_upwards(path): slashconf_vars = self._slashconf_vars_cache.get(dir_path) if slashconf_vars is None: slashconf_path = os.path.join(dir_path, 'slashconf.py') if os.path.isfile(slashconf_path): with dessert.rewrite_assertions_context(): slashconf_vars = self._slashconf_vars_cache[dir_path] = vars(import_file(slashconf_path)) if slashconf_vars is not None: confstack.append(slashconf_vars) returned = {} # start loading from the parent so that vars are properly overriden for slashconf_vars in reversed(confstack): returned.update(slashconf_vars) return returned
def _build_config(self, path): confstack = [] for dir_path in self._traverse_upwards(path): slashconf_vars = self._slashconf_vars_cache.get(dir_path) if slashconf_vars is None: slashconf_path = os.path.join(dir_path, 'slashconf.py') if os.path.isfile(slashconf_path): self.duplicate_funcs |= check_duplicate_functions(slashconf_path) with dessert.rewrite_assertions_context(): slashconf_vars = self._slashconf_vars_cache[dir_path] = vars(import_file(slashconf_path)) if slashconf_vars is not None: confstack.append(slashconf_vars) returned = {} # start loading from the parent so that vars are properly overriden for slashconf_vars in reversed(confstack): returned.update(slashconf_vars) return returned
def test_importing_doesnt_emit_warnings(tmpdir, recwarn, add_init_py): value = str(uuid4()) warnings.simplefilter('always') directory = tmpdir.join('files') filename = directory.join('testfile.py') with directory.join('utils.py').open('w', ensure=True) as f: f.write('value = {0!r}'.format(value)) with filename.open('w') as f: f.write('from .utils import value as new_value') if add_init_py: with directory.join('__init__.py').open('w') as f: pass module = import_file(str(filename)) assert module.new_value == value assert len(recwarn.list) == 0
def assertion_error(tmpdir): filename = tmpdir.join("file.py") filename.write(""" def f(x): return x def g(x): return x def func(): assert f(g(1)) == g(f(2))""") with dessert.rewrite_assertions_context(): module = emport.import_file(str(filename)) try: module.func() except: return Error.capture_exception() else: assert False, "Did not fail"
def test_module_specs(tmpdir, recwarn): package_dir = tmpdir.join('package') subpackage_dir = package_dir.join('sub') for p in (package_dir, subpackage_dir): with p.join('__init__.py').open('w', ensure=True) as f: pass with subpackage_dir.join('utils.py').open('w') as f: pass filename = subpackage_dir.join('module.py') with filename.open('w', ensure=True) as f: f.write('from . import utils') mod = emport.import_file(str(filename)) for m in (mod, mod.utils): metapackage_name, remainder = m.__spec__.name.split('.', 1) assert remainder == 'package.sub.{}'.format(m.__name__.split('.')[-1]) # pylint: disable=no-member assert mod.__spec__.parent == '{0}.package.sub'.format(metapackage_name) # pylint: disable=no-member metapackage = sys.modules[metapackage_name] assert metapackage.__spec__ is not None assert metapackage.__spec__.origin == str(package_dir.dirname) assert metapackage.__spec__.name == metapackage_name # TODO: restore this #assert metapackage.__spec__.parent == metapackage_name assert metapackage.__spec__.submodule_search_locations is not None assert str(package_dir.dirname) in metapackage.__spec__.submodule_search_locations assert metapackage.__package__ == metapackage_name assert not recwarn.list
def test_module_specs(tmpdir, recwarn): package_dir = tmpdir.join('package') subpackage_dir = package_dir.join('sub') for p in (package_dir, subpackage_dir): with p.join('__init__.py').open('w', ensure=True) as f: pass with subpackage_dir.join('utils.py').open('w') as f: pass filename = subpackage_dir.join('module.py') with filename.open('w', ensure=True) as f: f.write('from . import utils') mod = emport.import_file(str(filename)) for m in (mod, mod.utils): metapackage_name, remainder = m.__spec__.name.split('.', 1) assert remainder == 'package.sub.{}'.format(m.__name__.split('.')[-1]) # pylint: disable=no-member assert mod.__spec__.parent == '{0}.package.sub'.format( metapackage_name) # pylint: disable=no-member metapackage = sys.modules[metapackage_name] assert metapackage.__spec__ is not None assert metapackage.__spec__.origin == str(package_dir.dirname) assert metapackage.__spec__.name == metapackage_name # TODO: restore this #assert metapackage.__spec__.parent == metapackage_name assert metapackage.__spec__.submodule_search_locations is not None assert str( package_dir.dirname) in metapackage.__spec__.submodule_search_locations assert metapackage.__package__ == metapackage_name assert not recwarn.list
def test_importing_directory_no_init_file(tmpdir): with pytest.raises(emport.NoInitFileFound): emport.import_file(str(tmpdir))
def from_response(cls, response: requests.Response) -> "CombadgePython": with NamedTemporaryFile(mode="w", suffix=".py", delete=False) as combadge_file: combadge_file.write(response.text) combadge_file.flush() return cls(emport.import_file(combadge_file.name))
def test_relative_imports(directory): module = import_file(str(directory.join('proxy.py'))) assert module.file_1_value == 'file_1'
def test__importing_directory_no_init_file(self): with self.assertRaises(emport.NoInitFileFound): emport.import_file(self.root)