def hammett_tests_pass(config, callback): # noinspection PyUnresolvedReferences from hammett import main_cli modules_before = set(sys.modules.keys()) # set up timeout import _thread from threading import ( Timer, current_thread, main_thread, ) timed_out = False def timeout(): _thread.interrupt_main() nonlocal timed_out timed_out = True assert current_thread() is main_thread() timer = Timer(config.baseline_time_elapsed * 10, timeout) timer.daemon = True timer.start() # Run tests try: class StdOutRedirect(TextIOBase): def write(self, s): callback(s) return len(s) redirect = StdOutRedirect() sys.stdout = redirect sys.stderr = redirect returncode = main_cli(shlex.split(config.test_command[len(hammett_prefix):])) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ timer.cancel() except KeyboardInterrupt: timer.cancel() if timed_out: raise TimeoutError('In process tests timed out') raise modules_to_force_unload = {x.partition(os.sep)[0].replace('.py', '') for x in config.paths_to_mutate} for module_name in list(sorted(set(sys.modules.keys()) - set(modules_before), reverse=True)): if any(module_name.startswith(x) for x in modules_to_force_unload) or module_name.startswith('tests') or module_name.startswith('django'): del sys.modules[module_name] return returncode == 0
from hammett import main_cli exit(main_cli())