Created
April 15, 2021 11:10
-
-
Save cuu508/7a28d8c4912b0296bec4746a3f89a6bb to your computer and use it in GitHub Desktop.
Detect which lines can be removed from a .py file without breaking tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
path = "hc/api/views.py" | |
test_cmd = "python manage.py test --keepdb --parallel=8 -v 0" | |
original = open(path, "r").read() | |
report = open("report-%s.txt" % path.replace("/", "."), "w") | |
lines = original.split("\n") | |
for idx in range(0, len(lines)): | |
print("Processing %d / %d" % (idx, len(lines))) | |
# Optimization: skip empty lines and comments | |
if not lines[idx].strip() or lines[idx].strip().startswith("#"): | |
report.write(" %s\n" % lines[idx]) | |
continue | |
ll = lines[:idx] + lines[idx + 1 :] | |
with open(path, "w") as f: | |
f.write("\n".join(ll)) | |
flag = " " if subprocess.call(test_cmd, shell=True) else "-" | |
report.write("%s %s\n" % (flag, lines[idx])) | |
# Restore original: | |
with open(path, "w") as f: | |
f.write(original) | |
report.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment