1# Setup hook for pytest
2echo "Sourcing pytest-check-hook"
3
4declare -ar disabledTests
5declare -a disabledTestPaths
6
7function _concatSep {
8 local result
9 local sep="$1"
10 local -n arr=$2
11 for index in ${!arr[*]}; do
12 if [ $index -eq 0 ]; then
13 result="${arr[index]}"
14 else
15 result+=" $sep ${arr[index]}"
16 fi
17 done
18 echo "$result"
19}
20
21function _pytestComputeDisabledTestsString () {
22 declare -a tests
23 local tests=($1)
24 local prefix="not "
25 prefixed=( "${tests[@]/#/$prefix}" )
26 result=$(_concatSep "and" prefixed)
27 echo "$result"
28}
29
30function pytestCheckPhase() {
31 echo "Executing pytestCheckPhase"
32 runHook preCheck
33
34 # Compose arguments
35 args=" -m pytest"
36 if [ -n "$disabledTests" ]; then
37 disabledTestsString=$(_pytestComputeDisabledTestsString "${disabledTests[@]}")
38 args+=" -k \""$disabledTestsString"\""
39 fi
40
41 if [ -n "${disabledTestPaths-}" ]; then
42 eval "disabledTestPaths=($disabledTestPaths)"
43 fi
44
45 for path in ${disabledTestPaths[@]}; do
46 if [ ! -e "$path" ]; then
47 echo "Disabled tests path \"$path\" does not exist. Aborting"
48 exit 1
49 fi
50 args+=" --ignore=\"$path\""
51 done
52 args+=" ${pytestFlagsArray[@]}"
53 eval "@pythonCheckInterpreter@ $args"
54
55 runHook postCheck
56 echo "Finished executing pytestCheckPhase"
57}
58
59if [ -z "${dontUsePytestCheck-}" ] && [ -z "${installCheckPhase-}" ]; then
60 echo "Using pytestCheckPhase"
61 preDistPhases+=" pytestCheckPhase"
62
63 # It's almost always the case that setuptoolsCheckPhase should not be ran
64 # when the pytestCheckHook is being ran
65 if [ -z "${useSetuptoolsCheck-}" ]; then
66 dontUseSetuptoolsCheck=1
67
68 # Remove command if already injected into preDistPhases
69 if [[ "$preDistPhases" =~ "setuptoolsCheckPhase" ]]; then
70 echo "Removing setuptoolsCheckPhase"
71 preDistPhases=${preDistPhases/setuptoolsCheckPhase/}
72 fi
73 fi
74fi