forked from nunit/nunit-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-checks.cake
289 lines (243 loc) · 9.95 KB
/
package-checks.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
// The checks in this script basically do no more than what the programmer might
// do in opening the package itself and examining the content. In particular, the
// packages are not actually installed, leaving that for manual testing before
// each release. This might be automated at some future point. However, they do
// provide some degree of regression testing, since changes in the package
// organization will result in a failure.
//
// Note that msi packages are not being checked at this time.
public void CheckAllPackages()
{
string[] ENGINE_FILES = { "nunit.engine.dll", "nunit.engine.core.dll", "nunit.engine.api.dll", "testcentric.engine.metadata.dll" };
string[] AGENT_FILES = {
"nunit-agent.exe", "nunit-agent.exe.config", "nunit-agent-x86.exe", "nunit-agent-x86.exe.config", "nunit.engine.core.dll", "nunit.engine.api.dll", "testcentric.engine.metadata.dll" };
string[] CONSOLE_FILES = { "nunit3-console.exe", "nunit3-console.exe.config" };
bool isOK =
CheckNuGetPackage(
"NUnit.Console",
HasFile("LICENSE.txt")) &
CheckNuGetPackage(
"NUnit.ConsoleRunner",
HasFiles("LICENSE.txt", "NOTICES.txt", "CHANGES.txt"),
HasDirectory("tools").WithFiles(CONSOLE_FILES).AndFiles(ENGINE_FILES).AndFile("nunit.console.nuget.addins"),
HasDirectory("tools/agents/net20").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins"),
HasDirectory("tools/agents/net40").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins")) &
CheckNuGetPackage("NUnit.Engine",
HasFiles("LICENSE.txt", "NOTICES.txt", "CHANGES.txt"),
HasDirectory("lib/net20").WithFiles(ENGINE_FILES),
HasDirectory("lib/netstandard1.6").WithFiles(ENGINE_FILES),
HasDirectory("lib/netstandard2.0").WithFiles(ENGINE_FILES),
HasDirectory("contentFiles/any/lib/net20").WithFile("nunit.engine.nuget.addins"),
HasDirectory("contentFiles/any/lib/netstandard1.6").WithFile("nunit.engine.nuget.addins"),
HasDirectory("contentFiles/any/lib/netstandard2.0").WithFile("nunit.engine.nuget.addins"),
HasDirectory("contentFiles/any/agents/net20").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins"),
HasDirectory("contentFiles/any/agents/net40").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins")) &
CheckNuGetPackage(
"NUnit.Engine.Api",
HasFile("LICENSE.txt"),
HasDirectory("lib/net20").WithFile("nunit.engine.api.dll"),
HasDirectory("lib/netstandard1.6").WithFile("nunit.engine.api.dll"),
HasDirectory("lib/netstandard2.0").WithFile("nunit.engine.api.dll")) &
CheckNuGetPackage(
"NUnit.Runners",
HasFile("LICENSE.txt")) &
CheckChocolateyPackage(
"nunit-console-runner",
HasDirectory("tools").WithFiles("LICENSE.txt", "NOTICES.txt", "CHANGES.txt", "VERIFICATION.txt").AndFiles(CONSOLE_FILES).AndFiles(ENGINE_FILES).AndFile("nunit.choco.addins"),
HasDirectory("tools/agents/net20").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins"),
HasDirectory("tools/agents/net40").WithFiles(AGENT_FILES).AndFile("nunit.agent.addins")) &
CheckChocolateyPackage(
"nunit-console-with-extensions",
HasDirectory("tools").WithFiles("LICENSE.txt", "NOTICES.txt", "VERIFICATION.txt")) &
CheckZipPackage(
"NUnit.Console",
HasFiles("LICENSE.txt", "license.rtf", "NOTICES.txt", "CHANGES.txt"),
HasDirectory("bin/net20").WithFiles("nunit3-console.exe", "nunit3-console.exe.config").AndFiles(ENGINE_FILES),
HasDirectory("bin/net35").WithFiles("nunit3-console.exe", "nunit3-console.exe.config").AndFiles(ENGINE_FILES),
HasDirectory("bin/netstandard1.6").WithFiles(ENGINE_FILES),
HasDirectory("bin/netstandard2.0").WithFiles(ENGINE_FILES),
HasDirectory("bin/netcoreapp1.1").WithFiles(ENGINE_FILES),
HasDirectory("bin/netcoreapp1.1").WithFiles(ENGINE_FILES),
HasDirectory("bin/agents/net20").WithFiles(AGENT_FILES),
HasDirectory("bin/agents/net40").WithFiles(AGENT_FILES)) &
CheckMsiPackage("NUnit.Console",
HasDirectory("NUnit.org").WithFiles("LICENSE.txt", "NOTICES.txt", "nunit.ico"),
HasDirectory("NUnit.org/nunit-console").WithFiles("nunit3-console.exe", "nunit3-console.exe.config").AndFiles(ENGINE_FILES).AndFile("nunit.bundle.addins"),
HasDirectory("NUnit.org/nunit-console/agents/net20").WithFiles("nunit-agent.exe", "nunit-agent.exe.config", "nunit-agent-x86.exe", "nunit-agent-x86.exe.config", "nunit.agent.addins"),
HasDirectory("NUnit.org/nunit-console/agents/net40").WithFiles("nunit-agent.exe", "nunit-agent.exe.config", "nunit-agent-x86.exe", "nunit-agent-x86.exe.config", "nunit.agent.addins"),
HasDirectory("Nunit.org/nunit-console/addins").WithFiles("nunit.core.dll", "nunit.core.interfaces.dll", "nunit.v2.driver.dll", "nunit-project-loader.dll", "vs-project-loader.dll", "nunit-v2-result-writer.dll", "teamcity-event-listener.dll"));
if (!isOK)
throw new Exception("One or more package checks failed. See listing.");
}
private bool CheckNuGetPackage(string packageId, params ICheck[] checks)
{
return CheckPackage($"{PACKAGE_DIR}{packageId}.{productVersion}.nupkg", checks);
}
private bool CheckChocolateyPackage(string packageId, params ICheck[] checks)
{
return CheckPackage($"{PACKAGE_DIR}{packageId}.{productVersion}.nupkg", checks);
}
private bool CheckZipPackage(string packageName, params ICheck[] checks)
{
return CheckPackage($"{PACKAGE_DIR}{packageName}-{productVersion}.zip", checks);
}
// NOTE: Msi package currently uses "version" rather than "productVersion"
private bool CheckMsiPackage(string packageName, params ICheck[] checks)
{
return CheckPackage($"{PACKAGE_DIR}{packageName}-{version}.msi", checks);
}
private bool CheckPackage(string package, params ICheck[] checks)
{
Console.WriteLine("\nPackage Name: " + System.IO.Path.GetFileName(package));
if (!FileExists(package))
{
WriteError("Package was not found!");
return false;
}
if (checks.Length == 0)
{
WriteWarning("Package found but no checks were specified.");
return true;
}
bool isMsi = package.EndsWith(".msi");
string tempDir = isMsi
? InstallMsiToTempDir(package)
: UnzipToTempDir(package);
if (!System.IO.Directory.Exists(tempDir))
{
WriteError("Temporary directory was not created!");
return false;
}
try
{
bool allPassed = ApplyChecks(tempDir, checks);
if (allPassed)
WriteInfo("All checks passed!");
return allPassed;
}
finally
{
DeleteDirectory(tempDir, new DeleteDirectorySettings()
{
Recursive = true,
Force = true
});
}
}
private string InstallMsiToTempDir(string package)
{
// Msiexec does not tolerate forward slashes!
package = package.Replace("/", "\\");
var tempDir = GetTempDirectoryPath();
WriteInfo("Installing to " + tempDir);
int rc = StartProcess("msiexec", $"/a {package} TARGETDIR={tempDir} /q");
if (rc != 0)
WriteError($"Installer returned {rc.ToString()}");
return tempDir;
}
private string UnzipToTempDir(string package)
{
var tempDir = GetTempDirectoryPath();
WriteInfo("Unzipping to " + tempDir);
Unzip(package, tempDir);
return tempDir;
}
private string GetTempDirectoryPath()
{
return System.IO.Path.GetTempPath() + System.IO.Path.GetRandomFileName() + "\\";
}
private bool ApplyChecks(string dir, ICheck[] checks)
{
bool allOK = true;
foreach (var check in checks)
allOK &= check.Apply(dir);
return allOK;
}
private interface ICheck
{
bool Apply(string dir);
}
private class FileCheck : ICheck
{
string[] _paths;
public FileCheck(string[] paths)
{
_paths = paths;
}
public bool Apply(string dir)
{
var isOK = true;
foreach (string path in _paths)
{
if (!System.IO.File.Exists(dir + path))
{
WriteError($"File {path} was not found.");
isOK = false;
}
}
return isOK;
}
}
private class DirectoryCheck : ICheck
{
private string _path;
private List<string> _files = new List<string>();
public DirectoryCheck(string path)
{
_path = path;
}
public DirectoryCheck WithFiles(params string[] files)
{
_files.AddRange(files);
return this;
}
public DirectoryCheck AndFiles(params string[] files)
{
return WithFiles(files);
}
public DirectoryCheck WithFile(string file)
{
_files.Add(file);
return this;
}
public DirectoryCheck AndFile(string file)
{
return AndFiles(file);
}
public bool Apply(string dir)
{
if (!System.IO.Directory.Exists(dir + _path))
{
WriteError($"Directory {_path} was not found.");
return false;
}
bool isOK = true;
if (_files != null)
{
foreach (var file in _files)
{
if (!System.IO.File.Exists(System.IO.Path.Combine(dir + _path, file)))
{
WriteError($"File {file} was not found in directory {_path}.");
isOK = false;
}
}
}
return isOK;
}
}
private FileCheck HasFile(string file) => HasFiles(new [] { file });
private FileCheck HasFiles(params string[] files) => new FileCheck(files);
private DirectoryCheck HasDirectory(string dir) => new DirectoryCheck(dir);
private static void WriteError(string msg)
{
Console.WriteLine(" ERROR: " + msg);
}
private static void WriteWarning(string msg)
{
Console.WriteLine(" WARNING: " + msg);
}
private static void WriteInfo(string msg)
{
Console.WriteLine(" " + msg);
}