-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathDeterministicBuild.cs
384 lines (340 loc) · 17.4 KB
/
DeterministicBuild.cs
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
// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Coverlet.Core;
using Coverlet.Tests.Utils;
using Newtonsoft.Json;
using Xunit;
namespace Coverlet.Integration.Tests
{
public class DeterministicBuild : BaseTest, IDisposable
{
private static readonly string s_projectName = "coverlet.integration.determisticbuild";
//private readonly string _buildTargetFramework;
private string? _testProjectTfm;
private readonly string _testProjectPath = TestUtils.GetTestProjectPath(s_projectName);
private readonly string _testBinaryPath = TestUtils.GetTestBinaryPath(s_projectName);
private readonly string _testResultsPath = TestUtils.GetTestResultsPath(s_projectName);
private const string PropsFileName = "DeterministicTest.props";
private readonly string _buildConfiguration;
private readonly ITestOutputHelper _output;
private readonly Type _type;
private readonly FieldInfo? _testMember;
public DeterministicBuild(ITestOutputHelper output)
{
_buildConfiguration = TestUtils.GetAssemblyBuildConfiguration().ToString();
//_buildTargetFramework = TestUtils.GetAssemblyTargetFramework();
_output = output;
_type = output.GetType();
_testMember = _type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
}
private void CreateDeterministicTestPropsFile()
{
var deterministicTestProps = new XDocument();
deterministicTestProps.Add(
new XElement("Project",
new XElement("PropertyGroup",
new XElement("coverletMsbuildVersion", GetPackageVersion("*msbuild*.nupkg")),
new XElement("coverletCollectorsVersion", GetPackageVersion("*collector*.nupkg")))));
_testProjectTfm = XElement.Load(Path.Combine(_testProjectPath, "coverlet.integration.determisticbuild.csproj"))!.
Descendants("PropertyGroup")!.Single().Element("TargetFramework")!.Value;
deterministicTestProps.Save(Path.Combine(_testProjectPath, PropsFileName));
}
private protected void AssertCoverage(string standardOutput = "", bool checkDeterministicReport = true)
{
if (_buildConfiguration == "Debug")
{
bool coverageChecked = false;
string reportFilePath = "";
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput), "coverage.json", SearchOption.AllDirectories))
{
Classes? document = JsonConvert.DeserializeObject<Modules>(File.ReadAllText(coverageFile))?.Document("DeepThought.cs");
if (document != null)
{
document.Class("Coverlet.Integration.DeterministicBuild.DeepThought")
.Method("System.Int32 Coverlet.Integration.DeterministicBuild.DeepThought::AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything()")
.AssertLinesCovered((6, 1), (7, 1), (8, 1));
coverageChecked = true;
reportFilePath = coverageFile;
}
}
Assert.True(coverageChecked, $"Coverage check fail\n{standardOutput}");
File.Delete(reportFilePath);
Assert.False(File.Exists(reportFilePath));
if (checkDeterministicReport)
{
// Verify deterministic report
foreach (string coverageFile in Directory.GetFiles(GetReportPath(standardOutput), "coverage.cobertura.xml", SearchOption.AllDirectories))
{
Assert.Contains("/_/test/coverlet.integration.determisticbuild/DeepThought.cs", File.ReadAllText(coverageFile));
File.Delete(coverageFile);
}
}
}
}
[Fact]
public void Msbuild()
{
string testResultPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}");
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
if (!string.IsNullOrEmpty(buildError))
{
_output.WriteLine(buildError);
}
else
{
_output.WriteLine(buildOutput);
}
Assert.Contains("Build succeeded.", buildOutput);
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string testResultFile = Path.Join(testResultPath, "coverage.json");
string cmdArgument = $"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"{testResultFile}\" /p:DeterministicReport=true /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
_output.WriteLine($"Command: dotnet {cmdArgument}");
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
Assert.True(File.Exists(testResultFile));
AssertCoverage(standardOutput);
CleanupBuildOutput();
}
[Fact]
public void Msbuild_SourceLink()
{
string testResultPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}");
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
CreateDeterministicTestPropsFile();
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
if (!string.IsNullOrEmpty(buildError))
{
_output.WriteLine(buildError);
}
else
{
_output.WriteLine(buildOutput);
}
Assert.Contains("Build succeeded.", buildOutput);
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
Assert.False(string.IsNullOrEmpty(File.ReadAllText(sourceRootMappingFilePath)));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string testResultFile = Path.Join(testResultPath, "coverage.json");
string cmdArgument = $"test -c {_buildConfiguration} --no-build /p:CollectCoverage=true /p:CoverletOutput=\"{testResultFile}\" /p:CoverletOutputFormat=\"cobertura%2cjson\" /p:UseSourceLink=true /p:Include=\"[coverletsample.integration.determisticbuild]*DeepThought\" /p:IncludeTestAssembly=true";
_output.WriteLine($"Command: dotnet {cmdArgument}");
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
Assert.Contains("| coverletsample.integration.determisticbuild | 100% | 100% | 100% |", standardOutput);
Assert.True(File.Exists(testResultFile));
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(testResultFile));
AssertCoverage(standardOutput, checkDeterministicReport: false);
CleanupBuildOutput();
}
[Fact]
public void Collectors()
{
string testResultPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}");
string testLogFilesPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}", "log");
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
CreateDeterministicTestPropsFile();
DeleteLogFiles(testLogFilesPath);
DeleteCoverageFiles(testResultPath);
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
if (!string.IsNullOrEmpty(buildError))
{
_output.WriteLine(buildError);
}
else
{
_output.WriteLine(buildOutput);
}
Assert.Contains("Build succeeded.", buildOutput);
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", deterministicReport: true);
string cmdArgument = $"test -c {_buildConfiguration} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
_output.WriteLine($"Command: dotnet {cmdArgument}");
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(standardOutput);
// delete irrelevant generated files
DeleteTestIntermediateFiles(testResultPath);
// Check out/in process collectors injection
string dataCollectorLogContent = File.ReadAllText(Directory.GetFiles(testLogFilesPath, "log.datacollector.*.txt").Single());
Assert.Contains("[coverlet]Initializing CoverletCoverageDataCollector with configuration:", dataCollectorLogContent);
Assert.Contains("[coverlet]Initialize CoverletInProcDataCollector", File.ReadAllText(Directory.GetFiles(testLogFilesPath, "log.host.*.txt").Single()));
Assert.Contains("[coverlet]Mapping resolved", dataCollectorLogContent);
CleanupBuildOutput();
}
[Fact]
public void Collectors_SourceLink()
{
string testResultPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}");
string testLogFilesPath = Path.Join(_testResultsPath, $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}", "log");
string logFilename = $"{TestContext.Current.TestClass?.TestClassName}.{TestContext.Current.TestMethod?.MethodName}.binlog";
CreateDeterministicTestPropsFile();
DeleteLogFiles(testLogFilesPath);
DeleteCoverageFiles(testResultPath);
DotnetCli($"build -c {_buildConfiguration} -bl:build.{logFilename} --verbosity normal /p:DeterministicSourcePaths=true", out string buildOutput, out string buildError, _testProjectPath);
if (!string.IsNullOrEmpty(buildError))
{
_output.WriteLine(buildError);
}
else
{
_output.WriteLine(buildOutput);
}
Assert.Contains("Build succeeded.", buildOutput);
string sourceRootMappingFilePath = Path.Combine(_testBinaryPath, _buildConfiguration.ToLowerInvariant(), "CoverletSourceRootsMapping_coverletsample.integration.determisticbuild");
Assert.True(File.Exists(sourceRootMappingFilePath), $"File not found: {sourceRootMappingFilePath}");
Assert.NotEmpty(File.ReadAllText(sourceRootMappingFilePath));
Assert.Contains("=/_/", File.ReadAllText(sourceRootMappingFilePath));
string runSettingsPath = AddCollectorRunsettingsFile(_testProjectPath, "[coverletsample.integration.determisticbuild]*DeepThought", sourceLink: true);
string cmdArgument = $"test -c {_buildConfiguration} --no-build --collect:\"XPlat Code Coverage\" --results-directory:\"{testResultPath}\" --settings \"{runSettingsPath}\" --diag:{Path.Combine(testLogFilesPath, "log.txt")}";
_output.WriteLine($"Command: dotnet {cmdArgument}");
int result = DotnetCli(cmdArgument, out string standardOutput, out string standardError, _testProjectPath);
if (!string.IsNullOrEmpty(standardError))
{
_output.WriteLine(standardError);
}
else
{
_output.WriteLine(standardOutput);
}
Assert.Equal(0, result);
Assert.Contains("Passed!", standardOutput);
AssertCoverage(standardOutput, checkDeterministicReport: false);
// delete irrelevant generated files
DeleteTestIntermediateFiles(testResultPath);
string[] fileList = Directory.GetFiles(testResultPath, "coverage.cobertura.xml", SearchOption.AllDirectories);
if (fileList.Length > 1)
{
_output.WriteLine("multiple coverage.cobertura.xml exist: ");
foreach (string file in fileList)
{
_output.WriteLine(file);
}
}
Assert.Single(fileList);
Assert.Contains("raw.githubusercontent.com", File.ReadAllText(fileList[0]));
// Check out/in process collectors injection
string dataCollectorLogContent = File.ReadAllText(Directory.GetFiles(testLogFilesPath, "log.datacollector.*.txt").Single());
Assert.Contains("[coverlet]Initializing CoverletCoverageDataCollector with configuration:", dataCollectorLogContent);
Assert.Contains("[coverlet]Initialize CoverletInProcDataCollector", File.ReadAllText(Directory.GetFiles(testLogFilesPath, "log.host.*.txt").Single()));
Assert.Contains("[coverlet]Mapping resolved", dataCollectorLogContent);
CleanupBuildOutput();
}
private static void DeleteTestIntermediateFiles(string testResultsPath)
{
if (Directory.Exists(testResultsPath))
{
DirectoryInfo hdDirectory = new DirectoryInfo(testResultsPath);
// search for directory "In" which has second copy e.g. '_fv-az365-374_2023-10-10_14_26_42\In\fv-az365-374\coverage.json'
DirectoryInfo[] intermediateFolder = hdDirectory.GetDirectories("In", SearchOption.AllDirectories);
foreach (DirectoryInfo foundDir in intermediateFolder)
{
DirectoryInfo? parentDir = Directory.GetParent(foundDir.FullName);
Directory.Delete(parentDir!.FullName, true);
}
}
}
private static void DeleteLogFiles(string directory)
{
if (Directory.Exists(directory))
{
DirectoryInfo hdDirectory = new DirectoryInfo(directory);
FileInfo[] filesInDir = hdDirectory.GetFiles("log.*.txt");
foreach (FileInfo foundFile in filesInDir)
{
string fullName = foundFile.FullName;
File.Delete(fullName);
}
}
}
private void CleanupBuildOutput()
{
if (Directory.Exists(_testBinaryPath))
{
Directory.Delete(_testBinaryPath, recursive: true);
}
string intermediateBuildOutput = _testBinaryPath.Replace("bin", "obj");
if (Directory.Exists(intermediateBuildOutput))
{
Directory.Delete(intermediateBuildOutput, recursive: true);
}
}
private static void DeleteCoverageFiles(string directory)
{
if (Directory.Exists(directory))
{
DirectoryInfo hdDirectory = new DirectoryInfo(directory);
FileInfo[] filesInDir = hdDirectory.GetFiles("coverage.cobertura.xml");
foreach (FileInfo foundFile in filesInDir)
{
string fullName = foundFile.FullName;
File.Delete(fullName);
}
DirectoryInfo[] dirsInDir = hdDirectory.GetDirectories();
foreach (DirectoryInfo foundDir in dirsInDir)
{
foreach (FileInfo foundFile in foundDir.GetFiles("coverage.cobertura.xml"))
{
string fullName = foundFile.FullName;
File.Delete(fullName);
}
}
}
}
private string GetReportPath(string standardOutput)
{
string reportPath = "";
if (standardOutput.Contains("coverage.json"))
{
reportPath = standardOutput.Split('\n').FirstOrDefault(line => line.Contains("coverage.json"))!.TrimStart();
reportPath = reportPath[reportPath.IndexOf(Directory.GetDirectoryRoot(_testProjectPath))..];
reportPath = reportPath[..reportPath.IndexOf("coverage.json")];
}
return reportPath;
}
public void Dispose()
{
File.Delete(Path.Combine(_testProjectPath, PropsFileName));
}
}
}