-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalyptia-bats.sh
executable file
·127 lines (108 loc) · 4.63 KB
/
calyptia-bats.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
#!/bin/bash
set -euo pipefail
# The root of the tests to run locally, i.e. the custom test files we want to execute.
export TEST_ROOT=${TEST_ROOT:?}
# The branch to use in the Calyptia BATS repository
export CALYTPIA_BATS_REF=${CALYTPIA_BATS_REF:-main}
# THe local directory to check out Calyptia BATS into, if this exists we will do nothing for Calyptia BATS
export CALYPTIA_BATS_DIR=${CALYPTIA_BATS_DIR:-$PWD/calyptia-bats}
# Helper files can include custom functions to simplify testing
# This is the location of the default helpers.
export HELPERS_ROOT=${HELPERS_ROOT:-$CALYPTIA_BATS_DIR}
# This is th location of any custom helpers.
export CUSTOM_HELPERS_ROOT=${CUSTOM_HELPERS_ROOT:-$TEST_ROOT/helpers/}
# Some common options
export CONTAINER_RUNTIME=${CONTAINER_RUNTIME:-docker}
export BATS_FORMATTER=${BATS_FORMATTER:-tap}
export BATS_ARGS=${BATS_ARGS:---timing --verbose-run}
# BATS installation location
export BATS_ROOT=${BATS_ROOT:-$CALYPTIA_BATS_DIR/bats}
export BATS_FILE_ROOT=$BATS_ROOT/lib/bats-file
export BATS_SUPPORT_ROOT=$BATS_ROOT/lib/bats-support
export BATS_ASSERT_ROOT=$BATS_ROOT/lib/bats-assert
export BATS_DETIK_ROOT=$BATS_ROOT/lib/bats-detik
# BATS support tool versions
export BATS_ASSERT_VERSION=${BATS_ASSERT_VERSION:-2.0.0}
export BATS_SUPPORT_VERSION=${BATS_SUPPORT_VERSION:-0.3.0}
export BATS_FILE_VERSION=${BATS_FILE_VERSION:-0.3.0}
export BATS_DETIK_VERSION=${BATS_DETIK_VERSION:-1.0.0}
function install_calyptia_bats() {
if [[ -d "$CALYPTIA_BATS_DIR" ]]; then
echo "Found existing CALYPTIA_BATS_DIR directory so assuming already present."
else
git clone -b "$CALYTPIA_BATS_REF" https://github.com/calyptia/bats.git "$CALYPTIA_BATS_DIR"
fi
}
function install_bats() {
echo "Reinstalling BATS support libraries to $BATS_ROOT"
rm -rf "${BATS_ROOT}"
mkdir -p "${BATS_ROOT}/lib"
DOWNLOAD_TEMP_DIR=$(mktemp -d)
# Install BATS helpers using specified versions
pushd "${DOWNLOAD_TEMP_DIR}"
curl -sLO "https://github.com/bats-core/bats-assert/archive/refs/tags/v$BATS_ASSERT_VERSION.zip"
unzip -q "v$BATS_ASSERT_VERSION.zip"
mv -f "${DOWNLOAD_TEMP_DIR}/bats-assert-$BATS_ASSERT_VERSION" "${BATS_ASSERT_ROOT}"
rm -f "v$BATS_ASSERT_VERSION.zip"
curl -sLO "https://github.com/bats-core/bats-support/archive/refs/tags/v$BATS_SUPPORT_VERSION.zip"
unzip -q "v$BATS_SUPPORT_VERSION.zip"
mv -f "${DOWNLOAD_TEMP_DIR}/bats-support-$BATS_SUPPORT_VERSION" "${BATS_SUPPORT_ROOT}"
rm -f "v$BATS_SUPPORT_VERSION.zip"
curl -sLO "https://github.com/bats-core/bats-file/archive/refs/tags/v$BATS_FILE_VERSION.zip"
unzip -q "v$BATS_FILE_VERSION.zip"
mv -f "${DOWNLOAD_TEMP_DIR}/bats-file-$BATS_FILE_VERSION" "${BATS_FILE_ROOT}"
rm -f "v$BATS_FILE_VERSION.zip"
curl -sLO "https://github.com/bats-core/bats-detik/archive/refs/tags/v$BATS_DETIK_VERSION.zip"
unzip -q "v$BATS_DETIK_VERSION.zip"
mv -f "${DOWNLOAD_TEMP_DIR}/bats-detik-$BATS_DETIK_VERSION/lib" "${BATS_DETIK_ROOT}"
rm -f "v$BATS_DETIK_VERSION.zip"
popd
rm -rf "${DOWNLOAD_TEMP_DIR}"
}
# Helper function to run a set of tests based on our specific configuration
# This function will call `exit`, so any cleanup must be done inside of it.
function run_tests() {
local requested=${1:-}
local run=""
if [[ "$requested" == "all" ]] || [ -z "$requested" ]; then
# Empty => everything. Alternatively, explicitly ask for it.
# When running advanced we also need to run standard
run="--recursive ${TEST_ROOT}/"
elif [[ "$requested" =~ .*\.bats$ ]]; then
# One individual test
run="$requested"
elif [ -d "${TEST_ROOT}/$requested" ]; then
# Likely an individual integration suite
run="--recursive ${TEST_ROOT}/$requested"
fi
echo
echo
echo "========================"
echo "Starting tests."
echo "========================"
echo
echo
# We run BATS in a subshell to prevent it from inheriting our exit/err trap, which can mess up its internals
# We set +exu because unbound variables can cause test failures with zero context
set +xeu
# shellcheck disable=SC2086
(bats --formatter "${BATS_FORMATTER}" $run $BATS_ARGS)
local bats_retval=$?
echo
echo
echo "========================"
if [ "$bats_retval" -eq 0 ]; then
echo "All tests passed!"
else
echo "Some tests failed. Please inspect the output above for details."
fi
echo "========================"
echo
echo
exit $bats_retval
}
if [[ "${SKIP_BATS_INSTALL:-no}" != "yes" ]]; then
install_calyptia_bats
install_bats
fi
run_tests "$@"