Skip to content

Commit 5abdae1

Browse files
Simpler fix for dotnet test when using retry by @Youssef1313 in #5731 (backport to rel/3.9) (#5732)
Co-authored-by: Youssef1313 <[email protected]>
1 parent 6b9b6c9 commit 5abdae1

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class InvokeTestingPlatformTask : Build.Utilities.ToolTask, IDisposable
4040
private Task? _connectionLoopTask;
4141
private ModuleInfoRequest? _moduleInfo;
4242
private string? _outputFileName;
43-
private StreamWriter? _outputFileStream;
4443
private string? _toolCommand;
4544

4645
/// <summary>
@@ -373,15 +372,15 @@ public override bool Execute()
373372
bool returnValue = base.Execute();
374373
if (_toolCommand is not null)
375374
{
376-
StringBuilder sb = new();
377-
sb.AppendLine();
378-
sb.AppendLine("=== COMMAND LINE ===");
379-
sb.AppendLine(_toolCommand);
380-
_output.AppendLine(sb.ToString());
375+
_output.AppendLine();
376+
_output.AppendLine("=== COMMAND LINE ===");
377+
_output.AppendLine(_toolCommand);
381378
}
382379

383-
// Persist the output to the file.
384-
_outputFileStream?.WriteLine(_output);
380+
if (_outputFileName is not null)
381+
{
382+
_fileSystem.WriteAllText(_outputFileName, _output.ToString());
383+
}
385384

386385
_waitForConnections.Cancel();
387386
Dispose();
@@ -419,10 +418,6 @@ protected override bool HandleTaskExecutionErrors()
419418
_fileSystem.CreateDirectory(_outputFileName);
420419
_outputFileName = Path.Combine(_outputFileName, $"{Path.GetFileNameWithoutExtension(TargetPath.ItemSpec.Trim())}_{TargetFramework.ItemSpec}_{TestArchitecture.ItemSpec}.log");
421420
Log.LogMessage(MessageImportance.Low, $"Invalid command line exit code and empty output file name, creating default one '{_outputFileName}'");
422-
_outputFileStream = new StreamWriter(_fileSystem.CreateNew(_outputFileName), Encoding.Unicode)
423-
{
424-
AutoFlush = true,
425-
};
426421
}
427422

428423
Log.LogError(null, "run failed", null, TargetPath.ItemSpec.Trim(), 0, 0, 0, 0, Resources.MSBuildResources.TestFailed, _outputFileName, TargetFramework.ItemSpec, TestArchitecture.ItemSpec);
@@ -445,10 +440,6 @@ private Task<IResponse> HandleRequestAsync(IRequest request)
445440
_outputFileName = $"{Path.GetFileNameWithoutExtension(TargetPath.ItemSpec.Trim())}_{TargetFramework.ItemSpec}_{TestArchitecture.ItemSpec}.log";
446441
_outputFileName = Path.Combine(_moduleInfo.TestResultFolder, _outputFileName);
447442
Log.LogMessage(MessageImportance.Low, $"Initializing module info and output file '{_outputFileName}'");
448-
_outputFileStream = new StreamWriter(_fileSystem.CreateNew(_outputFileName), Encoding.Unicode)
449-
{
450-
AutoFlush = true,
451-
};
452443
}
453444
}
454445
}
@@ -530,7 +521,6 @@ public void Log<TState>(LogLevel logLevel, TState state, Exception? exception, F
530521
/// <inheritdoc />
531522
public void Dispose()
532523
{
533-
_outputFileStream?.Dispose();
534524
_waitForConnections.Cancel();
535525
_connectionLoopTask?.Wait();
536526

test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/RetryFailedTestsTests.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ await RetryHelper.RetryAsync(
180180
}, 3, TimeSpan.FromSeconds(5));
181181
}
182182

