Skip to content

Commit ff94933

Browse files
committed
Ensure only one test run can happen at a time
1 parent 72b8022 commit ff94933

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

src/lsptoolshost/unitTesting.ts

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,65 +37,79 @@ async function runTestsInContext(
3737
await runTests(request, languageServer, dotnetTestChannel);
3838
}
3939

40+
let _testRunInProgress = false;
41+
4042
async function runTests(
4143
request: RunTestsParams,
4244
languageServer: RoslynLanguageServer,
4345
dotnetTestChannel: vscode.OutputChannel
4446
) {
47+
if (_testRunInProgress) {
48+
vscode.window.showErrorMessage('Test run already in progress');
49+
return;
50+
}
51+
52+
_testRunInProgress = true;
53+
4554
dotnetTestChannel.show(true);
46-
vscode.window.withProgress(
47-
{
48-
location: vscode.ProgressLocation.Notification,
49-
title: 'Dotnet Test',
50-
cancellable: true,
51-
},
52-
async (progress, token) => {
53-
let totalReportedComplete = 0;
54-
const writeOutput = (output: RunTestsPartialResult) => {
55-
if (output.message) {
56-
dotnetTestChannel.appendLine(output.message);
57-
}
55+
vscode.window
56+
.withProgress(
57+
{
58+
location: vscode.ProgressLocation.Notification,
59+
title: 'Dotnet Test',
60+
cancellable: true,
61+
},
62+
async (progress, token) => {
63+
let totalReportedComplete = 0;
64+
const writeOutput = (output: RunTestsPartialResult) => {
65+
if (output.message) {
66+
dotnetTestChannel.appendLine(output.message);
67+
}
5868

59-
if (output.progress) {
60-
const totalTests = output.progress.totalTests;
61-
const completed =
62-
output.progress.testsPassed + output.progress.testsFailed + output.progress.testsSkipped;
69+
if (output.progress) {
70+
const totalTests = output.progress.totalTests;
71+
const completed =
72+
output.progress.testsPassed + output.progress.testsFailed + output.progress.testsSkipped;
6373

64-
// VSCode requires us to report the additional amount completed (in x out of 100) from this report compared to what we've previously reported.
65-
const reportIncrement = ((completed - totalReportedComplete) / totalTests) * 100;
66-
progress.report({ message: output.stage, increment: reportIncrement });
67-
totalReportedComplete = completed;
68-
} else {
69-
progress.report({ message: output.stage });
70-
}
71-
};
74+
// VSCode requires us to report the additional amount completed (in x out of 100) from this report compared to what we've previously reported.
75+
const reportIncrement = ((completed - totalReportedComplete) / totalTests) * 100;
76+
progress.report({ message: output.stage, increment: reportIncrement });
77+
totalReportedComplete = completed;
78+
} else {
79+
progress.report({ message: output.stage });
80+
}
81+
};
7282

73-
progress.report({ message: 'Saving files...' });
74-
// Ensure all files are saved before we run tests so they accurately reflect what the user has requested to run.
75-
await vscode.workspace.saveAll(/*includeUntitled*/ false);
83+
progress.report({ message: 'Saving files...' });
84+
// Ensure all files are saved before we run tests so they accurately reflect what the user has requested to run.
85+
await vscode.workspace.saveAll(/*includeUntitled*/ false);
7686

77-
progress.report({ message: 'Requesting server...' });
78-
const responsePromise = languageServer.sendRequestWithProgress(
79-
RunTestsRequest.type,
80-
request,
81-
async (p) => {
82-
writeOutput(p);
83-
},
84-
token
85-
);
87+
progress.report({ message: 'Requesting server...' });
88+
const responsePromise = languageServer.sendRequestWithProgress(
89+
RunTestsRequest.type,
90+
request,
91+
async (p) => {
92+
writeOutput(p);
93+
},
94+
token
95+
);
8696

87-
await responsePromise.then(
88-
(result) => {
89-
result.forEach((r) => {
90-
writeOutput(r);
91-
});
92-
return;
93-
},
94-
(err) => {
95-
dotnetTestChannel.appendLine(err);
96-
return;
97-
}
98-
);
99-
}
100-
);
97+
await responsePromise.then(
98+
(result) => {
99+
result.forEach((r) => {
100+
writeOutput(r);
101+
});
102+
return;
103+
},
104+
(err) => {
105+
dotnetTestChannel.appendLine(err);
106+
return;
107+
}
108+
);
109+
}
110+
)
111+
.then(
112+
() => (_testRunInProgress = false),
113+
() => (_testRunInProgress = false)
114+
);
101115
}

0 commit comments

Comments
 (0)