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: remove fs mocked test #1407

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Changes from all 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
65 changes: 9 additions & 56 deletions src/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
let linkedDir: string;
beforeEach(async () => {
linkedDir = sysPath.resolve(currentDir, '..', `${testId}-link`);
await fsp.symlink(currentDir, linkedDir, isWindows ? 'dir' : null);
await fsp.symlink(currentDir, linkedDir, isWindows ? 'dir' : undefined);
await fsp.mkdir(dpath('subdir'), PERM);
await write(dpath('subdir/add.txt'), 'b');
});
Expand Down Expand Up @@ -983,7 +983,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
spy.should.have.been.calledWith(EV.CHANGE, testFile);
});
it('should not recurse indefinitely on circular symlinks', async () => {
await fsp.symlink(currentDir, dpath('subdir/circular'), isWindows ? 'dir' : null);
await fsp.symlink(currentDir, dpath('subdir/circular'), isWindows ? 'dir' : undefined);
await new Promise<void>((resolve, reject) => {
const watcher = cwatch(currentDir, options);
watcher.on(EV.ERROR, () => {
Expand All @@ -1008,7 +1008,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
const watcher = cwatch(currentDir, options);
const spy = await aspy(watcher, EV.ALL);
await delay();
await fsp.symlink(dpath('subdir'), dpath('link'), isWindows ? 'dir' : null);
await fsp.symlink(dpath('subdir'), dpath('link'), isWindows ? 'dir' : undefined);
await waitFor([
spy.withArgs(EV.ADD, dpath('link/add.txt')),
spy.withArgs(EV.ADD_DIR, dpath('link')),
Expand All @@ -1028,7 +1028,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
options.followSymlinks = false;
const targetDir = dpath('subdir/nonexistent');
await fsp.mkdir(targetDir);
await fsp.symlink(targetDir, dpath('subdir/broken'), isWindows ? 'dir' : null);
await fsp.symlink(targetDir, dpath('subdir/broken'), isWindows ? 'dir' : undefined);
await fsp.rmdir(targetDir);
await delay();

Expand Down Expand Up @@ -1085,7 +1085,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
it('should emit ready event even when broken symlinks are encountered', async () => {
const targetDir = dpath('subdir/nonexistent');
await fsp.mkdir(targetDir);
await fsp.symlink(targetDir, dpath('subdir/broken'), isWindows ? 'dir' : null);
await fsp.symlink(targetDir, dpath('subdir/broken'), isWindows ? 'dir' : undefined);
await fsp.rmdir(targetDir);
const readySpy = sinon.spy(function readySpy() {});
const watcher = cwatch(dpath('subdir'), options).on(EV.READY, readySpy);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
it('should respect depth setting when following symlinks', async () => {
if (isWindows) return true; // skip on windows
options.depth = 1;
await fsp.symlink(dpath('subdir'), dpath('link'), isWindows ? 'dir' : null);
await fsp.symlink(dpath('subdir'), dpath('link'), isWindows ? 'dir' : undefined);
await delay();
const spy = await aspy(cwatch(currentDir, options), EV.ALL);
spy.should.have.been.calledWith(EV.ADD_DIR, dpath('link'));
Expand All @@ -1323,7 +1323,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
const linkPath = dpath('link');
const dirPath = dpath('link/subsub');
const spy = await aspy(cwatch(currentDir, options), EV.ALL);
await fsp.symlink(dpath('subdir'), linkPath, isWindows ? 'dir' : null);
await fsp.symlink(dpath('subdir'), linkPath, isWindows ? 'dir' : undefined);
await waitFor([[spy, 3], spy.withArgs(EV.ADD_DIR, dirPath)]);
spy.should.have.been.calledWith(EV.ADD_DIR, linkPath);
spy.should.have.been.calledWith(EV.ADD_DIR, dirPath);
Expand Down Expand Up @@ -1632,53 +1632,6 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
spy.should.have.been.calledWith(EV.UNLINK, filename);
spy.should.not.have.been.calledWith(EV.CHANGE, filename);
});
describe.skip('race condition', () => {
// Reproduces bug https://github.com/paulmillr/chokidar/issues/546, which was causing an
// uncaught exception. The race condition is likelier to happen when stat() is slow.
const _realStat = fs.stat;
let statStub: sinon.SinonSpy;

beforeEach(() => {
options.awaitWriteFinish = { pollInterval: 50, stabilityThreshold: 50 };
options.ignoreInitial = true;

// Stub fs.stat() to take a while to return.
statStub = sinon.stub(fs, 'stat').callsFake(async (path, cb) => {
await delay(250);
_realStat(path, cb as () => void);
});
});

afterEach(() => {
// Restore fs.stat() back to normal.
sinon.restore();
});

it('should handle unlink that happens while waiting for stat to return', async () => {
const testPath = dpath('add.txt');
const watcher = cwatch(currentDir, options);
const spy = await aspy(watcher, EV.ALL);
await fsp.writeFile(testPath, 'hello');
await waitFor([spy]);
spy.should.have.been.calledWith(EV.ADD, testPath);
statStub.resetHistory();
await fsp.writeFile(testPath, 'edit');
await delay();
// There will be a stat() call after we notice the change, plus pollInterval.
// After waiting a bit less, wait specifically for that stat() call.
statStub.resetHistory();
await waitFor([statStub]);
// Once stat call is made, it will take some time to return. Meanwhile, unlink
// the file and wait for that to be noticed.
await fsp.unlink(testPath);
await waitFor([spy.withArgs(EV.UNLINK)]);
await delay();
// Wait a while after unlink to ensure stat() had time to return. That's where
// an uncaught exception used to happen.
spy.should.have.been.calledWith(EV.UNLINK, testPath);
spy.should.not.have.been.calledWith(EV.CHANGE);
});
});
});
});
describe('getWatched', () => {
Expand Down Expand Up @@ -1891,7 +1844,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
const filePath = sysPath.join(folderPath, `file${i}.js`);
await write(sysPath.resolve(filePath), 'file content');
const symlinkPath = sysPath.join(linkPath, `folder${i}`);
await fsp.symlink(sysPath.resolve(folderPath), symlinkPath, isWindows ? 'dir' : null);
await fsp.symlink(sysPath.resolve(folderPath), symlinkPath, isWindows ? 'dir' : undefined);
watcher.add(sysPath.resolve(sysPath.join(symlinkPath, `file${i}.js`)));
}

Expand Down Expand Up @@ -1960,7 +1913,7 @@ const runTests = (baseopts: chokidar.ChokidarOptions) => {
await fsp.symlink(
sysPath.resolve(relativeWatcherDir),
linkedRelativeWatcherDir,
isWindows ? 'dir' : null
isWindows ? 'dir' : undefined
);
await delay();
const watcher = cwatch(linkedRelativeWatcherDir, {
Expand Down
Loading