Skip to content

Commit fd7b0d1

Browse files
committed
Add WaitEvent helper function
1 parent 9c26d26 commit fd7b0d1

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

test/features/DebugSession.test.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as platform from "../../src/platform";
1212
import { IPlatformDetails } from "../../src/platform";
1313
import { IEditorServicesSessionDetails, IPowerShellVersionDetails, SessionManager, SessionStatus } from "../../src/session";
1414
import * as utils from "../../src/utils";
15-
import { BuildBinaryModuleMock, InstallCSharpExtension, ensureEditorServicesIsConnected, stubInterface, testLogger } from "../utils";
15+
import { BuildBinaryModuleMock, InstallCSharpExtension, WaitEvent, ensureEditorServicesIsConnected, stubInterface, testLogger } from "../utils";
1616

1717
const TEST_NUMBER = 7357; //7357 = TEST. Get it? :)
1818

@@ -408,6 +408,7 @@ describe("DebugSessionFeature E2E", function slowTests() {
408408
let stopDebugSession: DebugSession;
409409
const interactiveSessionConfig = defaultDebugConfigurations[DebugConfig.InteractiveSession];
410410
// Asserts dont seem to fire in this event or the event doesnt resolve in the test code flow, so we need to "extract" the values for later use by the asserts
411+
411412
const startDebugEvent = debug.onDidStartDebugSession((newDebugSession) => {
412413
startDebugEvent.dispose();
413414
startDebugSession = newDebugSession;
@@ -469,37 +470,32 @@ describe("DebugSessionFeature E2E", function slowTests() {
469470
const cmdletSourcePath = Uri.joinPath(workspace.workspaceFolders![0].uri, "mocks/BinaryModule/TestSampleCmdletCommand.cs");
470471
const testScriptDocument = await workspace.openTextDocument(testScriptPath);
471472
await window.showTextDocument(testScriptDocument);
473+
474+
// We cant see when a breakpoint is hit because the code we would spy on is in the C# extension or is vscode private, but we can see if the debug session changes which should only happen when the debug session context switches to C#, so that's good enough.
475+
476+
//We wire this up before starting the debug session so the event is registered
477+
const dotnetDebugSessionActive = WaitEvent(debug.onDidChangeActiveDebugSession, (session) => {
478+
console.log(`Debug Session Changed: ${session?.name}`);
479+
return !!session?.name.match(/Dotnet Debugger/);
480+
});
481+
472482
// Break at beginProcessing of the cmdlet
473483
debug.addBreakpoints([
474484
new SourceBreakpoint({
475485
uri: cmdletSourcePath,
476486
range: new Range(26, 0, 26, 0) //BeginProcessing
477-
})
487+
}, true, undefined, undefined, "TEST-BinaryModuleBreakpoint")
478488
]);
479-
// HACK: Could not find a way to detect a breakpoint hit directly, but we can detect if the editor view changes to our breakpointed file.
480-
let editorChanged: () => void;
481-
let activeEditorChangedToCmdletSourcePath = false;
482-
const waitForEditorChange = async () => {
483-
return new Promise<void>((resolve) => {editorChanged = resolve;});
484-
};
485-
const activeEditorChanged = window.onDidChangeActiveTextEditor((editor) => {
486-
console.log(`Active editor changed to ${editor?.document.uri.path}`);
487-
if (editor?.document.uri.path == cmdletSourcePath.path) {
488-
activeEditorChanged.dispose();
489-
activeEditorChangedToCmdletSourcePath = true;
490-
editorChanged();
491-
} else {
492-
console.log(`${editor?.document.uri.path} doesn't match ${cmdletSourcePath.path}`);
493-
}
494-
});
489+
495490
const debugStarted = await debug.startDebugging(undefined, launchScriptConfig);
496491
console.log(debug.breakpoints);
497-
console.log("Waiting for editor change");
498-
await waitForEditorChange();
492+
const dotnetDebugSession = await dotnetDebugSessionActive;
493+
console.log(debug.activeDebugSession);
494+
console.log(debug.breakpoints);
499495
const debugStopped = await debug.stopDebugging(undefined);
500496

501497
assert.ok(debugStarted);
502-
assert.ok(activeEditorChangedToCmdletSourcePath);
498+
assert.ok(dotnetDebugSession);
503499
assert.ok(debugStopped);
504500
});
505501
});

test/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,18 @@ export function BuildBinaryModuleMock() {
163163
const buildResult = execSync(`dotnet publish ${projectPath}`);
164164
console.log(buildResult.toString());
165165
}
166+
167+
/** Waits until the registered vscode event is fired and returns the trigger result of the event.
168+
* @param event The event to wait for
169+
* @param filter An optional filter to apply to the event TResult. The filter will continue to monitor the event firings until the filter returns true.
170+
*/
171+
export function WaitEvent<TResult>(event: vscode.Event<TResult>, filter?: (event: TResult) => boolean | undefined): Promise<TResult> {
172+
return new Promise<TResult>((resolve) => {
173+
const listener = event((result: TResult) => {
174+
if (!filter || filter(result)) {
175+
listener.dispose();
176+
resolve(result);
177+
}
178+
});
179+
});
180+
}

0 commit comments

Comments
 (0)