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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Extensions;
using NUnit.Framework;
namespace Mono.Linker.Tests.TestCasesRunner {
public class ResultChecker
{
readonly BaseAssemblyResolver _originalsResolver;
readonly BaseAssemblyResolver _linkedResolver;
public ResultChecker ()
: this(new DefaultAssemblyResolver (), new DefaultAssemblyResolver ())
{
}
public ResultChecker (BaseAssemblyResolver originalsResolver, BaseAssemblyResolver linkedResolver)
{
_originalsResolver = originalsResolver;
_linkedResolver = linkedResolver;
}
public virtual void Check (LinkedTestCaseResult linkResult)
{
Assert.IsTrue (linkResult.OutputAssemblyPath.FileExists (), $"The linked output assembly was not found. Expected at {linkResult.OutputAssemblyPath}");
InitializeResolvers (linkResult);
try
{
var original = ResolveOriginalsAssembly (linkResult.ExpectationsAssemblyPath.FileNameWithoutExtension);
PerformOutputAssemblyChecks (original, linkResult.OutputAssemblyPath.Parent);
var linked = ResolveLinkedAssembly (linkResult.OutputAssemblyPath.FileNameWithoutExtension);
new AssemblyChecker (original, linked).Verify ();
VerifyLinkingOfOtherAssemblies (original);
}
finally
{
_originalsResolver.Dispose ();
_linkedResolver.Dispose ();
}
}
void InitializeResolvers (LinkedTestCaseResult linkedResult)
{
_originalsResolver.AddSearchDirectory (linkedResult.ExpectationsAssemblyPath.Parent.ToString ());
_linkedResolver.AddSearchDirectory (linkedResult.OutputAssemblyPath.Parent.ToString ());
}
AssemblyDefinition ResolveLinkedAssembly (string assemblyName)
{
var cleanAssemblyName = assemblyName;
if (assemblyName.EndsWith(".exe") || assemblyName.EndsWith(".dll"))
cleanAssemblyName = System.IO.Path.GetFileNameWithoutExtension (assemblyName);
return _linkedResolver.Resolve (new AssemblyNameReference (cleanAssemblyName, null));
}
AssemblyDefinition ResolveOriginalsAssembly (string assemblyName)
{
var cleanAssemblyName = assemblyName;
if (assemblyName.EndsWith (".exe") || assemblyName.EndsWith (".dll"))
cleanAssemblyName = Path.GetFileNameWithoutExtension (assemblyName);
return _originalsResolver.Resolve (new AssemblyNameReference (cleanAssemblyName, null));
}
void PerformOutputAssemblyChecks (AssemblyDefinition original, NPath outputDirectory)
{
var assembliesToCheck = original.MainModule.Types.SelectMany (t => t.CustomAttributes).Where (attr => ExpectationsProvider.IsAssemblyAssertion(attr));
foreach (var assemblyAttr in assembliesToCheck) {
var name = (string) assemblyAttr.ConstructorArguments.First ().Value;
var expectedPath = outputDirectory.Combine (name);
if (assemblyAttr.AttributeType.Name == nameof (RemovedAssemblyAttribute))
Assert.IsFalse (expectedPath.FileExists (), $"Expected the assembly {name} to not exist in {outputDirectory}, but it did");
else if (assemblyAttr.AttributeType.Name == nameof (KeptAssemblyAttribute))
Assert.IsTrue (expectedPath.FileExists (), $"Expected the assembly {name} to exist in {outputDirectory}, but it did not");
else
throw new NotImplementedException($"Unknown assembly assertion of type {assemblyAttr.AttributeType}");
}
}
void VerifyLinkingOfOtherAssemblies (AssemblyDefinition original)
{
var checks = BuildOtherAssemblyCheckTable (original);
foreach (var assemblyName in checks.Keys) {
using (var linkedAssembly = ResolveLinkedAssembly (assemblyName)) {
foreach (var checkAttrInAssembly in checks [assemblyName]) {
var expectedTypeName = checkAttrInAssembly.ConstructorArguments [1].Value.ToString ();
var linkedType = linkedAssembly.MainModule.GetType (expectedTypeName);
if (checkAttrInAssembly.AttributeType.Name == nameof (RemovedTypeInAssemblyAttribute)) {
if (linkedType != null)
Assert.Fail ($"Type `{expectedTypeName}' should have been removed");
} else if (checkAttrInAssembly.AttributeType.Name == nameof (KeptTypeInAssemblyAttribute)) {
if (linkedType == null)
Assert.Fail ($"Type `{expectedTypeName}' should have been kept");
} else if (checkAttrInAssembly.AttributeType.Name == nameof (RemovedMemberInAssemblyAttribute)) {
if (linkedType == null)
continue;
VerifyRemovedMemberInAssembly (checkAttrInAssembly, linkedType);
} else if (checkAttrInAssembly.AttributeType.Name == nameof (KeptMemberInAssemblyAttribute)) {
if (linkedType == null)
Assert.Fail ($"Type `{expectedTypeName}' should have been kept");
VerifyKeptMemberInAssembly (checkAttrInAssembly, linkedType);
} else {
throw new NotImplementedException ($"Type {expectedTypeName}, has an unknown other assembly attribute of type {checkAttrInAssembly.AttributeType}");
}
}
}
}
}
void VerifyRemovedMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType)
{
var originalType = GetOriginalTypeFromInAssemblyAttribute (inAssemblyAttribute);
foreach (var memberNameAttr in (CustomAttributeArgument[]) inAssemblyAttribute.ConstructorArguments [2].Value) {
string memberName = (string) memberNameAttr.Value;
// We will find the matching type from the original assembly first that way we can confirm
// that the name defined in the attribute corresponds to a member that actually existed
var originalFieldMember = originalType.Fields.FirstOrDefault (m => m.Name == memberName);
if (originalFieldMember != null) {
var linkedField = linkedType.Fields.FirstOrDefault (m => m.Name == memberName);
if (linkedField != null)
Assert.Fail ($"Field `{memberName}` on Type `{originalType}` should have been removed");
continue;
}
var originalPropertyMember = originalType.Properties.FirstOrDefault (m => m.Name == memberName);
if (originalPropertyMember != null) {
var linkedProperty = linkedType.Properties.FirstOrDefault (m => m.Name == memberName);
if (linkedProperty != null)
Assert.Fail ($"Property `{memberName}` on Type `{originalType}` should have been removed");
continue;
}
var originalMethodMember = originalType.Methods.FirstOrDefault (m => m.GetSignature () == memberName);
if (originalMethodMember != null) {
var linkedMethod = linkedType.Methods.FirstOrDefault (m => m.GetSignature () == memberName);
if (linkedMethod != null)
Assert.Fail ($"Method `{memberName}` on Type `{originalType}` should have been removed");
continue;
}
Assert.Fail ($"Invalid test assertion. No member named `{memberName}` exists on the original type `{originalType}`");
}
}
void VerifyKeptMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType)
{
var originalType = GetOriginalTypeFromInAssemblyAttribute (inAssemblyAttribute);
foreach (var memberNameAttr in (CustomAttributeArgument[]) inAssemblyAttribute.ConstructorArguments [2].Value) {
string memberName = (string) memberNameAttr.Value;
// We will find the matching type from the original assembly first that way we can confirm
// that the name defined in the attribute corresponds to a member that actually existed
var originalFieldMember = originalType.Fields.FirstOrDefault (m => m.Name == memberName);
if (originalFieldMember != null) {
var linkedField = linkedType.Fields.FirstOrDefault (m => m.Name == memberName);
if (linkedField == null)
Assert.Fail ($"Field `{memberName}` on Type `{originalType}` should have been kept");
continue;
}
var originalPropertyMember = originalType.Properties.FirstOrDefault (m => m.Name == memberName);
if (originalPropertyMember != null) {
var linkedProperty = linkedType.Properties.FirstOrDefault (m => m.Name == memberName);
if (linkedProperty == null)
Assert.Fail ($"Property `{memberName}` on Type `{originalType}` should have been kept");
continue;
}
var originalMethodMember = originalType.Methods.FirstOrDefault (m => m.GetSignature () == memberName);
if (originalMethodMember != null) {
var linkedMethod = linkedType.Methods.FirstOrDefault (m => m.GetSignature () == memberName);
if (linkedMethod == null)
Assert.Fail ($"Method `{memberName}` on Type `{originalType}` should have been kept");
continue;
}
Assert.Fail ($"Invalid test assertion. No member named `{memberName}` exists on the original type `{originalType}`");
}
}
TypeDefinition GetOriginalTypeFromInAssemblyAttribute (CustomAttribute inAssemblyAttribute)
{
var attributeValueAsTypeReference = inAssemblyAttribute.ConstructorArguments [1].Value as TypeReference;
if (attributeValueAsTypeReference != null)
return attributeValueAsTypeReference.Resolve ();
var assembly = ResolveOriginalsAssembly (inAssemblyAttribute.ConstructorArguments [0].Value.ToString ());
return assembly.MainModule.GetType (inAssemblyAttribute.ConstructorArguments [1].Value.ToString ());
}
static Dictionary<string, List<CustomAttribute>> BuildOtherAssemblyCheckTable (AssemblyDefinition original)
{
var checks = new Dictionary<string, List<CustomAttribute>> ();
foreach (var typeWithRemoveInAssembly in original.AllDefinedTypes ()) {
foreach (var attr in typeWithRemoveInAssembly.CustomAttributes.Where (IsTypeInOtherAssemblyAssertion)) {
var assemblyName = (string) attr.ConstructorArguments [0].Value;
List<CustomAttribute> checksForAssembly;
if (!checks.TryGetValue (assemblyName, out checksForAssembly))
checks [assemblyName] = checksForAssembly = new List<CustomAttribute> ();
checksForAssembly.Add (attr);
}
}
return checks;
}
static bool IsTypeInOtherAssemblyAssertion (CustomAttribute attr)
{
return attr.AttributeType.Name == nameof (RemovedTypeInAssemblyAttribute)
|| attr.AttributeType.Name == nameof (KeptTypeInAssemblyAttribute)
|| attr.AttributeType.Name == nameof (RemovedMemberInAssemblyAttribute)
|| attr.AttributeType.Name == nameof (KeptMemberInAssemblyAttribute);
}
}
}
|