Skip to content
Closed
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
25 changes: 25 additions & 0 deletions packages/core/src/utils/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
isSubpath,
shortenPath,
resolveToRealPath,
tildeifyPath,
homedir,
} from './paths.js';

vi.mock('node:fs', async (importOriginal) => {
Expand Down Expand Up @@ -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);
});
});
10 changes: 5 additions & 5 deletions packages/core/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Loading