diff --git a/src/Build.UnitTests/FileLogger_Tests.cs b/src/Build.UnitTests/FileLogger_Tests.cs index 28161a9a783..09d52d7dcaf 100644 --- a/src/Build.UnitTests/FileLogger_Tests.cs +++ b/src/Build.UnitTests/FileLogger_Tests.cs @@ -8,6 +8,7 @@ using Microsoft.Build.Framework; using Microsoft.Build.Logging; using Microsoft.Build.Shared; +using Shouldly; using Xunit; using EventSourceSink = Microsoft.Build.BackEnd.Logging.EventSourceSink; using Project = Microsoft.Build.Evaluation.Project; @@ -18,6 +19,13 @@ namespace Microsoft.Build.UnitTests { public class FileLogger_Tests { + private readonly ITestOutputHelper _output; + + public FileLogger_Tests(ITestOutputHelper output) + { + _output = output; + } + /// /// Basic test of the file logger. Writes to a log file in the temp directory. /// @@ -482,44 +490,48 @@ private void VerifyFileContent(string file, string expectedContent) [Fact] public void DistributedFileLoggerParameters() { - DistributedFileLogger fileLogger = new DistributedFileLogger(); - try - { - fileLogger.NodeId = 0; - fileLogger.Initialize(new EventSourceSink()); - Assert.Equal(0, string.Compare(fileLogger.InternalFilelogger.Parameters, "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=msbuild0.log;", StringComparison.OrdinalIgnoreCase)); - fileLogger.Shutdown(); + using TestEnvironment env = TestEnvironment.Create(_output); - fileLogger.NodeId = 3; - fileLogger.Parameters = "logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "mylogfile.log"); - fileLogger.Initialize(new EventSourceSink()); - Assert.Equal(0, string.Compare(fileLogger.InternalFilelogger.Parameters, "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "mylogfile3.log") + ";", StringComparison.OrdinalIgnoreCase)); - fileLogger.Shutdown(); + // Use a transient folder under TEMP as the current directory so log files (notably the + // default "msbuild0.log" produced when no logfile parameter is specified) don't leak into + // the test bin directory, where another process (e.g. an antivirus scanner or a parallel + // test) may transiently lock them and cause this test to fail. + TransientTestFolder folder = env.CreateFolder(); + env.SetCurrentDirectory(folder.Path); - fileLogger.NodeId = 4; - fileLogger.Parameters = "logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "mylogfile.log"); - fileLogger.Initialize(new EventSourceSink()); - Assert.Equal(0, string.Compare(fileLogger.InternalFilelogger.Parameters, "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "mylogfile4.log") + ";", StringComparison.OrdinalIgnoreCase)); - fileLogger.Shutdown(); + DistributedFileLogger fileLogger = new DistributedFileLogger(); - Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "tempura")); - fileLogger.NodeId = 1; - fileLogger.Parameters = "logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "tempura", "mylogfile.log"); - fileLogger.Initialize(new EventSourceSink()); - Assert.Equal(0, string.Compare(fileLogger.InternalFilelogger.Parameters, "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(Directory.GetCurrentDirectory(), "tempura", "mylogfile1.log") + ";", StringComparison.OrdinalIgnoreCase)); - fileLogger.Shutdown(); - } - finally - { - if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "tempura"))) - { - File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "tempura", "mylogfile1.log")); - FileUtilities.DeleteWithoutTrailingBackslash(Path.Combine(Directory.GetCurrentDirectory(), "tempura")); - } - File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "mylogfile0.log")); - File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "mylogfile3.log")); - File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "mylogfile4.log")); - } + fileLogger.NodeId = 0; + fileLogger.Initialize(new EventSourceSink()); + fileLogger.InternalFilelogger.Parameters.ShouldBe( + "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=msbuild0.log;", + StringCompareShould.IgnoreCase); + fileLogger.Shutdown(); + + fileLogger.NodeId = 3; + fileLogger.Parameters = "logfile=" + Path.Combine(folder.Path, "mylogfile.log"); + fileLogger.Initialize(new EventSourceSink()); + fileLogger.InternalFilelogger.Parameters.ShouldBe( + "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(folder.Path, "mylogfile3.log") + ";", + StringCompareShould.IgnoreCase); + fileLogger.Shutdown(); + + fileLogger.NodeId = 4; + fileLogger.Parameters = "logfile=" + Path.Combine(folder.Path, "mylogfile.log"); + fileLogger.Initialize(new EventSourceSink()); + fileLogger.InternalFilelogger.Parameters.ShouldBe( + "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(folder.Path, "mylogfile4.log") + ";", + StringCompareShould.IgnoreCase); + fileLogger.Shutdown(); + + TransientTestFolder subfolder = folder.CreateDirectory("tempura"); + fileLogger.NodeId = 1; + fileLogger.Parameters = "logfile=" + Path.Combine(subfolder.Path, "mylogfile.log"); + fileLogger.Initialize(new EventSourceSink()); + fileLogger.InternalFilelogger.Parameters.ShouldBe( + "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(subfolder.Path, "mylogfile1.log") + ";", + StringCompareShould.IgnoreCase); + fileLogger.Shutdown(); } [Fact]