Skip to content

Commit

Permalink
Fix chdir behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lettertwo committed Oct 4, 2023
1 parent 329fb07 commit c6c3fa0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
10 changes: 1 addition & 9 deletions packages/core/fs/src/OverlayFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,7 @@ export class OverlayFS implements FileSystem {
}

chdir(path: FilePath): void {
try {
this.readable.chdir(path);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
this.writable.chdir(path);
}
this._cwd = path;
this._cwd = this._checkExists(path);
}

// eslint-disable-next-line require-await
Expand Down
68 changes: 68 additions & 0 deletions packages/core/fs/test/OverlayFS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,72 @@ describe('OverlayFS', () => {
assert(fs.existsSync('foo/baz'));
assert(!fs.existsSync('foo/baz/bat'));
});

it('supports changing to a dir that is only on the readable fs', async () => {
await fsFixture(underlayFS)`
foo/bar: baz
`;

assert.equal(fs.cwd(), path.resolve('/'));
fs.chdir('/foo');
assert.equal(fs.cwd(), path.resolve('/foo'));
});

it('supports changing to a dir that is only on the writable fs', async () => {
await fsFixture(underlayFS)`
foo/bar: bar
`;

await fs.mkdirp('/bar');
assert(!underlayFS.existsSync('/bar'));

assert.equal(fs.cwd(), path.resolve('/'));
fs.chdir('/bar');
assert.equal(fs.cwd(), path.resolve('/bar'));
});

it('supports changing dir relative to cwd', async () => {
await fsFixture(underlayFS)`
foo/bar: bar
`;

assert.equal(fs.cwd(), path.resolve('/'));
fs.chdir('foo');
assert.equal(fs.cwd(), path.resolve('/foo'));
});

it('changes dir without changing underlying fs dir', async () => {
await fsFixture(underlayFS)`
foo/bar: baz
foo/bat/baz: qux
`;

assert.equal(fs.cwd(), path.resolve('/'));
assert.equal(underlayFS.cwd(), path.resolve('/'));

fs.chdir('foo');

assert.equal(fs.cwd(), path.resolve('/foo'));
assert.equal(underlayFS.cwd(), path.resolve('/'));
});

it('errors when changing to a dir that does not exist on either fs', async () => {
await fsFixture(underlayFS)`
foo/bar: bar
`;

assert.throws(() => fs.chdir('/bar'), /ENOENT/);
});

it('errors when changing to a deleted dir', async () => {
await fsFixture(underlayFS)`
foo/bar: bar
`;

await fs.rimraf('foo');
assert(!fs.existsSync('foo'));
assert(underlayFS.existsSync('foo'));

assert.throws(() => fs.chdir('/foo'), /ENOENT/);
});
});

0 comments on commit c6c3fa0

Please sign in to comment.