-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtests.cake
309 lines (245 loc) · 10.9 KB
/
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
// Customize this file when using a different test framework
#l "tests-variables.cake"
#l "tests-nunit.cake"
public class TestProcessor : ProcessorBase
{
public TestProcessor(BuildContext buildContext)
: base(buildContext)
{
}
public override bool HasItems()
{
return BuildContext.Tests.Items.Count > 0;
}
public override async Task PrepareAsync()
{
// Check whether projects should be processed, `.ToList()`
// is required to prevent issues with foreach
foreach (var testProject in BuildContext.Tests.Items.ToList())
{
if (IgnoreTestProject(testProject))
{
BuildContext.Tests.Items.Remove(testProject);
}
}
}
public override async Task UpdateInfoAsync()
{
// Not required
}
public override async Task BuildAsync()
{
if (!HasItems())
{
return;
}
foreach (var testProject in BuildContext.Tests.Items)
{
BuildContext.CakeContext.LogSeparator("Building test project '{0}'", testProject);
var projectFileName = GetProjectFileName(BuildContext, testProject);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Quiet, // Verbosity.Diagnostic
ToolVersion = MSBuildToolVersion.Default,
Configuration = BuildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
ConfigureMsBuild(BuildContext, msBuildSettings, testProject, "build");
// Always disable SourceLink
msBuildSettings.WithProperty("EnableSourceLink", "false");
// Force disable SonarQube
msBuildSettings.WithProperty("SonarQubeExclude", "true");
RunMsBuild(BuildContext, testProject, projectFileName, msBuildSettings, "build");
}
}
public override async Task PackageAsync()
{
// Not required
}
public override async Task DeployAsync()
{
// Not required
}
public override async Task FinalizeAsync()
{
// Not required
}
//-------------------------------------------------------------
private bool IgnoreTestProject(string projectName)
{
if (BuildContext.General.IsLocalBuild && BuildContext.General.MaximizePerformance)
{
BuildContext.CakeContext.Information($"Local build with maximized performance detected, ignoring test project for project '{projectName}'");
return true;
}
// In case of a local build and we have included / excluded anything, skip tests
if (BuildContext.General.IsLocalBuild &&
(BuildContext.General.Includes.Count > 0 || BuildContext.General.Excludes.Count > 0))
{
BuildContext.CakeContext.Information($"Skipping test project '{projectName}' because this is a local build with specific includes / excludes");
return true;
}
// Special unit test part assuming a few naming conventions:
// 1. [ProjectName].Tests
// 2. [SolutionName].Tests.[ProjectName]
//
// In both cases, we can simply remove ".Tests" and check if that project is being ignored
var expectedProjectName = projectName
.Replace(".Integration.Tests", string.Empty)
.Replace(".IntegrationTests", string.Empty)
.Replace(".Tests", string.Empty);
// Special case: if this is a "solution wide" test project, it must always run
if (!BuildContext.RegisteredProjects.Any(x => string.Equals(x, expectedProjectName, StringComparison.OrdinalIgnoreCase)))
{
BuildContext.CakeContext.Information($"Including test project '{projectName}' because there are no linked projects, assuming this is a solution wide test project");
return false;
}
if (!ShouldProcessProject(BuildContext, expectedProjectName))
{
BuildContext.CakeContext.Information($"Skipping test project '{projectName}' because project '{expectedProjectName}' should not be processed either");
return true;
}
return false;
}
}
//-------------------------------------------------------------
private static void RunUnitTests(BuildContext buildContext, string projectName)
{
var testResultsDirectory = System.IO.Path.Combine(buildContext.General.OutputRootDirectory, "testresults", projectName);
buildContext.CakeContext.CreateDirectory(testResultsDirectory);
var ranTests = false;
var failed = false;
var testTargetFrameworks = GetTestTargetFrameworks(buildContext, projectName);
try
{
foreach (var testTargetFramework in testTargetFrameworks)
{
LogSeparator(buildContext.CakeContext, "Running tests for target framework {0}", testTargetFramework);
if (IsDotNetCoreTargetFramework(buildContext, testTargetFramework))
{
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET core project, using 'dotnet test' to run the unit tests");
var projectFileName = GetProjectFileName(buildContext, projectName);
var dotNetTestSettings = new DotNetTestSettings
{
Configuration = buildContext.General.Solution.ConfigurationName,
// Loggers = new []
// {
// "nunit;LogFilePath=test-result.xml"
// },
NoBuild = true,
NoLogo = true,
NoRestore = true,
OutputDirectory = System.IO.Path.Combine(GetProjectOutputDirectory(buildContext, projectName), testTargetFramework),
ResultsDirectory = testResultsDirectory
};
if (IsNUnitTestProject(buildContext, projectName))
{
dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-- NUnit.TestOutputXml={testResultsDirectory}");
}
if (IsXUnitTestProject(buildContext, projectName))
{
var outputFileName = System.IO.Path.Combine(testResultsDirectory, $"{projectName}.xml");
dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-l:trx;LogFileName={outputFileName}");
}
var processBit = buildContext.Tests.ProcessBit.ToLower();
if (!string.IsNullOrWhiteSpace(processBit))
{
dotNetTestSettings.Runtime = $"{buildContext.Tests.OperatingSystem}-{processBit}";
}
buildContext.CakeContext.Information($"Runtime: '{dotNetTestSettings.Runtime}'");
buildContext.CakeContext.DotNetTest(projectFileName, dotNetTestSettings);
ranTests = true;
}
else
{
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET project, using '{buildContext.Tests.Framework} runner' to run the unit tests");
if (IsNUnitTestProject(buildContext, projectName))
{
RunTestsUsingNUnit(buildContext, projectName, testTargetFramework, testResultsDirectory);
ranTests = true;
}
}
}
}
catch (Exception ex)
{
buildContext.CakeContext.Warning($"An exception occurred: {ex.Message}");
failed = true;
}
if (ranTests)
{
buildContext.CakeContext.Information($"Results are available in '{testResultsDirectory}'");
}
else if (failed)
{
throw new Exception("Unit test execution failed");
}
else
{
buildContext.CakeContext.Warning("No tests were executed, check whether the used test framework '{0}' is available", buildContext.Tests.Framework);
}
}
//-------------------------------------------------------------
private static bool IsTestProject(BuildContext buildContext, string projectName)
{
if (IsNUnitTestProject(buildContext, projectName))
{
return true;
}
if (IsXUnitTestProject(buildContext, projectName))
{
return true;
}
return false;
}
//-------------------------------------------------------------
private static bool IsNUnitTestProject(BuildContext buildContext, string projectName)
{
var projectFileName = GetProjectFileName(buildContext, projectName);
var projectFileContents = System.IO.File.ReadAllText(projectFileName);
if (projectFileContents.ToLower().Contains("nunit"))
{
return true;
}
return false;
// Not sure, return framework from config
//return buildContext.Tests.Framework.ToLower().Equals("nunit");
}
//-------------------------------------------------------------
private static bool IsXUnitTestProject(BuildContext buildContext, string projectName)
{
var projectFileName = GetProjectFileName(buildContext, projectName);
var projectFileContents = System.IO.File.ReadAllText(projectFileName);
if (projectFileContents.ToLower().Contains("xunit"))
{
return true;
}
return false;
// Not sure, return framework from config
//return buildContext.Tests.Framework.ToLower().Equals("xunit");
}
//-------------------------------------------------------------
private static IReadOnlyList<string> GetTestTargetFrameworks(BuildContext buildContext, string projectName)
{
// Step 1: if defined, use defined value
var testTargetFramework = buildContext.Tests.TargetFramework;
if (!string.IsNullOrWhiteSpace(testTargetFramework))
{
buildContext.CakeContext.Information("Using test target framework '{0}', specified via the configuration", testTargetFramework);
return new []
{
testTargetFramework
};
}
buildContext.CakeContext.Information("Test target framework not specified, auto detecting test target frameworks");
var targetFrameworks = GetTargetFrameworks(buildContext, projectName);
buildContext.CakeContext.Information("Auto detected test target frameworks '{0}'", string.Join(", ", targetFrameworks));
if (targetFrameworks.Length == 0)
{
throw new Exception(string.Format("Test target framework could not automatically be detected for project '{0]'", projectName));
}
return targetFrameworks;
}