-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
171 lines (126 loc) · 3.99 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from invoke import task, Result
from invoke.exceptions import UnexpectedExit
from os.path import dirname, abspath
root_dir = dirname(abspath(__file__))
tests_root_dir = f"{root_dir}/tests"
def bats(c, subdirectory="", recursive=True):
recursive_flag = f"{'-r' if recursive is True else ''}"
return c.run(
f"bats {recursive_flag} {tests_root_dir}/{subdirectory}", pty=True
)
def black(c, check):
check_flag = f"{'--check' if check is True else ''}"
cmd = f"black {root_dir} --line-length=79 {check_flag}"
return c.run(cmd, pty=True)
@task(aliases=["fp"])
def format_python(c):
return black(c, False)
@task(aliases=["cfp", "fcp"])
def check_format_python(c):
return black(c, True)
def shfmt_bats(c, check):
mode = f"{'-d' if check is True else '-w'}"
cmd = (
"result=0 && "
f"for file in $(find {root_dir} -type f \\( -name '*.bats' \\) "
"! -path '*/submodules/*'); do "
f"shfmt {mode} -i 2 -ci --ln bats $file; "
"if [ $? -ne 0 ]; then result=1; fi; done && "
"exit $result"
)
return c.run(cmd, pty=True)
def shfmt_sh(c, check):
cmd = f"shfmt {'-d' if check is True else '-w'} ."
return c.run(cmd, pty=True)
@task(aliases=["fsb"])
def format_shell_bats(c):
return shfmt_bats(c, False)
@task(aliases=["cfsb", "fcsb"])
def check_format_shell_bats(c):
print("Running shfmt for *.bats files...")
result = shfmt_bats(c, True)
print("shfmt completed successfully for *.bats files")
return result
@task(aliases=["fss"])
def format_shell_sh(c):
return shfmt_sh(c, False)
@task(aliases=["cfss", "fcss"])
def check_format_shell_sh(c):
print("Running shfmt for *.sh files...")
result = shfmt_sh(c, True)
print("shfmt completed successfully for *.sh files")
return result
@task(aliases=["fs"], pre=[format_shell_sh, format_shell_bats])
def format_shell(c):
pass
@task(aliases=["cfs", "fcs"])
def check_format_shell(c):
sh_succeeded = True
try:
check_format_shell_sh(c)
except UnexpectedExit as err:
sh_succeeded = False
bats_succeeded = True
try:
check_format_shell_bats(c)
except UnexpectedExit as err:
bats_succeeded = False
if not sh_succeeded or not bats_succeeded:
raise UnexpectedExit(Result(command="check_format_shell", exited=1))
@task(aliases=["f"], pre=[format_shell, format_python])
def format(c):
pass
@task(aliases=["cf", "fc"])
def check_format(c):
shell_succeeded = True
try:
check_format_shell(c)
except UnexpectedExit as err:
shell_succeeded = False
python_succeeded = True
try:
check_format_python(c)
except UnexpectedExit as err:
python_succeeded = False
if not shell_succeeded or not python_succeeded:
raise UnexpectedExit(Result(command="check_format", exited=1))
@task(aliases=["lp"])
def lint_python(c):
print("Running pycodestyle...")
result = c.run(f"pycodestyle {root_dir}", pty=True)
print("pycodestyle completed successfully")
return result
@task(aliases=["ls"])
def lint_shell(c):
print("Running ShellCheck...")
cmd = (
f"find {root_dir} -type f \\( -name '*.sh' -o -name '*.bats' \\) "
f"! -path '*/submodules/*' | xargs shellcheck -x -P {root_dir}"
)
result = c.run(cmd, pty=True)
print("ShellCheck completed successfully")
return result
@task(aliases=["l"])
def lint(c):
shell_succeeded = True
try:
lint_shell(c)
except UnexpectedExit as err:
shell_succeeded = False
python_succeeded = True
try:
lint_python(c)
except UnexpectedExit as err:
python_succeeded = False
if not shell_succeeded or not python_succeeded:
raise UnexpectedExit(Result(command="lint", exited=1))
@task(aliases=["ut", "tu"])
def unit_tests(c):
print("Running unit tests...")
print()
return bats(c, subdirectory="unit")
@task(aliases=["t"])
def test(c):
print("Running all tests...")
print()
return bats(c)