summaryrefslogtreecommitdiff
path: root/packages/core/src/utils/paths.test.ts
diff options
context:
space:
mode:
authorchristine betts <[email protected]>2025-08-14 20:12:57 +0000
committerGitHub <[email protected]>2025-08-14 20:12:57 +0000
commit5c5fc89eb16afb65a5bbcb30e3bc576ed55e66b8 (patch)
treebf83a82c461085cf06d1082b251eadbbd6091ae1 /packages/core/src/utils/paths.test.ts
parente06d774996c0f09e1881201dca278720af2bf5b5 (diff)
[ide-mode] Support multi-folder workspaces (#6177)
Diffstat (limited to 'packages/core/src/utils/paths.test.ts')
-rw-r--r--packages/core/src/utils/paths.test.ts106
1 files changed, 104 insertions, 2 deletions
diff --git a/packages/core/src/utils/paths.test.ts b/packages/core/src/utils/paths.test.ts
index d688c072..0e964672 100644
--- a/packages/core/src/utils/paths.test.ts
+++ b/packages/core/src/utils/paths.test.ts
@@ -4,8 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import { describe, it, expect } from 'vitest';
-import { escapePath, unescapePath } from './paths.js';
+import { describe, it, expect, beforeAll, afterAll } from 'vitest';
+import { escapePath, unescapePath, isSubpath } from './paths.js';
describe('escapePath', () => {
it('should escape spaces', () => {
@@ -212,3 +212,105 @@ describe('unescapePath', () => {
expect(unescapePath('file\\\\\\(test\\).txt')).toBe('file\\\\(test).txt');
});
});
+
+describe('isSubpath', () => {
+ it('should return true for a direct subpath', () => {
+ expect(isSubpath('/a/b', '/a/b/c')).toBe(true);
+ });
+
+ it('should return true for the same path', () => {
+ expect(isSubpath('/a/b', '/a/b')).toBe(true);
+ });
+
+ it('should return false for a parent path', () => {
+ expect(isSubpath('/a/b/c', '/a/b')).toBe(false);
+ });
+
+ it('should return false for a completely different path', () => {
+ expect(isSubpath('/a/b', '/x/y')).toBe(false);
+ });
+
+ it('should handle relative paths', () => {
+ expect(isSubpath('a/b', 'a/b/c')).toBe(true);
+ expect(isSubpath('a/b', 'a/c')).toBe(false);
+ });
+
+ it('should handle paths with ..', () => {
+ expect(isSubpath('/a/b', '/a/b/../b/c')).toBe(true);
+ expect(isSubpath('/a/b', '/a/c/../b')).toBe(true);
+ });
+
+ it('should handle root paths', () => {
+ expect(isSubpath('/', '/a')).toBe(true);
+ expect(isSubpath('/a', '/')).toBe(false);
+ });
+
+ it('should handle trailing slashes', () => {
+ expect(isSubpath('/a/b/', '/a/b/c')).toBe(true);
+ expect(isSubpath('/a/b', '/a/b/c/')).toBe(true);
+ expect(isSubpath('/a/b/', '/a/b/c/')).toBe(true);
+ });
+});
+
+describe('isSubpath on Windows', () => {
+ const originalPlatform = process.platform;
+
+ beforeAll(() => {
+ Object.defineProperty(process, 'platform', {
+ value: 'win32',
+ });
+ });
+
+ afterAll(() => {
+ Object.defineProperty(process, 'platform', {
+ value: originalPlatform,
+ });
+ });
+
+ it('should return true for a direct subpath on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test', 'C:\\Users\\Test\\file.txt')).toBe(
+ true,
+ );
+ });
+
+ it('should return true for the same path on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test', 'C:\\Users\\Test')).toBe(true);
+ });
+
+ it('should return false for a parent path on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test\\file.txt', 'C:\\Users\\Test')).toBe(
+ false,
+ );
+ });
+
+ it('should return false for a different drive on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test', 'D:\\Users\\Test')).toBe(false);
+ });
+
+ it('should be case-insensitive for drive letters on Windows', () => {
+ expect(isSubpath('c:\\Users\\Test', 'C:\\Users\\Test\\file.txt')).toBe(
+ true,
+ );
+ });
+
+ it('should be case-insensitive for path components on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test', 'c:\\users\\test\\file.txt')).toBe(
+ true,
+ );
+ });
+
+ it('should handle mixed slashes on Windows', () => {
+ expect(isSubpath('C:/Users/Test', 'C:\\Users\\Test\\file.txt')).toBe(true);
+ });
+
+ it('should handle trailing slashes on Windows', () => {
+ expect(isSubpath('C:\\Users\\Test\\', 'C:\\Users\\Test\\file.txt')).toBe(
+ true,
+ );
+ });
+
+ it('should handle relative paths correctly on Windows', () => {
+ expect(isSubpath('Users\\Test', 'Users\\Test\\file.txt')).toBe(true);
+ expect(isSubpath('Users\\Test\\file.txt', 'Users\\Test')).toBe(false);
+ });
+});