Skip to content

Commit 1688b7e

Browse files
committed
attempt to fix flaky smoke tests
1 parent 8989323 commit 1688b7e

File tree

5 files changed

+223
-14
lines changed

5 files changed

+223
-14
lines changed

src/test/common.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,20 +459,39 @@ export async function waitForCondition(
459459
errorMessage: string,
460460
): Promise<void> {
461461
return new Promise<void>(async (resolve, reject) => {
462+
const startTime = Date.now();
463+
let checkCount = 0;
462464
const timeout = setTimeout(() => {
463465
clearTimeout(timeout);
464466

465467
// eslint-disable-next-line @typescript-eslint/no-use-before-define
466-
clearTimeout(timer);
467-
reject(new Error(errorMessage));
468+
clearInterval(timer);
469+
const elapsed = Date.now() - startTime;
470+
const detailedError = `${errorMessage} (waited ${elapsed}ms, checked ${checkCount} times)`;
471+
console.error(`[waitForCondition] Timeout: ${detailedError}`);
472+
reject(new Error(detailedError));
468473
}, timeoutMs);
469474
const timer = setInterval(async () => {
470-
if (!(await condition().catch(() => false))) {
471-
return;
475+
checkCount++;
476+
try {
477+
const result = await condition();
478+
if (!result) {
479+
return;
480+
}
481+
clearTimeout(timeout);
482+
clearInterval(timer);
483+
const elapsed = Date.now() - startTime;
484+
if (IS_SMOKE_TEST) {
485+
console.log(`[waitForCondition] Condition met after ${elapsed}ms (${checkCount} checks)`);
486+
}
487+
resolve();
488+
} catch (error) {
489+
// Condition check threw an error, log it but continue checking
490+
if (checkCount % 100 === 0) {
491+
// Log every 100 checks
492+
console.error(`[waitForCondition] Error checking condition (check #${checkCount}): ${error}`);
493+
}
472494
}
473-
clearTimeout(timeout);
474-
clearTimeout(timer);
475-
resolve();
476495
}, 10);
477496
});
478497
}

src/test/smoke/runInTerminal.smoke.test.ts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,92 @@ suite('Smoke Test: Run Python File In Terminal', () => {
4343
'smokeTests',
4444
'testExecInTerminal.log',
4545
);
46+
47+
console.log(`[runInTerminal.smoke] Test starting`);
48+
console.log(`[runInTerminal.smoke] Python file: ${file}`);
49+
console.log(`[runInTerminal.smoke] Output file: ${outputFile}`);
50+
console.log(`[runInTerminal.smoke] Python file exists: ${await fs.pathExists(file)}`);
51+
4652
if (await fs.pathExists(outputFile)) {
53+
console.log(`[runInTerminal.smoke] Output file already exists, deleting...`);
4754
await fs.unlink(outputFile);
55+
console.log(`[runInTerminal.smoke] Output file deleted`);
56+
} else {
57+
console.log(`[runInTerminal.smoke] Output file does not exist (clean state)`);
4858
}
59+
4960
const textDocument = await openFile(file);
61+
console.log(`[runInTerminal.smoke] File opened in editor`);
62+
63+
// Check active terminals before execution
64+
const terminalsBefore = vscode.window.terminals.length;
65+
console.log(`[runInTerminal.smoke] Number of terminals before execution: ${terminalsBefore}`);
66+
67+
const startTime = Date.now();
68+
console.log(`[runInTerminal.smoke] Executing 'python.execInTerminal' command at ${new Date().toISOString()}`);
5069

5170
await vscode.commands.executeCommand<void>('python.execInTerminal', textDocument.uri).then(undefined, (err) => {
71+
console.error(`[runInTerminal.smoke] Command failed with error: ${err}`);
5272
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
5373
});
54-
const checkIfFileHasBeenCreated = () => fs.pathExists(outputFile);
55-
await waitForCondition(checkIfFileHasBeenCreated, 30_000, `"${outputFile}" file not created`);
74+
75+
const commandCompleteTime = Date.now();
76+
console.log(`[runInTerminal.smoke] Command completed in ${commandCompleteTime - startTime}ms`);
77+
78+
// Check active terminals after execution
79+
const terminalsAfter = vscode.window.terminals.length;
80+
console.log(`[runInTerminal.smoke] Number of terminals after execution: ${terminalsAfter}`);
81+
if (vscode.window.activeTerminal) {
82+
console.log(`[runInTerminal.smoke] Active terminal name: ${vscode.window.activeTerminal.name}`);
83+
}
84+
85+
// Add additional wait to allow terminal to start processing
86+
// Windows may need more time for terminal to initialize and start executing
87+
const isWindows = process.platform === 'win32';
88+
const initialWaitTime = isWindows ? 2000 : 1000;
89+
console.log(
90+
`[runInTerminal.smoke] Waiting ${initialWaitTime}ms for terminal to start processing (isWindows: ${isWindows})...`,
91+
);
92+
await new Promise((resolve) => setTimeout(resolve, initialWaitTime));
93+
94+
// Verify the working directory matches expected
95+
const expectedDir = path.dirname(outputFile);
96+
console.log(`[runInTerminal.smoke] Expected output directory: ${expectedDir}`);
97+
console.log(`[runInTerminal.smoke] Directory exists: ${await fs.pathExists(expectedDir)}`);
98+
99+
let checkCount = 0;
100+
const checkIfFileHasBeenCreated = async () => {
101+
checkCount++;
102+
const exists = await fs.pathExists(outputFile);
103+
if (checkCount % 100 === 0) {
104+
// Log every 100 checks (~1 second)
105+
const elapsed = Date.now() - startTime;
106+
console.log(`[runInTerminal.smoke] File check #${checkCount} at ${elapsed}ms: ${exists}`);
107+
}
108+
return exists;
109+
};
110+
111+
try {
112+
await waitForCondition(checkIfFileHasBeenCreated, 30_000, `"${outputFile}" file not created`);
113+
const totalTime = Date.now() - startTime;
114+
console.log(`[runInTerminal.smoke] SUCCESS: File created after ${totalTime}ms (${checkCount} checks)`);
115+
} catch (error) {
116+
const totalTime = Date.now() - startTime;
117+
console.error(`[runInTerminal.smoke] FAILURE after ${totalTime}ms (${checkCount} checks)`);
118+
console.error(`[runInTerminal.smoke] Output file exists: ${await fs.pathExists(outputFile)}`);
119+
console.error(`[runInTerminal.smoke] Number of terminals: ${vscode.window.terminals.length}`);
120+
121+
// List directory contents to see if file is there
122+
const dir = path.dirname(outputFile);
123+
try {
124+
const fsModule = await import('fs');
125+
const files = fsModule.readdirSync(dir);
126+
console.error(`[runInTerminal.smoke] Directory contents (${dir}):`, files);
127+
} catch (e) {
128+
console.error(`[runInTerminal.smoke] Failed to list directory: ${e}`);
129+
}
130+
131+
throw error;
132+
}
56133
});
57134
});

