Skip to content

Commit

Permalink
test(path): add additional tests to normalize (#5036)
Browse files Browse the repository at this point in the history
add additional tests to stencil's wrapped version of `path.normalize`,
adding additional documentation to how the wrapped function works.
specifically, add tests to verify the following behavior:
1. how an empty string behaves
2. how relative paths ('.', '..') behave
3. how absolute paths ('/' for POSIX and '\\' for Windows) behave

questioning how thise function behaves/should behave was the impetus
for this change - I didn't understand what I was seeing at first when
debugging some Stencil 4.x related path related bugs. this commit is
spun out from that work.

related to: #5029, #5032
  • Loading branch information
rwaskiewicz authored Nov 9, 2023
1 parent 1dd59a5 commit 8e8f1e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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 {
/**
* 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

0 comments on commit 8e8f1e2

Please sign in to comment.