diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-06-12 09:45:41 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-06-12 09:45:41 -0400 |
commit | 1f4eb724612022db65e66186e6abfde4201f40e4 (patch) | |
tree | 1310997926d8baef5b7801b28086bd18a54e23c4 /tests/modules/process_test/try_execfile.py | |
parent | c525acb8599ac107d24bb9e20b1bcdf12b4f76b6 (diff) | |
download | python-coveragepy-git-1f4eb724612022db65e66186e6abfde4201f40e4.tar.gz |
Move try_execfile.py to an unambiguous place
Where it was, we were counting on "tests" referring to coverage.py's own tests
directory, but a -e install of a helper like unittest-mixins could provide a
tests directory, which prevented the imports we needed for the tests.
Moving try_execfile.py to a new subdirectory with a less-common name solves
the problem.
--HG--
rename : tests/try_execfile.py => tests/modules/process_test/try_execfile.py
Diffstat (limited to 'tests/modules/process_test/try_execfile.py')
-rw-r--r-- | tests/modules/process_test/try_execfile.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/modules/process_test/try_execfile.py b/tests/modules/process_test/try_execfile.py new file mode 100644 index 00000000..70905071 --- /dev/null +++ b/tests/modules/process_test/try_execfile.py @@ -0,0 +1,84 @@ +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt + +"""Test file for run_python_file. + +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 + if hasattr(os.path, "samefile"): + return os.path.samefile(p1, p2) + else: + norm1 = os.path.normcase(os.path.normpath(p1)) + norm2 = os.path.normcase(os.path.normpath(p2)) + return norm1 == norm2 + +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" + +import __main__ + +def my_function(a): + """A function to force execution of module-level values.""" + return "my_fn(%r)" % a + +FN_VAL = my_function("fooey") + +loader = globals().get('__loader__') +fullname = getattr(loader, 'fullname', None) or getattr(loader, 'name', None) + +globals_to_check = { + '__name__': __name__, + '__file__': __file__, + '__doc__': __doc__, + '__builtins__.has_open': hasattr(__builtins__, 'open'), + '__builtins__.dir': dir(__builtins__), + '__loader__ exists': loader is not None, + '__loader__.fullname': fullname, + '__package__': __package__, + 'DATA': DATA, + 'FN_VAL': FN_VAL, + '__main__.DATA': getattr(__main__, "DATA", "nothing"), + 'argv': sys.argv, + 'path': cleaned_sys_path, +} + +print(json.dumps(globals_to_check, indent=4, sort_keys=True)) |