Skip to content

Commit 75cd551

Browse files
committed
lab: improvements to coverage-goals
- Warning up-front - Works for line-only measurement - Correct the command description.
1 parent b0c0078 commit 75cd551

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lab/goals.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818

1919
from wcmatch import fnmatch as wcfnmatch # python -m pip install wcmatch
2020

21-
from coverage.results import Numbers # Note: an internal class!
21+
from coverage.results import Numbers # Note: an internal class!
2222

2323

24-
def get_data():
25-
with open("coverage.json") as j:
26-
return json.load(j)
27-
2824
def select_files(files, pat):
2925
flags = wcfnmatch.NEGATE | wcfnmatch.NEGATEALL
3026
selected = [f for f in files if wcfnmatch.fnmatch(f, pat, flags=flags)]
@@ -38,30 +34,35 @@ def total_for_files(data, files):
3834
n_statements=sel_summ["num_statements"],
3935
n_excluded=sel_summ["excluded_lines"],
4036
n_missing=sel_summ["missing_lines"],
41-
n_branches=sel_summ["num_branches"],
42-
n_partial_branches=sel_summ["num_partial_branches"],
43-
n_missing_branches=sel_summ["missing_branches"],
37+
n_branches=sel_summ.get("num_branches", 0),
38+
n_partial_branches=sel_summ.get("num_partial_branches", 0),
39+
n_missing_branches=sel_summ.get("missing_branches", 0),
4440
)
4541

4642
return total
4743

4844
def main(argv):
49-
parser = argparse.ArgumentParser(__doc__)
45+
parser = argparse.ArgumentParser(description=__doc__)
5046
parser.add_argument("--file", "-f", action="store_true", help="Check each file individually")
5147
parser.add_argument("--group", "-g", action="store_true", help="Check a group of files")
5248
parser.add_argument("--verbose", "-v", action="store_true", help="Be chatty about what's happening")
5349
parser.add_argument("goal", type=float, help="Coverage goal")
5450
parser.add_argument("pattern", type=str, nargs="+", help="Patterns to check")
5551
args = parser.parse_args(argv)
5652

53+
print("** Note: this is a proof-of-concept. Support is not promised. **")
54+
print("Read more: https://nedbatchelder.com/blog/202111/coverage_goals.html")
55+
print("Feedback is appreciated: https://github.com/nedbat/coveragepy/issues/691")
56+
5757
if args.file and args.group:
5858
print("Can't use --file and --group together")
5959
return 1
6060
if not (args.file or args.group):
6161
print("Need either --file or --group")
6262
return 1
6363

64-
data = get_data()
64+
with open("coverage.json") as j:
65+
data = json.load(j)
6566
all_files = list(data["files"].keys())
6667
selected = select_files(all_files, args.pattern)
6768

0 commit comments

Comments
 (0)