Skip to content
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

Extract Aspire.Hosting.Python.Tests project #4915

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Aspire.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand Down Expand Up @@ -514,6 +514,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Qdrant.Tests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Kafka.Tests", "tests\Aspire.Hosting.Kafka.Tests\Aspire.Hosting.Kafka.Tests.csproj", "{0A83AA67-221E-44B4-9BA9-DC64DC17949E}"
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
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Valkey.Tests", "tests\Aspire.Hosting.Valkey.Tests\Aspire.Hosting.Valkey.Tests.csproj", "{1C16DC2D-3B79-4081-AC1E-F3F965C61216}"
EndProject
Global
Expand Down Expand Up @@ -1334,6 +1336,10 @@ Global
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.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
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -1593,6 +1599,7 @@ Global
{C424395C-1235-41A4-BF55-07880A04368C} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{830A89EC-4029-4753-B25A-068BAE37DEC7} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{1BC02557-B78B-48CE-9D3C-488A6B7672F4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
{7425E5B2-BC47-4521-AC40-B8CECA329E08} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
{8E2AA85E-C351-47B4-AF91-58557FAD5840} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
{0A83AA67-221E-44B4-9BA9-DC64DC17949E} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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];
});

Expand Down Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this property is internal, and we don't want to use InternalsVisibleTo for extension tests. So I used reflection instead

GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory);
options.Args = ["--publisher", "manifest", "--output-path", manifestPath];
});

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -211,6 +217,13 @@ public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments()
Directory.Delete(projectDirectory, true);
}

private TestDistributedApplicationBuilder CreateTestDistributedApplicationBuilder(Action<DistributedApplicationOptions>? 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCurrent)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" />
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<ProjectReference Include="..\..\src\Aspire.Hosting.MySql\Aspire.Hosting.MySql.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Nats\Aspire.Hosting.Nats.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Testing\Aspire.Hosting.Testing.csproj" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\testproject\TestProject.AppHost\TestProject.AppHost.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\Aspire.Hosting.Tests.SharedShim\Aspire.Hosting.Tests.SharedShim.csproj" IsAspireProjectResource="false" Aliases="AspireHostingShared" />
Expand All @@ -44,7 +43,7 @@
<ProjectReference Include="..\..\src\Components\Aspire.MySqlConnector\Aspire.MySqlConnector.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Components\Aspire.Npgsql\Aspire.Npgsql.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Components\Aspire.Oracle.EntityFrameworkCore\Aspire.Oracle.EntityFrameworkCore.csproj" IsAspireProjectResource="false" />


<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public static TestDistributedApplicationBuilder Create(params string[] args)
return new TestDistributedApplicationBuilder(options => options.Args = args);
}

public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions> configureOptions)
public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions>? configureOptions)
{
return new TestDistributedApplicationBuilder(configureOptions);
}

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

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

configureOptions(applicationOptions);
configureOptions?.Invoke(applicationOptions);
}
}

Expand Down