Skip to content

Commit e2fe4cf

Browse files
committed
build: add commands to calculate coverage for set of files
1 parent 7e722d5 commit e2fe4cf

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

tools/make/lib/test-cov/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,16 @@ view-cov: view-javascript-cov
6262
clean-cov: clean-javascript-cov
6363

6464
.PHONY: clean-cov
65+
66+
#/
67+
# Runs a specified list of files containing unit tests and generates a test coverage report.
68+
#
69+
# @param {string} FILES - list of test file paths
70+
# @param {*} [FAST_FAIL] - flag indicating whether to stop running tests upon encountering a test failure
71+
#
72+
# @example
73+
# make test-cov-files FILES='/foo/test.js /bar/test.js'
74+
#/
75+
test-cov-files: test-javascript-cov-files
76+
77+
.PHONY: test-cov-files

tools/make/lib/test-cov/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ The command supports the following environment variables:
6565
$ make test-cov TESTS_FILTER='.*/blas/base/dasum/.*'
6666
```
6767

68+
#### test-cov-files
69+
70+
Runs a specified list of files containing unit tests and generates a test coverage report.
71+
72+
<!-- run-disable -->
73+
74+
```bash
75+
$ make test-cov-files FILES='/foo/test.js /bar/test.js'
76+
```
77+
78+
The command supports the following environment variables:
79+
80+
- **FAST_FAIL**: flag indicating whether to stop running tests upon encountering a test failure.
81+
- **FILES**: list of test file paths.
82+
6883
#### view-cov
6984

7085
Opens an HTML test coverage report in a local web browser.
@@ -110,6 +125,21 @@ The command supports the following environment variables:
110125
$ make test-javascript-cov TESTS_FILTER='.*/blas/base/dasum/.*'
111126
```
112127

128+
#### test-javascript-cov-files
129+
130+
Runs a specified list of files containing JavaScript unit tests and generates a test coverage report.
131+
132+
<!-- run-disable -->
133+
134+
```bash
135+
$ make test-javascript-cov-files FILES='/foo/test.js /bar/test.js'
136+
```
137+
138+
The command supports the following environment variables:
139+
140+
- **FAST_FAIL**: flag indicating whether to stop running tests upon encountering a test failure.
141+
- **FILES**: list of test file paths.
142+
113143
#### view-javascript-cov
114144

115145
Opens an HTML test coverage report in a local web browser.

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,40 @@ view-c8-report:
114114
$(QUIET) $(OPEN) $(C8_HTML_REPORT)
115115

116116
.PHONY: view-c8-report
117+
118+
#/
119+
# Runs specified unit tests and generates a test coverage report.
120+
#
121+
# ## Notes
122+
#
123+
# - Raw TAP output is piped to a TAP reporter.
124+
# - This command is useful when wanting to specify JavaScript test files.
125+
#
126+
#
127+
# @private
128+
# @param {string} [FILES] - space separated list of test files
129+
# @param {*} [FAST_FAIL] - flag indicating whether to stop running tests upon encountering a test failure
130+
#
131+
# @example
132+
# make test-c8-files FILES="test/one.js test/two.js"
133+
#/
134+
test-c8-files: $(NODE_MODULES)
135+
ifeq ($(FAIL_FAST), true)
136+
$(foreach file, $(FILES), \
137+
echo "Running test: $(file)"; \
138+
NODE_ENV="$(NODE_ENV_TEST)" \
139+
NODE_PATH="$(NODE_PATH_TEST)" \
140+
TEST_MODE=coverage \
141+
$(C8) $(C8_FLAGS) $(NODE) $(file) | $(TAP_REPORTER) || exit 1; \
142+
)
143+
else
144+
$(foreach file, $(FILES), \
145+
echo "Running test: $(file)"; \
146+
NODE_ENV="$(NODE_ENV_TEST)" \
147+
NODE_PATH="$(NODE_PATH_TEST)" \
148+
TEST_MODE=coverage \
149+
$(C8) $(C8_FLAGS) $(NODE) $(file) | $(TAP_REPORTER) || echo 'Tests failed.'; \
150+
)
151+
endif
152+
153+
.PHONY: test-c8-files

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,35 @@ clean-istanbul-instrument:
262262
$(QUIET) $(DELETE) $(DELETE_FLAGS) $(COVERAGE_INSTRUMENTATION_DIR)
263263

