Skip to content

Commit 6be75e8

Browse files
committed
build: ensure only one package's coverage is evaluated at a time
We were encountering a bug due to the HTML code coverage reports flattening their directory structure when all dependencies of the tested package reside in the same namespace as the package itself. While we could preserve the full project package structure with the --all option, setting it would be too expensive. Hence, we instead only generate the test coverage for the currently tested package by setting the appropriate c8 flags (for which the make command recipe had to be updated to accept custom flags) such that we can sidestep the need to resolve the correct folder to upload to the code coverage repository, Our previous approach of finding the folder for the tested package was not satisfactory as it didn't take the flattening behavior into account and the index.html file existence check was thus not always yielding the corect result. --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: passed - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 4ed750c commit 6be75e8

File tree

2 files changed

+20
-43
lines changed

2 files changed

+20
-43
lines changed

.github/workflows/scripts/run_tests_coverage

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,8 @@
2626
# file2 File name.
2727
# file3 File name.
2828
#
29-
#
30-
# Environment variables:
31-
#
32-
# GITHUB_REPO GitHub repository.
33-
# GITHUB_REF GitHub branch or tag.
34-
# LOG_FILE Log file.
35-
#
3629

37-
# shellcheck disable=SC2181,SC2153,SC2129
30+
# shellcheck disable=SC2181,SC2153,SC2129,SC2207,SC2317
3831

3932
# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails:
4033
set -o pipefail
@@ -48,15 +41,6 @@ coverage_base_url="${COVERAGE_BASE_URL:-https://coverage.stdlib.io}"
4841
# Get the list of changed files:
4942
changed="$*"
5043

51-
# Get the GitHub repository:
52-
github_repo="${GITHUB_REPO}"
53-
54-
# Get the GitHub branch or tag:
55-
github_ref="${GITHUB_REF}"
56-
57-
# Get the path to a log file as the third argument to the build script:
58-
log_file="${LOG_FILE}"
59-
6044
# Define a heartbeat interval to periodically print messages in order to prevent CI from prematurely ending a build due to long running commands:
6145
heartbeat_interval='30s'
6246

@@ -125,18 +109,18 @@ compare_cov() {
125109

126110
if [ "$old_cov_value" == 0 ]; then
127111
new_cov_percentage=$(awk "BEGIN {printf \"%.2f\", $new_cov_value*100}")
128-
echo "\$\\\\\\\\color{green}+$new_cov_percentage\\\\\\\\\\\\\\\\%\$"
112+
printf "\$\\\\\\\\color{green}+%s\\\\\\\\\\\\\\\\%%\$\n" "$new_cov_percentage"
129113
else
130114
percentage_change=$(awk "BEGIN {printf \"%.2f\", (($new_cov_value - $old_cov_value) / $old_cov_value) * 100}")
131115
color="green"
132116
sign=""
133-
if [ $(awk "BEGIN {if ($percentage_change >= 0) print 1; else print 0}") -eq 1 ]; then
117+
if [ "$(awk "BEGIN {if ($percentage_change >= 0) print 1; else print 0}")" -eq 1 ]; then
134118
sign="+"
135-
elif [ $(awk "BEGIN {if ($percentage_change < 0) print 1; else print 0}") -eq 1 ]; then
119+
elif [ "$(awk "BEGIN {if ($percentage_change < 0) print 1; else print 0}")" -eq 1 ]; then
136120
sign="-"
137121
color="red"
138122
fi
139-
echo "\$\\\\\\\\color{$color}$sign$percentage_change\\\\\\\\\\\\\\\\%\$"
123+
printf "\$\\\\\\\\color{%s}%s%s\\\\\\\\\\\\\\\\%%\$\n" "$color" "$sign" "$percentage_change"
140124
fi
141125
}
142126

