Skip to content

Commit 8cac0a3

Browse files
committed
Extract Aspire.Hosting.Python.Tests project
Contributes to #4294
1 parent f38b6cb commit 8cac0a3

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

Aspire.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Redis.Tests"
510510
EndProject
511511
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}"
512512
EndProject
513+
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}"
514+
EndProject
513515
Global
514516
GlobalSection(SolutionConfigurationPlatforms) = preSolution
515517
Debug|Any CPU = Debug|Any CPU
@@ -1332,6 +1334,10 @@ Global
13321334
{8E2AA85E-C351-47B4-AF91-58557FAD5840}.Debug|Any CPU.Build.0 = Debug|Any CPU
13331335
{8E2AA85E-C351-47B4-AF91-58557FAD5840}.Release|Any CPU.ActiveCfg = Release|Any CPU
13341336
{8E2AA85E-C351-47B4-AF91-58557FAD5840}.Release|Any CPU.Build.0 = Release|Any CPU
1337+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1338+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1339+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1340+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.Build.0 = Release|Any CPU
13351341
EndGlobalSection
13361342
GlobalSection(SolutionProperties) = preSolution
13371343
HideSolutionNode = FALSE
@@ -1576,6 +1582,7 @@ Global
15761582
{830A89EC-4029-4753-B25A-068BAE37DEC7} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
15771583
{1BC02557-B78B-48CE-9D3C-488A6B7672F4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
15781584
{8E2AA85E-C351-47B4-AF91-58557FAD5840} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
1585+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
15791586
EndGlobalSection
15801587
GlobalSection(ExtensibilityGlobals) = postSolution
15811588
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}

tests/Aspire.Hosting.Tests/Python/AddPythonProjectTests.cs renamed to tests/Aspire.Hosting.Python.Tests/AddPythonProjectTests.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
using System.Diagnostics;
99
using Aspire.Components.Common.Tests;
1010
using Xunit.Abstractions;
11+
using Aspire.Hosting.ApplicationModel;
12+
using System.Runtime.CompilerServices;
13+
using Microsoft.Extensions.Logging;
1114

12-
namespace Aspire.Hosting.Tests.Python;
15+
namespace Aspire.Hosting.Python.Tests;
1316

1417
public class AddPythonProjectTests(ITestOutputHelper outputHelper)
1518
{
@@ -21,9 +24,9 @@ public async Task AddPythonProjectProducesDockerfileResourceInManifest()
2124

2225
var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json");
2326

24-
using var builder = TestDistributedApplicationBuilder.Create(options =>
27+
using var builder = CreateTestDistributedApplicationBuilder(options =>
2528
{
26-
options.ProjectDirectory = Path.GetFullPath(projectDirectory);
29+
GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory);
2730
options.Args = ["--publisher", "manifest", "--output-path", manifestPath];
2831
});
2932

@@ -51,9 +54,9 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife
5154

5255
var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json");
5356

54-
using var builder = TestDistributedApplicationBuilder.Create(options =>
57+
using var builder = CreateTestDistributedApplicationBuilder(options =>
5558
{
56-
options.ProjectDirectory = Path.GetFullPath(projectDirectory);
59+
GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory);
5760
options.Args = ["--publisher", "manifest", "--output-path", manifestPath];
5861
});
5962

@@ -76,13 +79,16 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife
7679
Directory.Delete(projectDirectory, true);
7780
}
7881

