def test_running_pyo(self): pycfile = self.make_pyc() pyofile = re.sub(r"[.]pyc$", ".pyo", pycfile) assert pycfile != pyofile os.rename(pycfile, pyofile) run_python_file([pyofile]) assert self.stdout() == "I am here!\n"
def test_run_python_file(self): tryfile = os.path.join(here, "try_execfile.py") run_python_file(tryfile, [tryfile, "arg1", "arg2"]) mod_globs = eval(self.stdout()) # The file should think it is __main__ self.assertEqual(mod_globs['__name__'], "__main__") # It should seem to come from a file named try_execfile.py dunder_file = os.path.basename(mod_globs['__file__']) self.assertEqual(dunder_file, "try_execfile.py") # It should have its correct module data. self.assertEqual(mod_globs['__doc__'], "Test file for run_python_file.") self.assertEqual(mod_globs['DATA'], "xyzzy") self.assertEqual(mod_globs['FN_VAL'], "my_fn('fooey')") # It must be self-importable as __main__. self.assertEqual(mod_globs['__main__.DATA'], "xyzzy") # Argv should have the proper values. self.assertEqual(mod_globs['argv'], [tryfile, "arg1", "arg2"]) # __builtins__ should have the right values, like open(). self.assertEqual(mod_globs['__builtins__.has_open'], True)
def test_run_python_file(self): run_python_file([TRY_EXECFILE, "arg1", "arg2"]) mod_globs = json.loads(self.stdout()) # The file should think it is __main__ self.assertEqual(mod_globs['__name__'], "__main__") # It should seem to come from a file named try_execfile.py dunder_file = os.path.basename(mod_globs['__file__']) self.assertEqual(dunder_file, "try_execfile.py") # It should have its correct module data. self.assertEqual(mod_globs['__doc__'].splitlines()[0], "Test file for run_python_file.") self.assertEqual(mod_globs['DATA'], "xyzzy") self.assertEqual(mod_globs['FN_VAL'], "my_fn('fooey')") # It must be self-importable as __main__. self.assertEqual(mod_globs['__main__.DATA'], "xyzzy") # Argv should have the proper values. self.assertEqual(mod_globs['argv0'], TRY_EXECFILE) self.assertEqual(mod_globs['argv1-n'], ["arg1", "arg2"]) # __builtins__ should have the right values, like open(). self.assertEqual(mod_globs['__builtins__.has_open'], True)
def test_running_pyo(self): pycfile = self.make_pyc() pyofile = re.sub(r"[.]pyc$", ".pyo", pycfile) self.assertNotEqual(pycfile, pyofile) os.rename(pycfile, pyofile) run_python_file(pyofile, [pyofile]) self.assertEqual(self.stdout(), "I am here!\n")
def test_running_pyo(self): pycfile = self.make_pyc() pyofile = re.sub(r"[.]pyc$", ".pyo", pycfile) self.assertNotEqual(pycfile, pyofile) os.rename(pycfile, pyofile) run_python_file([pyofile]) self.assertEqual(self.stdout(), "I am here!\n")
def run(self, command_line): self.run_id = str(uuid.uuid4()) try: with TracerContext(self): self.publisher.send( 'start_run', {'run_id': self.run_id, 'cwd': os.getcwd(), 'command_line': command_line}, ) run_python_file( command_line[0], command_line, ) except ExceptionDuringRun as err: # TODO: Include more details about the traceback. self.publisher.send( 'exception', {'run_id': self.run_id, 'message': unicode(err), 'error': err, 'filename': '', }, ) finally: self.publisher.send( 'end_run', {'run_id': self.run_id}, ) self.run_id = None
def test_directory_with_main(self): self.make_file( "with_main/__main__.py", """\ print("I am __main__") """) run_python_file(["with_main"]) self.assertEqual(self.stdout(), "I am __main__\n")
def test_universal_newlines(self): # Make sure we can read any sort of line ending. pylines = """# try newlines|print('Hello, world!')|""".split('|') for nl in ('\n', '\r\n', '\r'): with open('nl.py', 'wb') as fpy: fpy.write(nl.join(pylines).encode('utf-8')) run_python_file('nl.py', ['nl.py']) self.assertEqual(self.stdout(), "Hello, world!\n"*3)
def test_universal_newlines(self): # Make sure we can read any sort of line ending. pylines = """# try newlines|print('Hello, world!')|""".split("|") for nl in ("\n", "\r\n", "\r"): with open("nl.py", "wb") as fpy: fpy.write(nl.join(pylines).encode("utf-8")) run_python_file("nl.py", ["nl.py"]) self.assertEqual(self.stdout(), "Hello, world!\n" * 3)
def test_universal_newlines(self): # Make sure we can read any sort of line ending. pylines = """# try newlines|print('Hello, world!')|""".split('|') for nl in ('\n', '\r\n', '\r'): with open('nl.py', 'wb') as fpy: fpy.write(nl.join(pylines).encode('utf-8')) run_python_file(['nl.py']) self.assertEqual(self.stdout(), "Hello, world!\n" * 3)
def test_no_extra_file(self): # Make sure that running a file doesn't create an extra compiled file. self.make_file("xxx", """\ desc = "a non-.py file!" """) self.assertEqual(os.listdir("."), ["xxx"]) run_python_file("xxx", ["xxx"]) self.assertEqual(os.listdir("."), ["xxx"])
def test_running_pyc_from_wrong_python(self): pycfile = self.make_pyc() # Jam Python 2.1 magic number into the .pyc file. with open(pycfile, "r+b") as fpyc: fpyc.seek(0) fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a])) with self.assertRaisesRegex(NoCode, "Bad magic number in .pyc file"): run_python_file(pycfile, [pycfile])
def test_missing_final_newline(self): # Make sure we can deal with a Python file with no final newline. self.make_file("abrupt.py", """\ if 1: a = 1 print("a is %r" % a) #""") abrupt = open("abrupt.py").read() self.assertEqual(abrupt[-1], '#') run_python_file("abrupt.py", ["abrupt.py"]) self.assertEqual(self.stdout(), "a is 1\n")
def test_missing_final_newline(self): # Make sure we can deal with a Python file with no final newline. self.make_file("abrupt.py", """\ if 1: a = 1 print("a is %r" % a) #""") with open("abrupt.py") as f: abrupt = f.read() assert abrupt[-1] == '#' run_python_file(["abrupt.py"]) assert self.stdout() == "a is 1\n"
def test_running_pyc_from_wrong_python(self): pycfile = self.make_pyc() # Jam Python 2.1 magic number into the .pyc file. with open(pycfile, "r+b") as fpyc: fpyc.seek(0) fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a])) with self.assertRaisesRegex(NoCode, "Bad magic number in .pyc file"): run_python_file([pycfile]) # In some environments, the pycfile persists and pollutes another test. os.remove(pycfile)
def test_running_py_from_binary(self): # Use make_file to get the bookkeeping. Ideally, it would # be able to write binary files. bf = self.make_file("binary") with open(bf, "wb") as f: f.write(b'\x7fELF\x02\x01\x01\x00\x00\x00') path = python_reported_file('binary') msg = ( re.escape(f"Couldn't run '{path}' as Python code: ") + r"(TypeError|ValueError): source code string cannot contain null bytes" ) with pytest.raises(Exception, match=msg): run_python_file([bf])
def test_excepthook_exit(self): self.make_file("excepthook_exit.py", """\ import sys def excepthook(*args): print('in excepthook') sys.exit(0) sys.excepthook = excepthook raise RuntimeError('Error Outside') """) with pytest.raises(SystemExit): run_python_file(["excepthook_exit.py"]) cov_out = self.stdout() assert cov_out == "in excepthook\n"
def main(argv=None): parser = argparse.ArgumentParser() add_arguments(parser) args, remaining_args = parser.parse_known_args(argv) for path in args.path: if path not in sys.path: sys.path.append(path) try: import coverage from coverage.execfile import run_python_module, run_python_file except ImportError: print("Error: coverage is not available.") sys.exit(1) cov = coverage.coverage(source=args.source) cov.erase() cov.clear_exclude() if args.no_pragmas: args.pragma = [] args.pragma = args.pragma or DEFAULT_PRAGMAS if args.show: args.show_missing = True for pragma in args.show: if pragma in args.pragma: args.pragma.remove(pragma) for pragma in args.pragma: cov.exclude('pragma: %s' % pragma) ret = 0 cov.start() try: if remaining_args[0] == '-m': run_python_module(remaining_args[1], remaining_args[1:]) else: run_python_file(remaining_args[0], remaining_args) except SystemExit as e: ret = e.code cov.stop() cov.save() cov.report(show_missing=args.show_missing) return ret
def test_running_py_from_binary(self): # Use make_file to get the bookkeeping. Ideally, it would # be able to write binary files. bf = self.make_file("binary") with open(bf, "wb") as f: f.write(b'\x7fELF\x02\x01\x01\x00\x00\x00') msg = ( r"Couldn't run 'binary' as Python code: " r"(TypeError|ValueError): " r"(" r"compile\(\) expected string without null bytes" # for py2 r"|" r"source code string cannot contain null bytes" # for py3 r")") with self.assertRaisesRegex(Exception, msg): run_python_file(bf, [bf])
def test_running_py_from_binary(self): # Use make_file to get the bookkeeping. Ideally, it would # be able to write binary files. bf = self.make_file("binary") with open(bf, "wb") as f: f.write(b'\x7fELF\x02\x01\x01\x00\x00\x00') msg = ( r"Couldn't run 'binary' as Python code: " r"(TypeError|ValueError): " r"(" r"compile\(\) expected string without null bytes" # for py2 r"|" r"source code string cannot contain null bytes" # for py3 r")" ) with self.assertRaisesRegex(Exception, msg): run_python_file([bf])
def test_code_exits(self): self.make_file("exit.py", """\ import sys def f1(): print("about to exit..") sys.exit(17) def f2(): f1() f2() """) with pytest.raises(SystemExit) as exc_info: run_python_file(["exit.py"]) assert exc_info.value.args == (17,) assert self.stdout() == "about to exit..\n" assert self.stderr() == ""
def test_code_throws(self): self.make_file("throw.py", """\ class MyException(Exception): pass def f1(): print("about to raise..") raise MyException("hey!") def f2(): f1() f2() """) with pytest.raises(SystemExit) as exc_info: run_python_file(["throw.py"]) assert exc_info.value.args == (1,) assert self.stdout() == "about to raise..\n" assert self.stderr() == ""
def test_excepthook_throw(self): self.make_file("excepthook_throw.py", """\ import sys def excepthook(*args): # Write this message to stderr so that we don't have to deal # with interleaved stdout/stderr comparisons in the assertions # in the test. sys.stderr.write('in excepthook\\n') raise RuntimeError('Error Inside') sys.excepthook = excepthook raise RuntimeError('Error Outside') """) with pytest.raises(ExceptionDuringRun) as exc_info: run_python_file(["excepthook_throw.py"]) # The ExceptionDuringRun exception has the RuntimeError as its argument. assert exc_info.value.args[1].args[0] == "Error Outside" stderr = self.stderr() assert "in excepthook\n" in stderr assert "Error in sys.excepthook:\n" in stderr assert "RuntimeError: Error Inside" in stderr
def run(self, command_line): self.run_id = str(uuid.uuid4()) context = TracerContext(self) try: with context: self.publisher.start_run( self.run_id, os.getcwd(), command_line, time.time(), ) run_python_file( command_line[0], command_line, ) except ExceptionDuringRun as err: # Unpack the wrapped exception err_type, orig_err, traceback = err.args try: self.publisher.end_run( self.run_id, end_time=time.time(), message=unicode(orig_err), traceback=traceback, stats=context.get_stats_data(), ) finally: del traceback # remove circular reference for GC else: self.publisher.end_run( self.run_id, time.time(), message=None, traceback=None, stats=context.get_stats_data(), ) self.run_id = None
def test_running_hashed_pyc(self): pycfile = self.make_pyc(invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH) run_python_file([pycfile]) assert self.stdout() == "I am here!\n"
def test_no_such_file(self): path = python_reported_file('xyzzy.py') msg = re.escape("No file to run: '{}'".format(path)) with self.assertRaisesRegex(NoSource, msg): run_python_file(["xyzzy.py"])
def test_running_pyc(self): pycfile = self.make_pyc() run_python_file(pycfile, [pycfile]) self.assertEqual(self.stdout(), "I am here!\n")
def test_no_such_pyc_file(self): path = python_reported_file('xyzzy.pyc') msg = re.escape(f"No file to run: '{path}'") with pytest.raises(NoCode, match=msg): run_python_file(["xyzzy.pyc"])
def test_running_pyc(self): pycfile = self.make_pyc() run_python_file([pycfile]) self.assertEqual(self.stdout(), "I am here!\n")
def test_no_such_pyc_file(self): with self.assertRaisesRegex(NoCode, "No file to run: 'xyzzy.pyc'"): run_python_file("xyzzy.pyc", [])
def test_no_such_file(self): with self.assertRaises(NoSource): run_python_file("xyzzy.py", [])
def test_running_pyc(self): pycfile = self.make_pyc() run_python_file([pycfile]) assert self.stdout() == "I am here!\n"
def test_no_such_file(self): with self.assertRaisesRegex(NoSource, "No file to run: 'xyzzy.py'"): run_python_file(["xyzzy.py"])
def test_no_such_file(self): path = python_reported_file('xyzzy.py') msg = re.escape("No file to run: '{}'".format(path)) with pytest.raises(NoSource, match=msg): run_python_file(["xyzzy.py"])
def test_directory_without_main(self): self.make_file("without_main/__init__.py", "") with self.assertRaisesRegex(NoSource, "Can't find '__main__' module in 'without_main'"): run_python_file("without_main", ["without_main"])
def test_directory_without_main(self): self.make_file("without_main/__init__.py", "") with self.assertRaisesRegex( NoSource, "Can't find '__main__' module in 'without_main'"): run_python_file(["without_main"])
def test_directory_with_main(self): self.make_file("with_main/__main__.py", """\ print("I am __main__") """) run_python_file("with_main", ["with_main"]) self.assertEqual(self.stdout(), "I am __main__\n")
def test_directory_without_main(self): self.make_file("without_main/__init__.py", "") with pytest.raises( NoSource, match="Can't find '__main__' module in 'without_main'"): run_python_file(["without_main"])