Add logging to tests to help diagnose intermittent test failures
Rob Mensching committed
Feb 10, 2022 at 14:25 UTC
5cac3441738744ca79ac6a4256d1f99631dbad8d
3 files changed
+55
-2
src/internal/WixBuildTools.TestSupport/TestData.cs
+30
-1
@@ -4,13 +4,42 @@ namespace WixBuildTools.TestSupport
4
{
5
using System;
6
using System.IO;
7
+ using System.Reflection;
8
+ using System.Runtime.CompilerServices;
9
10
public class TestData
11
{
12
public static string Get(params string[] paths)
13
{
12
- var localPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetCallingAssembly().CodeBase).LocalPath);
14
+ var localPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath);
15
return Path.Combine(localPath, Path.Combine(paths));
16
}
17
+
18
+ public static string GetUnitTestLogsFolder([CallerFilePath] string path = "", [CallerMemberName] string method = "")
19
+ {
20
+ var startingPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath);
21
+ var buildPath = startingPath;
22
+
23
+ while (!String.IsNullOrEmpty(buildPath))
24
+ {
25
+ var folderName = Path.GetFileName(buildPath);
26
+ if (String.Equals("build", folderName, StringComparison.OrdinalIgnoreCase))
27
+ {
28
+ break;
29
+ }
30
+
31
+ buildPath = Path.GetDirectoryName(buildPath);
32
+ }
33
+
34
+ if (String.IsNullOrEmpty(buildPath))
35
+ {
36
+ throw new InvalidOperationException($"Could not find the 'build' folder in the test path: {startingPath}. Cannot get test logs folder without being able to find the build folder.");
37
+ }
38
+
39
+ var testLogsFolder = Path.Combine(buildPath, "logs", "UnitTests", $"{Path.GetFileNameWithoutExtension(path)}_{method}");
40
+ Directory.CreateDirectory(testLogsFolder);
41
+
42
+ return testLogsFolder;
43
+ }
44
}
45
}
src/wix/test/WixToolsetTest.CoreIntegration/ValidationFixture.cs
+16
@@ -172,6 +172,10 @@ namespace WixToolsetTest.CoreIntegration
172
{
173
var folder = TestData.Get(@"TestData");
174
175
+ var testLogsFolder = TestData.GetUnitTestLogsFolder();
176
+ File.Delete(Path.Combine(testLogsFolder, "build.txt"));
177
+ File.Delete(Path.Combine(testLogsFolder, "validate.txt"));
178
+
179
using (var fs = new DisposableFileSystem())
180
{
181
var baseFolder = fs.GetFolder();
@@ -187,6 +191,8 @@ namespace WixToolsetTest.CoreIntegration
191
"-o", msiPath,
192
});
193
194
+ File.WriteAllLines(Path.Combine(testLogsFolder, "build.txt"), result.Messages.Select(m => m.ToString()));
195
+
196
result.AssertSuccess();
197
198
var validationResult = WixRunner.Execute(new[]
@@ -196,6 +202,8 @@ namespace WixToolsetTest.CoreIntegration
202
msiPath
203
});
204
205
+ File.WriteAllLines(Path.Combine(testLogsFolder, "validate.txt"), validationResult.Messages.Select(m => m.ToString()));
206
+
207
Assert.Equal(1, validationResult.ExitCode);
208
209
var messages = validationResult.Messages.Select(m => m.ToString()).ToArray();
@@ -212,6 +220,10 @@ namespace WixToolsetTest.CoreIntegration
220
{
221
var folder = TestData.Get(@"TestData");
222
223
+ var testLogsFolder = TestData.GetUnitTestLogsFolder();
224
+ File.Delete(Path.Combine(testLogsFolder, "build.txt"));
225
+ File.Delete(Path.Combine(testLogsFolder, "validate.txt"));
226
+
227
using (var fs = new DisposableFileSystem())
228
{
229
var baseFolder = fs.GetFolder();
@@ -227,6 +239,8 @@ namespace WixToolsetTest.CoreIntegration
239
"-o", msiPath,
240
});
241
242
+ File.WriteAllLines(Path.Combine(testLogsFolder, "build.txt"), result.Messages.Select(m => m.ToString()));
243
+
244
result.AssertSuccess();
245
246
var validationResult = WixRunner.Execute(warningsAsErrors: false, new[]
@@ -237,6 +251,8 @@ namespace WixToolsetTest.CoreIntegration
251
msiPath
252
});
253
254
+ File.WriteAllLines(Path.Combine(testLogsFolder, "validate.txt"), validationResult.Messages.Select(m => m.ToString()));
255
+
256
validationResult.AssertSuccess();
257
258
var messages = validationResult.Messages.Select(m => m.ToString()).ToArray();
src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs
+9
-1
@@ -6,6 +6,8 @@ namespace WixToolsetTest.Sdk
6
using System.Collections.Generic;
7
using System.IO;
8
using System.Linq;
9
+ using System.Runtime.CompilerServices;
10
+ using System.Threading;
11
using WixBuildTools.TestSupport;
12
using Xunit;
13
@@ -328,6 +330,10 @@ namespace WixToolsetTest.Sdk
330
{
331
var sourceFolder = TestData.Get(@"TestData\MsiPackageWithIceError\MsiPackage");
332
333
+ var testLogsFolder = TestData.GetUnitTestLogsFolder();
334
+ File.Delete(Path.Combine(testLogsFolder, buildSystem + ".binlog"));
335
+ File.Delete(Path.Combine(testLogsFolder, buildSystem + ".msi"));
336
+
337
using (var fs = new TestDataFolderFileSystem())
338
{
339
fs.Initialize(sourceFolder);
@@ -335,7 +341,9 @@ namespace WixToolsetTest.Sdk
341
var projectPath = Path.Combine(baseFolder, "MsiPackage.wixproj");
342
343
var result = MsbuildUtilities.BuildProject(buildSystem, projectPath, suppressValidation: false);
338
- Assert.Equal(1, result.ExitCode);
344
+
345
+ File.Copy(Path.ChangeExtension(projectPath, ".binlog"), Path.Combine(testLogsFolder, buildSystem + ".binlog"));
346
+ File.Copy(Path.Combine(baseFolder, "obj", "x86", "Release", "en-US", "MsiPackage.msi"), Path.Combine(testLogsFolder, buildSystem + ".msi"));
347
348
var iceIssues = result.Output.Where(line => line.Contains(": error") || line.Contains(": warning"))
349
.Select(line => line.Replace(baseFolder, "<baseFolder>")