|
| 1 | +/** |
| 2 | + * Unit tests for utility functions in util.ts |
| 3 | + */ |
| 4 | + |
1 | 5 | import * as util from '@cmt/util'; |
| 6 | +import * as vscode from 'vscode'; |
2 | 7 | import { expect } from '@test/util'; |
| 8 | +import * as path from 'path'; |
| 9 | +import * as sinon from 'sinon'; |
3 | 10 |
|
4 | 11 | suite('Utils test', () => { |
5 | 12 | test('Split path into elements', () => { |
@@ -29,3 +36,132 @@ suite('Utils test', () => { |
29 | 36 | } |
30 | 37 | }); |
31 | 38 | }); |
| 39 | + |
| 40 | +// Shared test helper for creating mock workspace folders |
| 41 | +function createMockWorkspaceFolder(fsPath: string, name: string): vscode.WorkspaceFolder { |
| 42 | + return { |
| 43 | + uri: vscode.Uri.file(fsPath), |
| 44 | + name: name, |
| 45 | + index: 0 |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +// Test base path that works on both Windows and Unix |
| 50 | +const testBasePath = process.platform === 'win32' ? 'C:\\Projects\\MyProject' : '/home/user/projects/myproject'; |
| 51 | + |
| 52 | +suite('expandExcludePath tests', () => { |
| 53 | + // Helper to get expected path using the same normalization as the code under test |
| 54 | + // The expandExcludePath function uses lightNormalizePath which converts backslashes to forward slashes |
| 55 | + // and vscode.Uri.file().fsPath may return lowercase drive letters on Windows |
| 56 | + function getExpectedPath(...segments: string[]): string { |
| 57 | + const folder = createMockWorkspaceFolder(segments[0], 'test'); |
| 58 | + return util.lightNormalizePath(path.join(...segments.map(s => s === segments[0] ? folder.uri.fsPath : s))); |
| 59 | + } |
| 60 | + |
| 61 | + test('Expand ${workspaceFolder} variable', () => { |
| 62 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 63 | + const result = util.expandExcludePath('${workspaceFolder}/subdir', folder); |
| 64 | + const expected = getExpectedPath(testBasePath, 'subdir'); |
| 65 | + expect(result).to.eq(expected); |
| 66 | + }); |
| 67 | + |
| 68 | + test('Expand multiple ${workspaceFolder} variables', () => { |
| 69 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 70 | + const result = util.expandExcludePath('${workspaceFolder}/foo/${workspaceFolder}/bar', folder); |
| 71 | + // For this case, we need to manually construct the expected path since it contains testBasePath twice |
| 72 | + const folderPath = folder.uri.fsPath; |
| 73 | + const expected = util.lightNormalizePath(path.join(folderPath, 'foo', folderPath, 'bar')); |
| 74 | + expect(result).to.eq(expected); |
| 75 | + }); |
| 76 | + |
| 77 | + test('Resolve relative path', () => { |
| 78 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 79 | + const result = util.expandExcludePath('subdir/nested', folder); |
| 80 | + const expected = getExpectedPath(testBasePath, 'subdir', 'nested'); |
| 81 | + expect(result).to.eq(expected); |
| 82 | + }); |
| 83 | + |
| 84 | + test('Absolute path remains unchanged', () => { |
| 85 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 86 | + const absolutePath = process.platform === 'win32' ? 'D:\\Other\\Path' : '/other/path'; |
| 87 | + const result = util.expandExcludePath(absolutePath, folder); |
| 88 | + const expected = util.lightNormalizePath(absolutePath); |
| 89 | + expect(result).to.eq(expected); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Expand ${workspaceFolder} and resolve relative path', () => { |
| 93 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 94 | + const result = util.expandExcludePath('${workspaceFolder}/../other', folder); |
| 95 | + const folderPath = folder.uri.fsPath; |
| 96 | + const expected = util.lightNormalizePath(path.join(folderPath, '..', 'other')); |
| 97 | + expect(result).to.eq(expected); |
| 98 | + }); |
| 99 | + |
| 100 | + test('Expand ${workspaceFolder:name} when named folder exists', () => { |
| 101 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 102 | + const otherBasePath = process.platform === 'win32' ? 'C:\\Projects\\OtherProject' : '/home/user/projects/otherproject'; |
| 103 | + const otherFolder = createMockWorkspaceFolder(otherBasePath, 'OtherProject'); |
| 104 | + |
| 105 | + const stub = sinon.stub(vscode.workspace, 'workspaceFolders').value([folder, otherFolder]); |
| 106 | + try { |
| 107 | + const result = util.expandExcludePath('${workspaceFolder:OtherProject}/subdir', folder); |
| 108 | + const expected = util.lightNormalizePath(path.join(otherFolder.uri.fsPath, 'subdir')); |
| 109 | + expect(result).to.eq(expected); |
| 110 | + } finally { |
| 111 | + stub.restore(); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + test('Expand ${workspaceFolder:name} is case-insensitive', () => { |
| 116 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 117 | + const otherBasePath = process.platform === 'win32' ? 'C:\\Projects\\OtherProject' : '/home/user/projects/otherproject'; |
| 118 | + const otherFolder = createMockWorkspaceFolder(otherBasePath, 'OtherProject'); |
| 119 | + |
| 120 | + const stub = sinon.stub(vscode.workspace, 'workspaceFolders').value([folder, otherFolder]); |
| 121 | + try { |
| 122 | + const result = util.expandExcludePath('${workspaceFolder:otherproject}/subdir', folder); |
| 123 | + const expected = util.lightNormalizePath(path.join(otherFolder.uri.fsPath, 'subdir')); |
| 124 | + expect(result).to.eq(expected); |
| 125 | + } finally { |
| 126 | + stub.restore(); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + test('${workspaceFolder:name} fallback when folder name not found', () => { |
| 131 | + // When ${workspaceFolder:name} references a folder that doesn't exist in the workspace, |
| 132 | + // the variable should be left unchanged and then resolved as a relative path |
| 133 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 134 | + const input = '${workspaceFolder:NonExistentFolder}/subdir'; |
| 135 | + const result = util.expandExcludePath(input, folder); |
| 136 | + // If vscode.workspace.workspaceFolders is empty or undefined, or if the folder is not found, |
| 137 | + // the ${workspaceFolder:NonExistentFolder} variable is left as-is |
| 138 | + // Then resolvePath treats it as a relative path segment |
| 139 | + const folderPath = folder.uri.fsPath; |
| 140 | + const expectedPath = util.lightNormalizePath(path.join(folderPath, '${workspaceFolder:NonExistentFolder}', 'subdir')); |
| 141 | + expect(result).to.eq(expectedPath); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +suite('expandExcludePaths tests', () => { |
| 146 | + test('Expand multiple paths', () => { |
| 147 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 148 | + const paths = [ |
| 149 | + '${workspaceFolder}/subdir1', |
| 150 | + 'relative/path', |
| 151 | + process.platform === 'win32' ? 'D:\\Absolute\\Path' : '/absolute/path' |
| 152 | + ]; |
| 153 | + const results = util.expandExcludePaths(paths, folder); |
| 154 | + const folderPath = folder.uri.fsPath; |
| 155 | + |
| 156 | + expect(results).to.have.lengthOf(3); |
| 157 | + expect(results[0]).to.eq(util.lightNormalizePath(path.join(folderPath, 'subdir1'))); |
| 158 | + expect(results[1]).to.eq(util.lightNormalizePath(path.join(folderPath, 'relative', 'path'))); |
| 159 | + expect(results[2]).to.eq(util.lightNormalizePath(process.platform === 'win32' ? 'D:\\Absolute\\Path' : '/absolute/path')); |
| 160 | + }); |
| 161 | + |
| 162 | + test('Empty array returns empty array', () => { |
| 163 | + const folder = createMockWorkspaceFolder(testBasePath, 'MyProject'); |
| 164 | + const results = util.expandExcludePaths([], folder); |
| 165 | + expect(results).to.have.lengthOf(0); |
| 166 | + }); |
| 167 | +}); |
0 commit comments