From a5c99bd70d006df4bdcfa46a88c23eec6beb8884 Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Mon, 9 Jan 2023 22:17:39 +0800 Subject: [PATCH] fix: ignore . and .. from readdir --- src/__tests__/node.test.ts | 6 +++--- src/__tests__/promises.test.ts | 2 +- src/__tests__/volume.test.ts | 20 ++++++++++---------- src/__tests__/volume/readdirSync.test.ts | 10 +++++----- src/node.ts | 4 ++-- src/volume.ts | 13 ++++++++----- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/__tests__/node.test.ts b/src/__tests__/node.test.ts index 7372a7268..1c3f911ba 100644 --- a/src/__tests__/node.test.ts +++ b/src/__tests__/node.test.ts @@ -75,7 +75,7 @@ describe('node.ts', () => { const oldCtime = node.ctime; node[field] = 1; const newCtime = node.ctime; - expect(newCtime !== oldCtime).toBeTruthy(); + expect(newCtime).not.toBe(oldCtime); }); }); @@ -91,8 +91,8 @@ describe('node.ts', () => { node[method](...args); const newAtime = node.atime; const newCtime = node.ctime; - expect(newAtime !== oldAtime).toBeTruthy(); - expect(newCtime !== oldCtime).toBeTruthy(); + expect(newAtime).not.toBe(oldAtime); + expect(newCtime).not.toBe(oldCtime); }); }); }); diff --git a/src/__tests__/promises.test.ts b/src/__tests__/promises.test.ts index 60267dc75..e49002799 100644 --- a/src/__tests__/promises.test.ts +++ b/src/__tests__/promises.test.ts @@ -462,7 +462,7 @@ describe('Promises API', () => { '/foo/baz': 'baz', }); it('Read an existing directory', async () => { - expect(await promises.readdir('/foo')).toEqual(['.', '..', 'bar', 'baz']); + expect(await promises.readdir('/foo')).toEqual(['bar', 'baz']); }); it('Reject when directory does not exist', () => { return expect(promises.readdir('/bar')).rejects.toBeInstanceOf(Error); diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 2d62525d2..9aea5c26c 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -261,7 +261,7 @@ describe('volume', () => { const stat = vol.statSync('/dir'); expect(stat.isDirectory()).toBe(true); - expect(vol.readdirSync('/dir')).toEqual(['.', '..']); + expect(vol.readdirSync('/dir')).toEqual([]); }); }); @@ -338,7 +338,7 @@ describe('volume', () => { expect(vol.root.getChild('test.txt')).toBeInstanceOf(Link); expect(typeof fd).toBe('number'); expect(fd).toBeGreaterThan(0); - expect(oldMtime !== newMtime).toBeTruthy(); + expect(oldMtime).not.toBe(newMtime); }); it('Error on file not found', () => { try { @@ -873,20 +873,20 @@ describe('volume', () => { vol.writeFileSync('/1.js', '123'); vol.writeFileSync('/2.js', '123'); const list = vol.readdirSync('/'); - expect(list.length).toBe(4); - expect(list).toEqual(['.', '..', '1.js', '2.js']); + expect(list.length).toBe(2); + expect(list).toEqual(['1.js', '2.js']); }); it('Returns a Dirent list', () => { const vol = new Volume(); vol.writeFileSync('/1', '123'); vol.mkdirSync('/2'); const list = vol.readdirSync('/', { withFileTypes: true }); - expect(list.length).toBe(4); - expect(list[2]).toBeInstanceOf(Dirent); - const dirent0 = list[2] as Dirent; + expect(list.length).toBe(2); + expect(list[0]).toBeInstanceOf(Dirent); + const dirent0 = list[0] as Dirent; expect(dirent0.name).toBe('1'); expect(dirent0.isFile()).toBe(true); - const dirent1 = list[3] as Dirent; + const dirent1 = list[1] as Dirent; expect(dirent1.name).toBe('2'); expect(dirent1.isDirectory()).toBe(true); }); @@ -1000,8 +1000,8 @@ describe('volume', () => { const child = tryGetChild(vol.root, 'test'); expect(child).toBeInstanceOf(Link); expect(child.getNode().isDirectory()).toBe(true); - expect(oldMtime !== newMtime).toBeTruthy(); - expect(newNlink === oldNlink + 1).toBeTruthy; + expect(oldMtime).not.toBe(newMtime); + expect(newNlink).toBe(oldNlink + 1); }); it('Create 2 levels deep folders', () => { const vol = new Volume(); diff --git a/src/__tests__/volume/readdirSync.test.ts b/src/__tests__/volume/readdirSync.test.ts index f0aaf535d..53ff2b999 100644 --- a/src/__tests__/volume/readdirSync.test.ts +++ b/src/__tests__/volume/readdirSync.test.ts @@ -7,7 +7,7 @@ describe('readdirSync()', () => { }); const dirs = vol.readdirSync('/'); - expect(dirs).toEqual(['.', '..', 'foo']); + expect(dirs).toEqual(['foo']); }); it('returns multiple directories', () => { @@ -20,14 +20,14 @@ describe('readdirSync()', () => { (dirs as any).sort(); - expect(dirs).toEqual(['.', '..', 'ab', 'foo', 'tro']); + expect(dirs).toEqual(['ab', 'foo', 'tro']); }); it('returns empty array when dir empty', () => { const vol = create({}); const dirs = vol.readdirSync('/'); - expect(dirs).toEqual(['.', '..']); + expect(dirs).toEqual([]); }); it('respects symlinks', () => { @@ -43,7 +43,7 @@ describe('readdirSync()', () => { (dirs as any).sort(); - expect(dirs).toEqual(['.', '..', 'a', 'aa']); + expect(dirs).toEqual(['a', 'aa']); }); it('respects recursive symlinks', () => { @@ -53,6 +53,6 @@ describe('readdirSync()', () => { const dirs = vol.readdirSync('/foo'); - expect(dirs).toEqual(['.', '..', 'foo']); + expect(dirs).toEqual(['foo']); }); }); diff --git a/src/node.ts b/src/node.ts index e68b74487..3f6702919 100644 --- a/src/node.ts +++ b/src/node.ts @@ -324,9 +324,9 @@ export class Link extends EventEmitter { if (node.isDirectory()) { link.children['..'] = this; this.getNode().nlink++; - this.getNode().mtime = new Date(); } + this.getNode().mtime = new Date(); this.emit('child:add', link, this); return link; @@ -337,11 +337,11 @@ export class Link extends EventEmitter { if (node.isDirectory()) { delete link.children['..']; this.getNode().nlink--; - this.getNode().mtime = new Date(); } delete this.children[link.getName()]; this.length--; + this.getNode().mtime = new Date(); this.emit('child:delete', link, this); } diff --git a/src/volume.ts b/src/volume.ts index 31710efdb..9282003a1 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1744,7 +1744,7 @@ export class Volume { for (const name in link.children) { const child = link.getChild(name); - if (!child) { + if (!child || name === '.' || name === '..') { continue; } @@ -1761,6 +1761,9 @@ export class Volume { const list: TDataOut[] = []; for (const name in link.children) { + if (name === '.' || name === '..') { + continue; + } list.push(strToEncoding(name, options.encoding)); } @@ -2801,8 +2804,8 @@ export class FSWatcher extends EventEmitter { }; // children nodes changed - Object.values(link.children).forEach(childLink => { - if (childLink) { + Object.entries(link.children).forEach(([name, childLink]) => { + if (childLink && name !== '.' && name !== '..') { watchLinkNodeChanged(childLink); } }); @@ -2817,8 +2820,8 @@ export class FSWatcher extends EventEmitter { }); if (recursive) { - Object.values(link.children).forEach(childLink => { - if (childLink) { + Object.entries(link.children).forEach(([name, childLink]) => { + if (childLink && name !== '.' && name !== '..') { watchLinkChildrenChanged(childLink); } });