82+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_projectDirectory")]
83+
static extern ref string? GetProjectDirectoryRef(DistributedApplicationOptions? @this);
84+
7985
[Fact]
8086
[RequiresTools(["python"])]
8187
public async Task PythonResourceFinishesSuccessfully()
8288
{
8389
var (projectDirectory, _, scriptName) = CreateTempPythonProject(outputHelper);
8490

85-
using var builder = TestDistributedApplicationBuilder.Create();
91+
using var builder = CreateTestDistributedApplicationBuilder();
8692
builder.AddPythonProject("pyproj", projectDirectory, scriptName);
8793

8894
using var app = builder.Build();
@@ -103,10 +109,10 @@ public async Task PythonResourceFinishesSuccessfully()
103109
[RequiresTools(["python"])]
104110
public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
105111
{
106-
using var builder = TestDistributedApplicationBuilder.Create();
112+
using var builder = CreateTestDistributedApplicationBuilder();
107113

108114
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper);
109-
115+
110116
builder.AddPythonProject("pythonProject", projectDirectory, scriptName);
111117

112118
var app = builder.Build();
@@ -118,7 +124,7 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
118124
Assert.Equal("pythonProject", pythonProjectResource.Name);
119125
Assert.Equal(projectDirectory, pythonProjectResource.WorkingDirectory);
120126

121-
if(OperatingSystem.IsWindows())
127+
if (OperatingSystem.IsWindows())
122128
{
123129
Assert.Equal(Path.Join(projectDirectory, ".venv", "Scripts", "python.exe"), pythonProjectResource.Command);
124130
}
@@ -139,10 +145,10 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
139145
[RequiresTools(["python"])]
140146
public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstrumentationExecutable()
141147
{
142-
using var builder = TestDistributedApplicationBuilder.Create();
148+
using var builder = CreateTestDistributedApplicationBuilder();
143149

144150
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper, instrument: true);
145-
151+
146152
builder.AddPythonProject("pythonProject", projectDirectory, scriptName, virtualEnvironmentPath: ".venv");
147153

148154
var app = builder.Build();
@@ -169,7 +175,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru
169175
Assert.Equal("otlp", commandArguments[5]);
170176
Assert.Equal(pythonExecutable, commandArguments[6]);
171177
Assert.Equal(scriptName, commandArguments[7]);
172-
178+
173179
// If we don't throw, clean up the directories.
174180
Directory.Delete(projectDirectory, true);
175181
}
@@ -178,7 +184,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru
178184
[RequiresTools(["python"])]
179185
public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments()
180186
{
181-
using var builder = TestDistributedApplicationBuilder.Create();
187+
using var builder = CreateTestDistributedApplicationBuilder();
182188

183189
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper);
184190

@@ -211,6 +217,13 @@ public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments()
211217
Directory.Delete(projectDirectory, true);
212218
}
213219

220+
private TestDistributedApplicationBuilder CreateTestDistributedApplicationBuilder(Action<DistributedApplicationOptions>? configureOptions = null)
221+
{
222+
var builder = TestDistributedApplicationBuilder.Create(configureOptions);
223+
builder.Services.AddXunitLogging(outputHelper);
224+
return builder;
225+
}
226+
214227
private static (string projectDirectory, string pythonExecutable, string scriptName) CreateTempPythonProject(ITestOutputHelper outputHelper, bool instrument = false)
215228
{
216229
var projectDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(NetCurrent)</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" />
9+
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" />
10+
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<ProjectReference Include="..\..\src\Aspire.Hosting.PostgreSQL\Aspire.Hosting.PostgreSQL.csproj" IsAspireProjectResource="false" />
3838
<ProjectReference Include="..\..\src\Aspire.Hosting.Qdrant\Aspire.Hosting.Qdrant.csproj" IsAspireProjectResource="false" />
3939
<ProjectReference Include="..\..\src\Aspire.Hosting.Testing\Aspire.Hosting.Testing.csproj" />
40-
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" IsAspireProjectResource="false" />
4140
<ProjectReference Include="..\..\src\Aspire.Hosting.Valkey\Aspire.Hosting.Valkey.csproj" IsAspireProjectResource="false" />
4241
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" IsAspireProjectResource="false" />
4342
<ProjectReference Include="..\testproject\TestProject.AppHost\TestProject.AppHost.csproj" IsAspireProjectResource="false" />

tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public static TestDistributedApplicationBuilder Create(params string[] args)
4141
return new TestDistributedApplicationBuilder(options => options.Args = args);
4242
}
4343

44-
public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions> configureOptions)
44+
public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions>? configureOptions)
4545
{
4646
return new TestDistributedApplicationBuilder(configureOptions);
4747
}
4848

4949
public static TestDistributedApplicationBuilder CreateWithTestContainerRegistry() =>
5050
Create(o => o.ContainerRegistryOverride = TestConstants.AspireTestContainerRegistry);
5151

52-
private TestDistributedApplicationBuilder(Action<DistributedApplicationOptions> configureOptions)
52+
private TestDistributedApplicationBuilder(Action<DistributedApplicationOptions>? configureOptions)
5353
{
5454
var appAssembly = typeof(TestDistributedApplicationBuilder).Assembly;
5555
var assemblyName = appAssembly.FullName;
@@ -80,7 +80,7 @@ void Configure(DistributedApplicationOptions applicationOptions, HostApplication
8080
["DcpPublisher:ResourceNameSuffix"] = $"{Random.Shared.Next():x}",
8181
});
8282

83-
configureOptions(applicationOptions);
83+
configureOptions?.Invoke(applicationOptions);
8484
}
8585
}
8686

0 commit comments

Comments
 (0)