Skip to content

Commit 0fadae5

Browse files
committed
fix: issue #6 catch coverage erros
if there is a coverage error in a directory, tell the user
1 parent 67e5905 commit 0fadae5

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

coverage_util/check_coverage.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import shutil
66
import subprocess
77
from collections import defaultdict
8+
import time
89

9-
ignored_wildcards = ["project_euler", "__init__.py", "tests", "__pycache__"]
10+
ignored_wildcards = ["project_euler", "__init__.py", "*/tests", "*/__pycache__"]
1011
root_dir = os.path.abspath(__file__).replace("/coverage_util/check_coverage.py", "")
1112
save_file = False
1213
dir_cov = {}
1314

1415

1516
def extend_wildcards():
1617
"""add the contents of the gitignore to ignored_wildcards"""
18+
global ignored_wildcards
1719
try:
18-
ignore = open(".gitignore")
20+
ignore = open("../.gitignore")
1921
except FileNotFoundError:
2022
pass
2123
else:
@@ -38,6 +40,8 @@ def create_dir_file_dict():
3840
# creates long regex for matching filenames/paths based on the wildcards
3941
excluded = r"|".join([fnmatch.translate(wc) for wc in ignored_wildcards])
4042
for dirpath, dirnames, filenames in os.walk(root_dir):
43+
if re.match(excluded, dirpath):
44+
continue
4145
dirnames[:] = [dir for dir in dirnames if not re.match(excluded, dir)]
4246
filenames[:] = [file for file in filenames if not re.match(excluded, file)]
4347
[dir_file_dict[dirpath].append(f) for f in filenames if ".py" in f]
@@ -71,6 +75,9 @@ def display_n_worst():
7175
n = 10 by default, or can be passed as an argument using '-n'
7276
"""
7377
global dir_cov
78+
if not dir_cov:
79+
print("No Results")
80+
return
7481
dir_cov = {k: v for k, v in sorted(dir_cov.items(), key=lambda item: item[1])}
7582
k, v = dir_cov.keys(), dir_cov.values()
7683
width = shutil.get_terminal_size().columns
@@ -115,7 +122,7 @@ def run_coverage(dir_file_dict):
115122
"""
116123
visits every directory that contains python files, and runs three coverage commands
117124
in the directory
118-
1) 'coverage run --source=. -m unittest *'
125+
1) 'coverage run --source=. -m unittest *py'
119126
checks the unittest coverage of the directory
120127
2) 'coverage run -a --source=. -m pytest --doctest-module'
121128
appends the coverage results of doctests in the directory
@@ -139,21 +146,24 @@ def run_coverage(dir_file_dict):
139146
for dir in directories:
140147
os.chdir(dir)
141148
subprocess.run(
142-
"coverage run --source=. -m unittest *",
143-
stderr=subprocess.DEVNULL,
144-
stdout=subprocess.DEVNULL,
149+
"coverage run --source=. -m unittest *.py",
145150
shell=True,
151+
stdout=subprocess.DEVNULL,
152+
stderr=subprocess.DEVNULL
146153
)
147154
subprocess.run(
148-
"coverage run -a --source=. -m pytest --doctest-modules",
149-
stderr=subprocess.DEVNULL,
150-
stdout=subprocess.DEVNULL,
155+
f"coverage run -a --source=. -m pytest --doctest-modules",
151156
shell=True,
157+
stdout=subprocess.DEVNULL,
158+
stderr=subprocess.DEVNULL
152159
)
153160
subprocess_output = subprocess.run(
154161
"coverage report -m", shell=True, capture_output=True
155162
)
156163
result = subprocess_output.stdout.decode()
164+
if "No" in result:
165+
print(f"There was an error running coverage tests in {dir}.")
166+
continue
157167
if save_file:
158168
save_results(dir, result)
159169
save_directory_results(dir, result)
@@ -172,14 +182,14 @@ def main():
172182
description="This is a tool for checking the test coverage of directories."
173183
)
174184
parser.add_argument(
175-
"-i",
185+
'-o',
176186
metavar="file",
177187
nargs="*",
178188
type=str,
179189
required=False,
180-
help="strings of shell-style wildcards of filepaths/ filensames to ignore \
181-
in coverage check (.gitignore is ignored by default) \
182-
ex. -i '*/test' 'z?'",
190+
help="strings of shell-style wildcards of filepaths/ filensames to omit \
191+
in coverage check (.gitignore is omitted by default) \
192+
MUST BE IN SINGLE QUOTES ex. -o '*/tests/*' ",
183193
)
184194
parser.add_argument(
185195
"-d",
@@ -203,8 +213,8 @@ def main():
203213
args = parser.parse_args()
204214
if args.d:
205215
root_dir += f"/{args.d.strip('/')}"
206-
if args.i:
207-
ignored_wildcards.extend(args.i)
216+
if args.o:
217+
ignored_wildcards.extend(args.o)
208218
save_file = args.s
209219
n_worst = args.n
210220
main()

0 commit comments

Comments
 (0)