-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
96 lines (71 loc) · 2.59 KB
/
tasks.py
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
85
86
87
88
89
90
91
92
93
94
95
96
import os
import re
import sys
from invoke import task
GOPATH = os.environ.get("GOPATH", os.environ.get("HOME") + "/go")
LINTER_VERSION = "1.38.0"
LINTER = f"{GOPATH}/bin/golangci-lint"
TESTER_VERSION = "version"
TESTER = f"{GOPATH}/bin/gotestsum"
def fail(message):
print(message)
sys.exit(1)
@task(
help={
"test": "<PACKAGE_PATH>::<TEST_NAME>. If empty, it will run all tests.",
"verbose": "Show stdout of tests.",
"show": "Show coverprofile page.",
"yes": "Automatically say yes to the following questions.",
}
)
def test(c, test="", verbose=False, show=False, yes=False):
"""Run tests."""
devtools(c, yes=yes)
test_regex = "./..."
test = test.split("::")
if len(test) == 2:
test_regex = f"-run {test[1]} {test[0]}"
r = c.run(
f"{TESTER} --format=testname --no-color=False -- {'-v' if verbose else ''} {f'--parallel={os.cpu_count()}' if os.cpu_count() else ''} -race -count=1 -cover {'-coverprofile=coverage.out' if show else ''} {test_regex}",
)
packages = 0
coverage = 0.0
for cover in re.findall(r"[0-9]+\.[0-9]+(?=%)", r.stdout):
packages += 1
coverage += float(cover)
if packages:
coverage = round(coverage / packages, 1)
title = "=" * (len(str(packages) + str(coverage)) + 34)
print(title, f" Total Coverage ({packages} pkg) : {coverage}%", title, sep="\n")
if show:
c.run("go tool cover -html=coverage.out")
c.run("rm -f coverage.out")
@task(
help={
"yes": "Automatically say yes to the following questions.",
}
)
def devtools(c, yes=False):
"""Check and install devtools."""
def installed():
tester = TESTER_VERSION in c.run(f"{TESTER} --version", warn=True, hide="both").stdout
linter = LINTER_VERSION in c.run(f"{LINTER} --version", warn=True, hide="both").stdout
return tester and linter
if not installed():
if not yes and input("Devtools not installed, install? y/n: ").lower() != "y":
fail("Aborting as devtools not installed!")
c.run(f"go install gotest.tools/gotestsum@latest")
c.run(
f"curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b {GOPATH}/bin v{LINTER_VERSION}"
)
if not installed():
fail("Aborting as devtools could not be installed!")
@task(
help={
"yes": "Automatically say yes to the following questions.",
}
)
def lint(c, yes=False):
"""Run linter."""
devtools(c, yes=yes)
c.run(f"{LINTER} run ./... -c .golangci.yaml")