Skip to content

Commit

Permalink
Enable usage of datacollectors in playground. (#3981)
Browse files Browse the repository at this point in the history
* Comment out console mode, add non-detailed output mode.

* Comment out console mode, add non-detailed output mode.

* Fix usage of datacollector.

* Remove maxCpuCount

* Update playground/TestPlatform.Playground/Program.cs

Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>

Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>
  • Loading branch information
nohwnd and Haplois committed Sep 7, 2022
1 parent 30a23a7 commit 8a864b8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 42 deletions.
127 changes: 86 additions & 41 deletions playground/TestPlatform.Playground/Program.cs
Expand Up @@ -39,7 +39,6 @@ static void Main()

var console = Path.Combine(here, "vstest.console", "vstest.console.exe");

var maxCpuCount = Environment.GetEnvironmentVariable("VSTEST_MAX_CPU_COUNT") ?? "0";
var sourceSettings = $$$"""
<RunSettings>
<RunConfiguration>
Expand All @@ -60,7 +59,23 @@ static void Main()
<Settings />
</SolutionSettings>
<ProjectSettings />
</GoogleTestAdapterSettings>
</GoogleTestAdapterSettings>

<!-- Blame hang -->
<!-- <LoggerRunSettings>
<Loggers>
<Logger friendlyName="blame" enabled="True" />
</Loggers>
</LoggerRunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="blame" enabled="True">
<Configuration>
<CollectDumpOnTestSessionHang TestTimeout="10s" HangDumpType="Full" />
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings> -->
</RunSettings>
""";

Expand All @@ -69,31 +84,34 @@ static void Main()
Path.Combine(playground, "MSTest1", "bin", "Debug", "net5.0", "MSTest1.dll"),
};

// console mode
var settingsFile = Path.GetTempFileName();
try
{
File.WriteAllText(settingsFile, sourceSettings);
var processStartInfo = new ProcessStartInfo
{
FileName = console,
Arguments = $"{string.Join(" ", sources)} --settings:{settingsFile} --listtests",
UseShellExecute = false,
};
EnvironmentVariables.Variables.ToList().ForEach(processStartInfo.Environment.Add);
var process = Process.Start(processStartInfo);
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Process failed with {process.ExitCode}");
}
}
finally
{
try { File.Delete(settingsFile); } catch { }
}
// Uncomment if providing command line parameters is easier for you
// than converting them to settings, or when you debug command line scenario specifically.
//// console mode
//var settingsFile = Path.GetTempFileName();
//try
//{
// File.WriteAllText(settingsFile, sourceSettings);
// var processStartInfo = new ProcessStartInfo
// {
// FileName = console,
// Arguments = $"{string.Join(" ", sources)} --settings:{settingsFile} --listtests",
// UseShellExecute = false,
// };
// EnvironmentVariables.Variables.ToList().ForEach(processStartInfo.Environment.Add);
// var process = Process.Start(processStartInfo);
// process.WaitForExit();
// if (process.ExitCode != 0)
// {
// throw new Exception($"Process failed with {process.ExitCode}");
// }
//}
//finally
//{
// try { File.Delete(settingsFile); } catch { }
//}

