-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapdir-test
executable file
·418 lines (378 loc) · 12.3 KB
/
snapdir-test
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env bash
# # snapdir-test
#
# Test utilities and runner for snapdir commands.
#
# ## Usage:
#
# # runs all tests for snapdir
# snapdir-test
#
# # soucing into an existing script
# . snapdir-test [sourced-test-file]
#
# The snapdir-test script is intended to be sourced by other
# scripts to expose the following functions.
#
# - describe: Describes a group of checks.
# - check: Using a check will instruct the test runner to expect
# a fail/pass. The test runner with grep for "check " entries
# to guess how many tests are expected to be implemented.
# - fail: Fail the test, shows the message and runs the tear down.
# - pass: Decrement the number of pending tests as tracked by check.
# - run_tests: Runs the tests.
# - run_tests_without_teardown: Runs the tests without tear down.
#
# A temporary directory is created for each test run and can be accessed
# via _SNAPDIR_TEST_TMP_DIR. This directory is removed when the test
# finishes unless tests are run with `run_tests_without_teardown`.
#
#
# ## Options:
#
# sourced-test-file When specified, the test file is sourced and
# the thest suite will take the basename of the
# file as the name of the test suite.
#
# ## Examples:
#
#
# # Import test utilities
# # shellcheck disable=SC1091
# . "$(dirname "${BASH_SOURCE[0]}")/snapdir-test" "${BASH_SOURCE[0]}"
#
# test_suite() {
# local result
#
# describe "group of checks description"
#
# check "check a"
# result=$(echo "a" 2>&1 || true)
# test "$result" == "a" || fail "expected '${result}' to be a" && pass
#
# }
#
# run_tests
#
# LICENSE: MIT Copyright (c) 2022 Bermi Ferrer
set -eEuo pipefail
IFS=$'\n\t'
if [[ ${BASH_VERSION:0:1} -lt "4" ]]; then
echo "Bash version 4.0 or higher is required. You have: ${BASH_VERSION}" >&2
exit 1
fi
_snapdir_test_run_unit_tests() {
set -eEuo pipefail
local dir
dir="$(dirname "${BASH_SOURCE[0]}")"
SECONDS=0
"$dir/snapdir-manifest" test
if command -v md5sum >/dev/null 2>&1; then
"$dir/snapdir-manifest" test --checksum-bin=md5sum
fi
if command -v sha256sum >/dev/null 2>&1; then
"$dir/snapdir-manifest" test --checksum-bin=sha256sum
fi
"$dir/snapdir" test
if test -f "$dir/snapdir-file-store"; then
"$dir/snapdir-file-store" test
else
echo "# skipping snapdir-file-store test, $dir/snapdir-file-store not found" 2>&1
fi
echo "# unit tests completed in ${SECONDS} seconds"
}
_snapdir_test_run_integration_tests() {
set -eEuo pipefail
local dir
dir="$(dirname "${BASH_SOURCE[0]}")"
echo "# integration tests"
SECONDS=0
# source .env if it exists
if test -f "$dir/.env"; then
set -a
# shellcheck disable=SC1091 source=./.env
. "$dir/.env"
set +a
fi
if test -f "$dir/snapdir-b2-store" && [[ ${SNAPDIR_B2_STORE_TEST_BUCKET:-} != "" ]]; then
"$dir/snapdir-b2-store" test --store "b2://${SNAPDIR_B2_STORE_TEST_BUCKET}"
else
echo "# skipping snapdir-b2-store test. Missing SNAPDIR_B2_STORE_TEST_BUCKET or $dir/snapdir-b2-store" >&2
fi
if test -f "$dir/snapdir-s3-store" && [[ ${SNAPDIR_S3_STORE_TEST_BUCKET:-} != "" ]]; then
"$dir/snapdir-s3-store" test --store "s3://${SNAPDIR_S3_STORE_TEST_BUCKET}"
else
echo "# skipping snapdir-s3-store test. Missing SNAPDIR_S3_STORE_TEST_BUCKET or $dir/snapdir-s3-store" >&2
fi
echo "# integration tests completed in ${SECONDS} seconds"
}
_snapdir_test_run_catalog_tests() {
set -eEuo pipefail
local dir
dir="$(dirname "${BASH_SOURCE[0]}")"
echo "# integration tests"
if command -v "sqlite3" >/dev/null; then
echo "# using sqlite3 --version: $(sqlite3 --version)"
"$dir"/snapdir-sqlite3-catalog test
SNAPDIR_CATALOG=sqlite3 "$dir"/snapdir test
else
echo "# skipping sqlite3 catalog test, sqlite3 not found locally" >&2
fi
}
_snapdir_test_utils() {
# note: using subshell – '(' instead of '{' – to avoid leaking helper functions
set -eEuo pipefail
local target_file="${1:?Missing target file}"
export _TARGET_TEST_FILE="$target_file"
_SNAPDIR_MODULE_NAME=$(basename "${target_file}")
export _SNAPDIR_MODULE_NAME
setup() {
set -eEuo pipefail
rm -rf /tmp/"${_SNAPDIR_MODULE_NAME}"_tests*
_SNAPDIR_TEST_TMP_DIR="$(mktemp -d -t "${_SNAPDIR_MODULE_NAME}"_tests.XXXXXXXXXX)"
# get the realpath to the tmp dir, on macos it might be prefixed with /private
_SNAPDIR_TEST_TMP_DIR=$(realpath "$_SNAPDIR_TEST_TMP_DIR")
umask 077 "$_SNAPDIR_TEST_TMP_DIR"
export _SNAPDIR_TEST_TMP_DIR
export _SNAPDIR_CWD="$_SNAPDIR_TEST_TMP_DIR"
export _SNAPDIR_CACHE_DIR="${_SNAPDIR_TEST_TMP_DIR}/.cache/${_SNAPDIR_MODULE_NAME}"
export SNAPDIR_CACHE_DIR="${_SNAPDIR_CACHE_DIR}"
export _SNAPDIR_MANIFEST_CACHE_DIR="${_SNAPDIR_CACHE_DIR}"
export SNAPDIR_SQLITE3_CATALOG_DB_PATH="${_SNAPDIR_TEST_TMP_DIR}/catalog.db"
cd "$_SNAPDIR_TEST_TMP_DIR" || return 1
setup_suite
}
_SNAPDIR_TEST_TOTAL_CHECKS=$(grep -c 'check "[^"]*"' "${target_file}" || echo "-1")
_SNAPDIR_TESTS_PENDING_CHECKS=$_SNAPDIR_TEST_TOTAL_CHECKS
_SNAPDIR_TEST_SUITE_DESCRIPTION="${_SNAPDIR_MODULE_NAME}"
_SNAPDIR_TEST_DESCRIPTION=$_SNAPDIR_TEST_SUITE_DESCRIPTION
if [[ $_SNAPDIR_TESTS_PENDING_CHECKS -lt 0 ]]; then
echo "not ok 0..0 - no tests found for $_SNAPDIR_TEST_SUITE_DESCRIPTION" >&2
echo "# could not find 'check \"some test description\"' in '$target_file'." >&2
exit 1
fi
echo "# running tests for ${_SNAPDIR_MODULE_NAME}"
echo "1..${_SNAPDIR_TEST_TOTAL_CHECKS}"
_SNAPDIR_TEST_TEARDOWN_COMPLETE=false
teardown() {
set -eEuo pipefail
# return if _SNAPDIR_TEST_TEARDOWN_COMPLETE is already true
if [[ ${_SNAPDIR_TEST_TEARDOWN_COMPLETE} == true ]]; then
return 0
fi
_SNAPDIR_TEST_TEARDOWN_COMPLETE=true
teardown_suite
rm -rf "$_SNAPDIR_TEST_TMP_DIR"
# exit with not ok if _SNAPDIR_TEST_TOTAL_CHECKS is not zero
if [[ $_SNAPDIR_TESTS_PENDING_CHECKS -gt 0 ]]; then
echo "not ok ${_SNAPDIR_TESTS_PENDING_CHECKS}..${_SNAPDIR_TEST_TOTAL_CHECKS} - ${_SNAPDIR_TESTS_PENDING_CHECKS} ${_SNAPDIR_TEST_SUITE_DESCRIPTION} tests pending" >&2
exit 1
else
echo "ok - all $_SNAPDIR_TEST_SUITE_DESCRIPTION tests passed in $SECONDS seconds"
exit 0
fi
}
describe() {
set -eEuo pipefail
_SNAPDIR_TEST_DESCRIPTION="$1"
_snapdir_test_log_line "describe: $1"
}
_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS=false
_SNAPDIR_TEST_CHECK_NAME="missing check calls on test"
check() {
set -eEuo pipefail
if [[ $_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS == true ]]; then
fail "calling check \"$1\" while pending pass is not allowed"
fi
_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS=true
_SNAPDIR_TEST_CHECK_NAME="$1"
_snapdir_test_log_line "check: $1"
}
# suites can override setup function to prepare the test environment
setup_suite() {
set -eEuo pipefail
}
# suites can override teardown function to clean up after tests finished
teardown_suite() {
set -eEuo pipefail
}
pass() {
set -eEuo pipefail
if [[ $_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS == true ]]; then
_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS=false
_SNAPDIR_TEST_NUMBER=$((_SNAPDIR_TEST_TOTAL_CHECKS - _SNAPDIR_TESTS_PENDING_CHECKS + 1))
_SNAPDIR_TESTS_PENDING_CHECKS=$((_SNAPDIR_TESTS_PENDING_CHECKS - 1))
fi
echo "ok ${_SNAPDIR_TEST_NUMBER:-0} - ${_SNAPDIR_TEST_DESCRIPTION} / $_SNAPDIR_TEST_CHECK_NAME"
}
skip() {
set -eEuo pipefail
_SNAPDIR_TEST_CHECK_LOCKED_UNTIL_PASS=false
_SNAPDIR_TEST_NUMBER=$((_SNAPDIR_TEST_TOTAL_CHECKS - _SNAPDIR_TESTS_PENDING_CHECKS + 1))
_SNAPDIR_TESTS_PENDING_CHECKS=$((_SNAPDIR_TESTS_PENDING_CHECKS - 1))
echo "ok ${_SNAPDIR_TEST_NUMBER} - # skip ${_SNAPDIR_TEST_DESCRIPTION} / $_SNAPDIR_TEST_CHECK_NAME"
}
fail() {
set -eEuo pipefail
_SNAPDIR_TEST_NUMBER=$((_SNAPDIR_TEST_TOTAL_CHECKS - _SNAPDIR_TESTS_PENDING_CHECKS + 1))
echo "not ok ${_SNAPDIR_TEST_NUMBER} - ${_SNAPDIR_TEST_DESCRIPTION} / $_SNAPDIR_TEST_CHECK_NAME: $*" >&2
echo "# fail called by: ${_TARGET_TEST_FILE}:$(caller 0 | awk '{print $1}')" >&2
teardown
exit 1
}
generate_files() {
set -eEuo pipefail
local dir=$_SNAPDIR_TEST_TMP_DIR
mkdir -p "$dir/files/"
echo "foo" >"$dir/files/foo"
echo "bar" >"$dir/files/bar"
}
generate_nested_files() {
set -eEuo pipefail
local dir=$_SNAPDIR_TEST_TMP_DIR
mkdir -p "$dir/files/a/a1" "$dir/files/b/b1"
echo "foo" >"$dir/files/a/a1/foo"
echo "bar" >"$dir/files/b/b1/bar"
}
clean_files() {
rm -rf "$_SNAPDIR_TEST_TMP_DIR/files"
}
run_tests() {
SECONDS=0
trap teardown EXIT
setup
test_suite || {
teardown
return 1
}
teardown
}
run_tests_without_teardown() {
SECONDS=0
_SNAPDIR_TEST_TEARDOWN_COMPLETE=true
setup
test_suite || {
echo "Left directory $_SNAPDIR_TEST_TMP_DIR untouched." >&2
return 1
}
echo "Left directory $_SNAPDIR_TEST_TMP_DIR untouched." >&2
}
}
_snapdir_test_help() {
set -eEuo pipefail
sed '/# LICENSE: MIT Copyright (c) 2022 Bermi Ferrer/q; 1,2d' "${BASH_SOURCE[0]}" | sed -E 's|^# ?||g; $d' | more
exit 0
}
# is any of the arguments -h or --help?
if [[ ${1:-} == -h || ${1:-} == --help ]]; then
_snapdir_test_help
fi
_snapdir_test_init_log() {
set -eEuo pipefail
# Saves the command into the run log for debugging, documentation, etc.
# _SNAPDIR_RUN_LOG_PATH=$(pwd)/test-log ./snapdir-test
if [[ ${_SNAPDIR_RUN_LOG_PATH:-} != "" ]] && test -f "$_SNAPDIR_RUN_LOG_PATH"; then
echo "" >"$_SNAPDIR_RUN_LOG_PATH"
export _SNAPDIR_RUN_LOG_PATH
else
unset _SNAPDIR_RUN_LOG_PATH
fi
}
_snapdir_test_log_line() {
set -eEuo pipefail
local line="$1"
if [[ ${_SNAPDIR_RUN_LOG_PATH:-} != "" ]]; then
echo "# $line" >>"$_SNAPDIR_RUN_LOG_PATH"
fi
}
_snapdir_test_normalize_log() {
set -eEuo pipefail
# Cleanup the run log to delete random stuff and make it
# easier to read and ensure it's valid bash.
if [[ ${_SNAPDIR_RUN_LOG_PATH:-} != "" ]] && test -f "$_SNAPDIR_RUN_LOG_PATH"; then
{
echo "#!/usr/bin/env bash"
# shellcheck disable=SC2016
echo '# WARNING, do not edit manually.
# generated by running:
# _SNAPDIR_RUN_LOG_PATH="$(pwd)/docs/tests/tested-commands.sh" ./snapdir-test integration
# We use the results to generate documentation and generative testing.
'
# shellcheck disable=SC2016
sed -E '
s|/tmp/([^.]*).[^/]*(/?)|/tmp/\1\2|g;
s|([a-f0-9]{3}/[a-f0-9]{3}/[a-f0-9]{3}/[a-f0-9]{55})|"${ID_PATH}"|g;
s|([a-f0-9]{32,64})|"${ID}"|g;
s|--([a-z0-9-]*)=|--\1 |g;
s|--([a-z]*)-file ([^ ]*)|--\1-file "${\U\1_PATH}"|g;
s#--([a-z]*)-(path|dir) ([^ ]*)#--\1-\2 "${\U\1_\2}"#g;
s# /tmp/[^ ]*# "${DIR}"#g;
s#( ([^ ]*)-dir|\..*)$# "${DIR}"#g;
s# ([^ ]*)-file$# "${FILE_PATH}"#g;
s#--(store|path) ([^ ]*)#--\1 "${\U\1}"#g;
s#--(exclude|include) ([^ ]*)#--\1 "${\U\1_PATTERN}"#g;
'
} <"${_SNAPDIR_RUN_LOG_PATH}" >"${_SNAPDIR_RUN_LOG_PATH}.tmp"
mv "${_SNAPDIR_RUN_LOG_PATH}.tmp" "${_SNAPDIR_RUN_LOG_PATH}"
# ensure the previous steps have not messed up with the shell commands
if command -v "shellcheck" >/dev/null 2>&1; then
shellcheck "${_SNAPDIR_RUN_LOG_PATH}"
fi
if command -v "shfmt" >/dev/null 2>&1; then
shfmt -w -s "${_SNAPDIR_RUN_LOG_PATH}"
fi
fi
}
if [[ "$(uname -s)" == "Darwin" ]]; then
_snapdir_test_readlink() {
set -eEuo pipefail
echo "$(cd "$(dirname "$1")" || echo "" && pwd)/$(basename "$1")"
}
# Disable on macOS, we expect to run this only when
# running the whole test suite, including integrations
_snapdir_test_init_log() {
unset _SNAPDIR_RUN_LOG_PATH
}
_snapdir_test_normalize_log() {
true
}
else
_snapdir_test_readlink() {
set -eEuo pipefail
readlink -f "$1"
}
fi
export ENVIRONMENT="test"
# Run if is not sourced
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
# does ${1:-} matches -h, --help or help?
if [[ ${1:-} == -h || ${1:-} == --help || ${1:-} == help ]]; then
_snapdir_test_help
exit 0
fi
# avoid trashing the user catalog if they are running the test suite
unset SNAPDIR_CATALOG
export _SNAPDIR_TEST_BIN_PATH="${_SNAPDIR_TEST_BIN_PATH:-$(_snapdir_test_readlink "${BASH_SOURCE[0]}")}"
_SNAPDIR_BIN_PATH="$(dirname "${_SNAPDIR_TEST_BIN_PATH}")/snapdir"
if [[ ${1:-} == integration ]]; then
_snapdir_test_init_log
fi
if ! test -f "$_SNAPDIR_BIN_PATH"; then
if snapdir -v 2>/dev/null >/dev/null; then
_SNAPDIR_BIN_PATH="snapdir"
else
echo "error: Could not find snapdir binary"
exit 1
fi
fi
_snapdir_test_run_unit_tests
_snapdir_test_run_catalog_tests
# was integration passed?
if [[ ${1:-} == integration ]]; then
_snapdir_test_run_integration_tests
_snapdir_test_normalize_log
fi
exit 0
elif [[ ${1:-""} != "" ]]; then
_snapdir_test_utils "${1}"
fi