This repository was archived by the owner on Jul 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathcovertest.py
More file actions
executable file
·84 lines (66 loc) · 3.15 KB
/
covertest.py
File metadata and controls
executable file
·84 lines (66 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
This module is used to run tests with full coverage-reports.
It's a way to provide accurate -coverpkg arguments to `go test`.
To run over all packages:
buildscripts/covertest.py --coverdir ".cover" --tags "pkcs11 mysql" --testopts="-v -race"
To run over some packages:
buildscripts/covertest.py --coverdir ".cover" --pkgs "X"
"""
from __future__ import print_function
from argparse import ArgumentParser
import os
import sys
import subprocess
BASE_PKG = "github.com/docker/notary"
def get_all_notary_pkgs(buildtags):
out = subprocess.check_output(["go", "list", "-tags", buildtags, "./..."]).strip()
return [
p.strip() for p in out.split("\n")
if not p.startswith(os.path.join(BASE_PKG, "vendor/"))
]
def get_coverprofile_filename(pkg, buildtags):
if buildtags:
buildtags = "." + buildtags.replace(' ', '.')
return pkg.replace('/', '-').replace(' ', '_') + buildtags + ".coverage.txt"
def run_test_with_coverage(buildtags="", coverdir=".cover", pkgs=None, opts="", covermode="atomic"):
"""
Run go test with coverage over the the given packages, with the following options
"""
all_pkgs = get_all_notary_pkgs(buildtags)
all_pkgs.sort()
pkgs = pkgs or all_pkgs
allpkg_string = ",".join(all_pkgs)
base_cmd = ["go", "test", "-covermode", covermode, "-coverpkg", allpkg_string] + opts.split()
if buildtags:
base_cmd.extend(["-tags", buildtags])
allpkg_string = ", ".join(all_pkgs)
longest_pkg_len = max([len(pkg) for pkg in pkgs])
for pkg in pkgs:
cmd = base_cmd + ["-coverprofile", os.path.join(coverdir, get_coverprofile_filename(pkg, buildtags)), pkg]
app = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in app.stdout:
# we are trying to generate coverage reports for everything in the base package, and some may not be
# actually exercised in the test. So ignore this particular warning. Also, we want to pretty-print
# the test success/failure results
if not line.startswith("warning: no packages being tested depend on github.com/docker/notary"):
line = line.replace(allpkg_string, "<all packages>").replace(pkg, pkg.ljust(longest_pkg_len))
sys.stdout.write(line)
app.wait()
if app.returncode != 0:
print("\n\nTests failed.\n")
sys.exit(app.returncode)
def parseArgs(args=None):
"""
CLI option parsing
"""
parser = ArgumentParser()
parser.add_argument("--coverdir", help="The coverage directory in which to put coverage files", default=".cover")
parser.add_argument("--testopts", help="Options to pass for testing, such as -race or -v", default="")
parser.add_argument("--pkgs", help="Packages to test specifically, otherwise we test all the packages", default="")
parser.add_argument("--tags", help="Build tags to pass to go", default="")
return parser.parse_args(args)
if __name__ == "__main__":
args = parseArgs()
pkgs = args.pkgs.strip().split()
run_test_with_coverage(coverdir=args.coverdir, buildtags=args.tags, pkgs=pkgs, opts=args.testopts)