src/test/smoke/smartSend.smoke.test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,110 @@ suite('Smoke Test: Run Smart Selection and Advance Cursor', async () => {
3535
'smart_send_smoke.txt',
3636
);
3737

38+
console.log(`[smartSend.smoke] Test starting`);
39+
console.log(`[smartSend.smoke] Python file: ${file}`);
40+
console.log(`[smartSend.smoke] Output file: ${outputFile}`);
41+
console.log(`[smartSend.smoke] Python file exists: ${await fs.pathExists(file)}`);
42+
43+
const outputFileExistsBefore = await fs.pathExists(outputFile);
44+
console.log(`[smartSend.smoke] Output file exists before cleanup: ${outputFileExistsBefore}`);
3845
await fs.remove(outputFile);
46+
console.log(`[smartSend.smoke] Output file removed`);
3947

4048
const textDocument = await openFile(file);
49+
console.log(`[smartSend.smoke] File opened in editor`);
4150

4251
if (vscode.window.activeTextEditor) {
4352
const myPos = new vscode.Position(0, 0);
4453
vscode.window.activeTextEditor!.selections = [new vscode.Selection(myPos, myPos)];
54+
console.log(`[smartSend.smoke] Cursor set to position (0, 0)`);
4555
}
56+
57+
const terminalsBefore = vscode.window.terminals.length;
58+
console.log(`[smartSend.smoke] Number of terminals before execution: ${terminalsBefore}`);
59+
60+
const startTime = Date.now();
61+
console.log(`[smartSend.smoke] Executing first 'python.execSelectionInTerminal' command at ${new Date().toISOString()}`);
62+
4663
await vscode.commands
4764
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
4865
.then(undefined, (err) => {
66+
console.error(`[smartSend.smoke] First command failed: ${err}`);
4967
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
5068
});
5169

52-
const checkIfFileHasBeenCreated = () => fs.pathExists(outputFile);
53-
await waitForCondition(checkIfFileHasBeenCreated, 20_000, `"${outputFile}" file not created`);
70+
const firstCmdTime = Date.now();
71+
console.log(`[smartSend.smoke] First command completed in ${firstCmdTime - startTime}ms`);
72+
73+
const terminalsAfter = vscode.window.terminals.length;
74+
console.log(`[smartSend.smoke] Number of terminals after first execution: ${terminalsAfter}`);
75+
if (vscode.window.activeTerminal) {
76+
console.log(`[smartSend.smoke] Active terminal name: ${vscode.window.activeTerminal.name}`);
77+
}
78+
79+
// Add additional wait to allow terminal to start processing
80+
// Windows may need more time for terminal to initialize and start executing
81+
const isWindows = process.platform === 'win32';
82+
const initialWaitTime = isWindows ? 2000 : 1000;
83+
console.log(`[smartSend.smoke] Waiting ${initialWaitTime}ms for terminal to start processing (isWindows: ${isWindows})...`);
84+
await new Promise(resolve => setTimeout(resolve, initialWaitTime));
5485

86+
// Verify the working directory matches expected
87+
const expectedDir = path.dirname(outputFile);
88+
console.log(`[smartSend.smoke] Expected output directory: ${expectedDir}`);
89+
console.log(`[smartSend.smoke] Directory exists: ${await fs.pathExists(expectedDir)}`);
90+
91+
let checkCount = 0;
92+
const checkIfFileHasBeenCreated = async () => {
93+
checkCount++;
94+
const exists = await fs.pathExists(outputFile);
95+
if (checkCount % 100 === 0) { // Log every 100 checks (~1 second)
96+
const elapsed = Date.now() - startTime;
97+
console.log(`[smartSend.smoke] File creation check #${checkCount} at ${elapsed}ms: ${exists}`);
98+
}
99+
return exists;
100+
};
101+
102+
try {
103+
await waitForCondition(checkIfFileHasBeenCreated, 20_000, `"${outputFile}" file not created`);
104+
const createTime = Date.now() - startTime;
105+
console.log(`[smartSend.smoke] SUCCESS: File created after ${createTime}ms (${checkCount} checks)`);
106+
} catch (error) {
107+
const totalTime = Date.now() - startTime;
108+
console.error(`[smartSend.smoke] FAILURE: File not created after ${totalTime}ms (${checkCount} checks)`);
109+
console.error(`[smartSend.smoke] Output file exists: ${await fs.pathExists(outputFile)}`);
110+
console.error(`[smartSend.smoke] Number of terminals: ${vscode.window.terminals.length}`);
111+
112+
// List directory contents
113+
const dir = path.dirname(outputFile);
114+
try {
115+
const fsModule = await import('fs');
116+
const files = fsModule.readdirSync(dir);
117+
console.error(`[smartSend.smoke] Directory contents (${dir}):`, files);
118+
} catch (e) {
119+
console.error(`[smartSend.smoke] Failed to list directory: ${e}`);
120+
}
121+
122+
throw error;
123+
}
124+
125+
console.log(`[smartSend.smoke] Executing second 'python.execSelectionInTerminal' command`);
55126
await vscode.commands
56127
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
57128
.then(undefined, (err) => {
129+
console.error(`[smartSend.smoke] Second command failed: ${err}`);
58130
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
59131
});
132+
console.log(`[smartSend.smoke] Second command completed`);
133+
134+
console.log(`[smartSend.smoke] Executing third 'python.execSelectionInTerminal' command`);
60135
await vscode.commands
61136
.executeCommand<void>('python.execSelectionInTerminal', textDocument.uri)
62137
.then(undefined, (err) => {
138+
console.error(`[smartSend.smoke] Third command failed: ${err}`);
63139
assert.fail(`Something went wrong running the Python file in the terminal: ${err}`);
64140
});
141+
console.log(`[smartSend.smoke] Third command completed`);
65142

66143
async function wait() {
67144
return new Promise<void>((resolve) => {
@@ -71,12 +148,16 @@ suite('Smoke Test: Run Smart Selection and Advance Cursor', async () => {
71148
});
72149
}
73150

151+
console.log(`[smartSend.smoke] Waiting 10s for file deletion to complete...`);
74152
await wait();
75153

76154
const deletedFile = !(await fs.pathExists(outputFile));
155+
console.log(`[smartSend.smoke] File exists after deletion commands: ${!deletedFile}`);
77156
if (deletedFile) {
157+
console.log(`[smartSend.smoke] SUCCESS: File has been deleted as expected`);
78158
assert.ok(true, `"${outputFile}" file has been deleted`);
79159
} else {
160+
console.error(`[smartSend.smoke] FAILURE: File still exists`);
80161
assert.fail(`"${outputFile}" file still exists`);
81162
}
82163
});
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
with open('smart_send_smoke.txt', 'w') as f:
2-
f.write('This is for smart send smoke test')
31
import os
42

5-
os.remove('smart_send_smoke.txt')
3+
# Use absolute path to avoid working directory issues
4+
script_dir = os.path.dirname(os.path.abspath(__file__))
5+
file_path = os.path.join(script_dir, 'smart_send_smoke.txt')
6+
7+
# For debugging: print working directory and file path
8+
print(f"Working directory: {os.getcwd()}", flush=True)
9+
print(f"File path: {file_path}", flush=True)
10+
print(f"Script location: {os.path.abspath(__file__)}", flush=True)
11+
12+
with open(file_path, 'w') as f:
13+
f.write('This is for smart send smoke test')
14+
f.flush() # Explicitly flush to ensure write completes
15+
16+
print(f"Successfully created {file_path}", flush=True)
17+
18+
print(f"About to delete {file_path}", flush=True)
19+
os.remove(file_path)
20+
print(f"Successfully deleted {file_path}", flush=True)

src/testMultiRootWkspc/smokeTests/testExecInTerminal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,22 @@
1212
if len(args) == 2:
1313
log_file = args[1]
1414

15+
# Ensure we use absolute path to avoid working directory issues
16+
if not os.path.isabs(log_file):
17+
# Get the directory where this script is located
18+
script_dir = os.path.dirname(os.path.abspath(__file__))
19+
log_file = os.path.join(script_dir, log_file)
20+
21+
# For debugging: print working directory and file path
22+
print(f"Working directory: {os.getcwd()}", flush=True)
23+
print(f"Log file path: {log_file}", flush=True)
24+
print(f"Script location: {os.path.abspath(__file__)}", flush=True)
25+
26+
# Ensure parent directory exists
27+
os.makedirs(os.path.dirname(log_file), exist_ok=True)
28+
1529
with open(log_file, "a") as f:
1630
f.write(sys.executable)
31+
f.flush() # Explicitly flush to ensure write completes
32+
33+
print(f"Successfully wrote to {log_file}", flush=True)

0 commit comments

Comments
 (0)