Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions src/__tests__/path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isAbsolute, normalize } from '../path';

describe('Path utils', () => {
describe('isAbsolute fn', () => {
test.each([
'c:\\foo\\bar.json',
'c:\\',
'c:/',
'c:/foo/bar.json',
'/home/test',
'/',
'/var/lib/test/',
'/var/bin.d',
])('should treat %s path as absolute', filepath => {
expect(isAbsolute(filepath)).toBe(true);
});
});

test.each(['\\foo\\bar.json', 'foo/bar', 'test'])('should treat %s path as non-absolute', filepath => {
expect(isAbsolute(filepath)).toBe(false);
});

describe('normalize fn', () => {
test('should replace windows-like slashes with POSIX-compatible ones', () => {
expect(normalize('c:\\foo\\bar')).toEqual('c:/foo/bar');
});

test('should ignore POSIX slashes', () => {
expect(normalize('/d/foo')).toEqual('/d/foo');
});
});
});
12 changes: 12 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as path from 'path';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the tests, could we perhaps run tests twice, once with path mocked/set to path.posix and once with path.win32? https://nodejs.org/api/path.html#path_windows_vs_posix

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. Just don't use path

import * as URI from 'urijs';
import { URI as VSURI } from 'vscode-uri';

export const isAbsolute = (filepath: string) => path.isAbsolute(filepath) || new URI(filepath).is('absolute');

export const join = (...parts: string[]) => parts.join('/');
Copy link
Contributor

@marbemac marbemac Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.join(filepath).replace(/\\/g, '/');?

export const normalize = (filepath: string) => path.normalize(filepath).replace(/\\/g, '/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when non file e.g. https://localhost:3000/bar.json?page=1#hello is pass into all of these helpers? Can you add a few test cases for this?


export function toFSPath(uri: string) {
return path.normalize(VSURI.file(uri).fsPath);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.