264264
.PHONY: clean-istanbul-instrument
265+
266+
#/
267+
# Runs unit tests and generates a test coverage report for a list of JavaScript test files.
268+
#
269+
# @param {string} [FILES] - space-separated list of JavaScript test files
270+
# @param {*} [FAST_FAIL] - flag indicating whether to stop running tests upon encountering a test failure
271+
#
272+
# @private
273+
#
274+
# @example
275+
# make test-istanbul-files FILES='./lib/foo.js ./lib/bar.js'
276+
#/
277+
test-istanbul-files: $(NODE_MODULES) test-istanbul-instrument
278+
$(QUIET) $(MKDIR_RECURSIVE) $(COVERAGE_DIR)
279+
$(QUIET) $(MAKE_EXECUTABLE) $(COVERAGE_REPORT_NAME)
280+
$(QUIET) for file in $(FILES); do \
281+
test_dir=$(ISTANBUL_INSTRUMENT_OUT)/$$(dirname $$file); \
282+
echo ''; \
283+
echo "Running tests for file: $$file"; \
284+
echo ''; \
285+
NODE_ENV="$(NODE_ENV_TEST)" \
286+
NODE_PATH="$(NODE_PATH_TEST)" \
287+
TEST_MODE=coverage \
288+
$(ISTANBUL_TEST_RUNNER) \
289+
$(ISTANBUL_TEST_RUNNER_FLAGS) \
290+
--output $$($(COVERAGE_REPORT_NAME) $(ISTANBUL_INSTRUMENT_OUT) $$test_dir $(COVERAGE_DIR)) \
291+
"$$test_dir/**/$(basename $$file)" \
292+
| $(TAP_REPORTER) || exit 1; \
293+
done
294+
$(QUIET) $(MAKE) -f $(this_file) test-istanbul-report
295+
296+
.PHONY: test-istanbul-files

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,24 @@ clean-javascript-cov:
8585
$(QUIET) $(DELETE) $(DELETE_FLAGS) $(COVERAGE_DIR)
8686

8787
.PHONY: clean-javascript-cov
88+
89+
#/
90+
# Runs a specified list of files containing JavaScript unit tests and generates a test coverage report.
91+
#
92+
# @param {string} FILES - list of JavaScript test file paths
93+
# @param {string} [JAVASCRIPT_CODE_INSTRUMENTER] - JavaScript code instrumenter
94+
# @param {*} [FAST_FAIL] - flag indicating whether to stop running tests upon encountering a test failure
95+
#
96+
# @example
97+
# make test-javascript-cov-files FILES='/foo/test.js /bar/test.js'
98+
#/
99+
test-javascript-cov-files: clean-javascript-cov
100+
ifeq ($(JAVASCRIPT_CODE_INSTRUMENTER), istanbul)
101+
$(QUIET) NODE_ENV_TEST="$(NODE_ENV_TEST)" NODE_PATH_TEST="$(NODE_PATH_TEST)" FILES="$(FILES)" $(MAKE) -f $(this_file) test-istanbul-files
102+
else
103+
ifeq ($(JAVASCRIPT_CODE_INSTRUMENTER), c8)
104+
$(QUIET) NODE_ENV_TEST="$(NODE_ENV_TEST)" NODE_PATH_TEST="$(NODE_PATH_TEST)" FILES="$(FILES)" $(MAKE) -f $(this_file) test-c8-files
105+
endif
106+
endif
107+
108+
.PHONY: test-javascript-cov-files

0 commit comments

Comments
 (0)