forked from nunit/nunit3-vs-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
366 lines (301 loc) · 11.8 KB
/
build.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
#tool vswhere&version=3.1.1
#tool Microsoft.TestPlatform&version=17.4.0
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
//////////////////////////////////////////////////////////////////////
// SET PACKAGE VERSION
//////////////////////////////////////////////////////////////////////
var version = "4.5.0";
var modifier = "";
var dbgSuffix = configuration.ToLower() == "debug" ? "-dbg" : "";
var packageVersion = version + modifier + dbgSuffix;
Information("Packageversion: "+packageVersion);
if (BuildSystem.IsRunningOnAppVeyor)
{
var tag = AppVeyor.Environment.Repository.Tag;
if (tag.IsTag)
{
packageVersion = tag.Name;
}
else
{
var buildNumber = AppVeyor.Environment.Build.Number.ToString("00000");
var branch = AppVeyor.Environment.Repository.Branch.Replace(".", "").Replace("/", "");
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
if (branch == "master" && !isPullRequest)
{
packageVersion = version + "-dev-" + buildNumber + dbgSuffix;
}
else
{
var suffix = "-ci-" + buildNumber + dbgSuffix;
if (isPullRequest)
suffix += "-pr-" + AppVeyor.Environment.PullRequest.Number;
else
suffix += "-" + System.Text.RegularExpressions.Regex.Replace(branch, "[^0-9A-Za-z-]+", "-");
// Nuget limits "special version part" to 20 chars. Add one for the hyphen.
if (suffix.Length > 21)
suffix = suffix.Substring(0, 21);
packageVersion = version + suffix;
}
}
AppVeyor.UpdateBuildVersion(packageVersion);
}
var packageName = "NUnit3TestAdapter-" + packageVersion;
//////////////////////////////////////////////////////////////////////
// DEFINE RUN CONSTANTS
//////////////////////////////////////////////////////////////////////
// Directories
var PROJECT_DIR = Context.Environment.WorkingDirectory.FullPath + "/";
var PACKAGE_DIR = PROJECT_DIR + "package/";
var PACKAGE_IMAGE_DIR = PACKAGE_DIR + packageName + "/";
var SRC_DIR = PROJECT_DIR + "src/";
var BIN_DIR = PROJECT_DIR + "bin/" + configuration + "/";
var ADAPTER_PROJECT = SRC_DIR + "NUnitTestAdapter/NUnit.TestAdapter.csproj";
var NETCOREAPP_TFM = "netcoreapp3.1";
var ADAPTER_BIN_DIR_NET462 = SRC_DIR + $"NUnitTestAdapter/bin/{configuration}/net462/";
var ADAPTER_BIN_DIR_NETCOREAPP = SRC_DIR + $"NUnitTestAdapter/bin/{configuration}/{NETCOREAPP_TFM}/";
var BIN_DIRS = new [] {
PROJECT_DIR + "src/empty-assembly/bin",
PROJECT_DIR + "src/mock-assembly/bin",
PROJECT_DIR + "src/NUnit3TestAdapterInstall/bin",
PROJECT_DIR + "src/NUnit3TestAdapter/bin",
PROJECT_DIR + "src/NUnit3TestAdapterTests/bin",
};
// Solution
var ADAPTER_SOLUTION = PROJECT_DIR + "NUnit3TestAdapter.sln";
//////////////////////////////////////////////////////////////////////
// CLEAN
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
foreach(var dir in BIN_DIRS)
CleanDirectory(dir);
});
Task("CleanPackages")
.Does(()=>
{
CleanDirectory(PACKAGE_DIR);
});
//////////////////////////////////////////////////////////////////////
// BUILD
//////////////////////////////////////////////////////////////////////
Task("Build")
.Does(() =>
{
if (IsRunningOnWindows())
{
// Workaround for https://github.com/cake-build/cake/issues/2128
// cannot find pure preview installations of visual studio
var vsInstallation =
VSWhereLatest(new VSWhereLatestSettings { Requires = "Microsoft.Component.MSBuild" })
?? VSWhereLatest(new VSWhereLatestSettings { Requires = "Microsoft.Component.MSBuild", IncludePrerelease = true });
if(vsInstallation == null)
{
throw new Exception($"Failed to find any Visual Studio version");
}
FilePath msBuildPath = vsInstallation.CombineWithFilePath(@"MSBuild\Current\Bin\MSBuild.exe");
if (!FileExists(msBuildPath))
{
msBuildPath = vsInstallation.CombineWithFilePath(@"MSBuild\15.0\Bin\MSBuild.exe");
}
Information("Building using MSBuild at " + msBuildPath);
Information("Configuration is:"+configuration);
MSBuild(ADAPTER_SOLUTION, new MSBuildSettings
{
Configuration = configuration,
ToolPath = msBuildPath,
EnvironmentVariables = new Dictionary<string, string>
{
["PackageVersion"] = packageVersion
},
Verbosity = Verbosity.Minimal,
Restore = true
});
}
else
{
var settings = new DotNetCoreBuildSettings
{
Configuration = configuration,
EnvironmentVariables = new Dictionary<string, string>
{
["PackageVersion"] = packageVersion
}
};
DotNetCoreBuild(@"src\NUnitTestAdapterTests", settings);
DotNetCoreBuild(@"src\NUnit.TestAdapter.Tests.Acceptance", settings);
}
});
//////////////////////////////////////////////////////////////////////
// TEST
//////////////////////////////////////////////////////////////////////
string GetTestAssemblyPath(string framework)
{
return SRC_DIR + $"NUnitTestAdapterTests/bin/{configuration}/{framework}/NUnit.VisualStudio.TestAdapter.Tests.dll";
}
foreach (var (framework, vstestFramework, adapterDir) in new[] {
("net462", "Framework45", ADAPTER_BIN_DIR_NET462),
(NETCOREAPP_TFM, NETCOREAPP_TFM, ADAPTER_BIN_DIR_NETCOREAPP)
})
{
Task($"VSTest-{framework}")
.IsDependentOn("Build")
.Does(() =>
{
VSTest(GetTestAssemblyPath(framework), new VSTestSettings
{
TestAdapterPath = adapterDir,
// Enables the tests to run against the correct version of Microsoft.VisualStudio.TestPlatform.ObjectModel.dll.
// (The DLL they are compiled against depends on VS2012 at runtime.)
SettingsFile = File("DisableAppDomain.runsettings"),
Logger = $"trx;LogFileName=VSTest-{framework}.trx"
});
PublishTestResults($"VSTest-{framework}.trx");
});
Task($"DotnetTest-{framework}")
.IsDependentOn("Build")
.Does(() =>
{
DotNetCoreTest(SRC_DIR + "NUnitTestAdapterTests/NUnit.TestAdapter.Tests.csproj", new DotNetCoreTestSettings
{
Configuration = configuration,
Framework = framework,
NoBuild = true,
TestAdapterPath = adapterDir,
Settings = File("DisableAppDomain.runsettings"),
Logger = $"trx;LogFileName=DotnetTest-{framework}.trx",
ResultsDirectory = MakeAbsolute(Directory("TestResults"))
});
PublishTestResults($"DotnetTest-{framework}.trx");
});
}
void PublishTestResults(string fileName)
{
if (EnvironmentVariable("TF_BUILD", false))
{
TFBuild.Commands.PublishTestResults(new TFBuildPublishTestResultsData
{
TestResultsFiles = { @"TestResults\" + fileName },
TestRunTitle = fileName,
TestRunner = TFTestRunnerType.VSTest,
PublishRunAttachments = true,
Configuration = configuration
});
}
}
//////////////////////////////////////////////////////////////////////
// PACKAGE
//////////////////////////////////////////////////////////////////////
Task("CreatePackageDir")
.Does(() =>
{
CreateDirectory(PACKAGE_DIR);
});
Task("CreateWorkingImage")
.IsDependentOn("CreatePackageDir")
.Does(() =>
{
CreateDirectory(PACKAGE_IMAGE_DIR);
CleanDirectory(PACKAGE_IMAGE_DIR);
CopyFileToDirectory("LICENSE", PACKAGE_IMAGE_DIR);
// dotnet publish doesn't work for .NET 3.5
var net462Files = new FilePath[]
{
ADAPTER_BIN_DIR_NET462 + "NUnit3.TestAdapter.dll",
ADAPTER_BIN_DIR_NET462 + "NUnit3.TestAdapter.pdb",
ADAPTER_BIN_DIR_NET462 + "nunit.engine.dll",
ADAPTER_BIN_DIR_NET462 + "nunit.engine.api.dll",
ADAPTER_BIN_DIR_NET462 + "nunit.engine.core.dll",
ADAPTER_BIN_DIR_NET462 + "testcentric.engine.metadata.dll"
};
var net462Dir = PACKAGE_IMAGE_DIR + "build/net462";
CreateDirectory(net462Dir);
CopyFiles(net462Files, net462Dir);
CopyFileToDirectory("nuget/net462/NUnit3TestAdapter.props", net462Dir);
var netcoreDir = PACKAGE_IMAGE_DIR + "build/" + NETCOREAPP_TFM;
DotNetCorePublish(ADAPTER_PROJECT, new DotNetCorePublishSettings
{
Configuration = configuration,
OutputDirectory = netcoreDir,
Framework = NETCOREAPP_TFM
});
CopyFileToDirectory($"nuget/{NETCOREAPP_TFM}/NUnit3TestAdapter.props", netcoreDir);
});
Task("PackageZip")
.IsDependentOn("CreateWorkingImage")
.Does(() =>
{
Zip(PACKAGE_IMAGE_DIR, File(PACKAGE_DIR + packageName + ".zip"));
});
Task("PackageNuGet")
.IsDependentOn("CreateWorkingImage")
.Does(() =>
{
NuGetPack("nuget/NUnit3TestAdapter.nuspec", new NuGetPackSettings()
{
Version = packageVersion,
BasePath = PACKAGE_IMAGE_DIR,
OutputDirectory = PACKAGE_DIR
});
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Rebuild")
.IsDependentOn("Clean")
.IsDependentOn("Build");
Task("Test")
.IsDependentOn("VSTest-net462")
.IsDependentOn("VSTest-" + NETCOREAPP_TFM)
.IsDependentOn("DotnetTest-net462")
.IsDependentOn("DotnetTest-" + NETCOREAPP_TFM);
Task("Package")
.IsDependentOn("PackageZip")
.IsDependentOn("PackageNuGet");
Task("QuickRelease")
.IsDependentOn("Build")
.IsDependentOn("Package");
Task("Release")
.IsDependentOn("Clean")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package")
.IsDependentOn("Acceptance");
Task("Acceptance")
.IsDependentOn("Build")
.IsDependentOn("PackageNuGet")
.Description("Ensures that known project configurations can use the produced NuGet package to restore, build, and run tests.")
.Does(() =>
{
// Target framework specified here should be exactly the same as the one in the acceptance project file.
var targetframework = "net48";
var testAssembly = SRC_DIR + $"NUnit.TestAdapter.Tests.Acceptance/bin/{configuration}/{targetframework}/NUnit.VisualStudio.TestAdapter.Tests.Acceptance.dll";
var keepWorkspaces = Argument<bool?>("keep-workspaces", false) ?? true;
VSTest(testAssembly, new VSTestSettings
{
SettingsFile = keepWorkspaces ? (FilePath)"KeepWorkspaces.runsettings" : null,
Logger = "trx;LogFileName=Acceptance.trx"
});
PublishTestResults("Acceptance.trx");
});
Task("CI")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package")
.IsDependentOn("Acceptance");
Task("Appveyor")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package")
.IsDependentOn("Acceptance");
Task("Default")
.IsDependentOn("Build");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);