Skip to content

Commit fc0a8e1

Browse files
committed
Revert naively quoting on older TFM's
1 parent 64bbd38 commit fc0a8e1

File tree

8 files changed

+36
-17
lines changed

8 files changed

+36
-17
lines changed

src/Proc/BufferedObservableProcess.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,16 @@ private IDisposable KickOff(IObserver<CharactersOut> observer)
7171
if (!StartProcess(observer)) return Disposable.Empty;
7272

7373
Started = true;
74+
_observer = observer;
7475

7576
if (Process.HasExited)
7677
{
77-
Process.ReadStandardErrBlocking(_observer, BufferSize, () => ContinueReadingFromProcessReaders());
78-
Process.ReadStandardOutBlocking(_observer, BufferSize, () => ContinueReadingFromProcessReaders());
78+
Process.ReadStandardErrBlocking(observer, BufferSize, () => ContinueReadingFromProcessReaders());
79+
Process.ReadStandardOutBlocking(observer, BufferSize, () => ContinueReadingFromProcessReaders());
7980
OnExit(observer);
8081
return Disposable.Empty;
8182
}
8283

83-
_observer = observer;
84-
8584
StartAsyncReads();
8685

8786
Process.Exited += (o, s) =>

src/Proc/Extensions/ArgumentExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ public static string NaivelyQuoteArguments(this IEnumerable<string> arguments)
1010
if (arguments == null) return string.Empty;
1111
var args = arguments.ToList();
1212
if (args.Count == 0) return string.Empty;
13+
1314
var quotedArgs = args
1415
.Select(a =>
1516
{
1617
if (!a.Contains(" ")) return a;
18+
if (a.StartsWith("\"")) return a;
1719
return $"\"{a}\"";
1820
})
1921
.ToList();

src/Proc/ObservableProcessBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ private Process CreateProcess()
143143
var processStartInfo = new ProcessStartInfo
144144
{
145145
FileName = s.Binary,
146-
#if !NETSTANDARD2_1 && !NET5_0_OR_GREATER
147-
Arguments = args.NaivelyQuoteArguments(),
146+
#if STRING_ARGS
147+
Arguments = args != null ? string.Join(" ", args) : string.Empty,
148148
#endif
149149
CreateNoWindow = true,
150150
UseShellExecute = false,
151151
RedirectStandardOutput = true,
152152
RedirectStandardError = true,
153153
RedirectStandardInput = true
154154
};
155-
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
155+
#if !STRING_ARGS
156156
foreach (var arg in s.Args)
157157
processStartInfo.ArgumentList.Add(arg);
158158
#endif

src/Proc/Proc.Exec.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using ProcNet.Extensions;
56

@@ -26,16 +27,16 @@ public static partial class Proc
2627
/// <returns>The exit code of the binary being run</returns>
2728
public static int Exec(ExecArguments arguments)
2829
{
29-
var args = arguments.Args.NaivelyQuoteArguments();
30+
var args = arguments.Args?.ToArray() ?? [];
3031
var info = new ProcessStartInfo(arguments.Binary)
3132
{
3233
UseShellExecute = false
3334
};
34-
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
35-
foreach (var arg in arguments.Args)
35+
#if !STRING_ARGS
36+
foreach (var arg in args)
3637
info.ArgumentList.Add(arg);
3738
#else
38-
info.Arguments = args;
39+
info.Arguments = args != null ? string.Join(" ", args) : string.Empty;
3940
#endif
4041

4142
var pwd = arguments.WorkingDirectory;
@@ -46,7 +47,7 @@ public static int Exec(ExecArguments arguments)
4647

4748
var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
4849
? $"\"{arguments.Binary}\""
49-
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
50+
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
5051

5152
using var process = new Process { StartInfo = info };
5253
if (!process.Start())

src/Proc/Proc.ExecAsync.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using ProcNet.Extensions;
@@ -18,12 +19,12 @@ public static partial class Proc
1819
/// <returns>The exit code of the binary being run</returns>
1920
public static async Task<int> ExecAsync(ExecArguments arguments, CancellationToken ctx = default)
2021
{
21-
var args = arguments.Args.NaivelyQuoteArguments();
22+
var args = arguments.Args?.ToArray() ?? [];
2223
var info = new ProcessStartInfo(arguments.Binary)
2324
{
2425
UseShellExecute = false
2526
};
26-
foreach (var arg in arguments.Args)
27+
foreach (var arg in args)
2728
info.ArgumentList.Add(arg);
2829

2930
var pwd = arguments.WorkingDirectory;
@@ -34,7 +35,7 @@ public static async Task<int> ExecAsync(ExecArguments arguments, CancellationTok
3435

3536
var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
3637
? $"\"{arguments.Binary}\""
37-
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
38+
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
3839

3940
using var process = new Process { StartInfo = info };
4041
if (!process.Start()) throw new ProcExecException($"Failed to start {printBinary}");

src/Proc/Proc.StartLongRunning.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public static LongRunningApplicationSubscription StartLongRunning(LongRunningArg
104104
var completed = subscription.WaitHandle.WaitOne(waitForStartedConfirmation);
105105
if (completed) return subscription;
106106
var pwd = arguments.WorkingDirectory;
107-
var args = arguments.Args.NaivelyQuoteArguments();
107+
var args = arguments.Args;
108108
var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
109109
? $"\"{arguments.Binary}\""
110-
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
110+
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
111111
throw new ProcExecException($"Could not yield started confirmation after {waitForStartedConfirmation} while running {printBinary}");
112112
}
113113

src/Proc/Proc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2727
<LangVersion>Latest</LangVersion>
2828
<PackageReadmeFile>README.md</PackageReadmeFile>
29+
<DefineConstants Condition="'$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'netstandard2.0'">STRING_ARGS</DefineConstants>
2930
</PropertyGroup>
3031

3132
<ItemGroup>

tests/Proc.Tests/PrintArgsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public void ProcSendsAllArguments()
1313
AssertOutput(testArgs);
1414
}
1515

16+
#if NETSTANDARD2_1
1617
[Fact]
1718
public void ArgumentsWithSpaceAreNotSplit()
1819
{
@@ -33,6 +34,20 @@ public void EscapedQuotes()
3334
AssertOutput(testArgs);
3435
}
3536

37+
#else
38+
[Fact]
39+
public void EscapedQuotes()
40+
{
41+
string[] testArgs = ["\"this argument has spaces\"", "hello", "world"];
42+
var args = TestCaseArguments("PrintArgs", testArgs);
43+
var outputWriter = new TestConsoleOutWriter(output);
44+
args.ConsoleOutWriter = outputWriter;
45+
var result = Proc.Start(args);
46+
result.ExitCode.Should().Be(0);
47+
result.ConsoleOut.Should().NotBeEmpty().And.HaveCount(testArgs.Length);
48+
}
49+
#endif
50+
3651
private void AssertOutput(string[] testArgs)
3752
{
3853
var args = TestCaseArguments("PrintArgs", testArgs);

0 commit comments

Comments
 (0)