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
40 changes: 40 additions & 0 deletions extensions/terminal-suggest/src/fig/shared/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
longestCommonPrefix,
compareNamedObjectsAlphabetically,
fieldsAreEqual,
getCWDForFilesAndFolders,
} from '../utils';

function expect<T>(a: T): { toEqual: (b: T) => void } {
Expand Down Expand Up @@ -141,3 +142,42 @@ suite('fig/shared/ compareNamedObjectsAlphabetically', () => {
ok(compareNamedObjectsAlphabetically({ name: ['c'] }, { name: ['x'] }) < 0);
});
});

suite('fig/shared/ getCWDForFilesAndFolders', () => {
test('should return / when cwd is null', () => {
expect(getCWDForFilesAndFolders(null, 'foo')).toEqual('/');
});

test('should return cwd with trailing slash when dirname is empty', () => {
expect(getCWDForFilesAndFolders('/home/user', 'foo')).toEqual('/home/user/');
expect(getCWDForFilesAndFolders('/home/user/', 'bar')).toEqual('/home/user/');
});

test('should normalize relative parent directory paths', () => {
expect(getCWDForFilesAndFolders('/home/user/project', '../')).toEqual('/home/user/');
expect(getCWDForFilesAndFolders('/home/user/project', '../sibling')).toEqual('/home/user/');
});

test('should normalize relative current directory paths', () => {
expect(getCWDForFilesAndFolders('/home/user/project', './')).toEqual('/home/user/project/');
expect(getCWDForFilesAndFolders('/home/user/project', './child')).toEqual('/home/user/project/');
});

test('should handle child directories', () => {
expect(getCWDForFilesAndFolders('/home/user/project', 'child/')).toEqual('/home/user/project/child/');
expect(getCWDForFilesAndFolders('/home/user/project', 'child/subdir/')).toEqual('/home/user/project/child/subdir/');
});

test('should handle absolute paths', () => {
expect(getCWDForFilesAndFolders('/home/user/project', '/absolute/path/')).toEqual('/absolute/path/');
});

test('should handle home directory paths', () => {
expect(getCWDForFilesAndFolders('/home/user/project', '~/documents/')).toEqual('~/documents/');
});

test('should normalize complex relative paths', () => {
expect(getCWDForFilesAndFolders('/home/user/project', '../other/../final/')).toEqual('/home/final/');
expect(getCWDForFilesAndFolders('/home/user/project', './child/../sibling/')).toEqual('/home/user/project/sibling/');
});
});
6 changes: 5 additions & 1 deletion extensions/terminal-suggest/src/fig/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as path from 'path';
import { osIsWindows } from '../../helpers/os.js';
import { createErrorInstance } from './errors.js';

Expand Down Expand Up @@ -216,9 +217,12 @@ export const getCWDForFilesAndFolders = (
return ensureTrailingSlash(cwd);
}

return dirname.startsWith('~/') || dirname.startsWith('/')
const result = dirname.startsWith('~/') || dirname.startsWith('/')
? dirname
: `${cwd}/${dirname}`;

// Normalize the path to resolve '..' and '.' components
return ensureTrailingSlash(path.normalize(result));
};

export function localProtocol(domain: string, path: string) {
Expand Down