-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-pytest
executable file
·171 lines (148 loc) · 4.55 KB
/
run-pytest
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
#!/bin/bash
# shellcheck disable=SC1091,SC2086
# Find directory where this script resides
CUR_DIR=$( dirname "$0" )
CUR_DIR=$( readlink -f "$CUR_DIR" )
SCRIPT_NAME=$( basename "$0" )
# Script help text
runhelp() {
echo ""
echo "Usage: Run Sandhill tests using PyTest"
echo ""
echo "Will run tests from: $CUR_DIR"
echo ""
echo "FLAGS:"
echo " -u|--unit"
echo " Run unit tests"
echo " -f|--functional"
echo " Run functional tests"
echo " -a|--a11y"
echo " Run accessibility tests"
echo " -m|--metadata"
echo " Run metadata tests"
echo " -k EXPRESSION"
echo " Only run tests matching the given EXPRESSION"
echo " -x|--exitfirst"
echo " Exit on first test failure"
echo " -v|--verbose"
echo " Show verbose output"
echo ""
}
if [[ -z "$1" || $1 == "-h" || $1 == "--help" || $1 == "help" ]]; then
runhelp
exit 0
fi
###############################
## Parse a value for a given config line
## $1 -> File to search
## $2 -> Name of parameter to get value for
## Prints the string value, or empty string if not found
config_param_get() {
# Use of xargs is to remove wrapped quotes from value
grep -E "^ *$2 *=" "$1" 2> /dev/null | tail -n 1 | cut -d= -f2- | sed -e 's/ *$//' -e 's/^ *//' | xargs
}
in_docker() {
[[ -f /.dockerenv ]]
}
cmd_exists() {
which "$1" >/dev/null
}
in_virtualenv() {
python3 -c "import sys; sys.exit(sys.prefix == sys.base_prefix);"
}
main() {
PYTEST_FLAGS=
# Parse flag arguments
while [[ $# -gt 0 ]]; do
case $1 in
-f|--functional)
TEST_TYPE=functional
shift ;;
-u|--unit)
TEST_TYPE=unit
shift ;;
-a|--a11y)
TEST_TYPE=a11y
shift ;;
-m|--metadata)
TEST_TYPE=metadata
shift ;;
-k)
PYTEST_FLAGS+=" -k $2"
shift 2 ;;
-x|--exitfirst)
PYTEST_FLAGS+=' -x'
shift ;;
-v|--verbose)
PYTEST_FLAGS+=' -vvv --assert=plain'
shift ;;
*)
echo "ERROR: Unknown flag: $1"
exit 1
esac
done
# Go to the script directory
echo "Testing from: $CUR_DIR"
pushd "$CUR_DIR" > /dev/null || true
# Activate the virtualenv
[[ -r env/bin/activate ]] && source env/bin/activate
if ! cmd_exists "pytest"; then
echo "ERROR: Could not find pytest. Need either virtualenv, to be inside container, or set SANDHILL_CONTAINER within instance/sandhill.cfg file."
exit 1
fi
# Read in local env vars
[[ -r .env ]] && source .env
# Set env vars
export DISPLAY=:4
export PATH=/usr/local/bin:/usr/bin:$PATH
# Run the tests
if [[ $TEST_TYPE == "functional" ]]; then
# Set instance dir
export INSTANCE_DIR=$CUR_DIR/instance
pytest -m functional --no-cov -n 4 --showlocals $PYTEST_FLAGS
PYTEST_EXIT_CODE=$?
elif [[ $TEST_TYPE == "metadata" ]]; then
# Set instance dir
export INSTANCE_DIR=$CUR_DIR/instance
pytest -m metadata --no-cov -n 4 --showlocals $PYTEST_FLAGS
PYTEST_EXIT_CODE=$?
elif [[ $TEST_TYPE == "a11y" ]]; then
# Starting x virtual frame buffer, needed for headless browser
Xvfb :4 -ac &
PID=$!
echo "Running Xvfb with PID: $PID"
# Set instance dir
export INSTANCE_DIR=$CUR_DIR/instance
echo "Running Accessibility tests against $SERVER_NAME"
pytest -m a11y --no-cov -n 4 $PYTEST_FLAGS
PYTEST_EXIT_CODE=$?
# Stop headless server
kill $PID
elif [[ $TEST_TYPE == "unit" ]]; then
export INSTANCE_DIR=$CUR_DIR/tests/instance
pytest $PYTEST_FLAGS
PYTEST_EXIT_CODE=$?
else
echo "ERROR: Unknown test type of '$TEST_TYPE'"
exit 1
fi
# Return
popd > /dev/null || true
exit $PYTEST_EXIT_CODE
}
find_container_by_prefix() {
if [[ -f $CUR_DIR/instance/sandhill.cfg ]]; then
declare CONTAINER_PREFIX
CONTAINER_PREFIX=$(config_param_get "$CUR_DIR/instance/sandhill.cfg" SANDHILL_CONTAINER)
cmd_exists "docker" && \
[[ -n $CONTAINER_PREFIX ]] && \
docker ps --filter "name=${CONTAINER_PREFIX}" --format '{{.Names}}'
fi
}
CONTAINER_NAME=$(find_container_by_prefix)
if ! in_virtualenv && ! in_docker && [[ -n $CONTAINER_NAME ]]; then
echo "Running command from within Docker: $CONTAINER_NAME"
docker exec -t "$CONTAINER_NAME" ./"$SCRIPT_NAME" "$@"
else
main "$@"
fi