Skip to content

Commit 699a1fa

Browse files
committed
Add script to run unit tests against specific Node.js versions
1 parent fd206cf commit 699a1fa

1 file changed

Lines changed: 246 additions & 0 deletions

File tree

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Script to run unit tests against multiple Node.js versions.
4+
#
5+
# Note that this script depends on [nvm][1]. To install `nvm`, see the nvm [documentation][1].
6+
#
7+
# [1]: https://github.com/creationix/nvm
8+
9+
10+
# DEPENDENCIES #
11+
12+
# Source nvm to ensure that the `nvm` command is available:
13+
. "${HOME}/.nvm/nvm.sh"
14+
15+
16+
# VARIABLES #
17+
18+
# Initialize the test file list:
19+
tests=''
20+
21+
# Define the Node.js versions to test:
22+
versions=(0.10 0.12 1 2 3 4 5 6 node)
23+
24+
# Cache the current Node.js version:
25+
current_version=$(nvm current)
26+
27+
28+
# FUNCTIONS #
29+
30+
# Defines an error handler.
31+
#
32+
# $1 - error status
33+
on_error() {
34+
echo 'ERROR: An error was encountered during execution.' >&2
35+
cleanup
36+
exit "$1"
37+
}
38+
39+
# Runs clean-up tasks.
40+
cleanup() {
41+
init "${current_version}"
42+
}
43+
44+
# Prints usage information.
45+
usage() {
46+
echo '' >&2
47+
echo 'Usage: test_node_versions [options] file1 file2 ...' >&2
48+
echo '' >&2
49+
echo 'Options:' >&2
50+
echo '' >&2
51+
echo ' -h, --help Print this message.' >&2
52+
echo '' >&2
53+
}
54+
55+
# Prints a success message.
56+
print_success() {
57+
echo 'Success!' >&2
58+
}
59+
60+
# Installs a Node.js version.
61+
#
62+
# $1 - version
63+
install_version() {
64+
echo "Installing Node.js version: $1..." >&2
65+
nvm install "$1"
66+
if [[ "$?" -ne 0 ]]; then
67+
echo "Unable to install Node.js version: $1." >&2
68+
return 1
69+
fi
70+
echo "Successfully installed Node.js version: $1." >&2
71+
return 0
72+
}
73+
74+
# Sets the Node.js version.
75+
#
76+
# $1 - version
77+
set_version() {
78+
echo "Switching to Node.js version: $1..." >&2
79+
nvm use "$1"
80+
if [[ "$?" -ne 0 ]]; then
81+
echo "Unable to set Node.js version to $1." >&2
82+
return 1
83+
fi
84+
echo "Node.js version set to $1." >&2
85+
return 0
86+
}
87+
88+
# Removes a Node.js version.
89+
#
90+
# $1 - version
91+
uninstall_version() {
92+
echo "Removing Node.js version: $1..." >&2
93+
nvm uninstall "$1"
94+
if [[ "$?" -ne 0 ]]; then
95+
echo "Unable to remove Node.js version: $1." >&2
96+
return 1
97+
fi
98+
echo "Successfully removed Node.js version: $1." >&2
99+
return 0
100+
}
101+
102+
# Remove node modules.
103+
clean_node() {
104+
echo 'Removing node module dependencies...' >&2
105+
make clean-node
106+
if [[ "$?" -ne 0 ]]; then
107+
echo 'Error when attempting to remove dependencies.' >&2
108+
return 1
109+
fi
110+
echo 'Dependencies successfully removed.' >&2
111+
return 0
112+
}
113+
114+
# Installs dependencies.
115+
install() {
116+
echo 'Installing...' >&2
117+
make install-node
118+
if [[ "$?" -ne 0 ]]; then
119+
echo 'Error occurred during install.' >&2
120+
return 1
121+
fi
122+
echo 'Install successful.' >&2
123+
return 0
124+
}
125+
126+
# Initializes a Node.js environment.
127+
#
128+
# $1 - Node.js version
129+
init() {
130+
set_version "$1"
131+
if [[ "$?" -ne 0 ]]; then
132+
return 1
133+
fi
134+
clean_node
135+
if [[ "$?" -ne 0 ]]; then
136+
return 1
137+
fi
138+
install
139+
if [[ "$?" -ne 0 ]]; then
140+
return 1
141+
fi
142+
return 0
143+
}
144+
145+
# Runs unit tests.
146+
#
147+
# $1 - list of Node.js versions to test
148+
run_tests() {
149+
local version
150+
local flg
151+
152+
for v in $(echo "$1"); do
153+
# Reset the cleanup flag:
154+
flg='0'
155+
156+
# Resolve a version to a locally installed version:
157+
version=$(nvm version "${v}")
158+
if [[ "${version}" = "N/A" ]]; then
159+
echo "Unable to resolve \`${v}\` to a locally installed version." >&2
160+
flg='1'
161+
install_version "${v}"
162+
if [[ "$?" -ne 0 ]]; then
163+
on_error 1
164+
fi
165+
else
166+
echo "Resolved \`${v}\` to locally installed version \`${version}\`." >&2
167+
fi
168+
init "${v}"
169+
if [[ "$?" -ne 0 ]]; then
170+
on_error 1
171+
fi
172+
make TESTS="'""${tests}""'" test
173+
# Note: we don't explicitly handle return values here in order to let tests complete across all versions.
174+
175+
# If the version was not already installed, reset the current version and remove the recently installed version...
176+
if [[ "${flg}" = "1" ]]; then
177+
set_version "${current_version}"
178+
uninstall_version "${v}"
179+
if [[ "$?" -ne 0 ]]; then
180+
on_error 1
181+
fi
182+
fi
183+
done
184+
}
185+
186+
# Main execution sequence.
187+
main() {
188+
run_tests "${versions[*]}"
189+
print_success
190+
cleanup
191+
exit 0
192+
}
193+
194+
# Parse command-line options...
195+
while :; do
196+
case "$1" in
197+
'-h' | '--help')
198+
usage
199+
exit 0
200+
;;
201+
202+
'--versions')
203+
if [[ -n "$2" ]]; then
204+
versions="$2"
205+
shift
206+
else
207+
printf 'ERROR: "--versions" option requires a non-empty option argument.\n' >&2
208+
on_error 1
209+
fi
210+
;;
211+
212+
'--versions='?*)
213+
# Delete everything up to "=" and assign the remainder:
214+
versions="${1#*=}"
215+
;;
216+
217+
'--versions=')
218+
# Handle empty `--versions=` option:
219+
printf 'ERROR: "--versions" option requires a non-empty option argument.\n' >&2
220+
on_error 1
221+
;;
222+
223+
'--')
224+
# End of all options:
225+
shift
226+
break
227+
;;
228+
229+
-?*)
230+
printf 'WARNING: unknown option (ignored): %s\n.' "$1" >&2
231+
break
232+
;;
233+
234+
*)
235+
# Default case (e.g., if no more options) break out of loop:
236+
break
237+
esac
238+
239+
shift
240+
done
241+
242+
# Test file paths are the rest of the positional parameters:
243+
tests="$@"
244+
245+
# Run main:
246+
main

0 commit comments

Comments
 (0)