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 __enter__(self): self._exit_stack = ExitStack() self._exit_stack.__enter__() try: self._exit_stack.enter_context(self._prelude_logging_context()) self._exit_stack.enter_context(self._sigterm_context()) with dessert.rewrite_assertions_context(): site.load(working_directory=self._working_directory) cli_utils.configure_arg_parser_by_plugins(self.arg_parser) cli_utils.configure_arg_parser_by_config(self.arg_parser) argv = cli_utils.add_pending_plugins_from_commandline(self._get_argv()) self._parsed_args, self._positional_args = self.arg_parser.parse_known_args(argv) self._exit_stack.enter_context( cli_utils.get_modified_configuration_from_args_context(self.arg_parser, self._parsed_args) ) self.session = Session(reporter=self.get_reporter(), console_stream=self._report_stream) trigger_hook.configure() # pylint: disable=no-member plugins.manager.configure_for_parallel_mode() plugins.manager.activate_pending_plugins() cli_utils.configure_plugins_from_args(self._parsed_args) self._exit_stack.enter_context(self.session) self._emit_prelude_logs() return self except: self._emit_prelude_logs() self.__exit__(*sys.exc_info()) raise
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 _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 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 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 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 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 _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 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"
import sys if __name__ == "__main__": [_, example_name] = sys.argv # pylint: disable=unbalanced-tuple-unpacking import dessert with dessert.rewrite_assertions_context(): import examples func = getattr(examples, example_name) try: func() except AssertionError as e: print(e) else: sys.exit('Assertion not matched')