-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrun-tests.sh
More file actions
executable file
·221 lines (185 loc) · 7.21 KB
/
run-tests.sh
File metadata and controls
executable file
·221 lines (185 loc) · 7.21 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
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
#!/usr/bin/env bash
#######################################
# (c) Copyright IBM Corp. 2021
#######################################
set -eo pipefail
WATCH=false
SCOPE=""
PACKAGE=""
VERSION=""
GREP_PATTERN=""
TEST_FILES=""
TEST_NAME_FILTER=""
# Helper function to normalize package names to folder names
# e.g., '@redis/client' becomes 'redis_client'
normalize_folder_name() {
echo "$1" | sed 's|^@||' | sed 's|/|_|g'
}
for arg in "$@"; do
case $arg in
--watch)
WATCH=true
shift
;;
--esm)
export RUN_ESM=true
shift
;;
--scope=*)
SCOPE="${arg#*=}"
shift
;;
*)
# Check if arg contains @ but NOT at the beginning (version separator)
# If @ is at the beginning, it's a scoped package name
if [[ $arg == @* ]]; then
# Scoped package - check if there's a version separator after the package name
# Pattern: @scope/name@version
if [[ $arg =~ ^(@[^@]+)@(.+)$ ]]; then
PACKAGE="${BASH_REMATCH[1]}"
VERSION="${BASH_REMATCH[2]}"
else
PACKAGE="$arg"
fi
elif [[ $arg == *"@"* ]]; then
# Non-scoped package with version
PACKAGE="${arg%@*}"
VERSION="${arg#*@}"
elif [ -n "$PACKAGE" ]; then
TEST_NAME_FILTER="$arg"
else
PACKAGE="$arg"
fi
shift
;;
esac
done
# Regenerate test folders if needed
if [ "$SCOPE" = "@instana/collector" ]; then
node "$(dirname "$0")/sync-test-folders.js"
fi
args=""
if [ "$WATCH" = true ]; then
args="-- --watch"
fi
npm_command="npm run test:debug"
# Derive test base directory from SCOPE (e.g., @instana/collector -> packages/collector/test)
SCOPE_PKG_NAME="${SCOPE#@instana/}"
TEST_BASE_DIR="packages/${SCOPE_PKG_NAME}/test"
if [ -n "$PACKAGE" ]; then
# Normalize package name for scoped packages (e.g., @redis/client -> redis_client)
NORMALIZED_PACKAGE=$(normalize_folder_name "$PACKAGE")
PACKAGE_DIR=""
while IFS= read -r _candidate; do
[ -z "$_candidate" ] && continue
if ls "$_candidate"/_v* &>/dev/null; then
PACKAGE_DIR="$_candidate"
break
fi
[ -z "$PACKAGE_DIR" ] && PACKAGE_DIR="$_candidate"
done < <(find "$TEST_BASE_DIR" -type d \( -path "*/$PACKAGE" -o -path "*/$NORMALIZED_PACKAGE" \) ! -path "*/node_modules/*" ! -path "*/_v*" 2>/dev/null)
if [ -z "$PACKAGE_DIR" ]; then
# No directory found — try matching a test file by name (e.g. cmdline -> cmdline.test.js or cmdline_test.js)
MATCH_FILE=$(find "$TEST_BASE_DIR" -type f \( -name "${PACKAGE}.test.js" -o -name "${PACKAGE}_test.js" -o -name "${PACKAGE}.test.mjs" -o -name "${PACKAGE}_test.mjs" \) ! -path "*/node_modules/*" ! -path "*/_v*" 2>/dev/null | head -1)
if [ -n "$MATCH_FILE" ]; then
RELATIVE_FILE=$(echo "$MATCH_FILE" | sed "s|packages/${SCOPE_PKG_NAME}/||g")
echo "Running test file: $RELATIVE_FILE"
npm_command="npm run test:debug:files -- $RELATIVE_FILE"
npx lerna exec --scope="$SCOPE" "$npm_command $args"
exit $?
fi
echo "Error: Test folder or file for '$PACKAGE' not found"
exit 1
fi
ACTUAL_PACKAGE=$(basename "$PACKAGE_DIR")
AVAILABLE_VERSIONS=$(find "$PACKAGE_DIR" -maxdepth 1 -type d -name "_v*" 2>/dev/null | sed 's/.*_v/v/' | sort -V | tr '\n' ', ' | sed 's/,$//')
if [ -n "$VERSION" ]; then
if [ "$VERSION" = "all" ]; then
ALL_VERSION_DIRS=$(find "$PACKAGE_DIR" -maxdepth 1 -type d -name "_v*" 2>/dev/null | sort -V)
if [ -z "$ALL_VERSION_DIRS" ]; then
echo "Error: No versioned directories found for '$PACKAGE'. Available versions: $AVAILABLE_VERSIONS"
exit 1
fi
echo "Running tests for all versions of $ACTUAL_PACKAGE"
echo "Available versions: $AVAILABLE_VERSIONS"
SEARCH_PATTERN="*.test.js"
if [ -n "$TEST_NAME_FILTER" ]; then
SEARCH_PATTERN="*${TEST_NAME_FILTER}*.test.js"
fi
for VERSION_DIR in $ALL_VERSION_DIRS; do
FOUND_FILES=$(find "$VERSION_DIR" -maxdepth 2 -name "$SEARCH_PATTERN" ! -name "test_base.js")
if [ -n "$FOUND_FILES" ]; then
TEST_FILES="$TEST_FILES $FOUND_FILES"
fi
done
GREP_PATTERN="$ACTUAL_PACKAGE@v"
else
if [[ ! $VERSION =~ ^v ]]; then
VERSION="v$VERSION"
fi
if [[ $VERSION =~ ^v[0-9]+(\.[0-9]+)*$ ]]; then
FULL_VERSION=$(find "$PACKAGE_DIR" -maxdepth 1 -type d -name "_${VERSION}.*" 2>/dev/null | sed 's/.*_v//' | sort -V | tail -1)
if [ -n "$FULL_VERSION" ]; then
VERSION="v$FULL_VERSION"
fi
fi
if [ ! -d "$PACKAGE_DIR/_${VERSION}" ]; then
echo "Error: Version not found. We only have: $AVAILABLE_VERSIONS"
exit 1
fi
GREP_PATTERN="$ACTUAL_PACKAGE@$VERSION"
# Check for test files in the version directory
SEARCH_PATTERN="*.test.js"
if [ -n "$TEST_NAME_FILTER" ]; then
SEARCH_PATTERN="*${TEST_NAME_FILTER}*.test.js"
fi
FOUND_FILES=$(find "$PACKAGE_DIR/_${VERSION}" -maxdepth 2 -name "$SEARCH_PATTERN" ! -name "test_base.js")
if [ -n "$FOUND_FILES" ]; then
TEST_FILES=$(echo "$FOUND_FILES" | tr '\n' ' ')
fi
fi
else
HIGHEST_VERSION=$(find "$PACKAGE_DIR" -maxdepth 1 -type d -name "_v*" 2>/dev/null | sed 's/.*_v//' | sort -V | tail -1)
if [ -n "$HIGHEST_VERSION" ]; then
GREP_PATTERN="$ACTUAL_PACKAGE@v$HIGHEST_VERSION"
# Check for test files in the highest version directory
SEARCH_PATTERN="*.test.js"
if [ -n "$TEST_NAME_FILTER" ]; then
SEARCH_PATTERN="*${TEST_NAME_FILTER}*.test.js"
fi
FOUND_FILES=$(find "$PACKAGE_DIR/_v${HIGHEST_VERSION}" -maxdepth 2 -name "$SEARCH_PATTERN" ! -name "test_base.js")
if [ -n "$FOUND_FILES" ]; then
TEST_FILES=$(echo "$FOUND_FILES" | tr '\n' ' ')
fi
else
GREP_PATTERN="$ACTUAL_PACKAGE@v"
# Fallback for non-versioned packages (supports both *.test.js and *_test.js conventions)
if [ -f "$PACKAGE_DIR/default.test.js" ]; then
TEST_FILES="$PACKAGE_DIR/default.test.js"
elif [ -f "$PACKAGE_DIR/test.js" ]; then
TEST_FILES="$PACKAGE_DIR/test.js"
else
TEST_FILES=$(find "$PACKAGE_DIR" \( -name "*.test.js" -o -name "*_test.js" \) ! -path "*/node_modules/*" ! -name "test_base.js" | tr '\n' ' ')
fi
fi
fi
if [ -n "$TEST_FILES" ]; then
# Make paths relative to the package directory for the npm script
RELATIVE_TEST_FILES=$(echo "$TEST_FILES" | sed "s|packages/${SCOPE_PKG_NAME}/||g")
# Use test:debug:files to run only the specific files
npm_command="npm run test:debug:files -- $RELATIVE_TEST_FILES"
# We still pass grep pattern just in case, though mocha might not need it if we pass specific files
# But usually filtering by file is enough.
# args="-- --grep \"$GREP_PATTERN\" $args"
echo "Running tests for $ACTUAL_PACKAGE${VERSION:+ version $VERSION}"
echo "Target files: $RELATIVE_TEST_FILES"
else
# If we couldn't determine specific files, fall back to old behavior but warn
args="-- --grep \"$GREP_PATTERN\" $args"
echo "Running tests for $ACTUAL_PACKAGE${VERSION:+ version $VERSION} (grep pattern: $GREP_PATTERN)"
echo "Warning: Could not identify specific test files, running full suite with filter."
fi
else
echo "Running all tests with $SCOPE $args"
fi
npx lerna exec --scope="$SCOPE" "$npm_command $args"