-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add cross-platform E2E tests for task host factory lifecycle #13023
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 13 commits
bf4a7e9
3b7be7e
f605b81
92c0e91
297044c
b83d19b
4966a51
c099a98
1e08f36
35de0ce
2fddfdf
bb9fd50
48ade07
b8afa00
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using Microsoft.Build.UnitTests; | ||
| using Microsoft.Build.UnitTests.Shared; | ||
| using Shouldly; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.Build.Engine.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// End-to-end tests for task host factory lifecycle behavior. | ||
| /// | ||
| /// Tests validate the behavior based on whether the TaskHost runtime matches | ||
| /// the executing MSBuild runtime and whether TaskHostFactory is explicitly requested. | ||
| /// | ||
| /// This is a regression test for https://github.com/dotnet/msbuild/issues/13013 | ||
| /// </summary> | ||
| public class TaskHostFactoryLifecycle_E2E_Tests | ||
| { | ||
| private static string AssemblyLocation { get; } = Path.Combine(Path.GetDirectoryName(typeof(TaskHostFactoryLifecycle_E2E_Tests).Assembly.Location) ?? System.AppContext.BaseDirectory); | ||
|
|
||
| private static string TestAssetsRootPath { get; } = Path.Combine(AssemblyLocation, "TestAssets", "TaskHostLifecycle"); | ||
|
|
||
| private readonly ITestOutputHelper _output; | ||
|
|
||
| public TaskHostFactoryLifecycle_E2E_Tests(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates task host lifecycle behavior for all scenarios. | ||
| /// | ||
| /// Test scenarios: | ||
| /// 1. Runtime matches + TaskHostFactory requested → short-lived out of proc (nodereuse:False) | ||
| /// 2. Runtime matches + TaskHostFactory NOT requested → in-proc execution | ||
| /// 3. Runtime doesn't match + TaskHostFactory requested → short-lived out of proc (nodereuse:False) | ||
| /// 4. Runtime doesn't match + TaskHostFactory NOT requested → long-lived sidecar out of proc (nodereuse:True) | ||
| /// </summary> | ||
| /// <param name="runtimeToUse">The runtime to use for the task (CurrentRuntime or NET)</param> | ||
| /// <param name="taskFactoryToUse">The task factory to use (TaskHostFactory or AssemblyTaskFactory)</param> | ||
| /// <param name="expectedNodeReuse">Expected node reuse value (true for long-lived, false for short-lived, null for in-proc)</param> | ||
| [Theory] | ||
| [InlineData("CurrentRuntime", "TaskHostFactory", false)] // Match + Explicit → short-lived out-of-proc | ||
| [InlineData("CurrentRuntime", "AssemblyTaskFactory", null)] // Match + No Explicit → in-proc | ||
|
|
||
| // Not test-able on .NET msbuild as it can't run a CLR2/CLR4 task host (out-of-proc) | ||
| #if !NET | ||
| [InlineData("NET", "TaskHostFactory", false)] // No Match + Explicit → short-lived out-of-proc | ||
| [InlineData("NET", "AssemblyTaskFactory", true)] // No Match + No Explicit → long-lived sidecar out-of-proc | ||
| #endif | ||
| public void TaskHostLifecycle_ValidatesAllScenarios( | ||
| string runtimeToUse, | ||
| string taskFactoryToUse, | ||
| bool? expectedNodeReuse) | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| string testProjectPath = Path.Combine(TestAssetsRootPath, "TaskHostLifecycleTestApp.csproj"); | ||
|
|
||
| string testTaskOutput = RunnerUtilities.ExecBootstrapedMSBuild($"{testProjectPath} -restore -v:n /p:RuntimeToUse={runtimeToUse} /p:TaskFactoryToUse={taskFactoryToUse}", out bool successTestTask); | ||
|
Member
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. Is
Contributor
Author
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. Removed
Member
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. We can't use Runtime="NET" on a project that doesn't use the Microsoft.NET.Sdk when running with desktop msbuild. |
||
|
|
||
| if (!successTestTask) | ||
| { | ||
| _output.WriteLine(testTaskOutput); | ||
| } | ||
|
|
||
| successTestTask.ShouldBeTrue(); | ||
|
|
||
| // Verify execution mode (out-of-proc vs in-proc) and node reuse behavior | ||
| if (expectedNodeReuse.HasValue) | ||
| { | ||
| // For out-of-proc scenarios, validate the task runs in a separate process | ||
| // by checking for the presence of command-line arguments that indicate task host execution | ||
| testTaskOutput.ShouldContain("/nodemode:", | ||
| customMessage: "Task should run out-of-proc and have /nodemode: in its command-line arguments"); | ||
|
|
||
| // Validate the nodereuse flag in the task's command-line arguments | ||
| string expectedFlag = expectedNodeReuse.Value ? "/nodereuse:True" : "/nodereuse:False"; | ||
| testTaskOutput.ShouldContain(expectedFlag, | ||
| customMessage: $"Task should have {expectedFlag} in its command-line arguments"); | ||
| } | ||
| else | ||
| { | ||
| // For in-proc scenarios, validate the task does NOT run in a task host | ||
| // by ensuring task host specific command-line flags are not present | ||
| testTaskOutput.ShouldNotContain("/nodemode:", | ||
| customMessage: "Task should run in-proc and not have task host command-line arguments like /nodemode:"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <TestProjectFolder>$([System.IO.Path]::GetFullPath('$([System.IO.Path]::Combine('$(AssemblyLocation)', '..'))'))</TestProjectFolder> | ||
| <ExampleTaskPath>$([System.IO.Path]::Combine('$(TestProjectFolder)', '$(TargetFramework)', 'ExampleTask.dll'))</ExampleTaskPath> | ||
| </PropertyGroup> | ||
|
|
||
| <UsingTask | ||
| TaskName="ExampleTask" | ||
| AssemblyFile="$(ExampleTaskPath)" | ||
| TaskFactory="$(TaskFactoryToUse)" | ||
| Runtime="$(RuntimeToUse)"/> | ||
|
|
||
| <Target Name="TestTask" BeforeTargets="Build"> | ||
| <ExampleTask /> | ||
| </Target> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot if you pass the
ITestOutputHelperhere you don't have to explicitly log it later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to pass
ITestOutputHelpertoExecBootstrapedMSBuildvia theoutputHelperparameter. This automatically handles test output logging, eliminating the need for manual logging. Commit: b8afa00