183+
[TestMethod]
184+
public async Task RetryFailedTests_PassingFromFirstTime_UsingOldDotnetTest_MoveFiles_Succeeds()
185+
{
186+
string resultDirectory = Path.Combine(AssetFixture.TargetAssetPath, Guid.NewGuid().ToString("N"));
187+
188+
DotnetMuxerResult result = await DotnetCli.RunAsync(
189+
$"test \"{AssetFixture.TargetAssetPath}\" -- --retry-failed-tests 1 --results-directory \"{resultDirectory}\"",
190+
AcceptanceFixture.NuGetGlobalPackagesFolder.Path,
191+
workingDirectory: AssetFixture.TargetAssetPath);
192+
193+
Assert.AreEqual(ExitCodes.Success, result.ExitCode);
194+
195+
string[] logFilesFromInvokeTestingPlatformTask = Directory.GetFiles(resultDirectory, "RetryFailedTests_*_*.log", SearchOption.AllDirectories);
196+
Assert.AreEqual(TargetFrameworks.All.Length, logFilesFromInvokeTestingPlatformTask.Length);
197+
foreach (string logFile in logFilesFromInvokeTestingPlatformTask)
198+
{
199+
string logFileContents = File.ReadAllText(logFile);
200+
Assert.Contains("Test run summary: Passed!", logFileContents);
201+
Assert.Contains("total: 3", logFileContents);
202+
Assert.Contains("succeeded: 3", logFileContents);
203+
Assert.Contains("Tests suite completed successfully in 1 attempts", logFileContents);
204+
}
205+
}
206+
183207
public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
184208
{
185209
public string TargetAssetPath => GetAssetPath(AssetName);
@@ -202,21 +226,30 @@ public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.
202226
<OutputType>Exe</OutputType>
203227
<UseAppHost>true</UseAppHost>
204228
<LangVersion>preview</LangVersion>
229+
<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
230+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
231+
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
205232
</PropertyGroup>
206233
<ItemGroup>
207234
<PackageReference Include="Microsoft.Testing.Extensions.CrashDump" Version="$MicrosoftTestingPlatformVersion$" />
208235
<PackageReference Include="Microsoft.Testing.Extensions.Retry" Version="$MicrosoftTestingPlatformVersion$" />
209236
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="$MicrosoftTestingPlatformVersion$" />
237+
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="$MicrosoftTestingPlatformVersion$" />
210238
</ItemGroup>
211239
</Project>
212240
241+
#file dotnet.config
242+
[dotnet.test.runner]
243+
name= "VSTest"
244+
213245
#file Program.cs
214246
using Microsoft.Testing.Extensions;
215247
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
216248
using Microsoft.Testing.Platform.Builder;
217249
using Microsoft.Testing.Platform.Capabilities.TestFramework;
218250
using Microsoft.Testing.Platform.Extensions.Messages;
219251
using Microsoft.Testing.Platform.Extensions.TestFramework;
252+
using Microsoft.Testing.Platform.MSBuild;
220253
using Microsoft.Testing.Platform.Services;
221254
222255
public class Program
@@ -230,6 +263,7 @@ public static async Task<int> Main(string[] args)
230263
builder.AddCrashDumpProvider();
231264
builder.AddTrxReportProvider();
232265
builder.AddRetryProvider();
266+
builder.AddMSBuild();
233267
using ITestApplication app = await builder.BuildAsync();
234268
return await app.RunAsync();
235269
}
@@ -270,7 +304,7 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
270304
string resultDir = Environment.GetEnvironmentVariable("RESULTDIR")!;
271305
bool crash = Environment.GetEnvironmentVariable("CRASH") == "1";
272306
273-
if (await TestMethod1(fail, resultDir, crash))
307+
if (TestMethod1(fail, resultDir, crash))
274308
{
275309
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
276310
new TestNode() { Uid = "1", DisplayName = "TestMethod1", Properties = new(PassedTestNodeStateProperty.CachedInstance) }));
@@ -281,7 +315,7 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
281315
new TestNode() { Uid = "1", DisplayName = "TestMethod1", Properties = new(new FailedTestNodeStateProperty()) }));
282316
}
283317
284-
if (await TestMethod2(fail, resultDir))
318+
if (TestMethod2(fail, resultDir))
285319
{
286320
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
287321
new TestNode() { Uid = "2", DisplayName = "TestMethod2", Properties = new(PassedTestNodeStateProperty.CachedInstance) }));
@@ -292,7 +326,7 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
292326
new TestNode() { Uid = "2", DisplayName = "TestMethod2", Properties = new(new FailedTestNodeStateProperty()) }));
293327
}
294328
295-
if (await TestMethod3(fail, resultDir))
329+
if (TestMethod3(fail, resultDir))
296330
{
297331
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
298332
new TestNode() { Uid = "3", DisplayName = "TestMethod3", Properties = new(PassedTestNodeStateProperty.CachedInstance) }));
@@ -306,7 +340,7 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
306340
context.Complete();
307341
}
308342
309-
private async Task<bool> TestMethod1(bool fail, string resultDir, bool crash)
343+
private bool TestMethod1(bool fail, string resultDir, bool crash)
310344
{
311345
if (crash)
312346
{
@@ -330,7 +364,7 @@ private async Task<bool> TestMethod1(bool fail, string resultDir, bool crash)
330364
return assert;
331365
}
332366
333-
private async Task<bool> TestMethod2(bool fail, string resultDir)
367+
private bool TestMethod2(bool fail, string resultDir)
334368
{
335369
bool envVar = Environment.GetEnvironmentVariable("METHOD2") is null;
336370
System.Console.WriteLine("envVar " + envVar);
@@ -350,7 +384,7 @@ private async Task<bool> TestMethod2(bool fail, string resultDir)
350384
return assert;
351385
}
352386
353-
private async Task<bool> TestMethod3(bool fail, string resultDir)
387+
private bool TestMethod3(bool fail, string resultDir)
354388
{
355389
bool envVar = Environment.GetEnvironmentVariable("METHOD3") is null;
356390

0 commit comments

Comments
 (0)