Skip to content
Open
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
58 changes: 36 additions & 22 deletions src/Utilities.UnitTests/ToolTask_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,35 +572,49 @@ public void FailToEnumerateStandardLoggingImportance(bool isErr, string invalidL
[Fact]
public void ToolTaskCanChangeCanonicalErrorFormat()
{
string tempFile = FileUtilities.GetTemporaryFileName();
File.WriteAllText(tempFile, @"
using var env = TestEnvironment.Create(_output);
var toolOutput = env.CreateFile(
"tool-output.txt",
"""
Main.cs(17,20): warning CS0168: The variable 'foo' is declared but never used.
BADTHINGHAPPENED: This is my custom error format that's not in canonical error format.
");
""");

using (MyTool t = new MyTool())
var engine = new MockEngine(_output);

using var task = new MyTool
{
MockEngine3 engine = new MockEngine3();
t.BuildEngine = engine;
BuildEngine = engine,
// The command we're giving is the command to spew the contents of the temp
// file we created above.
t.MockCommandLineCommands = NativeMethodsShared.IsWindows
? $"/C type \"{tempFile}\""
: $"-c \"cat \'{tempFile}\'\"";

t.Execute();

// The above command logged a canonical warning, as well as a custom error.
engine.AssertLogContains("CS0168");
engine.AssertLogContains("The variable 'foo' is declared but never used");
engine.AssertLogContains("BADTHINGHAPPENED");
engine.AssertLogContains("This is my custom error format");

engine.Warnings.ShouldBe(1); // "Expected one warning in log."
engine.Errors.ShouldBe(1); // "Expected one error in log."
}
MockCommandLineCommands = NativeMethodsShared.IsWindows
? $"/C type \"{toolOutput.Path}\""
: $"-c \"cat '{toolOutput.Path}'\"",
};

File.Delete(tempFile);
task.Execute().ShouldBeFalse();

engine.ShouldSatisfyAllConditions(
() => task.ExitCode.ShouldBe(-1),
() => engine.Warnings.ShouldBe(1),
Comment thread
rainersigwald marked this conversation as resolved.
() => engine.Errors.ShouldBe(1));

engine.WarningEvents
.ShouldHaveSingleItem()
.ShouldSatisfyAllConditions(
w => w.Code.ShouldBe("CS0168"),
w => w.File.ShouldBe("Main.cs"),
w => w.LineNumber.ShouldBe(17),
w => w.ColumnNumber.ShouldBe(20),
w => w.Message.ShouldContain("foo"));
Comment thread
rainersigwald marked this conversation as resolved.

engine.ErrorEvents
.ShouldHaveSingleItem()
.ShouldSatisfyAllConditions(
e => e.LineNumber.ShouldBe(0),
e => e.ColumnNumber.ShouldBe(0),
e => e.Message.ShouldContain("BADTHINGHAPPENED"),
e => e.Message.ShouldContain("custom error format"));
}

/// <summary>
Expand Down
Loading