Skip to content

Commit 7490fc8

Browse files
bcoeaddaleax
authored andcommitted
test: switch to native v8 coverage
PR-URL: #25157 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 593714e commit 7490fc8

File tree

7 files changed

+40
-89
lines changed

7 files changed

+40
-89
lines changed

BUILDING.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,15 @@ $ CI_JS_SUITES=child-process CI_NATIVE_SUITES= make coverage
290290
The above command executes tests for the `child-process` subsystem and
291291
outputs the resulting coverage report.
292292

293-
The `make coverage` command downloads some tools to the project root directory
294-
and overwrites the `lib/` directory. To clean up after generating the coverage
295-
reports:
293+
Alternatively, for the JavaScript test suite, you can use the `CI_JS_SUITES`
294+
variable to run tests in isolation, outputting reports:
295+
296+
```text
297+
$ CI_JS_SUITES=fs CI_NATIVE_SUITES= make coverage-run-js
298+
```
299+
300+
The `make coverage` command downloads some tools to the project root directory.
301+
To clean up after generating the coverage reports:
296302

297303
```console
298304
$ make coverage-clean

Makefile

+27-23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ STAGINGSERVER ?= node-www
1111
LOGLEVEL ?= silent
1212
OSTYPE := $(shell uname -s | tr '[A-Z]' '[a-z]')
1313
COVTESTS ?= test-cov
14+
COV_SKIP_TESTS ?= core_line_numbers.js,testFinalizer.js,test_function/test.js
1415
GTEST_FILTER ?= "*"
1516
GNUMAKEFLAGS += --no-print-directory
1617
GCOV ?= gcov
@@ -181,7 +182,6 @@ coverage-clean:
181182
$(RM) -r node_modules
182183
$(RM) -r gcovr build
183184
$(RM) -r out/$(BUILDTYPE)/.coverage
184-
$(RM) -r .cov_tmp
185185
$(RM) out/$(BUILDTYPE)/obj.target/node/gen/*.gcda
186186
$(RM) out/$(BUILDTYPE)/obj.target/node/src/*.gcda
187187
$(RM) out/$(BUILDTYPE)/obj.target/node/src/tracing/*.gcda
@@ -201,55 +201,51 @@ coverage: coverage-test ## Run the tests and generate a coverage report.
201201

202202
.PHONY: coverage-build
203203
coverage-build: all
204-
mkdir -p node_modules
205-
if [ ! -d node_modules/nyc ]; then \
206-
$(NODE) ./deps/npm install nyc@13 --no-save --no-package-lock; fi
204+
-$(MAKE) coverage-build-js
207205
if [ ! -d gcovr ]; then git clone -b 3.4 --depth=1 \
208206
--single-branch https://github.com/gcovr/gcovr.git; fi
209207
if [ ! -d build ]; then git clone --depth=1 \
210208
--single-branch https://github.com/nodejs/build.git; fi
211209
if [ ! -f gcovr/scripts/gcovr.orig ]; then \
212210
(cd gcovr && patch -N -p1 < \
213211
"$(CURDIR)/build/jenkins/scripts/coverage/gcovr-patches-3.4.diff"); fi
214-
if [ -d lib_ ]; then $(RM) -r lib; mv lib_ lib; fi
215-
mv lib lib_
216-
NODE_DEBUG=nyc $(NODE) ./node_modules/.bin/nyc instrument --extension .js \
217-
--extension .mjs --exit-on-error lib_/ lib/
218212
$(MAKE)
219213

214+
.PHONY: coverage-build-js
215+
coverage-build-js:
216+
mkdir -p node_modules
217+
if [ ! -d node_modules/c8 ]; then \
218+
$(NODE) ./deps/npm install c8@next --no-save --no-package-lock;\
219+
fi
220+
220221
.PHONY: coverage-test
221222
coverage-test: coverage-build
222-
$(RM) -r out/$(BUILDTYPE)/.coverage
223-
$(RM) -r .cov_tmp
224223
$(RM) out/$(BUILDTYPE)/obj.target/node/gen/*.gcda
225224
$(RM) out/$(BUILDTYPE)/obj.target/node/src/*.gcda
226225
$(RM) out/$(BUILDTYPE)/obj.target/node/src/tracing/*.gcda
227226
$(RM) out/$(BUILDTYPE)/obj.target/node_lib/gen/*.gcda
228227
$(RM) out/$(BUILDTYPE)/obj.target/node_lib/src/*.gcda
229228
$(RM) out/$(BUILDTYPE)/obj.target/node_lib/src/tracing/*.gcda
230-
-$(MAKE) $(COVTESTS)
231-
mv lib lib__
232-
mv lib_ lib
233-
mkdir -p coverage .cov_tmp
234-
$(NODE) ./node_modules/.bin/nyc merge 'out/Release/.coverage' \
235-
.cov_tmp/libcov.json
236-
(cd lib && .$(NODE) ../node_modules/.bin/nyc report \
237-
--temp-dir "$(CURDIR)/.cov_tmp" \
238-
--report-dir "$(CURDIR)/coverage" \
239-
--reporter html)
229+
-NODE_V8_COVERAGE=out/$(BUILDTYPE)/.coverage $(MAKE) $(COVTESTS)
230+
$(MAKE) coverage-report-js
240231
-(cd out && "../gcovr/scripts/gcovr" --gcov-exclude='.*deps' \
241232
--gcov-exclude='.*usr' -v -r Release/obj.target \
242233
--html --html-detail -o ../coverage/cxxcoverage.html \
243234
--gcov-executable="$(GCOV)")
244-
mv lib lib_
245-
mv lib__ lib
246235
@echo -n "Javascript coverage %: "
247236
@grep -B1 Lines coverage/index.html | head -n1 \
248237
| sed 's/<[^>]*>//g'| sed 's/ //g'
249238
@echo -n "C++ coverage %: "
250239
@grep -A3 Lines coverage/cxxcoverage.html | grep style \
251240
| sed 's/<[^>]*>//g'| sed 's/ //g'
252241

242+
.PHONY: coverage-report-js
243+
coverage-report-js:
244+
$(NODE) ./node_modules/.bin/c8 report --reporter=html \
245+
--temp-directory=out/$(BUILDTYPE)/.coverage --omit-relative=false \
246+
--resolve=./lib --exclude="deps/" --exclude="test/" --exclude="tools/" \
247+
--wrapper-length=0
248+
253249
.PHONY: cctest
254250
# Runs the C++ tests using the built `cctest` executable.
255251
cctest: all
@@ -276,6 +272,14 @@ jstest: build-addons build-js-native-api-tests build-node-api-tests ## Runs addo
276272
$(CI_JS_SUITES) \
277273
$(CI_NATIVE_SUITES)
278274

275+
.PHONY: coverage-run-js
276+
coverage-run-js:
277+
$(RM) -r out/$(BUILDTYPE)/.coverage
278+
$(MAKE) coverage-build-js
279+
-NODE_V8_COVERAGE=out/$(BUILDTYPE)/.coverage CI_SKIP_TESTS=$(COV_SKIP_TESTS) \
280+
$(MAKE) jstest
281+
$(MAKE) coverage-report-js
282+
279283
.PHONY: test
280284
# This does not run tests of third-party libraries inside deps.
281285
test: all ## Runs default tests, linters, and builds docs.
@@ -300,7 +304,7 @@ test-cov: all
300304
$(MAKE) build-js-native-api-tests
301305
$(MAKE) build-node-api-tests
302306
# $(MAKE) cctest
303-
CI_SKIP_TESTS=core_line_numbers.js $(MAKE) jstest
307+
CI_SKIP_TESTS=$(COV_SKIP_TESTS) $(MAKE) jstest
304308

305309
test-parallel: all
306310
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) parallel

lib/internal/bootstrap/node.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -301,26 +301,10 @@ function startup() {
301301
}
302302
});
303303

304-
// Set up coverage exit hooks.
305-
let originalReallyExit = process.reallyExit;
306-
// Core coverage generation using nyc instrumented lib/ files.
307-
// See `make coverage-build`. This does not affect user land.
308-
// TODO(joyeecheung): this and `with_instrumentation.js` can be
309-
// removed in favor of NODE_V8_COVERAGE once we switch to that
310-
// in https://coverage.nodejs.org/
311-
if (global.__coverage__) {
312-
const {
313-
writeCoverage
314-
} = NativeModule.require('internal/coverage-gen/with_instrumentation');
315-
process.on('exit', writeCoverage);
316-
originalReallyExit = process.reallyExit = (code) => {
317-
writeCoverage();
318-
originalReallyExit(code);
319-
};
320-
}
321304
// User-facing NODE_V8_COVERAGE environment variable that writes
322305
// ScriptCoverage to a specified file.
323306
if (process.env.NODE_V8_COVERAGE) {
307+
const originalReallyExit = process.reallyExit;
324308
const cwd = NativeModule.require('internal/process/execution').tryGetCwd();
325309
const { resolve } = NativeModule.require('path');
326310
// Resolve the coverage directory to an absolute path, and

lib/internal/coverage-gen/with_instrumentation.js

-36
This file was deleted.

node.gyp

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
'lib/internal/console/constructor.js',
100100
'lib/internal/console/global.js',
101101
'lib/internal/coverage-gen/with_profiler.js',
102-
'lib/internal/coverage-gen/with_instrumentation.js',
103102
'lib/internal/crypto/certificate.js',
104103
'lib/internal/crypto/cipher.js',
105104
'lib/internal/crypto/diffiehellman.js',

test/common/index.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ function platformTimeout(ms) {
238238
if (process.features.debug)
239239
ms = multipliers.two * ms;
240240

241-
if (global.__coverage__)
242-
ms = multipliers.four * ms;
243-
244241
if (isAIX)
245242
return multipliers.two * ms; // default localhost speed is slower on AIX
246243

@@ -310,11 +307,7 @@ function leakedGlobals() {
310307
}
311308
}
312309

313-
if (global.__coverage__) {
314-
return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname));
315-
} else {
316-
return leaked;
317-
}
310+
return leaked;
318311
}
319312

320313
process.on('exit', function() {

test/parallel/test-bootstrap-modules.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const common = require('../common');
99
const assert = require('assert');
1010

1111
const isMainThread = common.isMainThread;
12-
const kMaxModuleCount = isMainThread ? 64 : 84;
12+
const kCoverageModuleCount = process.env.NODE_V8_COVERAGE ? 1 : 0;
13+
const kMaxModuleCount = (isMainThread ? 64 : 84) + kCoverageModuleCount;
1314

1415
assert(list.length <= kMaxModuleCount,
1516
`Total length: ${list.length}\n` + list.join('\n')

0 commit comments

Comments
 (0)