Skip to content
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
49 changes: 49 additions & 0 deletions packages/cli/src/utils/resolvePath.test.ts
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`),
);

Copy link
Copy Markdown
Collaborator

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:

it('expands lowercase %userprofile% references', () => {
  expect(resolvePath('%userprofile%\\schemas\\input.json')).toBe(
    process.platform === 'win32'
      ? path.join(os.homedir(), 'schemas', 'input.json')
      : path.normalize(`${os.homedir()}\\schemas\\input.json`),
  );
});

— qwen3.7-max via Qwen Code /review

});

it('normalizes relative paths without resolving them', () => {
expect(resolvePath('nested/../schema.json')).toBe(
path.normalize('schema.json'),
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] Three untested edge cases for the new ~\\ branch:

  1. Bare ~\\ (just two characters) — exercises filter(Boolean) with an empty segment array
  2. Mixed separators like ~\\foo/bar\\baz — validates the [/\\\\]+ regex handles both styles
  3. Trailing separator ~\\foo\\ — would have caught the trailing-separator inconsistency with the ~/ branch
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

});
});
8 changes: 8 additions & 0 deletions packages/cli/src/utils/resolvePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] The ~\ branch uses path.join + split/filter, while the ~/ and %USERPROFILE% branches use string concatenation. This creates two issues:

  1. Trailing separator inconsistency: path.join strips trailing separators, but path.normalize (used by the other branches) preserves them. So resolvePath('~/foo/')homedir/foo/ but resolvePath('~\\foo\\')homedir/foo (trailing separator lost).

  2. Strategy divergence: different construction patterns make the function harder to maintain — a future change to one branch may not be correctly ported to the others.

A simpler approach using replaceAll matches the existing concat pattern and avoids both issues:

Suggested change
.substring(2)
} else if (p.startsWith('~\\')) {
expandedPath = os.homedir() + p.substring(1).replaceAll('\\', '/');

— qwen3.7-max via Qwen Code /review

.split(/[/\\]+/)
.filter(Boolean),
);
}
return path.normalize(expandedPath);
}
Loading