Files
node/test/parallel/test-compile-cache-api-readonly.js
Shelley Vohr dcaf43a167 module: disable the compile cache while V8 coverage is collected
V8 cannot collect block coverage for functions that are deserialized
from the code cache, so it only reports how many times each of them
ran. With a warm compile cache, branches that never ran inside those
functions are reported as covered, and tools that merge reports from
several processes can report functions that ran as never run.

The documentation recommended turning the cache off by hand. Do it
automatically when coverage collection starts with the process, through
NODE_V8_COVERAGE or --experimental-test-coverage.

The compile cache tests stop propagating NODE_V8_COVERAGE to the
processes they spawn, so that they keep working when the test suite
itself runs with coverage.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67120
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
2026-11-20 14:59:50 +00:00

66 lines
2.3 KiB
JavaScript

'use strict';
// This tests module.enableCompileCache({ directory, readOnly: true }): existing
// entries are read, nothing is written, and a missing directory is not created.
const common = require('../common');
const { spawnSyncAndAssert } = require('../common/compile-cache');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
tmpdir.refresh();
const wrapper = fixtures.path('compile-cache-wrapper-options.js');
const target = path.join(tmpdir.path, 'target.js');
fs.writeFileSync(target, 'module.exports = 1;');
const other = path.join(tmpdir.path, 'other.js');
fs.writeFileSync(other, 'module.exports = 2;');
const directory = path.join(tmpdir.path, 'cache');
const list = () => fs.readdirSync(directory, { recursive: true }).sort();
const run = (options, extraEnv, requires, check) => spawnSyncAndAssert(
process.execPath,
[...requires.flatMap((r) => ['-r', r]), target],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_TEST_COMPILE_CACHE_OPTIONS: JSON.stringify(options),
...extraEnv,
},
},
check);
// Read-only against a directory that does not exist: enabling fails and
// nothing is created.
run({ directory, readOnly: true }, {}, [wrapper], {
stderr: /read-only cache directory .*\.\.\.not found/,
});
assert(!fs.existsSync(directory));
// A normal run generates the cache for target.js.
run({ directory }, {}, [wrapper], {
stderr: /writing cache for .*target\.js.*success/,
});
const generated = list();
assert.notStrictEqual(generated.length, 0);
// Read-only against it: target.js's entry is accepted, other.js is compiled
// but not written, and persistence is skipped.
run({ directory, readOnly: true }, {}, [wrapper, other], {
stderr: common.mustCall((output) => {
assert.match(output, /cache for .*target\.js was accepted/);
assert.match(output, /read-only, skipping persistence/);
assert.doesNotMatch(output, /writing cache for/);
return true;
}),
});
assert.deepStrictEqual(list(), generated);
// The environment variable form.
run({ directory }, { NODE_COMPILE_CACHE_READONLY: '1' }, [wrapper, other], {
stderr: /read-only, skipping persistence/,
});
assert.deepStrictEqual(list(), generated);