Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions src/Build.UnitTests/FileLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,13 @@ namespace Microsoft.Build.UnitTests
{
public class FileLogger_Tests
{
private readonly ITestOutputHelper _output;

public FileLogger_Tests(ITestOutputHelper output)
{
_output = output;
}
Comment thread
JanProvaznik marked this conversation as resolved.

/// <summary>
/// Basic test of the file logger. Writes to a log file in the temp directory.
/// </summary>
Expand Down Expand Up @@ -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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Assertion style: replace Assert.Equal(0, string.Compare(...)) with Shouldly

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 DistributedFileLoggerParameters.

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();
Expand Down
Loading