-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.sh
More file actions
executable file
·123 lines (110 loc) · 2.43 KB
/
run-tests.sh
File metadata and controls
executable file
·123 lines (110 loc) · 2.43 KB
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
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="${script_dir}"
devcontainer_exec="${script_dir}/scripts/devcontainer-exec.sh"
usage() {
cat <<'USAGE'
Usage: ./run-tests.sh [--mode <basic|mongo|all>] [--coverage]
Options:
-m, --mode Test mode to run. Default: all
-c, --coverage Enable coverage for selected mode
-h, --help Show this help message
USAGE
}
mode="all"
coverage="false"
while [[ $# -gt 0 ]]; do
case "$1" in
-m|--mode)
if [[ $# -lt 2 ]]; then
echo "Error: --mode requires a value" >&2
usage
exit 1
fi
mode="$2"
shift 2
;;
--mode=*)
mode="${1#*=}"
shift
;;
-c|--coverage)
coverage="true"
shift
;;
-h|--help)
usage
exit 0
;;
basic|mongo|all)
mode="$1"
shift
;;
*)
echo "Error: unknown argument '$1'" >&2
usage
exit 1
;;
esac
done
case "${mode}" in
basic|mongo|all)
;;
*)
echo "Error: invalid mode '${mode}'. Use basic, mongo, or all." >&2
usage
exit 1
;;
esac
if [[ "${coverage}" == "true" ]]; then
case "${mode}" in
basic)
test_cmd=(npm run test:coverage:basic)
;;
mongo)
test_cmd=(npm run test:coverage -- --runInBand src/__tests__/storage/mongodb-storage.test.js)
;;
all)
test_cmd=(npm run test:coverage)
;;
esac
else
case "${mode}" in
basic)
test_cmd=(npm run test:basic)
;;
mongo)
test_cmd=(npm run test:mongo)
;;
all)
test_cmd=(npm test)
;;
esac
fi
docker_available() {
if ! command -v docker >/dev/null 2>&1; then
return 1
fi
docker ps >/dev/null 2>&1
}
# 1) If already inside any container, run tests directly.
if [[ -f "/.dockerenv" ]]; then
exec "${test_cmd[@]}"
fi
# 2) If Docker is unavailable, run tests locally on host.
if ! docker_available; then
exec "${test_cmd[@]}"
fi
# 3) If a devcontainer for this repo is running, run tests there.
container_id="$("${devcontainer_exec}" --find-container-id 2>/dev/null || true)"
if [[ -n "${container_id}" ]]; then
exec "${devcontainer_exec}" "${test_cmd[@]}"
fi
# 4) No devcontainer: run tests in a fresh disposable container (legacy behavior).
cmd_string="$(printf '%q ' "${test_cmd[@]}")"
exec docker run --rm -i \
-v "${repo_root}:/app" \
-w /app \
node:latest \
sh -c "npm install && ${cmd_string}"