Skip to content

Commit

Permalink
RemoteExecutor: support single file and NativeAOT
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWise committed Nov 4, 2022
1 parent 80b6be4 commit 149e5f0
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 29 deletions.
122 changes: 122 additions & 0 deletions src/Microsoft.DotNet.RemoteExecutor/SingleFileTests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;
using System.Xml.Linq;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

// @TODO medium-to-longer term, we should try to get rid of the special-unicorn-single-file runner in favor of making the real runner work for single file.
// https://github.com/dotnet/runtime/issues/70432
public class SingleFileTestRunner : XunitTestFramework
{
private SingleFileTestRunner(IMessageSink messageSink)
: base(messageSink) { }

public static int Main(string[] args)
{
int? maybeExitCode = Microsoft.DotNet.RemoteExecutor.Program.TryExecute(args);
if (maybeExitCode.HasValue)
{
return maybeExitCode.Value;
}

var asm = typeof(SingleFileTestRunner).Assembly;
Console.WriteLine("Running assembly:" + asm.FullName);

var diagnosticSink = new ConsoleDiagnosticMessageSink();
var testsFinished = new TaskCompletionSource();
var testSink = new TestMessageSink();
var summarySink = new DelegatingExecutionSummarySink(testSink,
() => false,
(completed, summary) => Console.WriteLine($"Tests run: {summary.Total}, Errors: {summary.Errors}, Failures: {summary.Failed}, Skipped: {summary.Skipped}. Time: {TimeSpan.FromSeconds((double)summary.Time).TotalSeconds}s"));
var resultsXmlAssembly = new XElement("assembly");
var resultsSink = new DelegatingXmlCreationSink(summarySink, resultsXmlAssembly);

testSink.Execution.TestSkippedEvent += args => { Console.WriteLine($"[SKIP] {args.Message.Test.DisplayName}"); };
testSink.Execution.TestFailedEvent += args => { Console.WriteLine($"[FAIL] {args.Message.Test.DisplayName}{Environment.NewLine}{Xunit.ExceptionUtility.CombineMessages(args.Message)}{Environment.NewLine}{Xunit.ExceptionUtility.CombineStackTraces(args.Message)}"); };

testSink.Execution.TestAssemblyFinishedEvent += args =>
{
Console.WriteLine($"Finished {args.Message.TestAssembly.Assembly}{Environment.NewLine}");
testsFinished.SetResult();
};

var assemblyConfig = new TestAssemblyConfiguration()
{
// Turn off pre-enumeration of theories, since there is no theory selection UI in this runner
PreEnumerateTheories = false,
};

var xunitTestFx = new SingleFileTestRunner(diagnosticSink);
var asmInfo = Reflector.Wrap(asm);
var asmName = asm.GetName();

var discoverySink = new TestDiscoverySink();
var discoverer = xunitTestFx.CreateDiscoverer(asmInfo);
discoverer.Find(false, discoverySink, TestFrameworkOptions.ForDiscovery(assemblyConfig));
discoverySink.Finished.WaitOne();

string xmlResultFileName = null;
XunitFilters filters = new XunitFilters();
// Quick hack wo much validation to get args that are passed (notrait, xml)
Dictionary<string, List<string>> noTraits = new Dictionary<string, List<string>>();
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("-notrait", StringComparison.OrdinalIgnoreCase))
{
var traitKeyValue=args[i + 1].Split("=", StringSplitOptions.TrimEntries);
if (!noTraits.TryGetValue(traitKeyValue[0], out List<string> values))
{
noTraits.Add(traitKeyValue[0], values = new List<string>());
}
values.Add(traitKeyValue[1]);
}
if (args[i].Equals("-xml", StringComparison.OrdinalIgnoreCase))
{
xmlResultFileName=args[i + 1].Trim();
}
}

foreach (KeyValuePair<string, List<string>> kvp in noTraits)
{
filters.ExcludedTraits.Add(kvp.Key, kvp.Value);
}

var filteredTestCases = discoverySink.TestCases.Where(filters.Filter).ToList();
var executor = xunitTestFx.CreateExecutor(asmName);
executor.RunTests(filteredTestCases, resultsSink, TestFrameworkOptions.ForExecution(assemblyConfig));

resultsSink.Finished.WaitOne();

// Helix need to see results file in the drive to detect if the test has failed or not
if(xmlResultFileName != null)
{
resultsXmlAssembly.Save(xmlResultFileName);
}

var failed = resultsSink.ExecutionSummary.Failed > 0 || resultsSink.ExecutionSummary.Errors > 0;
return failed ? 1 : 0;
}
}

