5
5
import shutil
6
6
import subprocess
7
7
from collections import defaultdict
8
+ import time
8
9
9
- ignored_wildcards = ["project_euler" , "__init__.py" , "tests" , "__pycache__" ]
10
+ ignored_wildcards = ["project_euler" , "__init__.py" , "*/ tests" , "*/ __pycache__" ]
10
11
root_dir = os .path .abspath (__file__ ).replace ("/coverage_util/check_coverage.py" , "" )
11
12
save_file = False
12
13
dir_cov = {}
13
14
14
15
15
16
def extend_wildcards ():
16
17
"""add the contents of the gitignore to ignored_wildcards"""
18
+ global ignored_wildcards
17
19
try :
18
- ignore = open (".gitignore" )
20
+ ignore = open ("../. gitignore" )
19
21
except FileNotFoundError :
20
22
pass
21
23
else :
@@ -38,6 +40,8 @@ def create_dir_file_dict():
38
40
# creates long regex for matching filenames/paths based on the wildcards
39
41
excluded = r"|" .join ([fnmatch .translate (wc ) for wc in ignored_wildcards ])
40
42
for dirpath , dirnames , filenames in os .walk (root_dir ):
43
+ if re .match (excluded , dirpath ):
44
+ continue
41
45
dirnames [:] = [dir for dir in dirnames if not re .match (excluded , dir )]
42
46
filenames [:] = [file for file in filenames if not re .match (excluded , file )]
43
47
[dir_file_dict [dirpath ].append (f ) for f in filenames if ".py" in f ]
@@ -71,6 +75,9 @@ def display_n_worst():
71
75
n = 10 by default, or can be passed as an argument using '-n'
72
76
"""
73
77
global dir_cov
78
+ if not dir_cov :
79
+ print ("No Results" )
80
+ return
74
81
dir_cov = {k : v for k , v in sorted (dir_cov .items (), key = lambda item : item [1 ])}
75
82
k , v = dir_cov .keys (), dir_cov .values ()
76
83
width = shutil .get_terminal_size ().columns
@@ -115,7 +122,7 @@ def run_coverage(dir_file_dict):
115
122
"""
116
123
visits every directory that contains python files, and runs three coverage commands
117
124
in the directory
118
- 1) 'coverage run --source=. -m unittest *'
125
+ 1) 'coverage run --source=. -m unittest *py '
119
126
checks the unittest coverage of the directory
120
127
2) 'coverage run -a --source=. -m pytest --doctest-module'
121
128
appends the coverage results of doctests in the directory
@@ -139,21 +146,24 @@ def run_coverage(dir_file_dict):
139
146
for dir in directories :
140
147
os .chdir (dir )
141
148
subprocess .run (
142
- "coverage run --source=. -m unittest *" ,
143
- stderr = subprocess .DEVNULL ,
144
- stdout = subprocess .DEVNULL ,
149
+ "coverage run --source=. -m unittest *.py" ,
145
150
shell = True ,
151
+ stdout = subprocess .DEVNULL ,
152
+ stderr = subprocess .DEVNULL
146
153
)
147
154
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" ,
151
156
shell = True ,
157
+ stdout = subprocess .DEVNULL ,
158
+ stderr = subprocess .DEVNULL
152
159
)
153
160
subprocess_output = subprocess .run (
154
161
"coverage report -m" , shell = True , capture_output = True
155
162
)
156
163
result = subprocess_output .stdout .decode ()
164
+ if "No" in result :
165
+ print (f"There was an error running coverage tests in { dir } ." )
166
+ continue
157
167
if save_file :
158
168
save_results (dir , result )
159
169
save_directory_results (dir , result )
@@ -172,14 +182,14 @@ def main():
172
182
description = "This is a tool for checking the test coverage of directories."
173
183
)
174
184
parser .add_argument (
175
- "-i" ,
185
+ '-o' ,
176
186
metavar = "file" ,
177
187
nargs = "*" ,
178
188
type = str ,
179
189
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/*' " ,
183
193
)
184
194
parser .add_argument (
185
195
"-d" ,
@@ -203,8 +213,8 @@ def main():
203
213
args = parser .parse_args ()
204
214
if args .d :
205
215
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 )
208
218
save_file = args .s
209
219
n_worst = args .n
210
220
main ()
0 commit comments