forked from datacommonsorg/data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·109 lines (98 loc) · 2.62 KB
/
run_tests.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
#!/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
# Array of top-level folders with Python code.
PYTHON_FOLDERS="util/ scripts/ import-automation/"
# Flag used signal if Python requirements have already been installed.
PYTHON_REQUIREMENTS_INSTALLED=false
function setup_python {
python3 -m venv .env
source .env/bin/activate
if [[ "$PYTHON_REQUIREMENTS_INSTALLED" = false ]]
then
echo "Installing Python requirements"
pip3 install -r requirements.txt -q
PYTHON_REQUIREMENTS_INSTALLED=true
fi
}
# Fixes lint
function run_py_lint_fix {
setup_python
echo "#### Fixing Python code"
yapf -r -i -p --style=google $PYTHON_FOLDERS
}
# Tests Python code style
function run_py_lint_test {
setup_python
echo "#### Testing Python lint"
if ! yapf -r --diff -p --style=google $PYTHON_FOLDERS; then
echo "ERROR: Fix lint errors by running ./run_test.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 help {
echo "Usage: $0 -rplaf"
echo "-r Install Python requirements"
echo "-l Test lint on Python code"
echo "-p Run Python tests in specified folder, e.g. ./run_test.sh -p util"
echo "-a Run all tests"
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}
;;
f)
echo "### Fixing lint errors"
run_py_lint_fix
;;
a)
echo "### Running all tests"
for FOLDER in $PYTHON_FOLDERS
do
run_py_test $FOLDER
done
run_py_lint_test
;;
*)
help
esac
done
if [ $OPTIND -eq 1 ]
then
help
fi