Bootstrapping a test for R2RDump (#42150)
authorAndrew Au <andrewau@microsoft.com>
Sat, 7 Nov 2020 01:41:16 +0000 (17:41 -0800)
committerGitHub <noreply@github.com>
Sat, 7 Nov 2020 01:41:16 +0000 (02:41 +0100)
src/tests/Common/Directory.Build.targets
src/tests/issues.targets
src/tests/r2rdump/R2RDumpTester.cs [new file with mode: 0644]
src/tests/r2rdump/R2RDumpTests.csproj [new file with mode: 0644]

index 93e0f2e..cf31232 100644 (file)
@@ -56,6 +56,9 @@
     <ItemGroup>
       <RunTimeArtifactsIncludeFolders Include="/" />
 
+      <!-- Experiment -->
+      <RunTimeArtifactsIncludeFolders Include="R2RDump/" />
+
       <!-- Used by the Crossgen comparison job -->
       <RunTimeArtifactsIncludeFolders Include="IL/" />
 
index 2a8fe8f..91baede 100644 (file)
         <ExcludeList Include="$(XunitTestBinBase)/profiler/rejit/rejit/rejit.sh">
             <Issue>needs triage</Issue>
         </ExcludeList>
+        <ExcludeList Include="$(XunitTestBinBase)/r2rdump/R2RDumpTests/*">
+            <Issue>These tests are not supposed to be run with mono.</Issue>
+        </ExcludeList>
         <ExcludeList Include="$(XunitTestBinBase)/readytorun/crossgen2/crossgen2smoke/**">
             <Issue>needs triage</Issue>
         </ExcludeList>
diff --git a/src/tests/r2rdump/R2RDumpTester.cs b/src/tests/r2rdump/R2RDumpTester.cs
new file mode 100644 (file)
index 0000000..2d813eb
--- /dev/null
@@ -0,0 +1,80 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.InteropServices;
+using Xunit;
+
+namespace R2RDumpTests
+{
+    public class R2RDumpTester : XunitBase
+    {
+        private const string CoreRoot = "CORE_ROOT";
+        private const string R2RDumpRelativePath = "R2RDump";
+        private const string R2RDumpFile = "R2RDump.dll";
+        private const string CoreRunFileName = "corerun";
+        
+        public static string FindExePath(string exe)
+        {
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            {
+                exe = exe + ".exe";
+            }
+            exe = Environment.ExpandEnvironmentVariables(exe);
+            if (!File.Exists(exe))
+            {
+                if (Path.GetDirectoryName(exe) == String.Empty)
+                {
+                    foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator))
+                    {
+                        string path = test.Trim();
+                        if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exe)))
+                            return Path.GetFullPath(path);
+                    }
+                }
+                throw new FileNotFoundException(new FileNotFoundException().Message, exe);
+            }
+            return Path.GetFullPath(exe);
+        }
+
+        [Fact]
+        public void DumpCoreLib()
+        {
+            string CoreRootVar = Environment.GetEnvironmentVariable(CoreRoot);
+            bool IsUnix = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
+            string R2RDumpAbsolutePath = Path.Combine(CoreRootVar, R2RDumpRelativePath, R2RDumpFile);
+            string CoreLibFile = "System.Private.CoreLib.dll";
+            string CoreLibAbsolutePath = Path.Combine(CoreRootVar, CoreLibFile);
+            string OutputFile = Path.GetTempFileName();
+            string TestDotNetCmdVar = Environment.GetEnvironmentVariable("__TestDotNetCmd");
+            string DotNetAbsolutePath = string.IsNullOrEmpty(TestDotNetCmdVar) ? FindExePath("dotnet") : TestDotNetCmdVar;
+
+            ProcessStartInfo processStartInfo = new ProcessStartInfo
+            {
+                UseShellExecute = false,
+                FileName = DotNetAbsolutePath,
+                // TODO, what flags do we like to test?
+                Arguments = string.Join(" ", new string[]{"exec", R2RDumpAbsolutePath, "--in", CoreLibAbsolutePath, "--out", OutputFile})
+            };
+
+            Process process = Process.Start(processStartInfo);
+            process.WaitForExit();
+            int exitCode = process.ExitCode;
+            string outputContent = File.ReadAllText(OutputFile);
+            File.Delete(OutputFile);
+            // TODO, here is a point where we can add more validation to outputs
+            // An uncaught exception (such as signature decoding error, would be caught by the error code)
+            bool failed = exitCode != 0;
+            if (failed)
+            {
+                Console.WriteLine("The process terminated with exit code {0}", exitCode);
+                Console.WriteLine(outputContent);
+                Assert.True(!failed);
+            }
+        }
+
+        public static int Main(string[] args)
+        {
+            return new R2RDumpTester().RunTests();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/tests/r2rdump/R2RDumpTests.csproj b/src/tests/r2rdump/R2RDumpTests.csproj
new file mode 100644 (file)
index 0000000..a60d535
--- /dev/null
@@ -0,0 +1,19 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <DisableProjectBuild Condition="'$(RuntimeFlavor)' != 'coreclr'">true</DisableProjectBuild>
+    <CLRTestTargetUnsupported Condition="'$(RuntimeFlavor)' != 'coreclr'">true</CLRTestTargetUnsupported>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="R2RDumpTester.cs" />
+    <Compile Include="..\Common\XunitBase.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Remove="Resources\**" />
+    <EmbeddedResource Remove="Resources\**" />
+    <None Remove="Resources\**" />
+  </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="xunit" Version="$(XUnitVersion)" />
+  </ItemGroup>
+</Project>