/
test.py
219 lines (184 loc) · 5.9 KB
/
test.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""Module of Invoke tasks regarding CODE QUALITY to be invoked from the command line. Try
invoke --list
from the command line for a list of all available commands.
"""
import pathlib
from invoke import task
TEST_FILES = " ".join(["tests",])
TEST_RESULTS = "test_results"
FILES = " ".join(["setup.py", "awesome_panel_extensions", "examples", "tests",])
@task
def bandit(command,):
"""Runs Bandit the security linter from PyCQA."""
print(
"""
Running Bandit the Python Security Linter
to identify common security issues in Python code
=================================================
"""
)
command.run(
"bandit -r ./", echo=True,
)
@task
def black(command,):
"""Runs black (autoformatter) on all .py files recursively"""
print(
"""
Running Black the Python code formatter
=======================================
"""
)
command.run(
"black . -l 100", echo=True,
)
@task
def isort(command,):
"""Runs isort (import sorter) on all .py files recursively"""
print(
"""
Running isort the Python code import sorter
===========================================
"""
)
command.run(
"isort -rc .", echo=True,
)
@task
def unittest(
command, test_files=TEST_FILES,
):
"""Runs pytest to identify failing tests
Arguments:
command {[type]} -- Invoke command object
Keyword Arguments:
root_dir {str} -- The directory from which to run the tests
test_files {str} -- A space separated list of folders and files to test. (default: {'tests})
"""
print(
"""
Running pytest the test framework
=================================
"""
)
# Build the command_string
command_string = f"pytest {test_files} --doctest-modules"
command_string += ' -m "not functionaltest and not integrationtest and not slow"'
# Run the command_string
command.run(command_string, echo=True)
@task
def pytest(
command, test_files=TEST_FILES, test_results=TEST_RESULTS,
):
"""Runs pytest to identify failing tests
Arguments:
command {[type]} -- Invoke command object
Keyword Arguments:
root_dir {str} -- The directory from which to run the tests
test_files {str} -- A space separated list of folders and files to test. (default: {'tests})
integrationtest {bool} -- If True tests marked integrationtest or
functionaltest will be run. Otherwise not. (default: {False})
These tests requires the test backend server running.
test_results {string} -- If not None test reports will be generated in the
test_results folder
# Print running pytest
"""
print(
"""
Running pytest the test framework
=================================
"""
)
# Build the command_string
command_string = f"pytest {test_files} awesome_panel_extensions --doctest-modules"
if test_results:
command_string += f" --junitxml={test_results}/test-results-docs.xml"
command_string += f" --cov-report html:{test_results}/cov_html"
# Run the command_string
command.run(
command_string, echo=True,
)
# Open the test coverage report in a browser
path = pathlib.Path(__file__).parent.parent / test_results / "cov_html" / "index.html"
print(f"""- generated test coverage file at {path}""")
@task()
def pylint(command, files=FILES, test_results=False):
"""Runs pylint (linter) on all .py files recursively to identify coding errors
Arguments:
command {[type]} -- [description]
files {string} -- A space separated list of files and folders to lint
"""
# https://stackoverflow.com/questions/22241435/pylint-discard-cached-file-state
# from astroid import MANAGER
# MANAGER.astroid_cache.clear()
print(
"""
Running pylint.
Pylint looks for programming errors, helps enforcing a coding standard,
sniffs for code smells and offers simple refactoring suggestions.
=======================================================================
"""
)
command_string = f"pylint {files}"
if test_results:
command_string += (
" --output-format=pylint2junit.JunitReporter 2>&1 > test_results/pylint-results-api.xml"
)
command.run(
command_string, echo=True,
)
@task
def mypy(command, files=FILES, test_results=False):
"""Runs mypy (static type checker) on all .py files recursively
Arguments:
command {[type]} -- [description]
files {string} -- A space separated list of files and folders to lint
"""
print(
"""
Running mypy for identifying Python type errors
===============================================
"""
)
command_string = f"mypy {files}"
if test_results:
command_string += " --junit-xml test_results/mypy-results-api.xml"
command.run(
command_string, echo=True,
)
@task
def autoflake(command,):
"""Runs autoflake to remove unused imports on all .py files recursively
Arguments:
command {[type]} -- [description]
"""
print(
"""
Running autoflake to remove unused imports on all .py files recursively
=======================================================================
"""
)
# command.run("RUN rm -rf .mypy_cache/; exit 0")
command.run(
"autoflake --imports=pytest --in-place --recursive .", echo=True,
)
# Note: Get Black added back in. black,
@task(
pre=[isort, autoflake, black, pylint, mypy, pytest,],
aliases=["pre_commit", "test",],
name="all",
)
def _all(command,): # pylint: disable=unused-argument
"""Runs isort, autoflake, black, pylint, mypy and pytest
Arguments:
command {[type]} -- [description]
"""
# If we get to this point all tests listed in 'pre' have passed
# unless we have run the task with the --warn flag
if not command.config.run.warn:
print(
"""
All Tests Passed Successfully
=============================
"""
)