mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
This caches the current working directory and only updates the variable if `process.chdir()` is called. PR-URL: https://github.com/nodejs/node/pull/27224 Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
'use strict';
|
|
const assert = require('assert');
|
|
const common = require('../common');
|
|
const { Worker, isMainThread, parentPort } = require('worker_threads');
|
|
|
|
// Do not use isMainThread directly, otherwise the test would time out in case
|
|
// it's started inside of another worker thread.
|
|
if (!process.env.HAS_STARTED_WORKER) {
|
|
process.env.HAS_STARTED_WORKER = '1';
|
|
if (!isMainThread) {
|
|
common.skip('This test can only run as main thread');
|
|
}
|
|
const w = new Worker(__filename);
|
|
process.chdir('..');
|
|
w.on('message', common.mustCall((message) => {
|
|
assert.strictEqual(message, process.cwd());
|
|
process.chdir('..');
|
|
w.postMessage(process.cwd());
|
|
}));
|
|
} else if (!process.env.SECOND_WORKER) {
|
|
process.env.SECOND_WORKER = '1';
|
|
const firstCwd = process.cwd();
|
|
const w = new Worker(__filename);
|
|
w.on('message', common.mustCall((message) => {
|
|
assert.strictEqual(message, process.cwd());
|
|
parentPort.postMessage(firstCwd);
|
|
parentPort.onmessage = common.mustCall((obj) => {
|
|
const secondCwd = process.cwd();
|
|
assert.strictEqual(secondCwd, obj.data);
|
|
assert.notStrictEqual(secondCwd, firstCwd);
|
|
w.postMessage(secondCwd);
|
|
parentPort.unref();
|
|
});
|
|
}));
|
|
} else {
|
|
const firstCwd = process.cwd();
|
|
parentPort.postMessage(firstCwd);
|
|
parentPort.onmessage = common.mustCall((obj) => {
|
|
const secondCwd = process.cwd();
|
|
assert.strictEqual(secondCwd, obj.data);
|
|
assert.notStrictEqual(secondCwd, firstCwd);
|
|
process.exit();
|
|
});
|
|
}
|