0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-compile-cache-api-success.js
Joyee Cheung ff5ef7083d
src: add JS APIs for compile cache and NODE_DISABLE_COMPILE_CACHE
This patch adds the following API for tools to enable compile
cache dynamically and query its status.

- module.enableCompileCache(cacheDir)
- module.getCompileCacheDir()

In addition this adds a NODE_DISABLE_COMPILE_CACHE environment
variable to disable the code cache enabled by the APIs as
an escape hatch to avoid unexpected/undesired effects of
the compile cache (e.g. less precise test coverage).

When the module.enableCompileCache() method is invoked without
a specified directory, Node.js will use the value of
the NODE_COMPILE_CACHE environment variable if it's set, or
defaults to `path.join(os.tmpdir(), 'node-compile-cache')`
otherwise. Therefore it's recommended for tools to call this
method without specifying the directory to allow overrides.

PR-URL: https://github.com/nodejs/node/pull/54501
Fixes: https://github.com/nodejs/node/issues/53639
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-08-28 23:22:57 +00:00

80 lines
2.7 KiB
JavaScript

'use strict';
// This tests module.enableCompileCache() and module.getCompileCacheDir() work.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const path = require('path');
{
// Test that it works with non-existent directory.
tmpdir.refresh();
const dir = tmpdir.resolve('.compile_cache_dir');
spawnSyncAndAssert(
process.execPath,
['-r', fixtures.path('compile-cache-wrapper.js'), fixtures.path('snapshot', 'typescript.js')],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: undefined,
NODE_TEST_COMPILE_CACHE_DIR: dir,
},
cwd: tmpdir.path
},
{
stdout(output) {
console.log(output); // Logging for debugging.
assert.match(output, /dir before enableCompileCache: undefined/);
assert.match(output, /Compile cache enabled\. .*\.compile_cache_dir/);
assert.match(output, /dir after enableCompileCache: .*\.compile_cache_dir/);
return true;
},
stderr(output) {
console.log(output); // Logging for debugging.
assert.match(output, /typescript\.js was not initialized, initializing the in-memory entry/);
assert.match(output, /writing cache for .*typescript\.js.*success/);
return true;
}
});
const cacheDir = fs.readdirSync(dir);
assert.strictEqual(cacheDir.length, 1);
const entries = fs.readdirSync(path.join(dir, cacheDir[0]));
assert.strictEqual(entries.length, 1);
// Second run reads the cache, but no need to re-write because it didn't change.
spawnSyncAndAssert(
process.execPath,
['-r', fixtures.path('compile-cache-wrapper.js'), fixtures.path('snapshot', 'typescript.js')],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: undefined,
NODE_TEST_COMPILE_CACHE_DIR: dir,
},
cwd: tmpdir.path
},
{
stdout(output) {
console.log(output); // Logging for debugging.
assert.match(output, /dir before enableCompileCache: undefined/);
assert.match(output, /Compile cache enabled\. .*\.compile_cache_dir/);
assert.match(output, /dir after enableCompileCache: .*\.compile_cache_dir/);
return true;
},
stderr(output) {
console.log(output); // Logging for debugging.
assert.match(output, /cache for .*typescript\.js was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*typescript\.js because cache was the same/);
return true;
}
});
}