// design mode
var detailedOutput = true;
var consoleOptions = new ConsoleParameters
{
EnvironmentVariables = EnvironmentVariables.Variables,
Expand All @@ -110,21 +128,21 @@ static void Main()
//// TestSessions
// r.StartTestSession(sources, sourceSettings, sessionHandler);
#pragma warning restore CS0618 // Type or member is obsolete
var discoveryHandler = new PlaygroundTestDiscoveryHandler();
var discoveryHandler = new PlaygroundTestDiscoveryHandler(detailedOutput);
var sw = Stopwatch.StartNew();
// Discovery
r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler);
var discoveryDuration = sw.ElapsedMilliseconds;
Console.WriteLine($"Discovery done in {discoveryDuration} ms");
sw.Restart();
// Run with test cases and custom testhost launcher
r.RunTestsWithCustomTestHost(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(), new DebuggerTestHostLauncher());
r.RunTestsWithCustomTestHost(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher());
//// Run with test cases and without custom testhost launcher
//r.RunTests(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler());
//r.RunTests(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput));
//// Run with sources and custom testhost launcher
//r.RunTestsWithCustomTestHost(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(), new DebuggerTestHostLauncher());
//r.RunTestsWithCustomTestHost(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher());
//// Run with sources
//r.RunTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler());
//r.RunTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput));
var rd = sw.ElapsedMilliseconds;
Console.WriteLine($"Discovery: {discoveryDuration} ms, Run: {rd} ms, Total: {discoveryDuration + rd} ms");
Console.WriteLine($"Settings:\n{sourceSettings}");
Expand All @@ -133,30 +151,45 @@ static void Main()
public class PlaygroundTestDiscoveryHandler : ITestDiscoveryEventsHandler, ITestDiscoveryEventsHandler2
{
private int _testCasesCount;
private readonly bool _detailedOutput;

public PlaygroundTestDiscoveryHandler(bool detailedOutput)
{
_detailedOutput = detailedOutput;
}

public List<TestCase> TestCases { get; internal set; } = new List<TestCase>();

public void HandleDiscoveredTests(IEnumerable<TestCase>? discoveredTestCases)
{
Console.WriteLine($"[DISCOVERY.PROGRESS]");
Console.WriteLine(WriteTests(discoveredTestCases));
if (_detailedOutput)
{
Console.WriteLine($"[DISCOVERY.PROGRESS]");
Console.WriteLine(WriteTests(discoveredTestCases));
}
_testCasesCount += discoveredTestCases.Count();
if (discoveredTestCases != null) { TestCases.AddRange(discoveredTestCases); }
}

public void HandleDiscoveryComplete(long totalTests, IEnumerable<TestCase>? lastChunk, bool isAborted)
{
Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {isAborted}, tests count: {totalTests}");
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
if (_detailedOutput)
{
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
}
if (lastChunk != null) { TestCases.AddRange(lastChunk); }
}

public void HandleDiscoveryComplete(DiscoveryCompleteEventArgs discoveryCompleteEventArgs, IEnumerable<TestCase>? lastChunk)
{
Console.WriteLine($"[DISCOVERY.COMPLETE] aborted? {discoveryCompleteEventArgs.IsAborted}, tests count: {discoveryCompleteEventArgs.TotalCount}, discovered count: {_testCasesCount}");
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
if (_detailedOutput)
{
Console.WriteLine("Last chunk:");
Console.WriteLine(WriteTests(lastChunk));
}
Console.WriteLine("Fully discovered:");
Console.WriteLine(WriteSources(discoveryCompleteEventArgs.FullyDiscoveredSources));
Console.WriteLine("Partially discovered:");
Expand Down Expand Up @@ -191,9 +224,11 @@ private static string WriteSources(IEnumerable<string>? sources)

public class TestRunHandler : ITestRunEventsHandler
{
private readonly bool _detailedOutput;

public TestRunHandler()
public TestRunHandler(bool detailedOutput)
{
_detailedOutput = detailedOutput;
}

public void HandleLogMessage(TestMessageLevel level, string? message)
Expand All @@ -203,19 +238,28 @@ public void HandleLogMessage(TestMessageLevel level, string? message)

public void HandleRawMessage(string rawMessage)
{
Console.WriteLine($"[RUN.MESSAGE]: {rawMessage}");
if (_detailedOutput)
{
Console.WriteLine($"[RUN.MESSAGE]: {rawMessage}");
}
}

public void HandleTestRunComplete(TestRunCompleteEventArgs testRunCompleteArgs, TestRunChangedEventArgs? lastChunkArgs, ICollection<AttachmentSet>? runContextAttachments, ICollection<string>? executorUris)
{
Console.WriteLine($"[RUN.COMPLETE]: err: {testRunCompleteArgs.Error}, lastChunk:");
Console.WriteLine(WriteTests(lastChunkArgs?.NewTestResults));
if (_detailedOutput)
{
Console.WriteLine(WriteTests(lastChunkArgs?.NewTestResults));
}
}

public void HandleTestRunStatsChange(TestRunChangedEventArgs? testRunChangedArgs)
{
Console.WriteLine($"[RUN.PROGRESS]");
Console.WriteLine(WriteTests(testRunChangedArgs?.NewTestResults));
if (_detailedOutput)
{
Console.WriteLine($"[RUN.PROGRESS]");
Console.WriteLine(WriteTests(testRunChangedArgs?.NewTestResults));
}
}

public int LaunchProcessWithDebuggerAttached(TestProcessStartInfo testProcessStartInfo)
Expand Down Expand Up @@ -260,6 +304,7 @@ public int LaunchTestHost(TestProcessStartInfo defaultTestHostStartInfo, Cancell

internal class TestSessionHandler : ITestSessionEventsHandler
{
public TestSessionHandler() { }
public TestSessionInfo? TestSessionInfo { get; private set; }

public void HandleLogMessage(TestMessageLevel level, string? message)
Expand Down
Expand Up @@ -18,7 +18,12 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DataCollectors\DumpMinitool.arm64\DumpMinitool.arm64.csproj" />
<ProjectReference Include="..\..\src\DataCollectors\DumpMinitool.x86\DumpMinitool.x86.csproj" />
<ProjectReference Include="..\..\src\DataCollectors\DumpMinitool\DumpMinitool.csproj" />
<ProjectReference Include="..\..\src\DataCollectors\Microsoft.TestPlatform.Extensions.EventLogCollector\Microsoft.TestPlatform.Extensions.EventLogCollector.csproj" />
<ProjectReference Include="..\..\src\datacollector\datacollector.csproj" />
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.Extensions.BlameDataCollector\Microsoft.TestPlatform.Extensions.BlameDataCollector.csproj" />
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.TestHostProvider\Microsoft.TestPlatform.TestHostProvider.csproj" />
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.VsTestConsole.TranslationLayer\Microsoft.TestPlatform.VsTestConsole.TranslationLayer.csproj" />
<ProjectReference Include="..\..\src\testhost.x86\testhost.x86.csproj" />
Expand All @@ -35,7 +40,7 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(OS)' == 'WINDOWS_NT' ">
<Exec Command="xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\vstest.console\bin\$(Configuration)\$(NetFrameworkMinimum)\ $(TargetDir)\vstest.console\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\Microsoft.TestPlatform.TestHostProvider\bin\$(Configuration)\$(NetFrameworkMinimum)\ $(TargetDir)\vstest.console\Extensions\&#xD;&#xA;REM copy net462, net47, net471, net472 and net48 testhosts&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net47\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net47\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net471\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net471\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net472\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net472\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net48\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net48\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\" />
<Exec Command="xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\vstest.console\bin\$(Configuration)\$(NetFrameworkMinimum)\ $(TargetDir)\vstest.console\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\Microsoft.TestPlatform.TestHostProvider\bin\$(Configuration)\$(NetFrameworkMinimum)\ $(TargetDir)\vstest.console\Extensions\&#xD;&#xA;REM copy net462, net47, net471, net472 and net48 testhosts&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net47\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net47\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net471\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net471\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net472\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net472\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost.x86\bin\$(Configuration)\net48\win7-x86 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\testhost\bin\$(Configuration)\net48\win7-x64 $(TargetDir)\vstest.console\TestHostNetFramework\&#xD;&#xA;REM copy datacollectors&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\datacollector\bin\$(Configuration)\net472\ $(TargetDir)\vstest.console\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\Microsoft.TestPlatform.Extensions.BlameDataCollector\bin\$(Configuration)\net472\ $(TargetDir)\vstest.console\Extensions\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\DataCollectors\Microsoft.TestPlatform.Extensions.EventLogCollector\bin\$(Configuration)\$(NetFrameworkMinimum)\ $(TargetDir)\vstest.console\Extensions\&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\DataCollectors\DumpMinitool\bin\$(Configuration)\$(NetFrameworkMinimum)\\win7-x64\ $(TargetDir)\vstest.console\Extensions\blame&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\DataCollectors\DumpMinitool.x86\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x86\ $(TargetDir)\vstest.console\Extensions\blame&#xD;&#xA;xcopy /i /y /f $(MSBuildProjectDirectory)\..\..\src\DataCollectors\DumpMinitool.arm64\bin\$(Configuration)\$(NetFrameworkMinimum)\win10-arm64\ $(TargetDir)\vstest.console\Extensions\blame" />
</Target>
<Import Project="$(TestPlatformRoot)scripts\build\TestPlatform.targets" />
</Project>

0 comments on commit 8a864b8

Please sign in to comment.