Skip to content

Commit b546eb2

Browse files
committed
chore: drop use of obsolete VsTest async APIs
1 parent 7d087f4 commit b546eb2

File tree

5 files changed

+50
-52
lines changed

5 files changed

+50
-52
lines changed

src/Stryker.Core/Stryker.Core.UnitTest/TestRunners/VsTestMockingHelper.cs

+39-39
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
1717
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollector.InProcDataCollector;
1818
using Moq;
19-
using Moq.Language.Flow;
2019
using Stryker.Core.Initialisation;
2120
using Stryker.Core.Mutants;
2221
using Stryker.Core.MutationTest;
@@ -130,18 +129,19 @@ protected TestProjectsInfo BuildTestProjectsInfo() =>
130129

131130
protected IReadOnlyList<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> TestCases { get; }
132131

133-
private static Task DiscoverTests(ITestDiscoveryEventsHandler discoveryEventsHandler, IReadOnlyCollection<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> tests, bool aborted) =>
134-
Task.Run(() => discoveryEventsHandler.HandleDiscoveredTests(tests)).
135-
ContinueWith((_, u) => discoveryEventsHandler.HandleDiscoveryComplete((int)u, null, aborted), tests.Count);
132+
private static void DiscoverTests(ITestDiscoveryEventsHandler discoveryEventsHandler, IReadOnlyCollection<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> tests, bool aborted)
133+
{
134+
discoveryEventsHandler.HandleDiscoveredTests(tests);
135+
discoveryEventsHandler.HandleDiscoveryComplete(tests.Count, null, aborted);
136+
}
136137

137138
protected Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase BuildCase(string name, TestFrameworks framework = TestFrameworks.xUnit, string displayName = null)
138139
=> new(name, framework == TestFrameworks.xUnit ? _xUnitUri : _NUnitUri, _testAssemblyPath) { Id = new Guid(), DisplayName = displayName ?? name };
139140

140141
private Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase FindOrBuildCase(string testResultId) => TestCases.FirstOrDefault(t => t.FullyQualifiedName == testResultId) ?? BuildCase(testResultId);
141142

142-
private static Task MockTestRun(ITestRunEventsHandler testRunEvents, IReadOnlyList<TestResult> testResults,
143-
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase timeOutTest = null) =>
144-
Task.Run(() =>
143+
private static void MockTestRun(ITestRunEventsHandler testRunEvents, IReadOnlyList<TestResult> testResults,
144+
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase timeOutTest = null)
145145
{
146146
if (testResults.Count == 0)
147147
{
@@ -174,7 +174,7 @@ private static Task MockTestRun(ITestRunEventsHandler testRunEvents, IReadOnlyLi
174174
new TestRunChangedEventArgs(null, Array.Empty<TestResult>(), new List<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase>()),
175175
null,
176176
null);
177-
});
177+
}
178178

179179
protected void SetupMockTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, bool testResult, IReadOnlyList<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases)
180180
{
@@ -200,25 +200,25 @@ protected void SetupMockTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IEnumera
200200
SetupMockTestRun(mockVsTest, results);
201201
}
202202

203-
protected IReturnsResult<IVsTestConsoleWrapper> SetupMockTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyList<TestResult> results) =>
203+
protected void SetupMockTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyList<TestResult> results) =>
204204
mockVsTest.Setup(x =>
205-
x.RunTestsWithCustomTestHostAsync(
205+
x.RunTestsWithCustomTestHost(
206206
It.Is<IEnumerable<string>>(t => t.Any(source => source == _testAssemblyPath)),
207207
It.Is<string>(settings => !settings.Contains("<Coverage")),
208208
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
209209
It.IsAny<ITestRunEventsHandler>(),
210-
It.IsAny<ITestHostLauncher>())).Returns(
210+
It.IsAny<ITestHostLauncher>())).Callback(
211211
(IEnumerable<string> _, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
212212
ITestHostLauncher _) => MockTestRun(testRunEvents, results));
213213

