Skip to content

Commit 4dd384b

Browse files
committed
Add collapse tilde tests, fix edge cases
1 parent 2b0eaed commit 4dd384b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/vs/platform/terminal/common/terminalEnvironment.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ export function collapseTildePath(path: string | undefined, userHome: string | u
2424
if (!userHome) {
2525
return path;
2626
}
27-
const normalizedPath = path.replace(/\\/g, '/\//').toLowerCase();
28-
const normalizedUserHome = userHome.replace(/\\/g, '/\//').toLowerCase();
27+
// Trim the trailing separator from the end if it exists
28+
if (userHome.match(/[\/\\]$/)) {
29+
userHome = userHome.slice(0, userHome.length - 1);
30+
}
31+
const normalizedPath = path.replace(/\\/g, '/').toLowerCase();
32+
const normalizedUserHome = userHome.replace(/\\/g, '/').toLowerCase();
2933
if (!normalizedPath.includes(normalizedUserHome)) {
3034
return path;
3135
}
32-
return `~${separator}${path.slice(userHome.length)}`;
36+
return `~${separator}${path.slice(userHome.length + 1)}`;
3337
}

src/vs/platform/terminal/test/common/terminalEnvironment.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ suite('terminalEnvironment', () => {
1818
});
1919
test('should not collapse when user home isn\'t present', () => {
2020
strictEqual(collapseTildePath('/foo', '/bar', '/'), '/foo');
21-
strictEqual(collapseTildePath('c:\\foo', 'C:\\bar', '\\'), 'C:\\foo');
21+
strictEqual(collapseTildePath('C:\\foo', 'C:\\bar', '\\'), 'C:\\foo');
2222
});
2323
test('should collapse with Windows separators', () => {
2424
strictEqual(collapseTildePath('C:\\foo\\bar', 'C:\\foo', '\\'), '~\\bar');
25+
strictEqual(collapseTildePath('C:\\foo\\bar', 'C:\\foo\\', '\\'), '~\\bar');
26+
strictEqual(collapseTildePath('C:\\foo\\bar\\baz', 'C:\\foo\\', '\\'), '~\\bar\\baz');
2527
strictEqual(collapseTildePath('C:\\foo\\bar\\baz', 'C:\\foo', '\\'), '~\\bar\\baz');
2628
});
29+
test('should collapse mixed case with Windows separators', () => {
30+
strictEqual(collapseTildePath('c:\\foo\\bar', 'C:\\foo', '\\'), '~\\bar');
31+
strictEqual(collapseTildePath('C:\\foo\\bar\\baz', 'c:\\foo', '\\'), '~\\bar\\baz');
32+
});
2733
test('should collapse with Posix separators', () => {
2834
strictEqual(collapseTildePath('/foo/bar', '/foo', '/'), '~/bar');
35+
strictEqual(collapseTildePath('/foo/bar', '/foo/', '/'), '~/bar');
2936
strictEqual(collapseTildePath('/foo/bar/baz', '/foo', '/'), '~/bar/baz');
37+
strictEqual(collapseTildePath('/foo/bar/baz', '/foo/', '/'), '~/bar/baz');
3038
});
3139
});
3240
});

0 commit comments

Comments
 (0)