/
CrossDomainConeRuner.cs
195 lines (160 loc) · 5.71 KB
/
CrossDomainConeRuner.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using Cone.Core;
namespace Cone.Runners
{
public interface ICrossDomainLogger
{
void Info(string message);
void Failure(string file, int line, int column, string message);
}
class CrossDomainSessionLoggerAdapter : ISessionLogger, ISuiteLogger, ITestLogger
{
readonly ICrossDomainLogger crossDomainLog;
public CrossDomainSessionLoggerAdapter(ICrossDomainLogger crossDomainLog) {
this.crossDomainLog = crossDomainLog;
}
public bool ShowProgress { get; set; }
public void WriteInfo(Action<ISessionWriter> output) {
var result = new StringWriter();
output(new TextSessionWriter(result));
crossDomainLog.Info(result.ToString());
}
public void BeginSession() { }
public ISuiteLogger BeginSuite(IConeSuite suite) {
return this;
}
public void EndSuite() { }
public ITestLogger BeginTest(IConeTest test) {
return this;
}
public void EndSession() { }
void ITestLogger.Failure(ConeTestFailure failure) {
crossDomainLog.Failure(
failure.File,
failure.Line,
failure.Column,
failure.Message);
}
void ITestLogger.Success() {
if (ShowProgress)
crossDomainLog.Info(".");
}
void ITestLogger.Pending(string reason) {
if (ShowProgress)
crossDomainLog.Info("?");
}
void ITestLogger.Skipped() { }
void ITestLogger.EndTest() { }
}
public class CrossDomainConeRunner
{
struct ResolveCandidate
{
public ResolveCandidate(string path) {
this.Path = path;
this.Name = System.IO.Path.GetFileNameWithoutExtension(path);
}
public readonly string Path;
public readonly string Name;
}
[Serializable]
class RunTestsCommand
{
public ICrossDomainLogger Logger;
public string[] AssemblyPaths;
public void Execute() {
var logger = new CrossDomainSessionLoggerAdapter(Logger) {
ShowProgress = false
};
new SimpleConeRunner().RunTests(new TestSession(logger), LoadTestAssemblies(AssemblyPaths, error => Logger.Info(error)));
}
}
static T WithTestDomain<T>(string configPath, string[] assemblyPaths, Func<AppDomain,T> @do) {
var domainSetup = new AppDomainSetup {
ApplicationBase = Path.GetDirectoryName(assemblyPaths.First()),
ShadowCopyFiles = "False",
};
if(string.IsNullOrEmpty(configPath) && assemblyPaths.Length == 1)
configPath = Path.GetFullPath(assemblyPaths[0] + ".config");
if(File.Exists(configPath))
domainSetup.ConfigurationFile = configPath;
var conePath = new Uri(typeof(CrossDomainConeRunner).Assembly.CodeBase).LocalPath;
var localCone = Path.Combine(domainSetup.ApplicationBase, Path.GetFileName(conePath));
if(!File.Exists(localCone))
File.Copy(conePath, localCone);
var testDomain = AppDomain.CreateDomain("Cone.TestDomain",
AppDomain.CurrentDomain.Evidence,
domainSetup,
new PermissionSet(PermissionState.Unrestricted));
Environment.CurrentDirectory = Path.GetFullPath(Path.GetDirectoryName(assemblyPaths.First()));
testDomain.SetData("assemblyPaths", assemblyPaths);
testDomain.Load(File.ReadAllBytes(new Uri(typeof(CrossDomainConeRunner).Assembly.CodeBase).LocalPath));
testDomain.AssemblyResolve += (_, e) => {
var candidates = (ResolveCandidate[])AppDomain.CurrentDomain.GetData("candidatePaths");
if(candidates == null) {
candidates = CandidateResolvePaths((string[])AppDomain.CurrentDomain.GetData("assemblyPaths"));
AppDomain.CurrentDomain.SetData("candidatePaths", candidates);
AppDomain.CurrentDomain.SetData("assemblyPaths", null);
}
var name = e.Name.Split(',')[0];
var found = Array.FindIndex(candidates, x => x.Name == name);
if(found != -1) {
return Assembly.LoadFrom(candidates[found].Path);
}
return null;
};
try {
return @do(testDomain);
} finally {
AppDomain.Unload(testDomain);
}
}
static ResolveCandidate[] CandidateResolvePaths(string[] assemblyPaths) {
return
assemblyPaths.ConvertAll(x => Path.GetDirectoryName(x))
.SelectMany(x => Directory.GetFiles(x).Where(IsExeOrDll))
.Select(x => new ResolveCandidate(x))
.ToArray();
}
static bool IsExeOrDll(string path) {
var ext = Path.GetExtension(path);
return ext == ".dll" || ext == ".exe";
}
public static TResult WithProxyInDomain<T,TResult>(string configPath, string[] assemblyPaths, Func<T, TResult> @do) {
return WithTestDomain(configPath, assemblyPaths, testDomain => {
var proxy = (T)testDomain.CreateInstanceFrom(typeof(T).Assembly.Location, typeof(T).FullName).Unwrap();
return @do(proxy);
});
}
public static IEnumerable<Assembly> LoadTestAssemblies(string[] assemblyPaths, Action<string> logError) {
if(assemblyPaths.IsEmpty())
throw new ArgumentException("No test assemblies specified");
var testAssemblies = new List<Assembly>();
for(var i = 0; i != assemblyPaths.Length; ++i)
try {
testAssemblies.Add(Assembly.LoadFrom(Path.GetFullPath(assemblyPaths[i])));
}
catch (FileNotFoundException) {
logError("Failed to load: " + assemblyPaths[i]);
}
return testAssemblies;
}
public static void RunTestsInTemporaryDomain(ICrossDomainLogger logger, string configPath, string[] assemblyPaths) {
WithTestDomain(configPath, assemblyPaths, testDomin => {
var runTests = new RunTestsCommand
{
Logger = logger,
AssemblyPaths = assemblyPaths
};
testDomin.DoCallBack(runTests.Execute);
return 0;
});
}
}
}