214214
protected void SetupFailingTestRun(Mock<IVsTestConsoleWrapper> mockVsTest) =>
215215
mockVsTest.Setup(x =>
216-
x.RunTestsWithCustomTestHostAsync(
216+
x.RunTestsWithCustomTestHost(
217217
It.Is<IEnumerable<string>>(t => t.Any(source => source == _testAssemblyPath)),
218218
It.Is<string>(settings => !settings.Contains("<Coverage")),
219219
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
220220
It.IsAny<ITestRunEventsHandler>(),
221-
It.IsAny<ITestHostLauncher>())).Returns(
221+
It.IsAny<ITestHostLauncher>())).Callback(
222222
(IEnumerable<string> _, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
223223
ITestHostLauncher _) =>
224224
// generate test results
@@ -239,14 +239,15 @@ protected void SetupFailingTestRun(Mock<IVsTestConsoleWrapper> mockVsTest) =>
239239
null,
240240
null);
241241
}));
242+
242243
protected void SetupFrozenTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, int repeated = 1) =>
243244
mockVsTest.Setup(x =>
244-
x.RunTestsWithCustomTestHostAsync(
245+
x.RunTestsWithCustomTestHost(
245246
It.Is<IEnumerable<string>>(t => t.Any(source => source == _testAssemblyPath)),
246247
It.Is<string>(settings => !settings.Contains("<Coverage")),
247248
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
248249
It.IsAny<ITestRunEventsHandler>(),
249-
It.IsAny<ITestHostLauncher>())).Returns(
250+
It.IsAny<ITestHostLauncher>())).Callback(
250251
(IEnumerable<string> _, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
251252
ITestHostLauncher _) =>
252253
// generate test results
@@ -276,18 +277,18 @@ protected void SetupFrozenTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, int re
276277
null);
277278
}
278279
}));
280+
279281
protected void SetupFrozenVsTest(Mock<IVsTestConsoleWrapper> mockVsTest, int repeated = 1) =>
280282
mockVsTest.Setup(x =>
281-
x.RunTestsWithCustomTestHostAsync(
283+
x.RunTestsWithCustomTestHost(
282284
It.Is<IEnumerable<string>>(t => t.Any(source => source == _testAssemblyPath)),
283285
It.Is<string>(settings => !settings.Contains("<Coverage")),
284286
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
285287
It.IsAny<ITestRunEventsHandler>(),
286-
It.IsAny<ITestHostLauncher>())).Returns(
288+
It.IsAny<ITestHostLauncher>())).Callback(
287289
(IEnumerable<string> _, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
288290
ITestHostLauncher _) =>
289291
// generate test results
290-
Task.Run(() =>
291292
{
292293
testRunEvents.HandleTestRunStatsChange(
293294
new TestRunChangedEventArgs(new TestRunStatistics(0, null), null, null));
@@ -303,18 +304,18 @@ protected void SetupFrozenVsTest(Mock<IVsTestConsoleWrapper> mockVsTest, int rep
303304

304305
if (repeated-->0)
305306
Thread.Sleep(1000);
306-
}));
307+
});
307308

308309
protected void SetupMockCoverageRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyDictionary<string, string> coverageResults) => SetupMockCoverageRun(mockVsTest, GenerateCoverageTestResults(coverageResults));
309310

310311
protected void SetupMockCoverageRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyList<TestResult> results) =>
311312
mockVsTest.Setup(x =>
312-
x.RunTestsWithCustomTestHostAsync(
313+
x.RunTestsWithCustomTestHost(
313314
It.Is<IEnumerable<string>>(t => t.Any(source => source == _testAssemblyPath)),
314315
It.Is<string>(settings => settings.Contains("<Coverage")),
315316
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
316317
It.IsAny<ITestRunEventsHandler>(),
317-
It.IsAny<ITestHostLauncher>())).Returns(
318+
It.IsAny<ITestHostLauncher>())).Callback(
318319
(IEnumerable<string> _, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
319320
ITestHostLauncher _) => MockTestRun(testRunEvents, results));
320321

