Skip to content

Commit

Permalink
fix: ignore . and .. from readdir
Browse files Browse the repository at this point in the history
  • Loading branch information
vangie committed Apr 11, 2023
1 parent a1aa7dc commit 85a0f5a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand All @@ -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);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
});

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/volume/readdirSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('readdirSync()', () => {
});
const dirs = vol.readdirSync('/');

expect(dirs).toEqual(['.', '..', 'foo']);
expect(dirs).toEqual(['foo']);
});

it('returns multiple directories', () => {
Expand All @@ -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', () => {
Expand All @@ -43,7 +43,7 @@ describe('readdirSync()', () => {

(dirs as any).sort();

expect(dirs).toEqual(['.', '..', 'a', 'aa']);
expect(dirs).toEqual(['a', 'aa']);
});

it('respects recursive symlinks', () => {
Expand All @@ -53,6 +53,6 @@ describe('readdirSync()', () => {

const dirs = vol.readdirSync('/foo');

expect(dirs).toEqual(['.', '..', 'foo']);
expect(dirs).toEqual(['foo']);
});
});
4 changes: 2 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ export class Volume {
for (const name in link.children) {
const child = link.getChild(name);

if (!child) {
if (!child || name === '.' || name === '..') {
continue;
}

Expand All @@ -1756,6 +1756,9 @@ export class Volume {

const list: TDataOut[] = [];
for (const name in link.children) {
if (name === '.' || name === '..') {
continue;
}
list.push(strToEncoding(name, options.encoding));
}

Expand Down

0 comments on commit 85a0f5a

Please sign in to comment.