From 5cac3441738744ca79ac6a4256d1f99631dbad8d Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 10 Feb 2022 14:25:09 -0800 Subject: Add logging to tests to help diagnose intermittent test failures --- src/internal/WixBuildTools.TestSupport/TestData.cs | 31 +++++++++++++++++++++- .../ValidationFixture.cs | 16 +++++++++++ src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs | 10 ++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/internal/WixBuildTools.TestSupport/TestData.cs b/src/internal/WixBuildTools.TestSupport/TestData.cs index 8587330d..309e9038 100644 --- a/src/internal/WixBuildTools.TestSupport/TestData.cs +++ b/src/internal/WixBuildTools.TestSupport/TestData.cs @@ -4,13 +4,42 @@ namespace WixBuildTools.TestSupport { using System; using System.IO; + using System.Reflection; + using System.Runtime.CompilerServices; public class TestData { public static string Get(params string[] paths) { - var localPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetCallingAssembly().CodeBase).LocalPath); + var localPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath); return Path.Combine(localPath, Path.Combine(paths)); } + + public static string GetUnitTestLogsFolder([CallerFilePath] string path = "", [CallerMemberName] string method = "") + { + var startingPath = Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().CodeBase).LocalPath); + var buildPath = startingPath; + + while (!String.IsNullOrEmpty(buildPath)) + { + var folderName = Path.GetFileName(buildPath); + if (String.Equals("build", folderName, StringComparison.OrdinalIgnoreCase)) + { + break; + } + + buildPath = Path.GetDirectoryName(buildPath); + } + + if (String.IsNullOrEmpty(buildPath)) + { + 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."); + } + + var testLogsFolder = Path.Combine(buildPath, "logs", "UnitTests", $"{Path.GetFileNameWithoutExtension(path)}_{method}"); + Directory.CreateDirectory(testLogsFolder); + + return testLogsFolder; + } } } diff --git a/src/wix/test/WixToolsetTest.CoreIntegration/ValidationFixture.cs b/src/wix/test/WixToolsetTest.CoreIntegration/ValidationFixture.cs index 69965c0d..fd8f6f87 100644 --- a/src/wix/test/WixToolsetTest.CoreIntegration/ValidationFixture.cs +++ b/src/wix/test/WixToolsetTest.CoreIntegration/ValidationFixture.cs @@ -172,6 +172,10 @@ namespace WixToolsetTest.CoreIntegration { var folder = TestData.Get(@"TestData"); + var testLogsFolder = TestData.GetUnitTestLogsFolder(); + File.Delete(Path.Combine(testLogsFolder, "build.txt")); + File.Delete(Path.Combine(testLogsFolder, "validate.txt")); + using (var fs = new DisposableFileSystem()) { var baseFolder = fs.GetFolder(); @@ -187,6 +191,8 @@ namespace WixToolsetTest.CoreIntegration "-o", msiPath, }); + File.WriteAllLines(Path.Combine(testLogsFolder, "build.txt"), result.Messages.Select(m => m.ToString())); + result.AssertSuccess(); var validationResult = WixRunner.Execute(new[] @@ -196,6 +202,8 @@ namespace WixToolsetTest.CoreIntegration msiPath }); + File.WriteAllLines(Path.Combine(testLogsFolder, "validate.txt"), validationResult.Messages.Select(m => m.ToString())); + Assert.Equal(1, validationResult.ExitCode); var messages = validationResult.Messages.Select(m => m.ToString()).ToArray(); @@ -212,6 +220,10 @@ namespace WixToolsetTest.CoreIntegration { var folder = TestData.Get(@"TestData"); + var testLogsFolder = TestData.GetUnitTestLogsFolder(); + File.Delete(Path.Combine(testLogsFolder, "build.txt")); + File.Delete(Path.Combine(testLogsFolder, "validate.txt")); + using (var fs = new DisposableFileSystem()) { var baseFolder = fs.GetFolder(); @@ -227,6 +239,8 @@ namespace WixToolsetTest.CoreIntegration "-o", msiPath, }); + File.WriteAllLines(Path.Combine(testLogsFolder, "build.txt"), result.Messages.Select(m => m.ToString())); + result.AssertSuccess(); var validationResult = WixRunner.Execute(warningsAsErrors: false, new[] @@ -237,6 +251,8 @@ namespace WixToolsetTest.CoreIntegration msiPath }); + File.WriteAllLines(Path.Combine(testLogsFolder, "validate.txt"), validationResult.Messages.Select(m => m.ToString())); + validationResult.AssertSuccess(); var messages = validationResult.Messages.Select(m => m.ToString()).ToArray(); diff --git a/src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs b/src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs index 0bee1b01..f0761762 100644 --- a/src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs +++ b/src/wix/test/WixToolsetTest.Sdk/MsbuildFixture.cs @@ -6,6 +6,8 @@ namespace WixToolsetTest.Sdk using System.Collections.Generic; using System.IO; using System.Linq; + using System.Runtime.CompilerServices; + using System.Threading; using WixBuildTools.TestSupport; using Xunit; @@ -328,6 +330,10 @@ namespace WixToolsetTest.Sdk { var sourceFolder = TestData.Get(@"TestData\MsiPackageWithIceError\MsiPackage"); + var testLogsFolder = TestData.GetUnitTestLogsFolder(); + File.Delete(Path.Combine(testLogsFolder, buildSystem + ".binlog")); + File.Delete(Path.Combine(testLogsFolder, buildSystem + ".msi")); + using (var fs = new TestDataFolderFileSystem()) { fs.Initialize(sourceFolder); @@ -335,7 +341,9 @@ namespace WixToolsetTest.Sdk var projectPath = Path.Combine(baseFolder, "MsiPackage.wixproj"); var result = MsbuildUtilities.BuildProject(buildSystem, projectPath, suppressValidation: false); - Assert.Equal(1, result.ExitCode); + + File.Copy(Path.ChangeExtension(projectPath, ".binlog"), Path.Combine(testLogsFolder, buildSystem + ".binlog")); + File.Copy(Path.Combine(baseFolder, "obj", "x86", "Release", "en-US", "MsiPackage.msi"), Path.Combine(testLogsFolder, buildSystem + ".msi")); var iceIssues = result.Output.Where(line => line.Contains(": error") || line.Contains(": warning")) .Select(line => line.Replace(baseFolder, "") -- cgit v1.2.3-55-g6feb