@@ -347,12 +348,12 @@ private List<TestResult> GenerateCoverageTestResults(IReadOnlyDictionary<string,
347348

348349
protected void SetupMockCoveragePerTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyDictionary<string, string> coverageResults) =>
349350
mockVsTest.Setup(x =>
350-
x.RunTestsWithCustomTestHostAsync(
351+
x.RunTestsWithCustomTestHost(
351352
It.Is<IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase>>(t => t.Any()),
352353
It.Is<string>(settings => settings.Contains("<Coverage")),
353354
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
354355
It.IsAny<ITestRunEventsHandler>(),
355-
It.IsAny<ITestHostLauncher>())).Returns(
356+
It.IsAny<ITestHostLauncher>())).Callback(
356357
(IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases, string _, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
357358
ITestHostLauncher _) =>
358359
{
@@ -368,7 +369,7 @@ protected void SetupMockCoveragePerTestRun(Mock<IVsTestConsoleWrapper> mockVsTes
368369
var result = BuildCoverageTestResult(key, coveredList);
369370
results.Add(result);
370371
}
371-
return MockTestRun(testRunEvents, results);
372+
MockTestRun(testRunEvents, results);
372373
});
373374

374375
protected TestResult BuildCoverageTestResult(string key, string[] coveredList)
@@ -390,14 +391,14 @@ protected TestResult BuildCoverageTestResult(string key, string[] coveredList)
390391

391392
protected static void SetupMockPartialTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyDictionary<string, string> results) =>
392393
mockVsTest.Setup(x =>
393-
x.RunTestsWithCustomTestHostAsync(
394+
x.RunTestsWithCustomTestHost(
394395
It.IsAny<IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase>>(),
395396
It.Is<string>(s => !s.Contains("<Coverage")),
396397
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
397398
It.IsAny<ITestRunEventsHandler>(),
398-
It.IsAny<ITestHostLauncher>())).Returns(
399+
It.IsAny<ITestHostLauncher>())).Callback(
399400
(IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> sources, string settings, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
400-
ITestHostLauncher _) => Task.Run(()=>
401+
ITestHostLauncher _) =>
401402
{
402403
var collector = new CoverageCollector();
403404
var start = new TestSessionStartArgs
@@ -435,20 +436,20 @@ protected static void SetupMockPartialTestRun(Mock<IVsTestConsoleWrapper> mockVs
435436
runResults.Add(result);
436437
}
437438
// setup a normal test run
438-
MockTestRun(testRunEvents, runResults).Wait();
439+
MockTestRun(testRunEvents, runResults);
439440
collector.TestSessionEnd(new TestSessionEndArgs());
440-
}));
441+
});
441442

442443
protected static void SetupMockTimeOutTestRun(Mock<IVsTestConsoleWrapper> mockVsTest, IReadOnlyDictionary<string, string> results, string timeoutTest) =>
443444
mockVsTest.Setup(x =>
444-
x.RunTestsWithCustomTestHostAsync(
445+
x.RunTestsWithCustomTestHost(
445446
It.IsAny<IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase>>(),
446447
It.IsAny<string>(),
447448
It.Is<TestPlatformOptions>(o => o != null && o.TestCaseFilter == null),
448449
It.IsAny<ITestRunEventsHandler>(),
449-
It.IsAny<ITestHostLauncher>())).Returns(
450+
It.IsAny<ITestHostLauncher>())).Callback(
450451
(IEnumerable<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> sources, string settings, TestPlatformOptions _, ITestRunEventsHandler testRunEvents,
451-
ITestHostLauncher _) => Task.Run(()=>
452+
ITestHostLauncher _) =>
452453
{
453454
var collector = new CoverageCollector();
454455
var start = new TestSessionStartArgs
@@ -490,10 +491,10 @@ protected static void SetupMockTimeOutTestRun(Mock<IVsTestConsoleWrapper> mockVs
490491
runResults.Add(result);
491492
}
492493
// setup a normal test run
493-
MockTestRun(testRunEvents, runResults, timeOutTestCase).Wait();
494+
MockTestRun(testRunEvents, runResults, timeOutTestCase);
494495
collector.TestSessionEnd(new TestSessionEndArgs());
495496

496-
}));
497+
});
497498

