88using System . Diagnostics ;
99using Aspire . Components . Common . Tests ;
1010using 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
1417public 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 ( ) ) ;
0 commit comments