forked from nunit/nunit-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-tests.cake
393 lines (347 loc) · 13.8 KB
/
package-tests.cake
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Representation of a single test to be run against a pre-built package.
public struct PackageTest
{
public string Name;
public string Description;
public string Arguments;
public ExpectedResult ExpectedResult;
public PackageTest(string name, string description, string arguments, ExpectedResult expectedResult)
{
Name = name;
Description = description;
Arguments = arguments;
ExpectedResult = expectedResult;
// Add Common tests here - currently there are none
// Derived classes may add more tests
}
}
// Abstract base for all package testers.
public abstract class PackageTester
{
protected ICakeContext _context;
protected string _packageVersion;
protected string _packageDir;
protected string _config;
protected string _outputDir;
public PackageTester(ICakeContext context, string packageVersion)
{
_context = context;
_packageVersion = packageVersion;
_packageDir = System.IO.Path.GetFullPath(context.Argument("artifact-dir", "package")) + "/";
_config = context.Argument("configuration", "Release");
_outputDir = System.IO.Path.GetFullPath($"bin/{_config}/");
PackageTests = new List<PackageTest>();
}
protected abstract string PackageName { get; }
protected abstract string PackageInstallDirectory { get; }
protected abstract string PackageResultDirectory { get; }
protected abstract string PackageBinDir { get; }
protected string PackageUnderTest => _packageDir + PackageName;
protected List<PackageTest> PackageTests { get; }
public void RunTests()
{
Console.WriteLine("Testing package " + PackageName);
Console.WriteLine($"Creating Test Directory:\n {PackageInstallDirectory}");
CreatePackageInstallDirectory();
RunPackageTests();
}
// Default is to just unzip... individual package testers may override
protected virtual void CreatePackageInstallDirectory()
{
_context.CleanDirectory(PackageInstallDirectory);
_context.Unzip(PackageUnderTest, PackageInstallDirectory);
}
private void RunPackageTests()
{
var reporter = new ResultReporter(PackageName);
_context.CleanDirectory(PackageResultDirectory);
foreach (var packageTest in PackageTests)
{
var resultDir = PackageResultDirectory + packageTest.Name + "/";
var resultFile = resultDir + "TestResult.xml";
DisplayBanner(packageTest.Description);
int rc = _context.StartProcess(
PackageBinDir + "nunit3-console.exe",
new ProcessSettings()
{
Arguments = $"{packageTest.Arguments} --work={resultDir}",
WorkingDirectory = _outputDir
});
try
{
var result = new ActualResult(resultFile);
var report = new TestReport(packageTest, result);
reporter.AddReport(report);
Console.WriteLine($"Ran tests in {_outputDir}");
Console.WriteLine(report.Errors.Count == 0
? "\nSUCCESS: Test Result matches expected result!"
: "\nERROR: Test Result not as expected!");
}
catch (Exception ex)
{
reporter.AddReport(new TestReport(packageTest, ex));
Console.WriteLine("\nERROR: No result found!");
}
}
bool hadErrors = reporter.ReportResults();
Console.WriteLine();
if (hadErrors)
throw new Exception("One or more package tests had errors!");
}
private void DisplayBanner(string message)
{
Console.WriteLine("\n=================================================="); ;
Console.WriteLine(message);
Console.WriteLine("==================================================");
}
}
// These are tests using the .NET Framework build of the console runner.
// However, they now include running tests under .Net Core.
public abstract class NetFXPackageTester : PackageTester
{
static readonly string DOTNET_EXE_X86 = @"C:\Program Files (x86)\dotnet\dotnet.exe";
public NetFXPackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion)
{
bool dotnetX86Available = _context.IsRunningOnWindows() && System.IO.File.Exists(DOTNET_EXE_X86);
// Add common tests for running under .NET Framework
PackageTests.Add(new PackageTest(
"net35",
"Run mock-assembly.dll under .NET 3.5",
"net35/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"net35-x86",
"Run mock-assembly-x86.dll under .NET 3.5",
"net35/mock-assembly-x86.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"net40",
"Run mock-assembly.dll under .NET 4.x",
"net40/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"net40-x86",
"Run mock-assembly-x86.dll under .NET 4.x",
"net40/mock-assembly-x86.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"net35_plus_net40",
"Run both copies of mock-assembly together",
"net35/mock-assembly.dll net40/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 2 * 37,
Passed = 2 * 23,
Failed = 2 * 5,
Warnings = 2 * 1,
Inconclusive = 2 * 1,
Skipped = 2 * 7
}));
PackageTests.Add(new PackageTest(
"netcoreapp3.1",
"Run mock-assembly.dll under .NET Core 3.1",
"netcoreapp3.1/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
if (dotnetX86Available)
PackageTests.Add(new PackageTest(
"netcoreapp3.1-x86",
"Run mock-assembly-x86.dll under .NET Core 3.1",
"netcoreapp3.1/mock-assembly-x86.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
}
}
public abstract class NetCorePackageTester : PackageTester
{
public NetCorePackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion)
{
// Add common tests for running under .NET Core (2.1 or higher)
PackageTests.Add(new PackageTest(
"netcoreapp2.1",
"Run mock-assembly.dll targeting .NET Core 2.1",
"netcoreapp2.1/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"netcoreapp3.1",
"Run mock-assembly targeting .NET Core 3.1",
"netcoreapp3.1/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 37,
Passed = 23,
Failed = 5,
Warnings = 1,
Inconclusive = 1,
Skipped = 7
}));
PackageTests.Add(new PackageTest(
"netcoreapp2.1_plus_netcoreapp3.1",
"Run both copies of mock-assembly together",
"netcoreapp2.1/mock-assembly.dll netcoreapp3.1/mock-assembly.dll",
new ExpectedResult("Failed")
{
Total = 2 * 37,
Passed = 2 * 23,
Failed = 2 * 5,
Warnings = 2 * 1,
Inconclusive = 2 * 1,
Skipped = 2 * 7
}));
}
}
public class NuGetNetFXPackageTester : NetFXPackageTester
{
public NuGetNetFXPackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion) { }
protected override string PackageName => $"NUnit.ConsoleRunner.{_packageVersion}.nupkg";
protected override string PackageInstallDirectory => _packageDir + "test/nuget-netfx/";
protected override string PackageBinDir => PackageInstallDirectory + "tools/";
protected override string PackageResultDirectory => _packageDir + "test-results/nuget-netfx/";
}
public class NuGetNetCorePackageTester : NetCorePackageTester
{
public NuGetNetCorePackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion) { }
protected override string PackageName => $"NUnit.ConsoleRunner.NetCore.{_packageVersion}.nupkg";
protected override string PackageInstallDirectory => _packageDir + "test/nuget-netcore/";
protected override string PackageBinDir => PackageInstallDirectory + "tools/netcoreapp3.1/any/";
protected override string PackageResultDirectory => _packageDir + "test-results/nuget-netcore/";
}
public class ChocolateyPackageTester : NetFXPackageTester
{
public ChocolateyPackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion) { }
protected override string PackageName => $"nunit-console-runner.{_packageVersion}.nupkg";
protected override string PackageInstallDirectory => _packageDir + "test/choco/";
protected override string PackageBinDir => PackageInstallDirectory + "tools/";
protected override string PackageResultDirectory => _packageDir + "test-results/choco/";
}
public class MsiPackageTester : NetFXPackageTester
{
public MsiPackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion)
{
// Add tests specific to the msi package
PackageTests.Add(new PackageTest(
"net35_plus_net40_project",
"Run project with both copies of mock-assembly",
$"../../NetFXTests.nunit --config={_config}",
new ExpectedResult("Failed")
{
Total = 2 * 37,
Passed = 2 * 23,
Failed = 2 * 5,
Warnings = 2 * 1,
Inconclusive = 2 * 1,
Skipped = 2 * 7
}));
}
protected override string PackageName => $"NUnit.Console-{_packageVersion}.msi";
protected override string PackageInstallDirectory => _packageDir + "test/msi/";
protected override string PackageBinDir => PackageInstallDirectory + "NUnit.org/nunit-console/";
protected override string PackageResultDirectory => _packageDir + "test-results/msi/";
protected override void CreatePackageInstallDirectory()
{
// Msiexec does not tolerate forward slashes!
string package = PackageUnderTest.ToString().Replace("/", "\\");
string testDir = PackageInstallDirectory.Replace("/", "\\");
int rc = _context.StartProcess("msiexec", $"/a {package} TARGETDIR={testDir} /q");
if (rc != 0)
Console.WriteLine($" ERROR: Installer returned {rc.ToString()}");
else
{
// Administrative install is used to create a file image, from which
// users may do their own installls. For security reasons, we can't
// do a full install so we simulate the user portion of the install,
// copying certain files to their final destination.
_context.CopyFiles(
PackageBinDir + "*.dll",
PackageBinDir + "agents/net20/");
_context.CopyFiles(
PackageBinDir + "*.dll",
PackageBinDir + "agents/net40/");
}
}
}
public class ZipPackageTester : NetFXPackageTester
{
public ZipPackageTester(ICakeContext context, string packageVersion)
: base(context, packageVersion)
{
// Add tests specific to the zip package
PackageTests.Add(new PackageTest(
"net35_plus_net40_project",
"Run project with both copies of mock-assembly",
$"../../NetFXTests.nunit --config={_config}",
new ExpectedResult("Failed")
{
Total = 2 * 37,
Passed = 2 * 23,
Failed = 2 * 5,
Warnings = 2 * 1,
Inconclusive = 2 * 1,
Skipped = 2 * 7
}));
}
protected override string PackageName => $"NUnit.Console-{_packageVersion}.zip";
protected override string PackageInstallDirectory => _packageDir + "test/zip/";
protected override string PackageBinDir => PackageInstallDirectory + "bin/net20/";
protected override string PackageResultDirectory => _packageDir + "test-results/zip/";
}