From e24b901cd115bfb103533c7a4475cc277b30b543 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 25 Feb 2022 23:35:45 +0100 Subject: [PATCH] Build self-contained test runners before running tests. --- .../ICSharpCode.Decompiler.TestRunner.csproj | 3 - ICSharpCode.Decompiler.TestRunner/Program.cs | 20 ++++- .../Helpers/Tester.cs | 77 ++++++------------- 3 files changed, 42 insertions(+), 58 deletions(-) diff --git a/ICSharpCode.Decompiler.TestRunner/ICSharpCode.Decompiler.TestRunner.csproj b/ICSharpCode.Decompiler.TestRunner/ICSharpCode.Decompiler.TestRunner.csproj index cfc349461..c60a25481 100644 --- a/ICSharpCode.Decompiler.TestRunner/ICSharpCode.Decompiler.TestRunner.csproj +++ b/ICSharpCode.Decompiler.TestRunner/ICSharpCode.Decompiler.TestRunner.csproj @@ -4,9 +4,6 @@ Exe net6.0-windows enable - x64 - false - ..\ICSharpCode.Decompiler.Tests\bin\$(Configuration)\$(TargetFramework)\win-x64\ diff --git a/ICSharpCode.Decompiler.TestRunner/Program.cs b/ICSharpCode.Decompiler.TestRunner/Program.cs index 50b04adbd..2326a80d6 100644 --- a/ICSharpCode.Decompiler.TestRunner/Program.cs +++ b/ICSharpCode.Decompiler.TestRunner/Program.cs @@ -1,4 +1,22 @@ -using System; +// Copyright (c) 2022 Siegfried Pammer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; using System.Reflection; using System.Runtime.Loader; diff --git a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs index 40a6b474d..b9715385d 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs +++ b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs @@ -90,6 +90,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers public static readonly string TesterPath; public static readonly string TestCasePath; + static readonly string testRunnerBasePath; static readonly string packagesPropsFile; static readonly string roslynLatestVersion; static readonly RoslynToolset roslynToolset; @@ -99,6 +100,11 @@ namespace ICSharpCode.Decompiler.Tests.Helpers { TesterPath = Path.GetDirectoryName(typeof(Tester).Assembly.Location); TestCasePath = Path.Combine(TesterPath, "../../../../TestCases"); +#if DEBUG + testRunnerBasePath = Path.Combine(TesterPath, "../../../../../ICSharpCode.Decompiler.TestRunner/bin/Debug/net6.0-windows"); +#else + testRunnerBasePath = Path.Combine(TesterPath, "../../../../../ICSharpCode.Decompiler.TestRunner/bin/Release/net6.0-windows"); +#endif packagesPropsFile = Path.Combine(TesterPath, "../../../../../packages.props"); roslynLatestVersion = XDocument.Load(packagesPropsFile).XPathSelectElement("//RoslynVersion").Value; roslynToolset = new RoslynToolset(); @@ -113,6 +119,21 @@ namespace ICSharpCode.Decompiler.Tests.Helpers await roslynToolset.Fetch(roslynLatestVersion).ConfigureAwait(false); await vswhereToolset.Fetch().ConfigureAwait(false); + +#if DEBUG + await BuildTestRunner("win-x86", "Debug").ConfigureAwait(false); + await BuildTestRunner("win-x64", "Debug").ConfigureAwait(false); +#else + await BuildTestRunner("win-x86", "Release").ConfigureAwait(false); + await BuildTestRunner("win-x64", "Release").ConfigureAwait(false); +#endif + } + + static async Task BuildTestRunner(string runtime, string config) + { + await Cli.Wrap("dotnet.exe") + .WithArguments(new[] { "build", Path.Combine(TesterPath, "../../../../../ICSharpCode.Decompiler.TestRunner/ICSharpCode.Decompiler.TestRunner.csproj"), "-r", runtime, "-c", config, "--self-contained" }) + .ExecuteAsync(); } public static async Task AssembleIL(string sourceFileName, AssemblerOptions options = AssemblerOptions.UseDebug) @@ -617,10 +638,8 @@ namespace ICSharpCode.Decompiler.Tests.Helpers public static async Task<(int ExitCode, string Output, string Error)> RunWithTestRunner(string assemblyFileName, bool force32Bit) { - string pathToRunner = force32Bit - ? Path.Combine(TesterPath, "ICSharpCode.Decompiler.TestRunner32.exe") - : Path.Combine(TesterPath, "ICSharpCode.Decompiler.TestRunner.exe"); - var command = Cli.Wrap(pathToRunner) + string testRunner = Path.Combine(testRunnerBasePath, force32Bit ? "win-x86" : "win-x64", "ICSharpCode.Decompiler.TestRunner.exe"); + var command = Cli.Wrap(testRunner) .WithArguments(assemblyFileName) .WithValidation(CommandResultValidation.None); @@ -628,57 +647,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers return (result.ExitCode, result.StandardOutput, result.StandardError); } - /* - public static int RunInContainer(string assemblyFileName, out string output, out string error) - { - AssemblyLoadContext context = new("RunInContainer", isCollectible: true); - context.Resolving += ContextResolving; - var (oldOut, newOut) = WrapOut(); - var (oldError, newError) = WrapError(); - - int result; - - try - { - var mainAssembly = context.LoadFromAssemblyPath(assemblyFileName); - var tmp = mainAssembly.EntryPoint!.Invoke(null, Array.Empty()); - result = tmp is int i ? i : 0; - } - finally - { - context.Unload(); - context.Resolving -= ContextResolving; - Console.SetOut(oldOut); - Console.SetError(oldError); - } - - output = newOut.ToString(); - error = newError.ToString(); - return result; - - static Assembly ContextResolving(AssemblyLoadContext context, AssemblyName name) - { - return null; - } - - static (TextWriter oldWriter, StringBuilder newOutput) WrapOut() - { - var oldWriter = Console.Out; - var newOutput = new StringBuilder(); - Console.SetOut(new StringWriter(newOutput)); - return (oldWriter, newOutput); - } - - static (TextWriter oldWriter, StringBuilder newOutput) WrapError() - { - var oldWriter = Console.Error; - var newOutput = new StringBuilder(); - Console.SetError(new StringWriter(newOutput)); - return (oldWriter, newOutput); - } - } - */ public static Task DecompileCSharp(string assemblyFileName, DecompilerSettings settings = null) { if (settings == null)