Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: prevent unwanted dependencyOwners removal #55565

Merged
merged 5 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ class FilesWatcher extends EventEmitter {
owners.forEach((owner) => {
this.#ownerDependencies.get(owner)?.forEach((dependency) => {
this.#filteredFiles.delete(dependency);
this.#dependencyOwners.delete(dependency);
this.#dependencyOwners.get(dependency)?.delete(owner);
if (this.#dependencyOwners.get(dependency)?.size === 0) {
this.#dependencyOwners.delete(dependency);
}
});
this.#filteredFiles.delete(owner);
this.#dependencyOwners.delete(owner);
Expand Down
126 changes: 126 additions & 0 deletions test/parallel/test-runner-complex-dependencies.mjs
pmarchini marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Flags: --expose-internals
import * as common from '../common/index.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { spawn } from 'node:child_process';
import { writeFileSync } from 'node:fs';
import tmpdir from '../common/tmpdir.js';

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

if (common.isAIX)
common.skip('folder watch capability is limited in AIX.');

tmpdir.refresh();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a new test, it would be helpful to include comments here describing what expected behaviors are being tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean like a general summary? Because each block of code below has a comment of the behaviour

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jasnell, is this comment a blocker for you?
If not, I suggest we go ahead and land it 😄


// Set up test files and dependencies
const fixtureContent = {
'dependency.js': 'module.exports = {};',
'test.js': `
const test = require('node:test');
require('./dependency.js');
test('first test has ran');`,
'test-2.js': `
const test = require('node:test');
require('./dependency.js');
test('second test has ran');`,
};

const fixturePaths = Object.fromEntries(Object.keys(fixtureContent)
.map((file) => [file, tmpdir.resolve(file)]));

Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));

describe('test runner watch mode with more complex setup', () => {
it('should re-run appropriate tests when dependencies change', async () => {
// Start the test runner in watch mode
const child = spawn(process.execPath,
['--watch', '--test'],
{ encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path });

let currentRunOutput = '';
const testRuns = [];

const firstRunCompleted = Promise.withResolvers();
const secondRunCompleted = Promise.withResolvers();
const thirdRunCompleted = Promise.withResolvers();
const fourthRunCompleted = Promise.withResolvers();

child.stdout.on('data', (data) => {
const str = data.toString();
currentRunOutput += str;

if (/duration_ms\s\d+/.test(str)) {
// Test run has completed
testRuns.push(currentRunOutput);
currentRunOutput = '';
switch (testRuns.length) {
case 1:
firstRunCompleted.resolve();
break;
case 2:
secondRunCompleted.resolve();
break;
case 3:
thirdRunCompleted.resolve();
break;
case 4:
fourthRunCompleted.resolve();
break;
}
}
});

// Wait for the initial test run to complete
await firstRunCompleted.promise;

// Modify 'dependency.js' to trigger re-run of both tests
writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true };');

// Wait for the second test run to complete
await secondRunCompleted.promise;

// Modify 'test.js' to trigger re-run of only 'test.js'
writeFileSync(fixturePaths['test.js'], `
const test = require('node:test');
require('./dependency.js');
test('first test has ran again');`);

// Wait for the third test run to complete
await thirdRunCompleted.promise;

// Modify 'dependency.js' again to trigger re-run of both tests
writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true, again: true };');

// Wait for the fourth test run to complete
await fourthRunCompleted.promise;

// Kill the child process
child.kill();

// Analyze the test runs
assert.strictEqual(testRuns.length, 4);

// First test run - Both tests should run
const firstRunOutput = testRuns[0];
assert.match(firstRunOutput, /first test has ran/);
assert.match(firstRunOutput, /second test has ran/);

// Second test run - We have modified 'dependency.js' only, so both tests should re-run
const secondRunOutput = testRuns[1];
assert.match(secondRunOutput, /first test has ran/);
assert.match(secondRunOutput, /second test has ran/);

// Third test run - We have modified 'test.js' only
const thirdRunOutput = testRuns[2];
assert.match(thirdRunOutput, /first test has ran again/);
assert.doesNotMatch(thirdRunOutput, /second test has ran/);

// Fourth test run - We have modified 'dependency.js' again, so both tests should re-run
const fourthRunOutput = testRuns[3];
assert.match(fourthRunOutput, /first test has ran again/);
assert.match(fourthRunOutput, /second test has ran/);
});
});
53 changes: 53 additions & 0 deletions test/parallel/test-watch-file-shared-dependency.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as common from '../common/index.mjs';

