-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
OutOfProcessTest.cs
146 lines (126 loc) · 5.04 KB
/
OutOfProcessTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CoreclrTestLib;
using Xunit;
namespace TestLibrary
{
public static class OutOfProcessTest
{
internal static bool runningInWindows;
internal static string reportBase;
internal static string testBinaryBase;
internal static string helixUploadRoot;
static OutOfProcessTest()
{
reportBase = Directory.GetCurrentDirectory();
testBinaryBase = AppContext.BaseDirectory;
helixUploadRoot = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT");
if (!String.IsNullOrEmpty(helixUploadRoot))
{
reportBase = Path.Combine(Path.GetFullPath(helixUploadRoot), "Reports");
}
if (String.IsNullOrEmpty(reportBase))
{
reportBase = Path.Combine(testBinaryBase, "Reports");
}
else
{
reportBase = Path.GetFullPath(reportBase);
}
}
public static bool OutOfProcessTestsSupported =>
!OperatingSystem.IsIOS()
&& !OperatingSystem.IsTvOS()
&& !OperatingSystem.IsAndroid()
&& !OperatingSystem.IsBrowser()
&& !OperatingSystem.IsOSPlatform("Wasi");
public static void RunOutOfProcessTest(string assemblyPath)
{
int ret = -100;
string baseDir = AppContext.BaseDirectory;
string outputDir = System.IO.Path.GetFullPath(Path.Combine(reportBase, Path.GetDirectoryName(assemblyPath)));
string outputFile = Path.Combine(outputDir, "output.txt");
string errorFile = Path.Combine(outputDir, "error.txt");
string testExecutable = null;
Exception infraEx = null;
try
{
CoreclrTestWrapperLib wrapper = new CoreclrTestWrapperLib();
if (OperatingSystem.IsWindows())
{
testExecutable = Path.Combine(baseDir, Path.ChangeExtension(assemblyPath, ".cmd"));
}
else
{
testExecutable = Path.Combine(baseDir, Path.ChangeExtension(assemblyPath.Replace("\\", "/"), ".sh"));
}
if (!File.Exists(testExecutable))
{
// Skip platform-specific test when running on the excluded platform
return;
}
System.IO.Directory.CreateDirectory(outputDir);
ret = wrapper.RunTest(testExecutable, outputFile, errorFile, Assembly.GetEntryAssembly()!.FullName!, testBinaryBase, outputDir);
}
catch (Exception ex)
{
infraEx = ex;
}
if (infraEx != null)
{
Assert.Fail("Test Infrastructure Failure: " + infraEx.ToString());
}
else
{
List<string> testOutput = new List<string>();
try
{
testOutput.AddRange(System.IO.File.ReadAllLines(errorFile));
}
catch (Exception ex)
{
testOutput.Add("Unable to read error file: " + errorFile);
testOutput.Add(ex.ToString());
}
testOutput.Add(string.Empty);
testOutput.Add("Return code: " + ret);
testOutput.Add("Raw output file: " + outputFile);
testOutput.Add("Raw output:");
try
{
testOutput.AddRange(System.IO.File.ReadAllLines(outputFile));
}
catch (Exception ex)
{
testOutput.Add("Unable to read output file: " + outputFile);
testOutput.Add(ex.ToString());
}
testOutput.Add("To run the test:");
testOutput.Add("Set up CORE_ROOT and run.");
testOutput.Add("> " + testExecutable);
var unicodeControlCharsRegex = new Regex("%5C%5Cp{C}+");
// Remove all characters that have no visual or spatial representation.
for (int i = 0; i < testOutput.Count; i++)
{
string line = testOutput[i];
line = unicodeControlCharsRegex.Replace(line, string.Empty);
testOutput[i] = line;
}
foreach (string line in testOutput)
{
Console.WriteLine(line);
}
Assert.True(ret == CoreclrTestWrapperLib.EXIT_SUCCESS_CODE, string.Join(Environment.NewLine, testOutput));
}
}
}
}