@@ -5,6 +5,91 @@ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55repo_root=" ${script_dir} "
66devcontainer_exec=" ${script_dir} /scripts/devcontainer-exec.sh"
77
8+ usage () {
9+ cat << 'USAGE '
10+ Usage: ./run-tests.sh [--mode <basic|mongo|all>] [--coverage]
11+
12+ Options:
13+ -m, --mode Test mode to run. Default: all
14+ -c, --coverage Enable coverage for selected mode
15+ -h, --help Show this help message
16+ USAGE
17+ }
18+
19+ mode=" all"
20+ coverage=" false"
21+
22+ while [[ $# -gt 0 ]]; do
23+ case " $1 " in
24+ -m|--mode)
25+ if [[ $# -lt 2 ]]; then
26+ echo " Error: --mode requires a value" >&2
27+ usage
28+ exit 1
29+ fi
30+ mode=" $2 "
31+ shift 2
32+ ;;
33+ --mode=* )
34+ mode=" ${1#* =} "
35+ shift
36+ ;;
37+ -c|--coverage)
38+ coverage=" true"
39+ shift
40+ ;;
41+ -h|--help)
42+ usage
43+ exit 0
44+ ;;
45+ basic|mongo|all)
46+ mode=" $1 "
47+ shift
48+ ;;
49+ * )
50+ echo " Error: unknown argument '$1 '" >&2
51+ usage
52+ exit 1
53+ ;;
54+ esac
55+ done
56+
57+ case " ${mode} " in
58+ basic|mongo|all)
59+ ;;
60+ * )
61+ echo " Error: invalid mode '${mode} '. Use basic, mongo, or all." >&2
62+ usage
63+ exit 1
64+ ;;
65+ esac
66+
67+ if [[ " ${coverage} " == " true" ]]; then
68+ case " ${mode} " in
69+ basic)
70+ test_cmd=(npm run test:coverage:basic)
71+ ;;
72+ mongo)
73+ test_cmd=(npm run test:coverage -- --runInBand src/__tests__/storage/mongodb-storage.test.js)
74+ ;;
75+ all)
76+ test_cmd=(npm run test:coverage)
77+ ;;
78+ esac
79+ else
80+ case " ${mode} " in
81+ basic)
82+ test_cmd=(npm run test:basic)
83+ ;;
84+ mongo)
85+ test_cmd=(npm run test:mongo)
86+ ;;
87+ all)
88+ test_cmd=(npm test)
89+ ;;
90+ esac
91+ fi
92+
893docker_available () {
994 if ! command -v docker > /dev/null 2>&1 ; then
1095 return 1
@@ -15,23 +100,24 @@ docker_available() {
15100
16101# 1) If already inside any container, run tests directly.
17102if [[ -f " /.dockerenv" ]]; then
18- exec npm test
103+ exec " ${test_cmd[@]} "
19104fi
20105
21106# 2) If Docker is unavailable, run tests locally on host.
22107if ! docker_available; then
23- exec npm test
108+ exec " ${test_cmd[@]} "
24109fi
25110
26111# 3) If a devcontainer for this repo is running, run tests there.
27112container_id=" $( " ${devcontainer_exec} " --find-container-id 2> /dev/null || true) "
28113if [[ -n " ${container_id} " ]]; then
29- exec " ${devcontainer_exec} " npm test
114+ exec " ${devcontainer_exec} " " ${test_cmd[@]} "
30115fi
31116
32117# 4) No devcontainer: run tests in a fresh disposable container (legacy behavior).
118+ cmd_string=" $( printf ' %q ' " ${test_cmd[@]} " ) "
33119exec docker run --rm -i \
34120 -v " ${repo_root} :/app" \
35121 -w /app \
36122 node:latest \
37- sh -c " npm install && npm test "
123+ sh -c " npm install && ${cmd_string} "
0 commit comments