1212
1313namespace Microsoft . DotNet . Docker . Tests ;
1414
15- public abstract class ProjectTemplateTestScenario : ITestScenario , IDisposable
15+ public abstract class ProjectTemplateTestScenario : ITestScenario
1616{
17- private bool _disposed ;
18-
1917 protected static string OSDockerfileSuffix { get ; } = DockerHelper . IsLinuxContainerModeEnabled ? "linux" : "windows" ;
2018 protected static string ? AdminUser { get ; } = DockerHelper . IsLinuxContainerModeEnabled ? "root" : null ;
2119 protected static string ? NonRootUser { get ; } = DockerHelper . IsLinuxContainerModeEnabled ? "app" : "ContainerUser" ;
2220
2321 protected DockerHelper DockerHelper { get ; }
2422 protected ProductImageData ImageData { get ; }
2523 protected ITestOutputHelper OutputHelper { get ; }
26- protected TestSolution TestSolution { get ; }
2724
2825 protected virtual bool NonRootUserSupported => DockerHelper . IsLinuxContainerModeEnabled ;
2926
@@ -44,11 +41,9 @@ public ProjectTemplateTestScenario(
4441 DockerHelper = dockerHelper ;
4542 ImageData = imageData ;
4643 OutputHelper = outputHelper ;
47-
48- TestSolution = new ( imageData , SampleName , dockerHelper , injectCustomTestCode : InjectCustomTestCode ) ;
4944 }
5045
51- protected string Build ( string stageTarget , string [ ] ? customBuildArgs )
46+ private string Build ( TestSolution testSolution , string stageTarget , string [ ] ? customBuildArgs )
5247 {
5348 const string DockerfileName = "Dockerfile" ;
5449 string dockerfilePath = Path . Combine ( DockerHelper . TestArtifactsDir , DockerfileName ) ;
@@ -100,7 +95,7 @@ protected string Build(string stageTarget, string[]? customBuildArgs)
10095 tag : tag ,
10196 dockerfile : dockerfilePath ,
10297 target : stageTarget ,
103- contextDir : TestSolution . SolutionDir ,
98+ contextDir : testSolution . SolutionDir ,
10499 platform : ImageData . Platform ,
105100 buildArgs : buildArgs . ToArray ( ) ) ;
106101 }
@@ -125,9 +120,12 @@ public async Task ExecuteAsync()
125120 }
126121
127122 List < string > tags = [ ] ;
123+ TestSolution ? testSolution = null ;
128124
129125 try
130126 {
127+ testSolution = new TestSolution ( ImageData , SampleName , DockerHelper , InjectCustomTestCode ) ;
128+
131129 OutputHelper . WriteLine (
132130 $ """
133131
@@ -144,12 +142,12 @@ public async Task ExecuteAsync()
144142 string [ ] customBuildArgs = [ ..CustomDockerBuildArgs , $ "rid={ ImageData . Rid } " ] ;
145143
146144 // Build and run app on SDK image
147- string buildTag = Build ( TestDockerfile . BuildStageName , customBuildArgs ) ;
145+ string buildTag = Build ( testSolution , TestDockerfile . BuildStageName , customBuildArgs ) ;
148146 tags . Add ( buildTag ) ;
149147 await RunAsync ( buildTag , command : "dotnet run --no-restore" ) ;
150148
151149 // Build and run app stage
152- string tag = Build ( TestDockerfile . AppStageName , customBuildArgs ) ;
150+ string tag = Build ( testSolution , TestDockerfile . AppStageName , customBuildArgs ) ;
153151 tags . Add ( tag ) ;
154152
155153 // Don't run the app if the build output is not executable
@@ -165,27 +163,9 @@ public async Task ExecuteAsync()
165163 finally
166164 {
167165 tags . ForEach ( DockerHelper . DeleteImage ) ;
166+ testSolution ? . Dispose ( ) ;
168167 }
169168 }
170169
171170 protected abstract Task RunAsync ( string image , string ? user = null , string ? command = null ) ;
172-
173- protected virtual void Dispose ( bool disposing )
174- {
175- if ( ! _disposed )
176- {
177- if ( disposing )
178- {
179- TestSolution . Dispose ( ) ;
180- }
181-
182- _disposed = true ;
183- }
184- }
185-
186- public void Dispose ( )
187- {
188- Dispose ( disposing : true ) ;
189- GC . SuppressFinalize ( this ) ;
190- }
191171}
0 commit comments