-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·160 lines (134 loc) · 4.08 KB
/
run.sh
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
#!/bin/bash
set -e
#####################
# --- Constants --- #
#####################
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
MINIMUM_TEST_COVERAGE_PERCENT=0
##########################
# --- Task Functions --- #
##########################
# install core and development Python dependencies into the currently activated venv
function install {
python -m pip install --upgrade pip
python -m pip install --editable "$THIS_DIR/[dev]"
}
# run linting, formatting, and other static code quality tools
function lint {
pre-commit run --all-files
}
# same as `lint` but with any special considerations for CI
function lint:ci {
# We skip no-commit-to-branch since that blocks commits to `main`.
# All merged PRs are commits to `main` so this must be disabled.
SKIP=no-commit-to-branch pre-commit run --all-files
}
# execute tests that are not marked as `slow`
function test:quick {
run-tests -m "not slow" ${@:-"$THIS_DIR/tests/"}
}
# execute tests against the installed package; assumes the wheel is already installed
function test:ci {
INSTALLED_PKG_DIR="$(python -c 'import pyprojen; print(pyprojen.__path__[0])')"
# in CI, we must calculate the coverage for the installed package, not the src/ folder
COVERAGE_DIR="$INSTALLED_PKG_DIR" run-tests
}
# (example) ./run.sh test tests/test_states_info.py::test__slow_add
function run-tests {
PYTEST_EXIT_STATUS=0
# clean the test-reports dir
rm -rf "$THIS_DIR/test-reports" || mkdir "$THIS_DIR/test-reports"
# execute the tests, calculate coverage, and generate coverage reports in the test-reports dir
python -m pytest ${@:-"$THIS_DIR/tests/"} \
--cov "${COVERAGE_DIR:-$THIS_DIR/src}" \
--cov-report html \
--cov-report term \
--cov-report xml \
--junit-xml "$THIS_DIR/test-reports/report.xml" \
--cov-fail-under "$MINIMUM_TEST_COVERAGE_PERCENT" || ((PYTEST_EXIT_STATUS+=$?))
mv coverage.xml "$THIS_DIR/test-reports/" || true
mv htmlcov "$THIS_DIR/test-reports/" || true
mv .coverage "$THIS_DIR/test-reports/" || true
return $PYTEST_EXIT_STATUS
}
function test:wheel-locally {
deactivate || true
rm -rf test-env || true
python -m venv test-env
source test-env/bin/activate
clean || true
pip install build
build
pip install ./dist/*.whl pytest pytest-cov
test:ci
deactivate || true
}
# serve the html test coverage report on localhost:8000
function serve-coverage-report {
python -m http.server --directory "$THIS_DIR/test-reports/htmlcov/" 8000
}
# build a wheel and sdist from the Python source code
function build {
python -m build --sdist --wheel "$THIS_DIR/"
}
function release:test {
lint
clean
build
publish:test
}
function release:prod {
release:test
publish:prod
}
function publish:test {
try-load-dotenv || true
twine upload dist/* \
--repository testpypi \
--username=__token__ \
--password="$TEST_PYPI_TOKEN"
}
function publish:prod {
try-load-dotenv || true
twine upload dist/* \
--repository pypi \
--username=__token__ \
--password="$PROD_PYPI_TOKEN"
}
# remove all files generated by tests, builds, or operating this codebase
function clean {
rm -rf dist build coverage.xml test-reports
find . \
-type d \
\( \
-name "*cache*" \
-o -name "*.dist-info" \
-o -name "*.egg-info" \
-o -name "*htmlcov" \
\) \
-not -path "*env/*" \
-exec rm -r {} + || true
find . \
-type f \
-name "*.pyc" \
-not -path "*env/*" \
-exec rm {} +
}
# export the contents of .env as environment variables
function try-load-dotenv {
if [ ! -f "$THIS_DIR/.env" ]; then
echo "no .env file found"
return 1
fi
while read -r line; do
export "$line"
done < <(grep -v '^#' "$THIS_DIR/.env" | grep -v '^$')
}
# print all functions in this file
function help {
echo "$0 <task> <args>"
echo "Tasks:"
compgen -A function | cat -n
}
TIMEFORMAT="Task completed in %3lR"
time ${@:-help}