internal class ConsoleDiagnosticMessageSink : IMessageSink
{
public bool OnMessage(IMessageSinkMessage message)
{
if (message is IDiagnosticMessage diagnosticMessage)
{
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Microsoft.DotNet.RemoteExecutor.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\tests\RemoteExecutorTests.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.4.2" ExcludeAssets="build" />
<PackageReference Include="xunit.analyzers" Version="1.0.0" ExcludeAssets="build" />
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" Version="8.0.0-beta.22524.5" />
<PackageReference Include="xunit.runner.utility" Version="2.4.2" />
</ItemGroup>

<PropertyGroup Condition="'$(PublishSingleFile)' == 'true'">
<UseAppHost>true</UseAppHost>
<SelfContained>true</SelfContained>
</PropertyGroup>

<ItemGroup Condition="'$(PublishAot)' == 'true'">
<RdXmlFile Include="rd.xml" />
<IlcArg Include="--nometadatablocking" />
<IlcArg Include="--feature:System.Reflection.IsTypeConstructionEagerlyValidated=false" />
</ItemGroup>

<PropertyGroup Condition="'$(PublishAot)' == 'true'">
<NoWarn>$(NoWarn);IL1005;IL3002</NoWarn>
<TrimMode>partial</TrimMode>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<SuppressAotAnalysisWarnings>true</SuppressAotAnalysisWarnings>

<!-- Forced by ILLink targets; we should fix the SDK -->
<SelfContained>true</SelfContained>
</PropertyGroup>

</Project>
82 changes: 82 additions & 0 deletions src/Microsoft.DotNet.RemoteExecutor/SingleFileTests/rd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<Directives>
<Application>
<!-- xunit will reflect on these to process certain InlineDataAttributes -->
<Assembly Name="System.Linq">
<Type Name="System.Linq.Enumerable">
<Method Name="Cast">
<GenericArgument Name="System.Char, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Char, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Boolean, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Boolean, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Byte, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Byte, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.SByte, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.SByte, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Int16, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Int16, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.UInt16, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.UInt16, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Int32, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Int32, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.UInt32, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.UInt32, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Int64, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Int64, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.UInt64, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.UInt64, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Single, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Single, System.Private.CoreLib" />
</Method>
<Method Name="Cast">
<GenericArgument Name="System.Double, System.Private.CoreLib" />
</Method>
<Method Name="ToArray">
<GenericArgument Name="System.Double, System.Private.CoreLib" />
</Method>
</Type>
</Assembly>
</Application>
</Directives>

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<IsPackable>true</IsPackable>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);IL3000</NoWarn>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<SuppressAotAnalysisWarnings>true</SuppressAotAnalysisWarnings>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 31 additions & 1 deletion src/Microsoft.DotNet.RemoteExecutor/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,40 @@ namespace Microsoft.DotNet.RemoteExecutor
/// <summary>
/// Provides an entry point in a new process that will load a specified method and invoke it.
/// </summary>
internal static class Program
public static class Program
{
private static int Main(string[] args)
{
int? maybeExitCode = TryExecute(args);
if (maybeExitCode.HasValue)
{
return maybeExitCode.Value;
}

// we should not get here
Console.Error.WriteLine("Remote executor EXE started, but missing magic environmental variable: " + RemoteExecutor.REMOTE_EXECUTOR_ENVIRONMENTAL_VARIABLE);
return -1;
}

/// <summary>
/// Checks if the command line arguments are for the remote executor. If so, attempts to parse and execute the remote function.
/// </summary>
/// <remarks>
/// This entry point is intended be called by single-file test hosts. It allows one applicaiton to both hosts the tests
/// and host the remote executor.
/// This method may exit the process before returning.
/// </remarks>
/// <returns>null the arguments are not for the remote executor, otherwise the exit code for the process as a result of running the remote executor</returns>
public static int? TryExecute(string[] args)
{
if (Environment.GetEnvironmentVariable(RemoteExecutor.REMOTE_EXECUTOR_ENVIRONMENTAL_VARIABLE) is null)
{
return null;
}

// Allow the remote executor to also start more remote executors.
Environment.SetEnvironmentVariable(RemoteExecutor.REMOTE_EXECUTOR_ENVIRONMENTAL_VARIABLE, null);

// The program expects to be passed the target assembly name to load, the type
// from that assembly to find, and the method from that assembly to invoke.
// Any additional arguments are passed as strings to the method.
Expand Down
Loading

0 comments on commit 149e5f0

Please sign in to comment.