summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test_execfile.py6
-rw-r--r--tests/try_execfile.py50
2 files changed, 49 insertions, 7 deletions
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 63fb919d..bf6d323f 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -1,6 +1,6 @@
"""Tests for coverage.execfile"""
-import compileall, os, re, sys
+import compileall, json, os, re, sys
from coverage.backward import binary_bytes
from coverage.execfile import run_python_file, run_python_module
@@ -16,7 +16,7 @@ class RunFileTest(CoverageTest):
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()) # pylint: disable=eval-used
+ mod_globs = json.loads(self.stdout())
# The file should think it is __main__
self.assertEqual(mod_globs['__name__'], "__main__")
@@ -26,7 +26,7 @@ class RunFileTest(CoverageTest):
self.assertEqual(dunder_file, "try_execfile.py")
# It should have its correct module data.
- self.assertEqual(mod_globs['__doc__'],
+ 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')")
diff --git a/tests/try_execfile.py b/tests/try_execfile.py
index 825fdb70..1035dff9 100644
--- a/tests/try_execfile.py
+++ b/tests/try_execfile.py
@@ -1,6 +1,48 @@
-"""Test file for run_python_file."""
+"""Test file for run_python_file.
-import os, pprint, sys
+This file is executed two ways::
+
+ $ coverage run try_execfile.py
+
+and::
+
+ $ python try_execfile.py
+
+The output is compared to see that the program execution context is the same
+under coverage and under Python.
+
+It is not crucial that the execution be identical, there are some differences
+that are OK. This program canonicalizes the output to gloss over those
+differences and get a clean diff.
+
+"""
+
+import json, os, sys
+
+# sys.path varies by execution environments. Coverage.py uses setuptools to
+# make console scripts, which means pkg_resources is imported. pkg_resources
+# removes duplicate entries from sys.path. So we do that too, since the extra
+# entries don't affect the running of the program.
+
+def same_file(p1, p2):
+ """Determine if `p1` and `p2` refer to the same existing file."""
+ if not p1:
+ return not p2
+ if not os.path.exists(p1):
+ return False
+ if not os.path.exists(p2):
+ return False
+ return os.path.samefile(p1, p2)
+
+def without_same_files(filenames):
+ """Return the list `filenames` with duplicates (by same_file) removed."""
+ reduced = []
+ for filename in filenames:
+ if not any(same_file(filename, other) for other in reduced):
+ reduced.append(filename)
+ return reduced
+
+cleaned_sys_path = [os.path.normcase(p) for p in without_same_files(sys.path)]
DATA = "xyzzy"
@@ -23,7 +65,7 @@ globals_to_check = {
'FN_VAL': FN_VAL,
'__main__.DATA': getattr(__main__, "DATA", "nothing"),
'argv': sys.argv,
- 'path': [os.path.normcase(p) for p in sys.path],
+ 'path': cleaned_sys_path,
}
-pprint.pprint(globals_to_check)
+print(json.dumps(globals_to_check))