Skip to content

Commit a47eb68

Browse files
committed
Make testSolution a local variable, and build it during Execute
1 parent 440af4a commit a47eb68

File tree

6 files changed

+16
-46
lines changed

6 files changed

+16
-46
lines changed

tests/Microsoft.DotNet.Docker.Tests/AspnetImageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task VerifyFxDependentAppScenario(ProductImageData imageData)
3232
return;
3333
}
3434

35-
using WebScenario scenario = new WebScenario.FxDependent(imageData, DockerHelper, OutputHelper);
35+
WebScenario scenario = new WebScenario.FxDependent(imageData, DockerHelper, OutputHelper);
3636
await scenario.ExecuteAsync();
3737
}
3838

tests/Microsoft.DotNet.Docker.Tests/RuntimeDepsImageTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task VerifySelfContainedConsoleScenario(ProductImageData imageData)
3838
return;
3939
}
4040

41-
using ConsoleAppScenario testScenario =
41+
ConsoleAppScenario testScenario =
4242
new ConsoleAppScenario.SelfContained(imageData, DockerHelper, OutputHelper);
4343
await testScenario.ExecuteAsync();
4444
}
@@ -54,8 +54,7 @@ public async Task VerifySelfContainedWebScenario(ProductImageData imageData)
5454
return;
5555
}
5656

57-
using WebScenario testScenario =
58-
new WebScenario.SelfContained(imageData, DockerHelper, OutputHelper);
57+
WebScenario testScenario = new WebScenario.SelfContained(imageData, DockerHelper, OutputHelper);
5958
await testScenario.ExecuteAsync();
6059
}
6160

@@ -71,7 +70,7 @@ public async Task VerifyAotWebScenario(ProductImageData imageData)
7170
return;
7271
}
7372

74-
using WebScenario scenario = new WebScenario.Aot(imageData, DockerHelper, OutputHelper);
73+
WebScenario scenario = new WebScenario.Aot(imageData, DockerHelper, OutputHelper);
7574
await scenario.ExecuteAsync();
7675
}
7776

tests/Microsoft.DotNet.Docker.Tests/RuntimeImageTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ public RuntimeImageTests(ITestOutputHelper outputHelper)
2828
[MemberData(nameof(GetImageData))]
2929
public async Task VerifyFxDependentAppScenario(ProductImageData imageData)
3030
{
31-
using ConsoleAppScenario testScenario =
32-
new ConsoleAppScenario.FxDependent(imageData, DockerHelper, OutputHelper);
31+
ConsoleAppScenario testScenario = new ConsoleAppScenario.FxDependent(imageData, DockerHelper, OutputHelper);
3332
await testScenario.ExecuteAsync();
3433
}
3534

3635
[DotNetTheory]
3736
[MemberData(nameof(GetImageData))]
3837
public async Task VerifyTestProjectScenario(ProductImageData imageData)
3938
{
40-
using ConsoleAppScenario testScenario =
41-
new ConsoleAppScenario.TestProject(imageData, DockerHelper, OutputHelper);
39+
ConsoleAppScenario testScenario = new ConsoleAppScenario.TestProject(imageData, DockerHelper, OutputHelper);
4240
await testScenario.ExecuteAsync();
4341
}
4442

tests/Microsoft.DotNet.Docker.Tests/SdkImageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async void VerifyBlazorWasmScenario(ProductImageData imageData)
8282
useWasmTools = false;
8383
}
8484

85-
using BlazorWasmScenario testScenario = new(imageData, DockerHelper, OutputHelper, useWasmTools);
85+
BlazorWasmScenario testScenario = new(imageData, DockerHelper, OutputHelper, useWasmTools);
8686
await testScenario.ExecuteAsync();
8787
}
8888

tests/Microsoft.DotNet.Docker.Tests/TestScenarios/ProjectTemplateTestScenario.cs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212

1313
namespace 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
}

tests/Microsoft.DotNet.Docker.Tests/TestSolution.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ public TestSolution(ProductImageData imageData, string sampleName, DockerHelper
4545

4646
private string CreateTestSolutionWithSdkImage(string solutionDir, string appType)
4747
{
48-
if (_imageData.Version.Major == 11)
49-
{
50-
// Project templates are not yet updated for .NET 11.
51-
// Re-enable when https://github.com/dotnet/sdk/issues/50295 is resolved.
52-
return "";
53-
}
54-
5548
Directory.CreateDirectory(solutionDir);
5649
string appProjectContainerName = _imageData.GetIdentifier($"create-{appType}");
5750
string testProjectContainerName = _imageData.GetIdentifier("create-test");

0 commit comments

Comments
 (0)