498499
protected Mock<IVsTestConsoleWrapper> BuildVsTestRunnerPool(StrykerOptions options,
499500
out VsTestRunnerPool runner, IReadOnlyCollection<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = null, TestProjectsInfo testProjectsInfo = null)
@@ -505,13 +506,13 @@ protected Mock<IVsTestConsoleWrapper> BuildVsTestRunnerPool(StrykerOptions optio
505506
mockedVsTestConsole.Setup(x => x.AbortTestRun());
506507
mockedVsTestConsole.Setup(x => x.EndSession());
507508
ITestDiscoveryEventsHandler discoveryHandler = null;
509+
508510
mockedVsTestConsole.Setup(x =>
509-
x.DiscoverTestsAsync(It.Is<IEnumerable<string>>(d => d.Any(e => e == _testAssemblyPath)),
511+
x.DiscoverTests(It.Is<IEnumerable<string>>(d => d.Any(e => e == _testAssemblyPath)),
510512
It.IsAny<string>(),
511-
It.IsAny<ITestDiscoveryEventsHandler>())).Callback(
513+
It.IsAny<ITestDiscoveryEventsHandler>()))/*.Callback(
512514
(IEnumerable<string> _, string _, ITestDiscoveryEventsHandler handler) =>
513-
discoveryHandler = handler).Returns(() => DiscoverTests(discoveryHandler, testCases, false));
514-
515+
discoveryHandler = handler)*/.Callback((IEnumerable<string> _, string _, ITestDiscoveryEventsHandler handler) => DiscoverTests(handler, testCases, false));
515516
var context = new VsTestContextInformation(
516517
options,
517518
new Mock<IVsTestHelper>().Object,
@@ -561,5 +562,4 @@ private class MockStrykerTestHostLauncher : IStrykerTestHostLauncher
561562

562563
public int ErrorCode { get; }
563564
}
564-
565565
}

