|
| 1 | +import { InitializeParams, WorkspaceFolder } from '@aws/language-server-runtimes/protocol' |
| 2 | +import { URI } from 'vscode-uri' |
| 3 | +import * as path from 'path' |
| 4 | +import assert = require('assert') |
| 5 | +import sinon = require('sinon') |
| 6 | +import { TestFeatures } from '@aws/language-server-runtimes/testing' |
| 7 | +import { Features } from '@aws/language-server-runtimes/server-interface/server' |
| 8 | +import { getWorkspaceFolders } from './initializeUtils' |
| 9 | + |
| 10 | +describe('initializeUtils', () => { |
| 11 | + let logging: Features['logging'] |
| 12 | + |
| 13 | + before(function () { |
| 14 | + logging = new TestFeatures().logging |
| 15 | + }) |
| 16 | + |
| 17 | + describe('getWorkspaceFolders', () => { |
| 18 | + const sampleWorkspaceUri = 'file:///path/to/folder' |
| 19 | + const sampleWorkspaceName = 'folder' |
| 20 | + const sampleWorkspaceFolder: WorkspaceFolder = { |
| 21 | + name: sampleWorkspaceName, |
| 22 | + uri: sampleWorkspaceUri, |
| 23 | + } |
| 24 | + |
| 25 | + const createParams = (params: Partial<InitializeParams>) => params as InitializeParams |
| 26 | + |
| 27 | + it('should return workspaceFolders when provided', () => { |
| 28 | + const workspaceFolders: WorkspaceFolder[] = [ |
| 29 | + sampleWorkspaceFolder, |
| 30 | + { name: 'folder2', uri: 'file:///path/to/folder2' }, |
| 31 | + ] |
| 32 | + const params = createParams({ workspaceFolders }) |
| 33 | + const result = getWorkspaceFolders(logging, params) |
| 34 | + |
| 35 | + assert.deepStrictEqual(result, workspaceFolders) |
| 36 | + }) |
| 37 | + |
| 38 | + describe('should create workspace folder from rootUri when workspaceFolders is not provided', () => { |
| 39 | + const invalidWorkspaceFolderCases = [ |
| 40 | + ['no workspaceFolder param', { rootUri: sampleWorkspaceUri }], |
| 41 | + ['empty workspaceFolder param params', { WorkspaceFolders: [], rootUri: sampleWorkspaceUri }], |
| 42 | + ] as const |
| 43 | + |
| 44 | + invalidWorkspaceFolderCases.forEach(([name, input]) => { |
| 45 | + it(`should return root uri for ${name}`, () => { |
| 46 | + const params = createParams(input) |
| 47 | + const result = getWorkspaceFolders(logging, params) |
| 48 | + assert.deepStrictEqual(result, [sampleWorkspaceFolder]) |
| 49 | + }) |
| 50 | + }) |
| 51 | + const params = createParams({ rootUri: sampleWorkspaceUri }) |
| 52 | + const result = getWorkspaceFolders(logging, params) |
| 53 | + |
| 54 | + assert.deepStrictEqual(result, [sampleWorkspaceFolder]) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should create workspace folder from rootPath when neither workspaceFolders nor rootUri is provided', () => { |
| 58 | + const rootPath = '/path/to/folder' |
| 59 | + const params = createParams({ rootPath: rootPath }) |
| 60 | + const result = getWorkspaceFolders(logging, params) |
| 61 | + |
| 62 | + assert.deepStrictEqual(result, [sampleWorkspaceFolder]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should use uri as folder name when URI basename is empty', () => { |
| 66 | + const rootUri = 'file:///' |
| 67 | + const params = createParams({ rootUri }) |
| 68 | + const result = getWorkspaceFolders(logging, params) |
| 69 | + |
| 70 | + assert.deepStrictEqual(result, [{ name: rootUri, uri: rootUri }]) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should handle Windows paths correctly', () => { |
| 74 | + const rootPath = 'C:\\Users\\test\\folder' |
| 75 | + const pathUri = URI.parse(rootPath).toString() |
| 76 | + const params = createParams({ rootPath }) |
| 77 | + |
| 78 | + const result = getWorkspaceFolders(logging, params) |
| 79 | + |
| 80 | + const expectedName = path.basename(URI.parse(pathUri).fsPath) |
| 81 | + assert.deepStrictEqual(result, [{ name: expectedName, uri: pathUri }]) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should handle rootUri with special characters', () => { |
| 85 | + const rootUri = 'file:///path/to/special%20project' |
| 86 | + const decodedPath = URI.parse(rootUri).path |
| 87 | + const folderName = path.basename(decodedPath) |
| 88 | + |
| 89 | + const params = createParams({ rootUri }) |
| 90 | + const result = getWorkspaceFolders(logging, params) |
| 91 | + |
| 92 | + assert.deepStrictEqual(result, [{ name: folderName, uri: rootUri }]) |
| 93 | + assert.equal('special project', result[0].name) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('should return empty workspaceFolder array', () => { |
| 97 | + const emptyArrayCases = [ |
| 98 | + ['no params', {}], |
| 99 | + ['undefined params', undefined as unknown as InitializeParams], |
| 100 | + ['null params', null as unknown as InitializeParams], |
| 101 | + ['empty workspaceFolders', { workspaceFolders: [] }], |
| 102 | + ] as const |
| 103 | + |
| 104 | + emptyArrayCases.forEach(([name, input]) => { |
| 105 | + it(`should return empty array for ${name}`, () => { |
| 106 | + const result = getWorkspaceFolders(logging, input as InitializeParams) |
| 107 | + assert.equal(result.length, 0) |
| 108 | + }) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should handle errors and return empty array', () => { |
| 113 | + const basenameStub = sinon.stub(path, 'basename').throws(new Error('Test error')) |
| 114 | + |
| 115 | + try { |
| 116 | + const badParams = createParams({ rootUri: sampleWorkspaceUri }) |
| 117 | + const result = getWorkspaceFolders(logging, badParams) |
| 118 | + |
| 119 | + assert.deepStrictEqual(result, []) |
| 120 | + } finally { |
| 121 | + basenameStub.restore() |
| 122 | + } |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments