File: test_node_versions

package info (click to toggle)
node-stdlib 0.0.96%2Bds1%2B~cs0.0.429-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 421,476 kB
  • sloc: javascript: 1,562,831; ansic: 109,702; lisp: 49,823; cpp: 27,224; python: 7,871; sh: 6,807; makefile: 6,089; fortran: 3,102; awk: 387
file content (266 lines) | stat: -rwxr-xr-x 5,480 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Script to run unit tests against multiple Node.js versions.
#
# Note that this script depends on [nvm][1]. To install `nvm`, see the nvm [documentation][1].
#
# [1]: https://github.com/creationix/nvm


# DEPENDENCIES #

# Source nvm to ensure that the `nvm` command is available:
. "${HOME}/.nvm/nvm.sh"


# VARIABLES #

# Initialize the test file list:
tests=''

# Define the Node.js versions to test:
versions=(0.10 0.12 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 node)

# Cache the current Node.js version:
current_version=$(nvm current)


# FUNCTIONS #

# Defines an error handler.
#
# $1 - error status
on_error() {
	echo 'ERROR: An error was encountered during execution.' >&2
	cleanup
	exit "$1"
}

# Runs clean-up tasks.
cleanup() {
	init "${current_version}"
}

# Prints usage information.
usage() {
	echo '' >&2
	echo 'Usage: test_node_versions [options] file1 file2 ...' >&2
	echo '' >&2
	echo 'Options:' >&2
	echo '' >&2
	echo '  -h,    --help                Print this message.' >&2
	echo '         --versions versions   Versions to test; e.g., `4,5,6`.' >&2
	echo '' >&2
}

# Prints a success message.
print_success() {
	echo 'Success!' >&2
}

# Installs a Node.js version.
#
# $1 - version
install_version() {
	echo "Installing Node.js version: $1..." >&2
	nvm install "$1"
	if [[ "$?" -ne 0 ]]; then
		echo "Unable to install Node.js version: $1." >&2
		return 1
	fi
	# Update the npm client. Older clients cannot, e.g., handle scoped modules.
	npm update -g npm

	echo "Successfully installed Node.js version: $1." >&2
	return 0
}

# Sets the Node.js version.
#
# $1 - version
set_version() {
	echo "Switching to Node.js version: $1..." >&2
	nvm use "$1"
	if [[ "$?" -ne 0 ]]; then
		echo "Unable to set Node.js version to $1." >&2
		return 1
	fi
	echo "Node.js version set to $1." >&2
	return 0
}

# Removes a Node.js version.
#
# $1 - version
uninstall_version() {
	echo "Removing Node.js version: $1..." >&2
	nvm uninstall "$1"
	if [[ "$?" -ne 0 ]]; then
		echo "Unable to remove Node.js version: $1." >&2
		return 1
	fi
	echo "Successfully removed Node.js version: $1." >&2
	return 0
}

# Remove node modules.
clean_node() {
	echo 'Removing node module dependencies...' >&2
	make clean-node
	if [[ "$?" -ne 0 ]]; then
		echo 'Error when attempting to remove dependencies.' >&2
		return 1
	fi
	echo 'Dependencies successfully removed.' >&2
	return 0
}

# Installs dependencies.
install() {
	echo 'Installing...' >&2
	make install-node
	if [[ "$?" -ne 0 ]]; then
		echo 'Error occurred during install.' >&2
		return 1
	fi
	echo 'Install successful.' >&2
	return 0
}

# Initializes a Node.js environment.
#
# $1 - Node.js version
init() {
	set_version "$1"
	if [[ "$?" -ne 0 ]]; then
		return 1
	fi
	clean_node
	if [[ "$?" -ne 0 ]]; then
		return 1
	fi
	install
	if [[ "$?" -ne 0 ]]; then
		return 1
	fi
	return 0
}

# Runs unit tests.
#
# $1 - list of Node.js versions to test
run_tests() {
	local version
	local flg

	for v in $(echo "$1"); do
		# Reset the cleanup flag:
		flg='0'

		# Resolve a version to a locally installed version:
		version=$(nvm version "${v}")
		if [[ "${version}" = "N/A" ]]; then
			echo "Unable to resolve \`${v}\` to a locally installed version." >&2
			flg='1'
			install_version "${v}"
			if [[ "$?" -ne 0 ]]; then
				on_error 1
			fi
		else
			echo "Resolved \`${v}\` to locally installed version \`${version}\`." >&2
		fi
		init "${v}"
		if [[ "$?" -ne 0 ]]; then
			on_error 1
		fi
		make TESTS="'""${tests}""'" test
		# Note: we don't explicitly handle return values here in order to let tests complete across all versions.

		# If the version was not already installed, reset the current version and remove the recently installed version...
		if [[ "${flg}" = "1" ]]; then
			set_version "${current_version}"
			uninstall_version "${v}"
			if [[ "$?" -ne 0 ]]; then
				on_error 1
			fi
		fi
	done
}

# Main execution sequence.
main() {
	run_tests "${versions[*]}"
	print_success
	cleanup
	exit 0
}

# Parse command-line options...
while :; do
	case "$1" in
		'-h' | '--help')
			usage
			exit 0
			;;

		'--versions')
			if [[ -n "$2" ]]; then
				versions="$2"
				shift
			else
				printf 'ERROR: "--versions" option requires a non-empty option argument.\n' >&2
				on_error 1
			fi
			;;

		'--versions='?*)
			# Delete everything up to "=" and assign the remainder:
			versions="${1#*=}"
			;;

		'--versions=')
			# Handle empty `--versions=` option:
			printf 'ERROR: "--versions" option requires a non-empty option argument.\n' >&2
				on_error 1
			;;

		'--')
			# End of all options:
			shift
			break
			;;

		-?*)
			printf 'WARNING: unknown option (ignored): %s\n' "$1" >&2
			break
			;;

		*)
			# Default case (e.g., if no more options) break out of loop:
			break
	esac

	shift
done

# Test file paths are the rest of the positional parameters:
tests="$@"

# Run main:
main