diff --git a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/GoAppHostTests.cs b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/GoAppHostTests.cs index 140772e41..1dd58489c 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/GoAppHostTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/GoAppHostTests.cs @@ -7,11 +7,11 @@ public class GoAppHostTests [Fact] public async Task GoAppHostCompiles() { - await GoAppHostTest.Run( + await SqlPolyglotAppHostTest.Run(() => GoAppHostTest.Run( appHostProject: "CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.AppHost.Go", exampleName: "sql-database-projects", // The prebuilt Windows AppHost server fails to load hostfxr for MSBuildLocator. validatePublish: !OperatingSystem.IsWindows(), - cancellationToken: TestContext.Current.CancellationToken); + cancellationToken: TestContext.Current.CancellationToken)); } } diff --git a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTest.cs b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTest.cs new file mode 100644 index 000000000..e74659b20 --- /dev/null +++ b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTest.cs @@ -0,0 +1,35 @@ +namespace CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests; + +internal static class SqlPolyglotAppHostTest +{ + private static readonly string[] s_msbuildEnvironmentVariables = + [ + "MSBUILD_EXE_PATH", + "MSBuildExtensionsPath", + "MSBuildSDKsPath" + ]; + + public static async Task Run(Func test) + { + // MSBuildLocator in other SQL tests configures in-process MSBuild; child dotnet builds + // must select their own SDK instead of inheriting those paths. + Dictionary originalValues = s_msbuildEnvironmentVariables.ToDictionary( + name => name, Environment.GetEnvironmentVariable); + try + { + foreach (string name in s_msbuildEnvironmentVariables) + { + Environment.SetEnvironmentVariable(name, null); + } + + await test(); + } + finally + { + foreach ((string name, string? value) in originalValues) + { + Environment.SetEnvironmentVariable(name, value); + } + } + } +} diff --git a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTestTests.cs b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTestTests.cs new file mode 100644 index 000000000..5d0eb94af --- /dev/null +++ b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/SqlPolyglotAppHostTestTests.cs @@ -0,0 +1,40 @@ +namespace CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests; + +public class SqlPolyglotAppHostTestTests +{ + [Fact] + public async Task RestoresMSBuildEnvironmentAfterFailure() + { + string[] variables = ["MSBUILD_EXE_PATH", "MSBuildExtensionsPath", "MSBuildSDKsPath"]; + Dictionary originalValues = variables.ToDictionary(name => name, Environment.GetEnvironmentVariable); + try + { + foreach (string variable in variables) + { + Environment.SetEnvironmentVariable(variable, "test-sdk-path"); + } + + await Assert.ThrowsAsync(() => SqlPolyglotAppHostTest.Run(() => + { + foreach (string variable in variables) + { + Assert.Null(Environment.GetEnvironmentVariable(variable)); + } + + throw new InvalidOperationException(); + })); + + foreach (string variable in variables) + { + Assert.Equal("test-sdk-path", Environment.GetEnvironmentVariable(variable)); + } + } + finally + { + foreach ((string variable, string? value) in originalValues) + { + Environment.SetEnvironmentVariable(variable, value); + } + } + } +} diff --git a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/TypeScriptAppHostTests.cs b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/TypeScriptAppHostTests.cs index 27163b8c9..fb689b2fc 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/TypeScriptAppHostTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/TypeScriptAppHostTests.cs @@ -9,11 +9,11 @@ public class TypeScriptAppHostTests [Fact] public async Task TypeScriptAppHostCompilesAndStarts() { - await TypeScriptAppHostTest.Run( + await SqlPolyglotAppHostTest.Run(() => TypeScriptAppHostTest.Run( appHostProject: "CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.AppHost.TypeScript", packageName: "CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects", exampleName: "sql-database-projects", waitForResources: [], - cancellationToken: TestContext.Current.CancellationToken); + cancellationToken: TestContext.Current.CancellationToken)); } } \ No newline at end of file