forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmartSend.smoke.test.ts
More file actions
250 lines (222 loc) · 11.8 KB
/
smartSend.smoke.test.ts
File metadata and controls
250 lines (222 loc) · 11.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import * as vscode from 'vscode';
import * as path from 'path';
import { assert } from 'chai';
import * as fs from '../../client/common/platform/fs-paths';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants';
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
import { openFile, waitForCondition } from '../common';
suite('Smoke Test: Run Smart Selection and Advance Cursor', async () => {
suiteSetup(async function () {
if (!IS_SMOKE_TEST) {
return this.skip();
}
await initialize();
// Ensure the environments extension is not used for this test
// execSelectionInTerminal was not updated to use the envs extension API
// when execInTerminal was updated in commit e78934859
await vscode.workspace
.getConfiguration('python')
.update('useEnvironmentsExtension', false, vscode.ConfigurationTarget.Global);
return undefined;
});
setup(initializeTest);
suiteTeardown(closeActiveWindows);
teardown(closeActiveWindows);
test('Smart Send', async () => {
const file = path.join(
EXTENSION_ROOT_DIR_FOR_TESTS,
'src',
'testMultiRootWkspc',
'smokeTests',
'create_delete_file.py',
);
const outputFile = path.join(
EXTENSION_ROOT_DIR_FOR_TESTS,
'src',
'testMultiRootWkspc',
'smokeTests',
'smart_send_smoke.txt',
);
console.log(`[smartSend.smoke] Test starting`);
console.log(`[smartSend.smoke] Python file: ${file}`);
console.log(`[smartSend.smoke] Output file: ${outputFile}`);
console.log(`[smartSend.smoke] Python file exists: ${await fs.pathExists(file)}`);
const outputFileExistsBefore = await fs.pathExists(outputFile);
console.log(`[smartSend.smoke] Output file exists before cleanup: ${outputFileExistsBefore}`);
await fs.remove(outputFile);
console.log(`[smartSend.smoke] Output file removed`);
const textDocument = await openFile(file);
console.log(`[smartSend.smoke] File opened in editor`);
console.log(`[smartSend.smoke] Document has ${textDocument.lineCount} lines`);
console.log(`[smartSend.smoke] First 5 lines of file:`);
for (let i = 0; i < Math.min(5, textDocument.lineCount); i++) {
console.log(`[smartSend.smoke] Line ${i}: ${textDocument.lineAt(i).text}`);
}
if (vscode.window.activeTextEditor) {
// Select the entire file content to execute
// The implementation of execSelectionInTerminal with empty selection only runs the current line,
// not smart selection. So we need to select the actual code we want to execute.
const document = vscode.window.activeTextEditor.document;
const fullRange = new vscode.Range(
document.lineAt(0).range.start,
document.lineAt(document.lineCount - 1).range.end,
);
vscode.window.activeTextEditor.selection = new vscode.Selection(fullRange.start, fullRange.end);
const selectedText = vscode.window.activeTextEditor.document.getText(
vscode.window.activeTextEditor.selection,
);
console.log(
`[smartSend.smoke] Selected entire file (${selectedText.split('\n').length} lines, ${
selectedText.length
} chars)`,
);
// Wait a bit for the editor state to settle
console.log(`[smartSend.smoke] Waiting 500ms for editor state to settle...`);
await new Promise((resolve) => setTimeout(resolve, 500));
}
const terminalsBefore = vscode.window.terminals.length;
console.log(`[smartSend.smoke] Number of terminals before execution: ${terminalsBefore}`);
// On Windows, if terminals exist from previous tests, they may not be ready for new commands
// Give them time to fully initialize before sending commands
if (terminalsBefore > 0 && process.platform === 'win32') {
console.log(
`[smartSend.smoke] Windows detected with ${terminalsBefore} existing terminals, waiting 3s for terminal readiness...`,
);
await new Promise((resolve) => setTimeout(resolve, 3000));
}
// Verify the active editor is correct before executing command
if (vscode.window.activeTextEditor) {
console.log(
`[smartSend.smoke] Active editor before command: ${vscode.window.activeTextEditor.document.uri.fsPath}`,
);
console.log(
`[smartSend.smoke] Active editor language: ${vscode.window.activeTextEditor.document.languageId}`,
);
} else {
console.error(`[smartSend.smoke] ERROR: No active text editor before command!`);
}
const startTime = Date.now();
console.log(
`[smartSend.smoke] Executing first 'python.execSelectionInTerminal' command at ${new Date().toISOString()}`,
);
console.log(`[smartSend.smoke] Passing document URI to execSelectionInTerminal: ${textDocument.uri.fsPath}`);
await vscode.commands
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
.then(undefined, (err) => {
console.error(`[smartSend.smoke] First command failed: ${err}`);
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
});
const firstCmdTime = Date.now();
console.log(`[smartSend.smoke] First command completed in ${firstCmdTime - startTime}ms`);
// Check if smart selection changed the selection
if (vscode.window.activeTextEditor) {
const selectionAfterCmd = vscode.window.activeTextEditor.selection;
const selectedText = vscode.window.activeTextEditor.document.getText(selectionAfterCmd);
console.log(
`[smartSend.smoke] Selection after command - start: (${selectionAfterCmd.start.line}, ${selectionAfterCmd.start.character}), end: (${selectionAfterCmd.end.line}, ${selectionAfterCmd.end.character})`,
);
console.log(
`[smartSend.smoke] Selected text after command (first 100 chars): "${selectedText
.substring(0, 100)
.replace(/\n/g, '\\n')}"`,
);
console.log(
`[smartSend.smoke] Active editor document URI: ${vscode.window.activeTextEditor.document.uri.fsPath}`,
);
} else {
console.error(`[smartSend.smoke] WARNING: No active text editor after command execution!`);
}
const terminalsAfter = vscode.window.terminals.length;
console.log(`[smartSend.smoke] Number of terminals after first execution: ${terminalsAfter}`);
if (vscode.window.activeTerminal) {
console.log(`[smartSend.smoke] Active terminal name: ${vscode.window.activeTerminal.name}`);
}
// Add additional wait to allow terminal to start processing
// Windows may need more time for terminal to initialize and start executing
const isWindows = process.platform === 'win32';
const initialWaitTime = isWindows ? 2000 : 1000;
console.log(
`[smartSend.smoke] Waiting ${initialWaitTime}ms for terminal to start processing (isWindows: ${isWindows})...`,
);
await new Promise((resolve) => setTimeout(resolve, initialWaitTime));
// Verify the working directory matches expected
const expectedDir = path.dirname(outputFile);
console.log(`[smartSend.smoke] Expected output directory: ${expectedDir}`);
console.log(`[smartSend.smoke] Directory exists: ${await fs.pathExists(expectedDir)}`);
let checkCount = 0;
const checkIfFileHasBeenCreated = async () => {
checkCount++;
const exists = await fs.pathExists(outputFile);
if (checkCount % 100 === 0) {
// Log every 100 checks (~1 second)
const elapsed = Date.now() - startTime;
console.log(`[smartSend.smoke] File creation check #${checkCount} at ${elapsed}ms: ${exists}`);
}
return exists;
};
try {
await waitForCondition(checkIfFileHasBeenCreated, 20_000, `"${outputFile}" file not created`);
const createTime = Date.now() - startTime;
console.log(`[smartSend.smoke] SUCCESS: File created after ${createTime}ms (${checkCount} checks)`);
} catch (error) {
const totalTime = Date.now() - startTime;
console.error(`[smartSend.smoke] FAILURE: File not created after ${totalTime}ms (${checkCount} checks)`);
console.error(`[smartSend.smoke] Output file exists: ${await fs.pathExists(outputFile)}`);
console.error(`[smartSend.smoke] Number of terminals: ${vscode.window.terminals.length}`);
// Check final editor state
if (vscode.window.activeTextEditor) {
console.error(
`[smartSend.smoke] Final active editor: ${vscode.window.activeTextEditor.document.uri.fsPath}`,
);
const finalSelection = vscode.window.activeTextEditor.selection;
console.error(
`[smartSend.smoke] Final selection - start: (${finalSelection.start.line}, ${finalSelection.start.character}), end: (${finalSelection.end.line}, ${finalSelection.end.character})`,
);
}
// List directory contents
const dir = path.dirname(outputFile);
try {
const fsModule = await import('fs');
const files = fsModule.readdirSync(dir);
console.error(`[smartSend.smoke] Directory contents (${dir}):`, files);
} catch (e) {
console.error(`[smartSend.smoke] Failed to list directory: ${e}`);
}
throw error;
}
console.log(`[smartSend.smoke] Executing second 'python.execSelectionInTerminal' command`);
await vscode.commands
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
.then(undefined, (err) => {
console.error(`[smartSend.smoke] Second command failed: ${err}`);
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
});
console.log(`[smartSend.smoke] Second command completed`);
console.log(`[smartSend.smoke] Executing third 'python.execSelectionInTerminal' command`);
await vscode.commands
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
.then(undefined, (err) => {
console.error(`[smartSend.smoke] Third command failed: ${err}`);
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
});
console.log(`[smartSend.smoke] Third command completed`);
async function wait() {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 10000);
});
}
console.log(`[smartSend.smoke] Waiting 10s for file deletion to complete...`);
await wait();
const deletedFile = !(await fs.pathExists(outputFile));
console.log(`[smartSend.smoke] File exists after deletion commands: ${!deletedFile}`);
if (deletedFile) {
console.log(`[smartSend.smoke] SUCCESS: File has been deleted as expected`);
assert.ok(true, `"${outputFile}" file has been deleted`);
} else {
console.error(`[smartSend.smoke] FAILURE: File still exists`);
assert.fail(`"${outputFile}" file still exists`);
}
});
});