From bf312182264de95e643d25dc69128089266211e5 Mon Sep 17 00:00:00 2001 From: Ning Cui Date: Wed, 3 Jun 2026 12:28:42 -0400 Subject: [PATCH] harness(chunk-1): Add a path-separator boundary check to tildeifyPath and add a regression test --- packages/core/src/utils/paths.test.ts | 25 +++++++++++++++++++++++++ packages/core/src/utils/paths.ts | 10 +++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/core/src/utils/paths.test.ts b/packages/core/src/utils/paths.test.ts index 6759b7978c9..c8371c1bba6 100644 --- a/packages/core/src/utils/paths.test.ts +++ b/packages/core/src/utils/paths.test.ts @@ -14,6 +14,8 @@ import { isSubpath, shortenPath, resolveToRealPath, + tildeifyPath, + homedir, } from './paths.js'; vi.mock('node:fs', async (importOriginal) => { @@ -529,3 +531,26 @@ describe('resolveToRealPath', () => { expect(resolveToRealPath(input)).toBe(expected); }); }); + +describe('tildeifyPath', () => { + const home = homedir(); + + it('should replace the home directory prefix with a tilde', () => { + const input = path.join(home, 'projects', 'file.txt'); + expect(tildeifyPath(input)).toBe(`~${path.sep}projects${path.sep}file.txt`); + }); + + it('should tildeify the home directory itself', () => { + expect(tildeifyPath(home)).toBe('~'); + }); + + it('should not tildeify a sibling path that merely starts with the home dir name', () => { + const sibling = `${home}foo${path.sep}bar`; + expect(tildeifyPath(sibling)).toBe(sibling); + }); + + it('should return an unrelated path unchanged', () => { + const other = path.join(path.sep, 'tmp', 'something'); + expect(tildeifyPath(other)).toBe(other); + }); +}); diff --git a/packages/core/src/utils/paths.ts b/packages/core/src/utils/paths.ts index 94ccd96cf30..0ce02e9cadf 100644 --- a/packages/core/src/utils/paths.ts +++ b/packages/core/src/utils/paths.ts @@ -43,15 +43,15 @@ export function tmpdir(): string { /** * Replaces the home directory with a tilde. - * @param path - The path to tildeify. + * @param filePath - The path to tildeify. * @returns The tildeified path. */ -export function tildeifyPath(path: string): string { +export function tildeifyPath(filePath: string): string { const homeDir = homedir(); - if (path.startsWith(homeDir)) { - return path.replace(homeDir, '~'); + if (filePath === homeDir || filePath.startsWith(homeDir + path.sep)) { + return filePath.replace(homeDir, '~'); } - return path; + return filePath; } /**