diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/modules/process_test/try_execfile.py | 15 | ||||
-rw-r--r-- | tests/test_process.py | 33 |
2 files changed, 39 insertions, 9 deletions
diff --git a/tests/modules/process_test/try_execfile.py b/tests/modules/process_test/try_execfile.py index 706fe39f..48f9d098 100644 --- a/tests/modules/process_test/try_execfile.py +++ b/tests/modules/process_test/try_execfile.py @@ -66,7 +66,7 @@ def my_function(a): FN_VAL = my_function("fooey") loader = globals().get('__loader__') -fullname = getattr(loader, 'fullname', None) or getattr(loader, 'name', None) +spec = globals().get('__spec__') # A more compact ad-hoc grouped-by-first-letter list of builtins. CLUMPS = "ABC,DEF,GHI,JKLMN,OPQR,ST,U,VWXYZ_,ab,cd,efg,hij,lmno,pqr,stuvwxyz".split(",") @@ -88,8 +88,8 @@ globals_to_check = { '__builtins__.has_open': hasattr(__builtins__, 'open'), '__builtins__.dir': builtin_dir, '__loader__ exists': loader is not None, - '__loader__.fullname': fullname, '__package__': __package__, + '__spec__ exists': spec is not None, 'DATA': DATA, 'FN_VAL': FN_VAL, '__main__.DATA': getattr(__main__, "DATA", "nothing"), @@ -98,4 +98,15 @@ globals_to_check = { 'path': cleaned_sys_path, } +if loader is not None: + globals_to_check.update({ + '__loader__.fullname': getattr(loader, 'fullname', None) or getattr(loader, 'name', None) + }) + +if spec is not None: + globals_to_check.update({ + '__spec__.' + aname: getattr(spec, aname) + for aname in ['name', 'origin', 'submodule_search_locations', 'parent', 'has_location'] + }) + print(json.dumps(globals_to_check, indent=4, sort_keys=True)) diff --git a/tests/test_process.py b/tests/test_process.py index 1579ec5e..06e429dd 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -866,17 +866,36 @@ class EnvironmentTest(CoverageTest): expected = self.run_command("python with_main") actual = self.run_command("coverage run with_main") - # The coverage.py results are not identical to the Python results, and - # I don't know why. For now, ignore those failures. If someone finds - # a real problem with the discrepancies, we can work on it some more. - ignored = r"__file__|__loader__|__package__" # PyPy includes the current directory in the path when running a # directory, while CPython and coverage.py do not. Exclude that from # the comparison also... if env.PYPY: - ignored += "|"+re.escape(os.getcwd()) - expected = re_lines(expected, ignored, match=False) - actual = re_lines(actual, ignored, match=False) + ignored = re.escape(os.getcwd()) + expected = re_lines(expected, ignored, match=False) + actual = re_lines(actual, ignored, match=False) + self.assert_tryexecfile_output(expected, actual) + + def test_coverage_run_dashm_dir_no_init_is_like_python(self): + with open(TRY_EXECFILE) as f: + self.make_file("with_main/__main__.py", f.read()) + + expected = self.run_command("python -m with_main") + actual = self.run_command("coverage run -m with_main") + if env.PY2: + assert expected.endswith("No module named with_main\n") + assert actual.endswith("No module named with_main\n") + else: + self.assert_tryexecfile_output(expected, actual) + + def test_coverage_run_dashm_dir_with_init_is_like_python(self): + if env.PY2: + self.skipTest("Python 2 runs __main__ twice, I can't be bothered to make it work.") + with open(TRY_EXECFILE) as f: + self.make_file("with_main/__main__.py", f.read()) + self.make_file("with_main/__init__.py", "") + + expected = self.run_command("python -m with_main") + actual = self.run_command("coverage run -m with_main") self.assert_tryexecfile_output(expected, actual) def test_coverage_run_dashm_equal_to_doubledashsource(self): |