| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
"""Measure coverage of each module by its test module.""" |
| 4 |
|
| 5 |
import glob |
| 6 |
import os.path |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
|
| 11 |
UNMAPPED_SRC_FILES = [ |
| 12 |
"websockets/typing.py", |
| 13 |
"websockets/version.py", |
| 14 |
] |
| 15 |
|
| 16 |
UNMAPPED_TEST_FILES = [ |
| 17 |
"tests/test_exports.py", |
| 18 |
] |
| 19 |
|
| 20 |
|
| 21 |
def check_environment(): |
| 22 |
"""Check that prerequisites for running this script are met.""" |
| 23 |
try: |
| 24 |
import websockets # noqa: F401 |
| 25 |
except ImportError: |
| 26 |
print("failed to import websockets; is src on PYTHONPATH?") |
| 27 |
return False |
| 28 |
try: |
| 29 |
import coverage # noqa: F401 |
| 30 |
except ImportError: |
| 31 |
print("failed to locate Coverage.py; is it installed?") |
| 32 |
return False |
| 33 |
return True |
| 34 |
|
| 35 |
|
| 36 |
def get_mapping(src_dir="src"): |
| 37 |
"""Return a dict mapping each source file to its test file.""" |
| 38 |
|
| 39 |
# List source and test files. |
| 40 |
|
| 41 |
src_files = glob.glob( |
| 42 |
os.path.join(src_dir, "websockets/**/*.py"), |
| 43 |
recursive=True, |
| 44 |
) |
| 45 |
test_files = glob.glob( |
| 46 |
"tests/**/*.py", |
| 47 |
recursive=True, |
| 48 |
) |
| 49 |
|
| 50 |
src_files = [ |
| 51 |
os.path.relpath(src_file, src_dir) |
| 52 |
for src_file in sorted(src_files) |
| 53 |
if "legacy" not in os.path.dirname(src_file) |
| 54 |
and os.path.basename(src_file) != "__init__.py" |
| 55 |
and os.path.basename(src_file) != "__main__.py" |
| 56 |
] |
| 57 |
test_files = [ |
| 58 |
test_file |
| 59 |
for test_file in sorted(test_files) |
| 60 |
if "legacy" not in os.path.dirname(test_file) |
| 61 |
and os.path.basename(test_file) != "__init__.py" |
| 62 |
and os.path.basename(test_file).startswith("test_") |
| 63 |
] |
| 64 |
|
| 65 |
# Map source files to test files. |
| 66 |
|
| 67 |
mapping = {} |
| 68 |
unmapped_test_files = set() |
| 69 |
|
| 70 |
for test_file in test_files: |
| 71 |
dir_name, file_name = os.path.split(test_file) |
| 72 |
assert dir_name.startswith("tests") |
| 73 |
assert file_name.startswith("test_") |
| 74 |
src_file = os.path.join( |
| 75 |
"websockets" + dir_name[len("tests") :], |
| 76 |
file_name[len("test_") :], |
| 77 |
) |
| 78 |
if src_file in src_files: |
| 79 |
mapping[src_file] = test_file |
| 80 |
else: |
| 81 |
unmapped_test_files.add(test_file) |
| 82 |
|
| 83 |
unmapped_src_files = set(src_files) - set(mapping) |
| 84 |
|
| 85 |
# Ensure that all files are mapped. |
| 86 |
|
| 87 |
assert unmapped_src_files == set(UNMAPPED_SRC_FILES) |
| 88 |
assert unmapped_test_files == set(UNMAPPED_TEST_FILES) |
| 89 |
|
| 90 |
return mapping |
| 91 |
|
| 92 |
|
| 93 |
def get_ignored_files(src_dir="src"): |
| 94 |
"""Return the list of files to exclude from coverage measurement.""" |
| 95 |
# */websockets matches src/websockets and .tox/**/site-packages/websockets. |
| 96 |
return [ |
| 97 |
# There are no tests for the __main__ module. |
| 98 |
"*/websockets/__main__.py", |
| 99 |
# There is nothing to test on type declarations. |
| 100 |
"*/websockets/typing.py", |
| 101 |
# This approach isn't applicable to the test suite of the legacy |
| 102 |
# implementation, due to the huge test_client_server test module. |
| 103 |
"*/websockets/legacy/*", |
| 104 |
"tests/legacy/*", |
| 105 |
] + [ |
| 106 |
# Exclude test utilities that are shared between several test modules. |
| 107 |
# Also excludes this script. |
| 108 |
test_file |
| 109 |
for test_file in sorted(glob.glob("tests/**/*.py", recursive=True)) |
| 110 |
if "legacy" not in os.path.dirname(test_file) |
| 111 |
and os.path.basename(test_file) != "__init__.py" |
| 112 |
and not os.path.basename(test_file).startswith("test_") |
| 113 |
] |
| 114 |
|
| 115 |
|
| 116 |
def run_coverage(mapping, src_dir="src"): |
| 117 |
# Initialize a new coverage measurement session. The --source option |
| 118 |
# includes all files in the report, even if they're never imported. |
| 119 |
print("\nInitializing session\n", flush=True) |
| 120 |
subprocess.run( |
| 121 |
[ |
| 122 |
sys.executable, |
| 123 |
"-m", |
| 124 |
"coverage", |
| 125 |
"run", |
| 126 |
"--source", |
| 127 |
",".join([os.path.join(src_dir, "websockets"), "tests"]), |
| 128 |
"--omit", |
| 129 |
",".join(get_ignored_files(src_dir)), |
| 130 |
"-m", |
| 131 |
"unittest", |
| 132 |
] |
| 133 |
+ list(UNMAPPED_TEST_FILES), |
| 134 |
check=True, |
| 135 |
) |
| 136 |
# Append coverage of each source module by the corresponding test module. |
| 137 |
for src_file, test_file in mapping.items(): |
| 138 |
print(f"\nTesting {src_file} with {test_file}\n", flush=True) |
| 139 |
subprocess.run( |
| 140 |
[ |
| 141 |
sys.executable, |
| 142 |
"-m", |
| 143 |
"coverage", |
| 144 |
"run", |
| 145 |
"--append", |
| 146 |
"--include", |
| 147 |
",".join([os.path.join(src_dir, src_file), test_file]), |
| 148 |
"-m", |
| 149 |
"unittest", |
| 150 |
test_file, |
| 151 |
], |
| 152 |
check=True, |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
if not check_environment(): |
| 158 |
sys.exit(1) |
| 159 |
src_dir = sys.argv[1] if len(sys.argv) == 2 else "src" |
| 160 |
mapping = get_mapping(src_dir) |
| 161 |
run_coverage(mapping, src_dir) |