Skip to content

Commit 00bcc04

Browse files
committed
fix: stop test run manually only once even if there are multiple requests
1 parent c2e3d0e commit 00bcc04

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/runner/runner.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class TestRunner extends vscode.Disposable {
2626

2727
private disposables: vscode.Disposable[] = []
2828

29+
private cancelled = false
30+
2931
constructor(
3032
private readonly controller: vscode.TestController,
3133
private readonly tree: TestTree,
@@ -260,19 +262,25 @@ export class TestRunner extends vscode.Disposable {
260262
}
261263
}
262264

263-
protected scheduleTestRunsQueue: (() => Promise<void>)[] = []
265+
protected scheduleTestRunsQueue: {
266+
runTests: () => Promise<void>
267+
resolveWithoutRunning: () => void
268+
}[] = []
264269

265270
private async runTestItems(request: vscode.TestRunRequest, token: vscode.CancellationToken) {
271+
this.cancelled = false
266272
this.nonContinuousRequest = request
267273

268274
this.disposables.push(
269275
token.onCancellationRequested(() => {
270-
this.endTestRun()
271276
if (request === this.nonContinuousRequest) {
272-
this.nonContinuousRequest = undefined
277+
this.cancelled = true
278+
this.api.cancelRun().then(() => {
279+
this.nonContinuousRequest = undefined
280+
this.endTestRun()
281+
})
282+
log.verbose?.('Test run was cancelled manually for', join(request.include))
273283
}
274-
this.api.cancelRun()
275-
log.verbose?.('Test run was cancelled manually for', join(request.include))
276284
}),
277285
)
278286

@@ -309,10 +317,13 @@ export class TestRunner extends vscode.Disposable {
309317
}
310318
else {
311319
log.verbose?.('Queueing a new test run to execute when the current one is finished.')
312-
return new Promise((resolve, reject) => {
313-
this.scheduleTestRunsQueue.push(() => {
314-
log.verbose?.('Scheduled test run is starting now.')
315-
return this.runTestItems(request, token).then(resolve, reject)
320+
return new Promise<void>((resolve, reject) => {
321+
this.scheduleTestRunsQueue.push({
322+
runTests: () => {
323+
log.verbose?.('Scheduled test run is starting now.')
324+
return this.runTestItems(request, token).then(resolve, reject)
325+
},
326+
resolveWithoutRunning: resolve,
316327
})
317328
})
318329
}
@@ -397,8 +408,15 @@ export class TestRunner extends vscode.Disposable {
397408
this.testRunDefer = Promise.withResolvers()
398409
this.testRunDefer.promise = this.testRunDefer.promise.finally(() => {
399410
run.end()
400-
log.verbose?.(`Test run promise is finished, the queue is ${this.scheduleTestRunsQueue.length}`)
401-
this.scheduleTestRunsQueue.shift()?.()
411+
if (this.cancelled) {
412+
log.verbose?.('Not starting a new test run because the previous one was cancelled manually.')
413+
this.scheduleTestRunsQueue.forEach(item => item.resolveWithoutRunning())
414+
this.scheduleTestRunsQueue.length = 0
415+
}
416+
else {
417+
log.verbose?.(`Test run promise is finished, the queue is ${this.scheduleTestRunsQueue.length}`)
418+
this.scheduleTestRunsQueue.shift()?.runTests()
419+
}
402420
})
403421

404422
for (const file of files) {

0 commit comments

Comments
 (0)