forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_affected_tests
More file actions
165 lines (133 loc) · 4.28 KB
/
run_affected_tests
File metadata and controls
165 lines (133 loc) · 4.28 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
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2023 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 affected tests for a given list of changed files.
#
# Usage: run_affected_tests file1 [file2 file3 ...]
#
# Arguments:
#
# file1 File name.
# file2 File name.
# file3 File name.
#
#
# Environment variables:
#
# LOG_FILE Log file.
#
# shellcheck disable=SC2181,SC2153,SC2129
# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails:
set -o pipefail
# VARIABLES #
# Get the list of packages to deprecate:
changed="$*"
# Get the path to a log file as the third argument to the build script:
log_file="${LOG_FILE}"
# Define a heartbeat interval to periodically print messages in order to prevent CI from prematurely ending a build due to long running commands:
heartbeat_interval='30s'
# Declare a variable for storing the heartbeat process id:
heartbeat_pid=""
# FUNCTIONS #
# Error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
stop_heartbeat
}
# Starts a heartbeat.
#
# $1 - heartbeat interval
start_heartbeat() {
echo 'Starting heartbeat...' >&2
# Create a heartbeat and send to background:
heartbeat "$1" &
# Capture the heartbeat pid:
heartbeat_pid=$!
echo "Heartbeat pid: ${heartbeat_pid}" >&2
}
# Runs an infinite print loop.
#
# $1 - heartbeat interval
heartbeat() {
while true; do
echo "$(date) - heartbeat..." >&2;
sleep "$1";
done
}
# Stops the heartbeat print loop.
stop_heartbeat() {
echo 'Stopping heartbeat...' >&2
kill "${heartbeat_pid}"
}
# Prints a success message.
print_success() {
echo 'Success!' >&2
}
# Main execution sequence.
main() {
start_heartbeat "${heartbeat_interval}"
# Only keep files which reside in package directories:
changed=$(echo "${changed}" | tr ' ' '\n' | grep '^lib/node_modules/@stdlib')
# Find unique package directories:
directories=$(echo "${changed}" | tr ' ' '\n' | sed -E 's/\/(bin|data|etc|include|lib|src|test)\/?$//' | uniq)
if [ -z "${directories}" ]; then
echo 'No packages to test.' >&2
cleanup
print_success
exit 0
fi
# Extract package names from changed package directories (e.g., @stdlib/math/base/special/sin) by removing the leading 'lib/node_modules/':
packages=$(echo "${directories}" | sed -E 's/^lib\/node_modules\///')
# Find all package directories which `require()` one of the changed packages:
required_by=""
for package in ${packages}; do
echo "Finding packages which depend on '${package}'..."
escaped_package=$(echo "$package" | sed 's/[\/&]/\\&/g')
dependents=$(find lib/node_modules/@stdlib -type f -name '*.js' -print0 | xargs -0 -L 200 grep -ol -E "require\( [']${escaped_package}['] \)")
echo "Found: ${dependents}"
if [ -n "${dependents}" ]; then
dependents=$(dirname $dependents | sed -E 's/\/(bin|data|etc|include|lib|src|test)\/?$//' | sort -u)
echo "List of dependents: ${dependents}"
required_by="${required_by} ${dependents}"
fi
done
# Concatenate the list of changed package directories and package directories which `require()` one of the changed packages:
if [ -n "${required_by}" ]; then
directories="${directories} ${required_by}"
fi
# Find all test files in package directories:
files=$(find ${directories} -maxdepth 2 -wholename '**/test/test*.js' | grep -v '/fixtures/' | sort -u | tr '\n' ' ')
# Exclude files residing in test fixtures directories:
files=$(echo "${files}" | grep -v '/fixtures/')
if [[ -n "${files}" ]]; then
make test-javascript-files-min FILES="${files}"
fi
cleanup
print_success
exit 0
}
# Set an error handler to print captured output and perform any clean-up tasks:
trap 'on_error' ERR
# Run main:
main