Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(path): add additional tests to normalize #5036

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
8 changes: 8 additions & 0 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ export function join(...paths: string[]): string {
* @returns a resolved path!
*/
export function resolve(...paths: string[]): string {
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no tests/additional PRs for resolve - so I threw a very similar comment in here for completeness sake

* When normalizing, we should _not_ attempt to relativize the path returned by the native Node `resolve` method. When
* calculating the path from each of the string-based parts, Node does not prepend './' to the calculated path.
*/
return normalizePath(path.resolve(...paths), false);
}

Expand All @@ -263,5 +267,9 @@ export function resolve(...paths: string[]): string {
* @returns a normalized path!
*/
export function normalize(toNormalize: string): string {
/**
* When normalizing, we should _not_ attempt to relativize the path returned by the native Node `normalize` method.
* When calculating the path from each of the string-based parts, Node does not prepend './' to the calculated path.
*/
return normalizePath(path.normalize(toNormalize), false);
}
5 changes: 5 additions & 0 deletions src/utils/test/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ describe('normalizeFsPathQuery', () => {
});

it('normalize should always return a POSIX path', () => {
expect(normalize('')).toBe('.');
expect(normalize('.')).toBe('.');
expect(normalize('..')).toBe('..');
expect(normalize('/')).toBe('/');
expect(normalize('\\')).toBe('/');
// these examples taken from
// https://nodejs.org/api/path.html#pathnormalizepath
expect(normalize('\\temp\\\\foo\\bar\\..\\')).toBe('/temp/foo');
Expand Down