Skip to content

Commit 91e2b5e

Browse files
committed
Fix the SingleFileApp test when running on netcoreapp2.1
Also fix possible concurrency issue by using different bin and obj directories.
1 parent 3d2e5df commit 91e2b5e

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

serilog-settings-configuration.sln

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ Global
5656
{B7CF5068-DD19-4868-A268-5280BDE90361}.Release|Any CPU.ActiveCfg = Release|Any CPU
5757
{B7CF5068-DD19-4868-A268-5280BDE90361}.Release|Any CPU.Build.0 = Release|Any CPU
5858
{C959B095-5B29-4663-A5B6-4742D5EA97AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59-
{C959B095-5B29-4663-A5B6-4742D5EA97AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
6059
{C959B095-5B29-4663-A5B6-4742D5EA97AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
61-
{C959B095-5B29-4663-A5B6-4742D5EA97AB}.Release|Any CPU.Build.0 = Release|Any CPU
6260
EndGlobalSection
6361
GlobalSection(SolutionProperties) = preSolution
6462
HideSolutionNode = FALSE

test/Serilog.Settings.Configuration.Tests/SingleFileAppTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ void SingleFileApp(bool includeAllContentForSelfExtract)
1919

2020
try
2121
{
22-
ProcessExtensions.RunDotnet(workingDirectory, "publish", "-c", "Release", $"-p:IncludeAllContentForSelfExtract={includeAllContentForSelfExtract.ToString().ToLower()}", "-o", publishDirectory);
22+
ProcessExtensions.RunDotnet(workingDirectory, "publish",
23+
"-c", "Release",
24+
$"-p:IncludeAllContentForSelfExtract={includeAllContentForSelfExtract.ToString().ToLower()}",
25+
$"-p:CliBaseOutputPath={Path.Combine(publishDirectory, "bin" + Path.DirectorySeparatorChar)}",
26+
$"-p:CliBaseIntermediateOutputPath={Path.Combine(publishDirectory, "obj" + Path.DirectorySeparatorChar)}",
27+
"-o", publishDirectory);
2328
var exeName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "TestSingleFileApp.exe" : "TestSingleFileApp";
2429
var exePath = Path.Combine(publishDirectory, exeName);
2530
var result = ProcessExtensions.RunCommand(exePath);

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Linq;
4+
using System.Text;
35

46
namespace Serilog.Settings.Configuration.Tests.Support
57
{
@@ -19,43 +21,63 @@ public CommandResult(string output, string error)
1921

2022
public static void RunDotnet(string workingDirectory, params string[] args)
2123
{
22-
RunCommand("dotnet", useShellExecute: true, workingDirectory, args);
24+
RunCommand("dotnet", workingDirectory, args);
2325
}
2426

2527
public static CommandResult RunCommand(string command, params string[] args)
2628
{
27-
return RunCommand(command, useShellExecute: false, "", args);
29+
return RunCommand(command, "", args);
2830
}
2931

30-
static CommandResult RunCommand(string command, bool useShellExecute, string workingDirectory, params string[] args)
32+
static CommandResult RunCommand(string command, string workingDirectory, params string[] args)
3133
{
32-
var arguments = $"\"{string.Join("\" \"", args)}\"";
33-
var redirect = !useShellExecute;
34-
var startInfo = new ProcessStartInfo(command, arguments)
34+
var arguments = new StringBuilder(args.Select(e => e.Length + 3).Sum());
35+
foreach (var arg in args)
36+
{
37+
var hasSpace = arg.Contains(" ");
38+
if (hasSpace) arguments.Append('"');
39+
arguments.Append(arg);
40+
if (hasSpace) arguments.Append('"');
41+
arguments.Append(' ');
42+
}
43+
var startInfo = new ProcessStartInfo(command, arguments.ToString())
3544
{
3645
CreateNoWindow = true,
3746
WindowStyle = ProcessWindowStyle.Hidden,
38-
UseShellExecute = useShellExecute,
47+
UseShellExecute = false,
3948
WorkingDirectory = workingDirectory,
40-
RedirectStandardOutput = redirect,
41-
RedirectStandardError = redirect,
49+
RedirectStandardOutput = true,
50+
RedirectStandardError = true,
4251
};
4352
var process = new Process { StartInfo = startInfo };
4453
process.Start();
4554
var timeout = TimeSpan.FromSeconds(30);
4655
var exited = process.WaitForExit((int)timeout.TotalMilliseconds);
4756
if (!exited)
4857
{
58+
process.Kill();
4959
throw new TimeoutException($"The command '{command} {arguments}' did not execute within {timeout.TotalSeconds} seconds");
5060
}
5161

52-
var error = redirect ? process.StandardError.ReadToEnd() : "";
62+
var output = process.StandardOutput.ReadToEnd();
63+
var error = process.StandardError.ReadToEnd();
5364
if (process.ExitCode != 0)
5465
{
55-
throw new InvalidOperationException($"The command '{command} {arguments}' exited with code {process.ExitCode}");
66+
var message = new StringBuilder();
67+
message.AppendLine($"The command '{command} {arguments}' exited with code {process.ExitCode}");
68+
if (output.Length > 0)
69+
{
70+
message.AppendLine("*** Output ***");
71+
message.AppendLine(output);
72+
}
73+
if (error.Length > 0)
74+
{
75+
message.AppendLine("*** Error ***");
76+
message.AppendLine(error);
77+
}
78+
throw new InvalidOperationException(message.ToString());
5679
}
5780

58-
var output = redirect ? process.StandardOutput.ReadToEnd() : "";
5981
return new CommandResult(output, error);
6082
}
6183
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project>
2+
<PropertyGroup>
3+
<BaseOutputPath Condition="$(MSBuildProjectName) == 'TestSingleFileApp' AND $(CliBaseOutputPath) != ''">$(CliBaseOutputPath)</BaseOutputPath>
4+
<BaseIntermediateOutputPath Condition="$(MSBuildProjectName) == 'TestSingleFileApp' AND $(CliBaseIntermediateOutputPath) != ''">$(CliBaseIntermediateOutputPath)</BaseIntermediateOutputPath>
5+
</PropertyGroup>
6+
</Project>

test/TestSingleFileApp/TestSingleFileApp.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
<DebugType>embedded</DebugType>
1111
<PublishSingleFile>true</PublishSingleFile>
1212
<SelfContained>false</SelfContained>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '6.0'))">
16+
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
17+
</PropertyGroup>
18+
19+
<PropertyGroup Condition="!$([MSBuild]::VersionGreaterThanOrEquals('$(NETCoreSdkVersion)', '6.0'))">
1320
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x64</RuntimeIdentifier>
1421
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('OSX'))">osx-x64</RuntimeIdentifier>
1522
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Windows'))">win-x64</RuntimeIdentifier>

0 commit comments

Comments
 (0)