forked from datacommonsorg/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.sh
executable file
·273 lines (254 loc) · 7.23 KB
/
run_test.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
# Copyright 2023 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
function setup_python {
python3 -m venv .env
source .env/bin/activate
pip3 install -r server/requirements.txt
python3 -m pip install --upgrade pip setuptools light-the-torch
ltt install torch --cpuonly
pip3 install -r nl_server/requirements.txt
}
# Run test for client side code.
function run_npm_test {
cd static
npm run test
cd ..
}
# Tests if lint is required on client side code
function run_npm_lint_test {
cd static
npm list eslint || npm install eslint
if ! npm run test-lint; then
echo "Fix lint errors by running ./run_test.sh -f"
exit 1
fi
cd ..
}
# Fixes lint
function run_lint_fix {
echo -e "#### Fixing client-side code"
cd static
npm list eslint || npm install eslint
npm run lint
cd ..
echo -e "#### Fixing Python code"
python3 -m venv .env
source .env/bin/activate
pip3 install yapf==0.33.0 -q
if ! command -v isort &> /dev/null
then
pip3 install isort -q
fi
yapf -r -i -p --style='{based_on_style: google, indent_width: 2}' server/ nl_server/ shared/ tools/ -e=*pb2.py -e=.env/*
isort server/ nl_server/ shared/ tools/ --skip-glob *pb2.py --profile google
deactivate
}
# Build client side code
function run_npm_build () {
cd static
if [[ $1 == true ]]
then
echo -e "#### Only installing production dependencies"
npm install --omit=dev
npm run-script build
else
npm install
npm run-script dev-build
fi
cd ..
}
# Run test and check lint for Python code.
function run_py_test {
# Run server pytest.
python3 -m venv .env
source .env/bin/activate
export FLASK_ENV=test
python3 -m pytest server/tests/ -s
python3 -m pytest shared/tests/ -s
python3 -m pytest nl_server/tests/ -s
# Tests within tools/nl/embeddings
echo "Running tests within tools/nl/embeddings:"
cd tools/nl/embeddings
pip3 install -r requirements.txt
python3 -m pytest ./ -s
cd ../../..
pip3 install yapf==0.33.0 -q
if ! command -v isort &> /dev/null
then
pip3 install isort -q
fi
echo -e "#### Checking Python style"
if ! yapf --recursive --diff --style='{based_on_style: google, indent_width: 2}' -p server/ nl_server/ tools/ -e=*pb2.py -e=.env/*; then
echo "Fix Python lint errors by running ./run_test.sh -f"
exit 1
fi
if ! isort server/ nl_server/ tools/ -c --skip-glob *pb2.py --profile google; then
echo "Fix Python import sort orders by running ./run_test.sh -f"
exit 1
fi
}
# Run test for webdriver automation test codes.
function run_webdriver_test {
python3 -m venv .env
source .env/bin/activate
printf '\n\e[1;35m%-6s\e[m\n\n' "!!! Have you generated the prod client packages? Run './run_test.sh -b' first to do so"
if [ ! -d server/dist ]
then
echo "no dist folder, please run ./run_test.sh -b to build js first."
exit 1
fi
export FLASK_ENV=webdriver
export GOOGLE_CLOUD_PROJECT=datcom-website-dev
python3 -m pytest -n 10 --reruns 2 server/webdriver/tests/
}
# Run test for screenshot test codes.
function run_screenshot_test {
python3 -m venv .env
source .env/bin/activate
printf '\n\e[1;35m%-6s\e[m\n\n' "!!! Have you generated the prod client packages? Run './run_test.sh -b' first to do so"
if [ ! -d server/dist ]
then
echo "no dist folder, please run ./run_test.sh -b to build js first."
exit 1
fi
export FLASK_ENV=webdriver
export GOOGLE_CLOUD_PROJECT=datcom-website-dev
export ENABLE_MODEL=true
export MIXER_API_KEY=
export PALM_API_KEY=
python3 -m pytest --reruns 2 server/webdriver/screenshot/
}
# Run integration test for NL and explore interface
# The first argument will be the test file under `integration_tests` folder
function run_integration_test {
python3 -m venv .env
source .env/bin/activate
export ENABLE_MODEL=true
export FLASK_ENV=integration_test
export GOOGLE_CLOUD_PROJECT=datcom-website-dev
export TEST_MODE=test
python3 -m pytest -vv --reruns 2 server/integration_tests/$1
}
function update_integration_test_golden {
python3 -m venv .env
source .env/bin/activate
export ENABLE_MODEL=true
export FLASK_ENV=integration_test
export GOOGLE_CLOUD_PROJECT=datcom-website-dev
export TEST_MODE=write
python3 -m pytest -vv server/integration_tests/topic_cache
python3 -m pytest -vv -n 5 --reruns 2 server/integration_tests/
python3 -m pytest -vv server/tests/nodejs_e2e_test.py
}
function run_all_tests {
run_py_test
run_npm_build
run_webdriver_test
run_npm_lint_test
run_npm_test
run_integration_test explore_test.py
run_integration_test nl_test.py
}
function help {
echo "Usage: $0 -pwblcsaf"
echo "-p Run server python tests"
echo "-w Run webdriver tests"
echo "--explore Run explore integration tests"
echo "--nl Run nl integration tests"
echo "--setup_python Setup python environment"
echo "-g Update integration test golden files"
echo "-o Build for production (ignores dev dependencies)"
echo "-b Run client install and build"
echo "-l Run client lint test"
echo "-c Run client tests"
echo "-a Run all tests"
echo "-f Fix lint"
exit 1
}
# Always reset the variable null.
while [[ "$#" -gt 0 ]]; do
case "$1" in
-p)
echo -e "### Running server tests"
run_py_test
shift 1
;;
-w)
echo -e "### Running webdriver tests"
run_webdriver_test
shift 1
;;
--explore)
echo --explore "### Running explore page integration tests"
run_integration_test explore_test.py
shift 1
;;
--nl)
echo --nl "### Running nl page integration tests"
run_integration_test nl_test.py
shift 1
;;
--setup_python)
echo --setup_python "### Set up python environment"
setup_python
shift 1
;;
-g)
echo -e "### Updating integration test goldens"
update_integration_test_golden
shift 1
;;
-o)
echo -e "### Production flag enabled"
PROD=true
shift 1
;;
-b)
echo -e "### Build client-side packages"
run_npm_build $PROD
shift 1
;;
-l)
echo -e "### Running lint"
run_npm_lint_test
shift 1
;;
-c)
echo -e "### Running client tests"
run_npm_test
shift 1
;;
-s)
echo -e "### Running screenshot tests"
run_screenshot_test
shift 1
;;
-f)
echo -e "### Fix lint errors"
run_lint_fix
shift 1
;;
-a)
echo -e "### Running all tests"
run_all_tests
shift 1
;;
*)
help
exit 1
;;
esac
done