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 relative #5029

Merged
merged 2 commits into from
Nov 8, 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
9 changes: 9 additions & 0 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,22 @@ const enum CharacterCodes {
* A wrapped version of node.js' {@link path.relative} which adds our custom
* normalization logic. This solves the relative path between `from` and `to`!
*
* The calculation of the returned path follows that of Node's logic, with one exception - if the calculated path
* results in an empty string, a string of length one with a period (`'.'`) is returned.
*
* @throws the underlying node.js function can throw if either path is not a
* string
* @param from the path where relative resolution starts
* @param to the destination path
* @returns the resolved relative path
*/
export function relative(from: string, to: string): string {
/**
* When normalizing, we should _not_ attempt to relativize the path returned by the native Node `relative` method.
* When finding the relative path between `from` and `to`, Node does not prepend './' to a non-zero length calculated
* path. However, our algorithm does differ from that of Node's, as described in this function's JSDoc when a zero
* length string is encountered.
*/
return normalizePath(path.relative(from, to), false);
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/test/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ describe('normalizeFsPathQuery', () => {
it('relative should always return a POSIX path', () => {
expect(relative('.', 'foo/bar')).toBe('foo/bar');
expect(relative('foo/bar', '..')).toBe('../../..');
expect(relative('foo', 'foo/bar/file.js')).toBe('bar/file.js');
expect(relative('foo/bar', 'foo/bar/file.js')).toBe('file.js');
expect(relative('foo/bar/baz', 'foo/bar/boz')).toBe('../boz');
expect(relative('foo/bar/file.js', 'foo/bar/file.js')).toBe('.');
expect(relative('.', '../foo/bar')).toBe('../foo/bar');
});

Expand Down