Repository navigation
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·137 lines (125 loc) · 3.64 KB
/
Copy pathrun_tests.sh
File metadata and controls
executable file
·137 lines (125 loc) · 3.64 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
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
#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
# Top-level folders whose unit tests are run with `unittest discover`.
# Lint and format checks run on the whole repo instead (see run_py_lint_*), so
# new Python folders are checked without having to be added here.
PYTHON_FOLDERS="util/ tools/ import-automation/executor import-automation/workflow scripts/ agents/common/ statvar_imports/"
# Allow overriding via environment; default to false when unset.
PYTHON_REQUIREMENTS_INSTALLED="${PYTHON_REQUIREMENTS_INSTALLED:-false}"
function setup_python {
if [[ "$PYTHON_REQUIREMENTS_INSTALLED" = false ]]
then
python3 -m venv .env
source .env/bin/activate
echo "Installing Python requirements"
if ! command -v uv &> /dev/null; then
echo "Installing uv"
pip3 install uv -q
fi
echo "Using uv for package installation"
uv pip install --torch-backend cpu -r requirements_all.txt -q
PYTHON_REQUIREMENTS_INSTALLED=true
else
if [ -f ".env/bin/activate" ]; then
source .env/bin/activate
fi
fi
}
# Fixes lint
function run_py_lint_fix {
setup_python
echo "#### Fixing Python lint (ruff)"
# Applies only safe fixes; remaining issues are reported for manual fixes.
ruff check --fix . || true
echo "#### Fixing Python code"
yapf -r -i -p --style=google .
}
# Tests Python code style
function run_py_lint_test {
setup_python
echo "#### Testing Python lint (ruff)"
if ! ruff check .; then
echo "ERROR: Fix lint errors (some can be fixed by running ./run_tests.sh -f)" >&2
exit 1
fi
echo "#### Testing Python format"
if ! yapf -r --diff -p --style=google .; then
echo "ERROR: Fix lint errors by running ./run_tests.sh -f" >&2
exit 1
fi
}
# Run Python tests. Requires folder to run tests in specified in $1.
function run_py_test {
if [ $# -eq 0 ]
then
echo "Please specify a folder to run tests in"
exit 1
fi
setup_python
echo "#### Testing Python code in $1"
python3 -m unittest discover -v -s $1 -p *_test.py >/dev/null
}
function run_all_py_tests {
for FOLDER in $PYTHON_FOLDERS
do
run_py_test $FOLDER
done
}
function help {
echo "Usage: $0 -rpluaf"
echo "-r Install Python requirements"
echo "-l Test lint on Python code"
echo "-p Run Python tests in specified folder, e.g. ./run_tests.sh -p util"
echo "-u Run all Python unit tests"
echo "-a Run all checks (unit tests and lint)"
echo "-f Fix lint"
exit 1
}
while getopts rp:lusiaf OPTION; do
case $OPTION in
r)
echo "### Installing Python requirements"
setup_python
;;
l)
echo "### Testing lint"
run_py_lint_test
;;
p)
echo "### Running Python tests in ${OPTARG}"
run_py_test ${OPTARG}
;;
u)
echo "### Running all Python unit tests"
run_all_py_tests
;;
f)
echo "### Fixing lint errors"
run_py_lint_fix
;;
a)
echo "### Running all checks (unit tests and lint)"
run_all_py_tests
run_py_lint_test
;;
*)
help
esac
done
if [ $OPTIND -eq 1 ]
then
help
fi