-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(cli): expand windows-style tilde paths #5298
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { resolvePath } from './resolvePath.js'; | ||
|
|
||
| describe('resolvePath', () => { | ||
| it('returns an empty string unchanged', () => { | ||
| expect(resolvePath('')).toBe(''); | ||
| }); | ||
|
|
||
| it('expands bare tilde to the home directory', () => { | ||
| expect(resolvePath('~')).toBe(path.normalize(os.homedir())); | ||
| }); | ||
|
|
||
| it('expands POSIX-style tilde paths', () => { | ||
| expect(resolvePath('~/schemas/input.json')).toBe( | ||
| path.join(os.homedir(), 'schemas', 'input.json'), | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps the existing POSIX-style trailing separator behavior', () => { | ||
| expect(resolvePath('~/')).toBe(path.normalize(`${os.homedir()}/`)); | ||
| }); | ||
|
|
||
| it('expands Windows-style tilde paths', () => { | ||
| expect(resolvePath('~\\schemas\\input.json')).toBe( | ||
| path.join(os.homedir(), 'schemas', 'input.json'), | ||
| ); | ||
| }); | ||
|
|
||
| it('expands USERPROFILE references case-insensitively', () => { | ||
| expect(resolvePath('%USERPROFILE%\\schemas\\input.json')).toBe( | ||
| path.normalize(`${os.homedir()}\\schemas\\input.json`), | ||
| ); | ||
| }); | ||
|
|
||
| it('normalizes relative paths without resolving them', () => { | ||
| expect(resolvePath('nested/../schema.json')).toBe( | ||
| path.normalize('schema.json'), | ||
| ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] Three untested edge cases for the new
it('expands bare backslash-tilde to the home directory', () => {
expect(resolvePath('~\\')).toBe(path.normalize(os.homedir()));
});
it('handles mixed separators in Windows-style tilde paths', () => {
expect(resolvePath('~\\foo/bar\\baz')).toBe(
path.join(os.homedir(), 'foo', 'bar', 'baz'),
);
});— qwen3.7-max via Qwen Code /review |
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,14 @@ export function resolvePath(p: string): string { | |||||||
| expandedPath = os.homedir() + p.substring('%userprofile%'.length); | ||||||||
| } else if (p === '~' || p.startsWith('~/')) { | ||||||||
| expandedPath = os.homedir() + p.substring(1); | ||||||||
| } else if (p.startsWith('~\\')) { | ||||||||
| expandedPath = path.join( | ||||||||
| os.homedir(), | ||||||||
| ...p | ||||||||
| .substring(2) | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The
A simpler approach using
Suggested change
— qwen3.7-max via Qwen Code /review |
||||||||
| .split(/[/\\]+/) | ||||||||
| .filter(Boolean), | ||||||||
| ); | ||||||||
| } | ||||||||
| return path.normalize(expandedPath); | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] On POSIX, both sides of this assertion produce a path with literal backslash characters (e.g.,
/home/user\schemas\input.json), which is not a valid POSIX path — the test validates a broken string. Additionally, the test name claims "case-insensitively" but only tests uppercase%USERPROFILE%, never%userprofile%.Consider making the expected value platform-conditional or adding a lowercase variant:
— qwen3.7-max via Qwen Code /review