From bd672b8bf117741c164676d165926c26612a1103 Mon Sep 17 00:00:00 2001 From: Jan Kratochvil Date: Tue, 7 Apr 2026 17:20:39 +0200 Subject: [PATCH] Migrates unit tests to use RoslynCodeTaskFactory to enable running tests under .NET Core --- .../BackEnd/TaskBuilder_Tests.cs | 93 +-------- src/Build.UnitTests/BackEnd/TaskHost_Tests.cs | 178 +++++++++--------- 2 files changed, 91 insertions(+), 180 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs b/src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs index 7d80283306d..cd7a3910c9f 100644 --- a/src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs @@ -690,16 +690,15 @@ public void MSBuildLoadContext_AcceptsNewerAssemblyVersions() #endif -#if FEATURE_CODETASKFACTORY /// - /// If an item being output from a task has null metadata, we shouldn't crash. + /// If an item being output from an inline task has null metadata, we shouldn't crash. /// [Fact] public void NullMetadataOnOutputItems_InlineTask() { string projectContents = @" - - + + @@ -719,9 +718,9 @@ public void NullMetadataOnOutputItems_InlineTask() - + - + @@ -731,88 +730,6 @@ public void NullMetadataOnOutputItems_InlineTask() logger.AssertLogContains("[foo: ]"); } - /// - /// If an item being output from a task has null metadata, we shouldn't crash. - /// - [Fact(Skip = "This test fails when diagnostic logging is available, as deprecated EscapingUtilities.UnescapeAll method cannot handle null value. This is not relevant to non-deprecated version of this method.")] - public void NullMetadataOnLegacyOutputItems_InlineTask() - { - string projectContents = @" - - - - - - - - metadata = new Dictionary(); - metadata.Add(`a`, null); - - OutputItems[0] = new TaskItem(`foo`, (IDictionary)metadata); - - return true; - ]]> - - - - - - - - - - - "; - - MockLogger logger = ObjectModelHelpers.BuildProjectExpectSuccess(projectContents, _testOutput); - logger.AssertLogContains("[foo: ]"); - } - - /// - /// If an item being output from a task has null metadata, we shouldn't crash. - /// - [Fact(Skip = "This test fails when diagnostic logging is available, as deprecated EscapingUtilities.UnescapeAll method cannot handle null value. This is not relevant to non-deprecated version of this method.")] - [Trait("Category", "non-mono-tests")] - public void NullMetadataOnLegacyOutputItems_InlineTask_Diagnostic() - { - string projectContents = @" - - - - - - - - metadata = new Dictionary(); - metadata.Add(`a`, null); - - OutputItems[0] = new TaskItem(`foo`, (IDictionary)metadata); - - return true; - ]]> - - - - - - - - - - - "; - - MockLogger logger = ObjectModelHelpers.BuildProjectExpectSuccess(projectContents, _testOutput, loggerVerbosity: LoggerVerbosity.Diagnostic); - logger.AssertLogContains("[foo: ]"); - } -#endif - /// /// Validates that the defining project metadata is set (or not set) as expected in /// various task output-related operations, using a task built against the current diff --git a/src/Build.UnitTests/BackEnd/TaskHost_Tests.cs b/src/Build.UnitTests/BackEnd/TaskHost_Tests.cs index 2e8503bf6ab..6cfb3ced2e9 100644 --- a/src/Build.UnitTests/BackEnd/TaskHost_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskHost_Tests.cs @@ -546,37 +546,35 @@ public void IsRunningMultipleNodes4Nodes() Assert.True(_taskHost.IsRunningMultipleNodes); // "Expect IsRunningMultipleNodes to be true with 4 nodes" } -#if FEATURE_CODETASKFACTORY /// /// Task logging after it's done should not crash us. /// [Fact] public void LogCustomAfterTaskIsDone() { - string projectFileContents = @" - - - - - - + string projectFileContents = """ + + + + + - { - Thread.Sleep(100); - Log.LogExternalProjectStarted(""a"", ""b"", ""c"", ""d""); // this logs a custom event - }); - + Log.LogWarning("[1]"); + ThreadPool.QueueUserWorkItem(state => + { + Thread.Sleep(100); + Log.LogExternalProjectStarted("a", "b", "c", "d"); + }); ]]> - - - - - - - - "; + + + + + + + + + """; MockLogger mockLogger = Helpers.BuildProjectWithNewOMExpectSuccess(projectFileContents); mockLogger.AssertLogContains("[1]"); @@ -589,30 +587,29 @@ public void LogCustomAfterTaskIsDone() [Fact] public void LogCommentAfterTaskIsDone() { - string projectFileContents = @" - - - - - - + string projectFileContents = """ + + + + + - { - Thread.Sleep(100); - Log.LogMessage(""[2]""); - }); - + Log.LogMessage("[1]"); + ThreadPool.QueueUserWorkItem(state => + { + Thread.Sleep(100); + Log.LogMessage("[2]"); + }); ]]> - - - - - - - - "; + + + + + + + + + """; MockLogger mockLogger = Helpers.BuildProjectWithNewOMExpectSuccess(projectFileContents); mockLogger.AssertLogContains("[1]"); @@ -620,35 +617,34 @@ public void LogCommentAfterTaskIsDone() } /// - /// Task logging after it's done should not crash us. + /// Task logging a warning after it's done should not crash us. /// [Fact] public void LogWarningAfterTaskIsDone() { - string projectFileContents = @" - - - - - - + string projectFileContents = """ + + + + + - { - Thread.Sleep(100); - Log.LogWarning(""[2]""); - }); - + Log.LogWarning("[1]"); + ThreadPool.QueueUserWorkItem(state => + { + Thread.Sleep(100); + Log.LogWarning("[2]"); + }); ]]> - - - - - - - - "; + + + + + + + + + """; MockLogger mockLogger = Helpers.BuildProjectWithNewOMExpectSuccess(projectFileContents); mockLogger.AssertLogContains("[1]"); @@ -656,41 +652,39 @@ public void LogWarningAfterTaskIsDone() } /// - /// Task logging after it's done should not crash us. + /// Task logging an error after it's done should not crash us. /// [Fact] public void LogErrorAfterTaskIsDone() { - string projectFileContents = @" - - - - - - + string projectFileContents = """ + + + + + - { - Thread.Sleep(100); - Log.LogError(""[2]""); - }); - + Log.LogError("[1]"); + ThreadPool.QueueUserWorkItem(state => + { + Thread.Sleep(100); + Log.LogError("[2]"); + }); ]]> - - - - - - - - "; + + + + + + + + + """; MockLogger mockLogger = Helpers.BuildProjectWithNewOMExpectSuccess(projectFileContents); mockLogger.AssertLogContains("[1]"); mockLogger.AssertLogContains("[3]"); // [2] may or may not appear. } -#endif /// /// Verifies that tasks can get global properties.