Skip to content

Commit

Permalink
fix(vfs): fix incorrect resolving absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
secundant committed Oct 21, 2023
1 parent 930466b commit 79a5023
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
33 changes: 28 additions & 5 deletions libs/vfs/src/__tests__/vsf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ describe('Tree', () => {
test('should ignore equal changes', async () => {
const vfs = await factory();

await vfs.write('/parent/parent-file.ts', 'new content');
await vfs.write('parent/parent-file.ts', 'new content');
await vfs.delete('root-file.ts');
await vfs.rename('/parent/child/child-file.ts', '/another-dir/file.ts');
await vfs.rename('parent/child/child-file.ts', 'another-dir/file.ts');

// Rename === write + delete
expect(await vfs.getChanges()).toHaveLength(4);
expectArrayEq(await vfs.readDir(), ['parent', 'another-dir']);

await vfs.write('root-file.ts', Buffer.from('root content'));
await vfs.write('/parent/parent-file.ts', 'parent content');
await vfs.write('parent/parent-file.ts', 'parent content');

// Instead of rename
await vfs.delete('/another-dir/file.ts');
await vfs.write('/parent/child/child-file.ts', 'child content');
await vfs.delete('another-dir/file.ts');
await vfs.write('parent/child/child-file.ts', 'child content');

expect(await vfs.getChanges()).toHaveLength(0);
});
Expand Down Expand Up @@ -232,6 +232,29 @@ describe('Tree', () => {
expect(await vfs.read('src/ignored.ejs', 'utf-8')).toBe('<%=foo% > ___');
});
});

test('should resolve different paths', async () => {
const vfs = createVfs('/root/path', {
virtual: true,
log
});

await vfs.write('relative', 'content');
await vfs.write('./relative-dot', 'content');
await vfs.write('../path/relative-jump', 'content');
await vfs.write('/root/path/absolute', 'content');
await vfs.write('/root/other/file-1', 'content');
await vfs.write('../other/file-2', 'content');

await vfs.applyChanges();
expectArrayEq(await vfs.readDir('../other'), ['file-1', 'file-2']);
expectArrayEq(await vfs.readDir('.'), [
'relative',
'relative-dot',
'relative-jump',
'absolute'
]);
});
});

// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
Expand Down
6 changes: 3 additions & 3 deletions libs/vfs/src/implementations/abstract-vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { colors } from '@neodx/colors';
import type { LoggerMethods } from '@neodx/log';
import { createLogger, pretty } from '@neodx/log/node';
import { compact, uniq } from '@neodx/std';
import { dirname, join, relative, sep } from 'pathe';
import { dirname, join, relative, resolve, sep } from 'pathe';
import type { BaseVFS, ContentLike, FileChange } from '../types';
import { FileChangeType } from '../types';

Expand Down Expand Up @@ -129,7 +129,7 @@ export abstract class AbstractVfs implements BaseVFS {
}
}

async readDir(path = '/'): Promise<string[]> {
async readDir(path = '.'): Promise<string[]> {
const normalized = this.normalizePath(path);
const currentList = await this.readDirImpl(normalized);

Expand Down Expand Up @@ -185,7 +185,7 @@ export abstract class AbstractVfs implements BaseVFS {
}

protected normalizePath(path: string) {
return relative(this.root, join(this.root, path)).replaceAll(sep, '/');
return relative(this.root, resolve(this.root, path)).replaceAll(sep, '/');
}

protected getNotDeletedChangesStartsFrom(path: string) {
Expand Down
2 changes: 1 addition & 1 deletion libs/vfs/src/implementations/virtual-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class VirtualFs extends AbstractVfs {
const names = Array.from(this.virtualFs.keys());

if (path === '') {
return names.map(name => name.split('/')[0]);
return names.map(name => name.split('/')[0]).filter(name => !name.startsWith('.'));
}
return names
.filter(name => name.startsWith(`${path}/`))
Expand Down

0 comments on commit 79a5023

Please sign in to comment.