src/Stryker.Core/Stryker.Core.UnitTest/TestRunners/VsTestRunnerPoolTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void RecycleRunnerOnError()
147147
var action = () => runner.TestMultipleMutants(SourceProjectInfo, null, new[] { Mutant }, null);
148148
action.ShouldThrow<GeneralStrykerException>();
149149
// the test will always end in a crash, VsTestRunner should retry at least a few times
150-
mockVsTest.Verify(m => m.RunTestsWithCustomTestHostAsync(It.IsAny<IEnumerable<string>>(),
150+
mockVsTest.Verify(m => m.RunTestsWithCustomTestHost(It.IsAny<IEnumerable<string>>(),
151151
It.IsAny<string>(), It.IsAny<TestPlatformOptions>(),
152152
It.IsAny<ITestRunEventsHandler>(),
153153
It.IsAny<IStrykerTestHostLauncher>()), Times.AtLeast(3));
@@ -189,11 +189,11 @@ public void ShouldRetryFrozenSession()
189189
var mockVsTest = BuildVsTestRunnerPool(new StrykerOptions(), out var runner);
190190
var defaultTimeOut = VsTestRunner.VsTestExtraTimeOutInMs;
191191
VsTestRunner.VsTestExtraTimeOutInMs = 100;
192-
// the test session will hung twice
192+
// the test session will freeze twice
193193
SetupFrozenTestRun(mockVsTest, 2);
194194
runner.TestMultipleMutants(SourceProjectInfo, new TimeoutValueCalculator(0, 10,9), new[] { Mutant }, null);
195195
VsTestRunner.VsTestExtraTimeOutInMs = defaultTimeOut;
196-
mockVsTest.Verify(m => m.RunTestsWithCustomTestHostAsync(It.IsAny<IEnumerable<string>>(),
196+
mockVsTest.Verify(m => m.RunTestsWithCustomTestHost(It.IsAny<IEnumerable<string>>(),
197197
It.IsAny<string>(), It.IsAny<TestPlatformOptions>(),
198198
It.IsAny<ITestRunEventsHandler>(),
199199
It.IsAny<IStrykerTestHostLauncher>()), Times.Exactly(3));
@@ -205,7 +205,7 @@ public void ShouldNotRetryFrozenVsTest()
205205
var mockVsTest = BuildVsTestRunnerPool(new StrykerOptions(), out var runner);
206206
var defaultTimeOut = VsTestRunner.VsTestExtraTimeOutInMs;
207207
// the test session will end properly, but VsTest will hang
208-
// it will be recyled
208+
// it will be recycled
209209
SetupFrozenVsTest(mockVsTest, 3);
210210
VsTestRunner.VsTestExtraTimeOutInMs = 100;
211211
runner.TestMultipleMutants(SourceProjectInfo, new TimeoutValueCalculator(0, 10,9), new[] { Mutant }, null);

src/Stryker.Core/Stryker.Core.UnitTest/TestRunners/VsTextContextInformationTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ private VsTestContextInformation BuildVsTextContext(StrykerOptions options, out
111111
mockedVsTestConsole.Setup(x => x.InitializeExtensions(It.IsAny<IEnumerable<string>>()));
112112
mockedVsTestConsole.Setup(x => x.EndSession());
113113
mockedVsTestConsole.Setup(x =>
114-
x.DiscoverTestsAsync(It.Is<IEnumerable<string>>(d => d.Any(e => e == _testAssemblyPath)),
114+
x.DiscoverTests(It.Is<IEnumerable<string>>(d => d.Any(e => e == _testAssemblyPath)),
115115
It.IsAny<string>(),
116116
It.IsAny<ITestDiscoveryEventsHandler>())).Callback(
117117
(IEnumerable<string> _, string _, ITestDiscoveryEventsHandler discoveryEventsHandler) =>
118-
DiscoverTests(discoveryEventsHandler, TestCases, false)).Returns(Task.CompletedTask);
118+
DiscoverTests(discoveryEventsHandler, TestCases, false));
119119

120120
var vsTestConsoleWrapper = mockedVsTestConsole.Object;
121121
return new VsTestContextInformation(

src/Stryker.Core/Stryker.Core/TestRunners/VsTest/VsTestContextInformation.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private void DiscoverTestsInSources(string newSource)
190190
var wrapper = BuildVsTestWrapper("TestDiscoverer");
191191
var messages = new List<string>();
192192
var handler = new DiscoveryEventHandler(messages);
193-
wrapper.DiscoverTestsAsync(new List<string> { newSource }, GenerateRunSettingsForDiscovery(), handler);
193+
wrapper.DiscoverTests(new List<string> { newSource }, GenerateRunSettingsForDiscovery(), handler);
194194

195195
handler.WaitEnd();
196196
if (handler.Aborted)

src/Stryker.Core/Stryker.Core/TestRunners/VsTest/VsTestRunner.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Threading;
5-
using System.Threading.Tasks;
64
using Microsoft.Extensions.Logging;
75
using Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces;
86
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
@@ -258,16 +256,16 @@ private void RunVsTest(ITestGuids tests, string source, string runSettings, Test
258256

259257
eventHandler.StartSession();
260258
_currentSessionCancelled = false;
261-
Task session;
259+
Task session = Task.Run(()=>{
262260
if (tests.IsEveryTest)
263261
{
264-
session = _vsTestConsole.RunTestsWithCustomTestHostAsync(new []{source}, runSettings, options, eventHandler, strykerVsTestHostLauncher);
262+
_vsTestConsole.RunTestsWithCustomTestHost(new []{source}, runSettings, options, eventHandler, strykerVsTestHostLauncher);
265263
}
266264
else
267265
{
268-
session = _vsTestConsole.RunTestsWithCustomTestHostAsync(tests.GetGuids().Select(t => _context.VsTests[t].Case),
266+
_vsTestConsole.RunTestsWithCustomTestHost(tests.GetGuids().Select(t => _context.VsTests[t].Case),
269267
runSettings, options, eventHandler, strykerVsTestHostLauncher);
270-
}
268+
} });
271269

272270
if (WaitForEnd(session, eventHandler, timeOut, ref attempt))
273271
{

0 commit comments

Comments
 (0)