@@ -163,23 +147,15 @@ main() {
163147
coverage=''
164148
for package in ${directories}; do
165149
# For each package, extract coverage values from the respective coverage report:
166-
pkg=`echo $package | sed -E 's/^.*stdlib\///'`
150+
pkg=$(echo "$package" | sed -E 's/^.*stdlib\///')
167151

168152
if [ -f "lib/node_modules/@stdlib/${pkg}/binding.gyp" ]; then
169153
NODE_ADDONS_PATTERN="${pkg}" make install-node-addons
170154
fi
171155

172-
make test-javascript-cov TESTS_FILTER=".*/${pkg}/test/.*"
156+
make test-javascript-cov TESTS_FILTER=".*/${pkg}/test/.*" C8_FLAGS="-n 'lib/node_modules/@stdlib/${pkg}/**'"
173157

174-
if [ ! -f reports/coverage/lcov-report/${pkg}/lib/index.html ]; then
175-
# Reports for packages with no dependencies are stored in the `lcov-report` directory
176-
coverage_path="reports/coverage/lcov-report/index.html"
177-
top_level_report=true
178-
else
179-
# Reports for packages with dependencies are stored in `lcov-report/<pkg>/lib`:
180-
coverage_path="reports/coverage/lcov-report/${pkg}/lib/index.html"
181-
top_level_report=false
182-
fi
158+
coverage_path="reports/coverage/lcov-report/index.html"
183159
pkg_cov_values=($(cat $coverage_path | grep "fraction" | grep -oP '\d+/\d+' | awk -F'/' '{if ($2 != 0) print $1/$2; else print 1}'))
184160
pkg_statements_cov=${pkg_cov_values[0]}
185161
pkg_branches_cov=${pkg_cov_values[1]}
@@ -214,26 +190,22 @@ main() {
214190
pkg_cov="| $pkg_statements_cov_fraction <br> $cov_change_statements | $pkg_branches_cov_fraction <br> $cov_change_branches | $pkg_functions_cov_fraction <br> $cov_change_functions | $pkg_lines_cov_fraction <br> $cov_change_lines |"
215191

216192
pkg_url="${coverage_base_url}/${pkg}/index.html"
217-
pkg_link="<a href=\"$pkg_url\">$pkg</a>"
193+
pkg_link="<a href=\"${pkg_url}\">${pkg}</a>"
218194
coverage="$coverage\n| $pkg_link $pkg_cov"
219195

220196
# Copy coverage report of the package to artifacts directory:
221-
if [ "$top_level_report" = true ]; then
222-
mkdir -p "artifacts/${pkg}" && cp -r "reports/coverage/lcov-report"/* "artifacts/${pkg}/"
223-
else
224-
mkdir -p "artifacts/${pkg}/lib/" && cp -r "reports/coverage/lcov-report/${pkg}/lib"/* "artifacts/${pkg}/"
225-
fi
197+
mkdir -p "artifacts/${pkg}" && cp -r "reports/coverage/lcov-report"/* "artifacts/${pkg}/"
226198

227199
# Cleanup coverage reports for next package:
228200
rm -rf reports/coverage/lcov-report/*
229201
done
230202

231203
# Format coverage as Markdown table row:
232-
table_body=`echo $coverage | sed -e 's/,/|/g; s/"/ /g; s/\[/|/g; s/\]/|/g'`
204+
table_body=$(echo "$coverage" | sed -e 's/,/|/g; s/"/ /g; s/\[/|/g; s/\]/|/g')
233205
table_header="| Package | Statements | Branches | Functions | Lines |\n| --------- | ------------ | ---------- | ----------- | ----- |"
234206
table="${table_header}${table_body}"
235207

236-
echo "table=$table" >> $GITHUB_OUTPUT
208+
echo "table=$table" >> "$GITHUB_OUTPUT"
237209

238210
cleanup
239211
print_success

tools/make/lib/test-cov/c8.mk

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ C8_EXCLUDES_FLAGS = \
4949
-x "**/$(CONFIG_FOLDER)/**" \
5050
-x "**/$(DOCUMENTATION_FOLDER)/**"
5151

52+
# Define user-supplied command-line options:
53+
C8_FLAGS ?=
54+
5255
# Define command-line options when generating coverage data:
53-
C8_FLAGS = \
56+
c8_flags = \
5457
$(C8_EXCLUDES_FLAGS) \
5558
--clean=false \
5659
--temp-directory $(COVERAGE_DIR)/tmp \
5760
--report-dir $(COVERAGE_DIR) \
5861
--reporter lcov
5962

63+
# Append user-supplied command-line options:
64+
c8_flags += $(C8_FLAGS)
6065

6166
# RULES #
6267

@@ -87,7 +92,7 @@ ifeq ($(FAIL_FAST), true)
8792
NODE_ENV="$(NODE_ENV_TEST)" \
8893
NODE_PATH="$(NODE_PATH_TEST)" \
8994
TEST_MODE=coverage \
90-
$(C8) $(C8_FLAGS) $(NODE) $$test | $(TAP_REPORTER) || exit 1; \
95+
$(C8) $(c8_flags) $(NODE) $$test | $(TAP_REPORTER) || exit 1; \
9196
done
9297
else
9398
$(QUIET) $(FIND_TESTS_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r test; do \
@@ -96,7 +101,7 @@ else
96101
NODE_ENV="$(NODE_ENV_TEST)" \
97102
NODE_PATH="$(NODE_PATH_TEST)" \
98103
TEST_MODE=coverage \
99-
$(C8) $(C8_FLAGS) $(NODE) $$test | $(TAP_REPORTER) || echo 'Tests failed.'; \
104+
$(C8) $(c8_flags) $(NODE) $$test | $(TAP_REPORTER) || echo 'Tests failed.'; \
100105
done
101106
endif
102107

0 commit comments

Comments
 (0)