From 8cac0a3bf07972f795a1d4b545f0b542953a6ccd Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Mon, 15 Jul 2024 18:40:18 -0500 Subject: [PATCH] Extract Aspire.Hosting.Python.Tests project Contributes to #4294 --- Aspire.sln | 7 ++++ .../AddPythonProjectTests.cs | 39 ++++++++++++------- .../Aspire.Hosting.Python.Tests.csproj | 13 +++++++ .../Aspire.Hosting.Tests.csproj | 1 - .../TestDistributedApplicationBuilder.cs | 6 +-- 5 files changed, 49 insertions(+), 17 deletions(-) rename tests/{Aspire.Hosting.Tests/Python => Aspire.Hosting.Python.Tests}/AddPythonProjectTests.cs (90%) create mode 100644 tests/Aspire.Hosting.Python.Tests/Aspire.Hosting.Python.Tests.csproj diff --git a/Aspire.sln b/Aspire.sln index 283cf1d1d3..2eb20a31f2 100644 --- a/Aspire.sln +++ b/Aspire.sln @@ -510,6 +510,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Redis.Tests" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Qdrant.Tests", "tests\Aspire.Hosting.Qdrant.Tests\Aspire.Hosting.Qdrant.Tests.csproj", "{8E2AA85E-C351-47B4-AF91-58557FAD5840}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Python.Tests", "tests\Aspire.Hosting.Python.Tests\Aspire.Hosting.Python.Tests.csproj", "{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1332,6 +1334,10 @@ Global {8E2AA85E-C351-47B4-AF91-58557FAD5840}.Debug|Any CPU.Build.0 = Debug|Any CPU {8E2AA85E-C351-47B4-AF91-58557FAD5840}.Release|Any CPU.ActiveCfg = Release|Any CPU {8E2AA85E-C351-47B4-AF91-58557FAD5840}.Release|Any CPU.Build.0 = Release|Any CPU + {72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1576,6 +1582,7 @@ Global {830A89EC-4029-4753-B25A-068BAE37DEC7} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60} {1BC02557-B78B-48CE-9D3C-488A6B7672F4} = {830A89EC-4029-4753-B25A-068BAE37DEC7} {8E2AA85E-C351-47B4-AF91-58557FAD5840} = {830A89EC-4029-4753-B25A-068BAE37DEC7} + {72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4} = {830A89EC-4029-4753-B25A-068BAE37DEC7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C} diff --git a/tests/Aspire.Hosting.Tests/Python/AddPythonProjectTests.cs b/tests/Aspire.Hosting.Python.Tests/AddPythonProjectTests.cs similarity index 90% rename from tests/Aspire.Hosting.Tests/Python/AddPythonProjectTests.cs rename to tests/Aspire.Hosting.Python.Tests/AddPythonProjectTests.cs index fcbfeb383e..06556e6615 100644 --- a/tests/Aspire.Hosting.Tests/Python/AddPythonProjectTests.cs +++ b/tests/Aspire.Hosting.Python.Tests/AddPythonProjectTests.cs @@ -8,8 +8,11 @@ using System.Diagnostics; using Aspire.Components.Common.Tests; using Xunit.Abstractions; +using Aspire.Hosting.ApplicationModel; +using System.Runtime.CompilerServices; +using Microsoft.Extensions.Logging; -namespace Aspire.Hosting.Tests.Python; +namespace Aspire.Hosting.Python.Tests; public class AddPythonProjectTests(ITestOutputHelper outputHelper) { @@ -21,9 +24,9 @@ public async Task AddPythonProjectProducesDockerfileResourceInManifest() var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json"); - using var builder = TestDistributedApplicationBuilder.Create(options => + using var builder = CreateTestDistributedApplicationBuilder(options => { - options.ProjectDirectory = Path.GetFullPath(projectDirectory); + GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory); options.Args = ["--publisher", "manifest", "--output-path", manifestPath]; }); @@ -51,9 +54,9 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json"); - using var builder = TestDistributedApplicationBuilder.Create(options => + using var builder = CreateTestDistributedApplicationBuilder(options => { - options.ProjectDirectory = Path.GetFullPath(projectDirectory); + GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory); options.Args = ["--publisher", "manifest", "--output-path", manifestPath]; }); @@ -76,13 +79,16 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife Directory.Delete(projectDirectory, true); } + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_projectDirectory")] + static extern ref string? GetProjectDirectoryRef(DistributedApplicationOptions? @this); + [Fact] [RequiresTools(["python"])] public async Task PythonResourceFinishesSuccessfully() { var (projectDirectory, _, scriptName) = CreateTempPythonProject(outputHelper); - using var builder = TestDistributedApplicationBuilder.Create(); + using var builder = CreateTestDistributedApplicationBuilder(); builder.AddPythonProject("pyproj", projectDirectory, scriptName); using var app = builder.Build(); @@ -103,10 +109,10 @@ public async Task PythonResourceFinishesSuccessfully() [RequiresTools(["python"])] public async Task AddPythonProject_SetsResourcePropertiesCorrectly() { - using var builder = TestDistributedApplicationBuilder.Create(); + using var builder = CreateTestDistributedApplicationBuilder(); var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper); - + builder.AddPythonProject("pythonProject", projectDirectory, scriptName); var app = builder.Build(); @@ -118,7 +124,7 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly() Assert.Equal("pythonProject", pythonProjectResource.Name); Assert.Equal(projectDirectory, pythonProjectResource.WorkingDirectory); - if(OperatingSystem.IsWindows()) + if (OperatingSystem.IsWindows()) { Assert.Equal(Path.Join(projectDirectory, ".venv", "Scripts", "python.exe"), pythonProjectResource.Command); } @@ -139,10 +145,10 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly() [RequiresTools(["python"])] public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstrumentationExecutable() { - using var builder = TestDistributedApplicationBuilder.Create(); + using var builder = CreateTestDistributedApplicationBuilder(); var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper, instrument: true); - + builder.AddPythonProject("pythonProject", projectDirectory, scriptName, virtualEnvironmentPath: ".venv"); var app = builder.Build(); @@ -169,7 +175,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru Assert.Equal("otlp", commandArguments[5]); Assert.Equal(pythonExecutable, commandArguments[6]); Assert.Equal(scriptName, commandArguments[7]); - + // If we don't throw, clean up the directories. Directory.Delete(projectDirectory, true); } @@ -178,7 +184,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru [RequiresTools(["python"])] public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments() { - using var builder = TestDistributedApplicationBuilder.Create(); + using var builder = CreateTestDistributedApplicationBuilder(); var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper); @@ -211,6 +217,13 @@ public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments() Directory.Delete(projectDirectory, true); } + private TestDistributedApplicationBuilder CreateTestDistributedApplicationBuilder(Action? configureOptions = null) + { + var builder = TestDistributedApplicationBuilder.Create(configureOptions); + builder.Services.AddXunitLogging(outputHelper); + return builder; + } + private static (string projectDirectory, string pythonExecutable, string scriptName) CreateTempPythonProject(ITestOutputHelper outputHelper, bool instrument = false) { var projectDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); diff --git a/tests/Aspire.Hosting.Python.Tests/Aspire.Hosting.Python.Tests.csproj b/tests/Aspire.Hosting.Python.Tests/Aspire.Hosting.Python.Tests.csproj new file mode 100644 index 0000000000..620f143127 --- /dev/null +++ b/tests/Aspire.Hosting.Python.Tests/Aspire.Hosting.Python.Tests.csproj @@ -0,0 +1,13 @@ + + + + $(NetCurrent) + + + + + + + + + diff --git a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj index ee9a9e1433..b47d6c8388 100644 --- a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj +++ b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj @@ -37,7 +37,6 @@ - diff --git a/tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs b/tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs index 07352a757c..4a033532bb 100644 --- a/tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs +++ b/tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs @@ -41,7 +41,7 @@ public static TestDistributedApplicationBuilder Create(params string[] args) return new TestDistributedApplicationBuilder(options => options.Args = args); } - public static TestDistributedApplicationBuilder Create(Action configureOptions) + public static TestDistributedApplicationBuilder Create(Action? configureOptions) { return new TestDistributedApplicationBuilder(configureOptions); } @@ -49,7 +49,7 @@ public static TestDistributedApplicationBuilder Create(Action Create(o => o.ContainerRegistryOverride = TestConstants.AspireTestContainerRegistry); - private TestDistributedApplicationBuilder(Action configureOptions) + private TestDistributedApplicationBuilder(Action? configureOptions) { var appAssembly = typeof(TestDistributedApplicationBuilder).Assembly; var assemblyName = appAssembly.FullName; @@ -80,7 +80,7 @@ void Configure(DistributedApplicationOptions applicationOptions, HostApplication ["DcpPublisher:ResourceNameSuffix"] = $"{Random.Shared.Next():x}", }); - configureOptions(applicationOptions); + configureOptions?.Invoke(applicationOptions); } }