-
Notifications
You must be signed in to change notification settings - Fork 1
/
_helper.bash
61 lines (48 loc) · 1.97 KB
/
_helper.bash
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
#!/usr/bin/env bash
#
# Helpers related to common testing functionality.
#
# Run with "--verbose-run" to see debug output.
#
################################################################################
# BATS HOOK IMPLEMENTATIONS #
################################################################################
setup() {
[ ! -d ".git" ] && echo "Tests must be run from the repository root directory." && exit 1
# For a list of available variables see:
# @see https://bats-core.readthedocs.io/en/stable/writing-tests.html#special-variables
# Register a path to libraries.
export BATS_LIB_PATH="${BATS_TEST_DIRNAME}/node_modules"
# Load 'bats-helpers' library.
bats_load_library bats-helpers
# Setup command mocking.
setup_mock
# Current directory where the test is run from.
# shellcheck disable=SC2155
export CUR_DIR="$(pwd)"
# Project directory root (where .git is located).
export ROOT_DIR="${CUR_DIR}"
# Directory where the shell command script will be running in.
export BUILD_DIR="${BUILD_DIR:-"${BATS_TEST_TMPDIR//\/\//\/}/shell-$(date +%s)"}"
fixture_prepare_dir "${BUILD_DIR}"
# Copy codebase at the last commit into the BUILD_DIR.
# Tests requiring to work with the copy of the codebase should opt-in using
# BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1.
# Note that during development of tests the local changes need to be
# committed.
fixture_export_codebase "${BUILD_DIR}" "${ROOT_DIR}"
# Print debug information if "--verbose-run" is passed.
# LCOV_EXCL_START
if [ "${BATS_VERBOSE_RUN-}" = "1" ]; then
echo "BUILD_DIR: ${BUILD_DIR}" >&3
fi
# LCOV_EXCL_END
# Change directory to the current project directory for each test. Tests
# requiring to operate outside of BUILD_DIR should change directory explicitly
# within their tests.
pushd "${BUILD_DIR}" >/dev/null || exit 1
}
teardown() {
# Move back to the original directory.
popd >/dev/null || cd "${CUR_DIR}" || exit 1
}