-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix flaky DistributedFileLoggerParameters test with test isolation #13669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Basic test of the file logger. Writes to a log file in the temp directory. | ||
| /// </summary> | ||
|
|
@@ -482,49 +490,53 @@ 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(); | ||
|
|
||
| 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(); | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| 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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Assertion style: replace Per repo testing conventions, Shouldly assertions should be used in modified code. The current pattern is verbose and produces poor failure diagnostics. Replace all occurrences in this method with: fileLogger.InternalFilelogger.Parameters.ShouldBe(
"ForceNoAlign;ShowEventId;ShowCommandLine;logfile=msbuild0.log;",
StringCompareShould.IgnoreCase);This applies to all 4 assertion blocks in |
||
| fileLogger.Initialize(new EventSourceSink()); | ||
| fileLogger.InternalFilelogger.Parameters.ShouldBe( | ||
| "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(folder.Path, "mylogfile4.log") + ";", | ||
| StringCompareShould.IgnoreCase); | ||
| fileLogger.Shutdown(); | ||
|
|
||
| string tempuraDir = Path.Combine(folder.Path, "tempura"); | ||
| Directory.CreateDirectory(tempuraDir); | ||
| fileLogger.NodeId = 1; | ||
| fileLogger.Parameters = "logfile=" + Path.Combine(tempuraDir, "mylogfile.log"); | ||
| fileLogger.Initialize(new EventSourceSink()); | ||
| fileLogger.InternalFilelogger.Parameters.ShouldBe( | ||
| "ForceNoAlign;ShowEventId;ShowCommandLine;logfile=" + Path.Combine(tempuraDir, "mylogfile1.log") + ";", | ||
| StringCompareShould.IgnoreCase); | ||
| fileLogger.Shutdown(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DistributedLoggerNullEmpty() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| TransientTestFolder folder = env.CreateFolder(); | ||
| env.SetCurrentDirectory(folder.Path); | ||
|
|
||
| Assert.Throws<LoggerException>(() => | ||
| { | ||
| DistributedFileLogger fileLogger = new DistributedFileLogger(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.