Skip to content

Commit 2cb45f5

Browse files
committed
Refactor the TestApp fixture
Add a new PublishAsync(PublishMode) method and also add a comment explaining why it would not work to publish multiple apps in parallel.
1 parent 4828123 commit 2cb45f5

File tree

1 file changed

+42
-33
lines changed
  • test/Serilog.Settings.Configuration.Tests/Support

1 file changed

+42
-33
lines changed

test/Serilog.Settings.Configuration.Tests/Support/TestApp.cs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,15 @@ Task IAsyncLifetime.DisposeAsync()
4747

4848
async Task CreateTestAppAsync()
4949
{
50+
// It might be tempting to do pack -> restore -> build --no-restore -> publish --no-build (and parallelize over publish modes)
51+
// But this would fail because of https://github.com/dotnet/sdk/issues/17526 and probably because of other unforeseen bugs
52+
// preventing from running multiple `dotnet publish` commands with different parameters.
53+
5054
await PackAsync();
5155
await RestoreAsync();
52-
53-
var publishDirectory = _workingDirectory.SubDirectory("publish");
54-
var fodyWeaversXml = _workingDirectory.File("FodyWeavers.xml");
55-
5656
foreach (var publishMode in GetPublishModes())
5757
{
58-
var outputDirectory = publishDirectory.SubDirectory(publishMode.ToString());
59-
60-
File.WriteAllText(fodyWeaversXml.FullName, publishMode == PublishMode.SingleFile && IsDesktop ? "<Weavers><Costura/></Weavers>" : "<Weavers/>");
61-
62-
var publishArgs = new[] {
63-
"publish",
64-
"--no-restore",
65-
"--configuration", "Release",
66-
"--output", outputDirectory.FullName,
67-
$"-p:TargetFramework={TargetFramework}"
68-
};
69-
var publishSingleFile = $"-p:PublishSingleFile={publishMode is PublishMode.SingleFile or PublishMode.SelfContained}";
70-
var selfContained = $"-p:SelfContained={publishMode is PublishMode.SelfContained}";
71-
await RunDotnetAsync(_workingDirectory, IsDesktop ? publishArgs : publishArgs.Append(publishSingleFile).Append(selfContained).ToArray());
72-
73-
var executableFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "TestApp.exe" : "TestApp";
74-
var executableFile = new FileInfo(Path.Combine(outputDirectory.FullName, executableFileName));
75-
executableFile.Exists.Should().BeTrue();
76-
var dlls = executableFile.Directory!.EnumerateFiles("*.dll");
77-
if (publishMode == PublishMode.Standard)
78-
{
79-
dlls.Should().NotBeEmpty(because: $"the test app was _not_ published as single-file ({publishMode})");
80-
}
81-
else
82-
{
83-
dlls.Should().BeEmpty(because: $"the test app was published as single-file ({publishMode})");
84-
executableFile.Directory.EnumerateFiles().Should().ContainSingle().Which.FullName.Should().Be(executableFile.FullName);
85-
}
86-
_executables[publishMode] = executableFile;
58+
await PublishAsync(publishMode);
8759
}
8860
}
8961

@@ -112,6 +84,43 @@ async Task RestoreAsync()
11284
await RunDotnetAsync(_workingDirectory, restoreArgs);
11385
}
11486

87+
async Task PublishAsync(PublishMode publishMode)
88+
{
89+
var publishDirectory = _workingDirectory.SubDirectory("publish");
90+
var fodyWeaversXml = _workingDirectory.File("FodyWeavers.xml");
91+
92+
var outputDirectory = publishDirectory.SubDirectory(publishMode.ToString());
93+
94+
File.WriteAllText(fodyWeaversXml.FullName, publishMode == PublishMode.SingleFile && IsDesktop ? "<Weavers><Costura/></Weavers>" : "<Weavers/>");
95+
96+
var publishArgs = new[] {
97+
"publish",
98+
"--no-restore",
99+
"--configuration", "Release",
100+
"--output", outputDirectory.FullName,
101+
$"-p:TargetFramework={TargetFramework}"
102+
};
103+
var publishSingleFile = $"-p:PublishSingleFile={publishMode is PublishMode.SingleFile or PublishMode.SelfContained}";
104+
var selfContained = $"-p:SelfContained={publishMode is PublishMode.SelfContained}";
105+
await RunDotnetAsync(_workingDirectory, IsDesktop ? publishArgs : publishArgs.Append(publishSingleFile).Append(selfContained).ToArray());
106+
107+
var executableFileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "TestApp.exe" : "TestApp";
108+
var executableFile = new FileInfo(Path.Combine(outputDirectory.FullName, executableFileName));
109+
executableFile.Exists.Should().BeTrue();
110+
var dlls = executableFile.Directory!.EnumerateFiles("*.dll");
111+
if (publishMode == PublishMode.Standard)
112+
{
113+
dlls.Should().NotBeEmpty(because: $"the test app was _not_ published as single-file ({publishMode})");
114+
}
115+
else
116+
{
117+
dlls.Should().BeEmpty(because: $"the test app was published as single-file ({publishMode})");
118+
executableFile.Directory.EnumerateFiles().Should().ContainSingle().Which.FullName.Should().Be(executableFile.FullName);
119+
}
120+
121+
_executables[publishMode] = executableFile;
122+
}
123+
115124
async Task RunDotnetAsync(DirectoryInfo workingDirectory, params string[] arguments)
116125
{
117126
_messageSink.OnMessage(new DiagnosticMessage($"cd {workingDirectory}"));

0 commit comments

Comments
 (0)