OLD | NEW |
(Empty) | |
| 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
| 3 |
| 4 """Test file for run_python_file. |
| 5 |
| 6 This file is executed two ways:: |
| 7 |
| 8 $ coverage run try_execfile.py |
| 9 |
| 10 and:: |
| 11 |
| 12 $ python try_execfile.py |
| 13 |
| 14 The output is compared to see that the program execution context is the same |
| 15 under coverage and under Python. |
| 16 |
| 17 It is not crucial that the execution be identical, there are some differences |
| 18 that are OK. This program canonicalizes the output to gloss over those |
| 19 differences and get a clean diff. |
| 20 |
| 21 """ |
| 22 |
| 23 import json, os, sys |
| 24 |
| 25 # sys.path varies by execution environments. Coverage.py uses setuptools to |
| 26 # make console scripts, which means pkg_resources is imported. pkg_resources |
| 27 # removes duplicate entries from sys.path. So we do that too, since the extra |
| 28 # entries don't affect the running of the program. |
| 29 |
| 30 def same_file(p1, p2): |
| 31 """Determine if `p1` and `p2` refer to the same existing file.""" |
| 32 if not p1: |
| 33 return not p2 |
| 34 if not os.path.exists(p1): |
| 35 return False |
| 36 if not os.path.exists(p2): |
| 37 return False |
| 38 if hasattr(os.path, "samefile"): |
| 39 return os.path.samefile(p1, p2) |
| 40 else: |
| 41 norm1 = os.path.normcase(os.path.normpath(p1)) |
| 42 norm2 = os.path.normcase(os.path.normpath(p2)) |
| 43 return norm1 == norm2 |
| 44 |
| 45 def without_same_files(filenames): |
| 46 """Return the list `filenames` with duplicates (by same_file) removed.""" |
| 47 reduced = [] |
| 48 for filename in filenames: |
| 49 if not any(same_file(filename, other) for other in reduced): |
| 50 reduced.append(filename) |
| 51 return reduced |
| 52 |
| 53 cleaned_sys_path = [os.path.normcase(p) for p in without_same_files(sys.path)] |
| 54 |
| 55 DATA = "xyzzy" |
| 56 |
| 57 import __main__ |
| 58 |
| 59 def my_function(a): |
| 60 """A function to force execution of module-level values.""" |
| 61 return "my_fn(%r)" % a |
| 62 |
| 63 FN_VAL = my_function("fooey") |
| 64 |
| 65 loader = globals().get('__loader__') |
| 66 fullname = getattr(loader, 'fullname', None) or getattr(loader, 'name', None) |
| 67 |
| 68 globals_to_check = { |
| 69 '__name__': __name__, |
| 70 '__file__': __file__, |
| 71 '__doc__': __doc__, |
| 72 '__builtins__.has_open': hasattr(__builtins__, 'open'), |
| 73 '__builtins__.dir': dir(__builtins__), |
| 74 '__loader__ exists': loader is not None, |
| 75 '__loader__.fullname': fullname, |
| 76 '__package__': __package__, |
| 77 'DATA': DATA, |
| 78 'FN_VAL': FN_VAL, |
| 79 '__main__.DATA': getattr(__main__, "DATA", "nothing"), |
| 80 'argv': sys.argv, |
| 81 'path': cleaned_sys_path, |
| 82 } |
| 83 |
| 84 print(json.dumps(globals_to_check, indent=4, sort_keys=True)) |
OLD | NEW |