-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcompletion.test.default.ts
More file actions
166 lines (148 loc) · 5.27 KB
/
Copy pathcompletion.test.default.ts
File metadata and controls
166 lines (148 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//===----------------------------------------------------------------------===//
// Copyright (c) 2025, Modular Inc. All rights reserved.
//
// Licensed under the Apache License v2.0 with LLVM Exceptions:
// https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import * as assert from 'assert';
import * as fs from 'fs/promises';
import * as path from 'path';
import * as vscode from 'vscode';
import { extension } from '../extension';
const modularHome = path.join(process.env.HOME!, '.modular');
const modularConfig = path.join(modularHome, 'modular.cfg');
const modularPackageRoot = path.join(
modularHome,
'pkg',
'packages.modular.com_mojo',
);
const modularLsp = path.join(modularPackageRoot, 'bin', 'mojo-lsp-server');
async function pathExists(target: string): Promise<boolean> {
try {
await fs.lstat(target);
return true;
} catch {
return false;
}
}
function createDerivedConfig(): string {
return [
'[max]',
'version = 0.0.0-test',
'',
'[mojo-max]',
`lsp_server_path = ${path.join(modularPackageRoot, 'bin', 'mojo-lsp-server')}`,
`mblack_path = ${path.join(modularPackageRoot, 'lib', 'mblack', 'mblack')}`,
`lldb_plugin_path = ${path.join(modularPackageRoot, 'lib', 'libMojoLLDB.dylib')}`,
`lldb_vscode_path = ${path.join(modularPackageRoot, 'bin', 'lldb-dap')}`,
`driver_path = ${path.join(modularPackageRoot, 'bin', 'mojo')}`,
`lldb_visualizers_path = ${path.join(modularPackageRoot, 'lib', 'lldb-visualizers')}`,
`lldb_path = ${path.join(modularPackageRoot, 'bin', 'lldb')}`,
'',
].join('\n');
}
async function withTimeout<T>(
promise: PromiseLike<T>,
timeoutMs: number,
label: string,
): Promise<T> {
let timeoutHandle: NodeJS.Timeout | undefined;
const timeout = new Promise<never>((_, reject) => {
timeoutHandle = setTimeout(() => {
reject(new Error(`${label} timed out after ${timeoutMs}ms`));
}, timeoutMs);
});
try {
return await Promise.race([promise, timeout]);
} finally {
if (timeoutHandle) {
clearTimeout(timeoutHandle);
}
}
}
suite('Completion', function () {
test('trigger-character completion should work immediately after typing "."', async function () {
if (!(await pathExists(modularConfig)) || !(await pathExists(modularLsp))) {
this.skip();
return;
}
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
assert.ok(workspaceFolder, 'expected a workspace folder');
const workspaceRoot = workspaceFolder.uri.fsPath;
const derivedPath = path.join(workspaceRoot, '.derived');
let derivedExisted = false;
try {
const derivedStat = await fs.lstat(derivedPath);
derivedExisted = true;
if (!derivedStat.isDirectory()) {
await fs.rm(derivedPath, { recursive: true, force: true });
derivedExisted = false;
}
} catch {
derivedExisted = false;
}
const createdDerivedDir = !derivedExisted;
if (createdDerivedDir) {
await fs.mkdir(derivedPath, { recursive: true });
}
await fs.writeFile(
path.join(derivedPath, path.basename(modularConfig)),
createDerivedConfig(),
);
const testFile = vscode.Uri.file(
path.join(workspaceRoot, 'completion-trigger-race.test.mojo'),
);
try {
await vscode.workspace.fs.writeFile(
testFile,
Buffer.from('import math\n\nfn main():\n math'),
);
await vscode.commands.executeCommand('mojo.extension.restart');
const document = await vscode.workspace.openTextDocument(testFile);
const editor = await vscode.window.showTextDocument(document);
assert.strictEqual(document.languageId, 'mojo');
await withTimeout(
extension.lspManager!.tryStartLanguageClient(document),
30000,
'language client startup',
);
assert.strictEqual(
extension.lspManager!.lspClient?.name,
'Mojo Language Client',
);
const line = document.lineAt(document.lineCount - 1);
const cursor = line.range.end;
editor.selection = new vscode.Selection(cursor, cursor);
await vscode.commands.executeCommand('default:type', { text: '.' });
const completion = await withTimeout(
vscode.commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
document.uri,
editor.selection.active,
'.',
),
30000,
'trigger-character completion request',
);
assert.ok(completion, 'expected a completion result');
assert.ok(
completion.items.length > 0,
'expected trigger-character completion items after typing "."',
);
} finally {
await vscode.commands.executeCommand(
'workbench.action.closeActiveEditor',
);
await vscode.workspace.fs.delete(testFile, { useTrash: false });
if (createdDerivedDir) {
await fs.rm(derivedPath, { recursive: true, force: true });
}
}
});
});