Check failure on line 1 in test/parallel/test-watch-file-shared-dependency.mjs

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:internal/modules/package_json_reader:269 throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null); ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'internal' imported from /home/runner/work/node/node/test/parallel/test-watch-file-shared-dependency.mjs at Object.getPackageJSONURL (node:internal/modules/package_json_reader:269:9) at packageResolve (node:internal/modules/esm/resolve:768:81) at moduleResolve (node:internal/modules/esm/resolve:854:18) at defaultResolve (node:internal/modules/esm/resolve:984:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:654:12) at #cachedDefaultResolve (node:internal/modules/esm/loader:603:25) at ModuleLoader.resolve (node:internal/modules/esm/loader:586:38) at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:242:38) at ModuleJob._link (node:internal/modules/esm/module_job:135:49) { code: 'ERR_MODULE_NOT_FOUND' } Node.js v24.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-watch-file-shared-dependency.mjs

Check failure on line 1 in test/parallel/test-watch-file-shared-dependency.mjs

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/modules/package_json_reader:269 throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null); ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'internal' imported from /Users/runner/work/node/node/test/parallel/test-watch-file-shared-dependency.mjs at Object.getPackageJSONURL (node:internal/modules/package_json_reader:269:9) at packageResolve (node:internal/modules/esm/resolve:768:81) at moduleResolve (node:internal/modules/esm/resolve:854:18) at defaultResolve (node:internal/modules/esm/resolve:984:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:654:12) at #cachedDefaultResolve (node:internal/modules/esm/loader:603:25) at ModuleLoader.resolve (node:internal/modules/esm/loader:586:38) at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:242:38) at ModuleJob._link (node:internal/modules/esm/module_job:135:49) { code: 'ERR_MODULE_NOT_FOUND' } Node.js v24.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-watch-file-shared-dependency.mjs
Ceres6 marked this conversation as resolved.
Show resolved Hide resolved
import { describe, it } from 'node:test';
import assert from 'node:assert';
import tmpdir from '../common/tmpdir.js';
import watcher from 'internal/watch_mode/files_watcher';
import { writeFileSync } from 'node:fs';

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

if (common.isAIX)
common.skip('folder watch capability is limited in AIX.');

tmpdir.refresh();

const { FilesWatcher } = watcher;

tmpdir.refresh();

// Set up test files and dependencies
const fixtureContent = {
'dependency.js': 'module.exports = {};',
'test.js': 'require(\'./dependency.js\');',
'test-2.js': 'require(\'./dependency.js\');',
};

const fixturePaths = Object.fromEntries(Object.keys(fixtureContent)
.map((file) => [file, tmpdir.resolve(file)]));

Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));

describe('watch file with shared dependency', () => {
it('should not remove shared dependencies when unfiltering an owner', () => {
const controller = new AbortController();
const watcher = new FilesWatcher({ signal: controller.signal, debounce: 200 });

watcher.on('changed', ({ owners }) => {
assert.strictEqual(owners.size, 2);
assert.ok(owners.has(fixturePaths['test.js']));
assert.ok(owners.has(fixturePaths['test-2.js']));
controller.abort();
});
watcher.filterFile(fixturePaths['test.js']);
watcher.filterFile(fixturePaths['test-2.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test-2.js']);
watcher.unfilterFilesOwnedBy([fixturePaths['test.js']]);
watcher.filterFile(fixturePaths['test.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']);
writeFileSync(fixturePaths['dependency.js'], 'module.exports = { modified: true };');
});
});
Loading