diff --git a/src/Utilities.UnitTests/ToolTask_Tests.cs b/src/Utilities.UnitTests/ToolTask_Tests.cs index 183a1d7f56c..012b62af83d 100644 --- a/src/Utilities.UnitTests/ToolTask_Tests.cs +++ b/src/Utilities.UnitTests/ToolTask_Tests.cs @@ -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), + () => 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")); + + 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")); } ///