Skip to content

Commit 2b664a2

Browse files
committed
Add recipes for using c8 for code coverage
1 parent c14821c commit 2b664a2

File tree

1 file changed

+117
-0
lines changed
  • tools/make/lib/test-cov

1 file changed

+117
-0
lines changed

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

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2022 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
# Define the path to the C8 executable.
22+
#
23+
# ## Notes
24+
#
25+
# - To install c8:
26+
#
27+
# ```bash
28+
# $ npm install c8
29+
# ```
30+
#
31+
# [1]: https://github.com/bcoe/c8
32+
C8 ?= $(BIN_DIR)/c8
33+
34+
# Define the output file path for the HTML report generated by c8:
35+
C8_HTML_REPORT ?= $(COVERAGE_DIR)/lcov-report/index.html
36+
37+
# Define which files and directories to exclude from coverage instrumentation:
38+
C8_EXCLUDES_FLAGS = \
39+
--exclude-node-modules=false \
40+
-x 'node_modules/**' \
41+
-x 'reports/**' \
42+
-x 'tmp/**' \
43+
-x 'deps/**' \
44+
-x 'dist/**' \
45+
-x "**/$(SRC_FOLDER)/**" \
46+
-x "**/$(TESTS_FOLDER)/**" \
47+
-x "**/$(EXAMPLES_FOLDER)/**" \
48+
-x "**/$(BENCHMARKS_FOLDER)/**" \
49+
-x "**/$(CONFIG_FOLDER)/**" \
50+
-x "**/$(DOCUMENTATION_FOLDER)/**"
51+
52+
# Define command-line options when generating coverage data:
53+
C8_FLAGS = \
54+
$(C8_EXCLUDES_FLAGS) \
55+
--clean=false \
56+
--temp-directory $(COVERAGE_DIR)/tmp \
57+
--report-dir $(COVERAGE_DIR) \
58+
--reporter lcov
59+
60+
61+
# RULES #
62+
63+
#/
64+
# Runs unit tests and generates a test coverage report.
65+
#
66+
# ## Notes
67+
#
68+
# - Raw TAP output is piped to a TAP reporter.
69+
# - This command is useful when wanting to glob for JavaScript test files (e.g., generate a test coverage report for all JavaScript tests for a particular package).
70+
#
71+
#
72+
# @private
73+
# @param {string} [TESTS_FILTER] - file path pattern (e.g., `.*/blas/base/dasum/.*`)
74+
# @param {*} [FAST_FAIL] - flag indicating whether to stop running tests upon encountering a test failure
75+
#
76+
#
77+
# @example
78+
# make test-c8
79+
#
80+
# @example
81+
# make test-c8 TESTS_FILTER=".*/blas/base/dasum/.*"
82+
#/
83+
test-c8: $(NODE_MODULES)
84+
ifeq ($(FAIL_FAST), true)
85+
$(QUIET) $(FIND_TESTS_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r test; do \
86+
echo ''; \
87+
echo "Running test: $$test"; \
88+
NODE_ENV="$(NODE_ENV_TEST)" \
89+
NODE_PATH="$(NODE_PATH_TEST)" \
90+
TEST_MODE=coverage \
91+
$(C8) $(C8_FLAGS) $(NODE) $$test | $(TAP_REPORTER) || exit 1; \
92+
done
93+
else
94+
$(QUIET) $(FIND_TESTS_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r test; do \
95+
echo ''; \
96+
echo "Running test: $$test"; \
97+
NODE_ENV="$(NODE_ENV_TEST)" \
98+
NODE_PATH="$(NODE_PATH_TEST)" \
99+
TEST_MODE=coverage \
100+
$(C8) $(C8_FLAGS) $(NODE) $$test | $(TAP_REPORTER) || echo 'Tests failed.';; \
101+
done
102+
endif
103+
104+
.PHONY: test-c8
105+
106+
#/
107+
# Opens an HTML test coverage report in a local web browser.
108+
#
109+
# @private
110+
#
111+
# @example
112+
# make view-c8-report
113+
#/
114+
view-c8-report:
115+
$(QUIET) $(OPEN) $(C8_HTML_REPORT)
116+
117+
.PHONY: view-c8-report

0 commit comments

Comments
 (0)