Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Proc/BufferedObservableProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,16 @@ private IDisposable KickOff(IObserver<CharactersOut> observer)
if (!StartProcess(observer)) return Disposable.Empty;

Started = true;
_observer = observer;

if (Process.HasExited)
{
Process.ReadStandardErrBlocking(_observer, BufferSize, () => ContinueReadingFromProcessReaders());
Process.ReadStandardOutBlocking(_observer, BufferSize, () => ContinueReadingFromProcessReaders());
Process.ReadStandardErrBlocking(observer, BufferSize, () => ContinueReadingFromProcessReaders());
Process.ReadStandardOutBlocking(observer, BufferSize, () => ContinueReadingFromProcessReaders());
OnExit(observer);
return Disposable.Empty;
}

_observer = observer;

StartAsyncReads();

Process.Exited += (o, s) =>
Expand Down
2 changes: 2 additions & 0 deletions src/Proc/Extensions/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public static string NaivelyQuoteArguments(this IEnumerable<string> arguments)
if (arguments == null) return string.Empty;
var args = arguments.ToList();
if (args.Count == 0) return string.Empty;

var quotedArgs = args
.Select(a =>
{
if (!a.Contains(" ")) return a;
if (a.StartsWith("\"")) return a;
return $"\"{a}\"";
})
.ToList();
Expand Down
6 changes: 3 additions & 3 deletions src/Proc/ObservableProcessBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ private Process CreateProcess()
var processStartInfo = new ProcessStartInfo
{
FileName = s.Binary,
#if !NETSTANDARD2_1 && !NET5_0_OR_GREATER
Arguments = args.NaivelyQuoteArguments(),
#if STRING_ARGS
Arguments = args != null ? string.Join(" ", args) : string.Empty,
#endif
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true
};
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
#if !STRING_ARGS
foreach (var arg in s.Args)
processStartInfo.ArgumentList.Add(arg);
#endif
Expand Down
11 changes: 6 additions & 5 deletions src/Proc/Proc.Exec.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using ProcNet.Extensions;

Expand All @@ -26,16 +27,16 @@ public static partial class Proc
/// <returns>The exit code of the binary being run</returns>
public static int Exec(ExecArguments arguments)
{
var args = arguments.Args.NaivelyQuoteArguments();
var args = arguments.Args?.ToArray() ?? [];
var info = new ProcessStartInfo(arguments.Binary)
{
UseShellExecute = false
};
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
foreach (var arg in arguments.Args)
#if !STRING_ARGS
foreach (var arg in args)
info.ArgumentList.Add(arg);
#else
info.Arguments = args;
info.Arguments = args != null ? string.Join(" ", args) : string.Empty;
#endif

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

var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
? $"\"{arguments.Binary}\""
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";

using var process = new Process { StartInfo = info };
if (!process.Start())
Expand Down
7 changes: 4 additions & 3 deletions src/Proc/Proc.ExecAsync.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ProcNet.Extensions;
Expand All @@ -18,12 +19,12 @@ public static partial class Proc
/// <returns>The exit code of the binary being run</returns>
public static async Task<int> ExecAsync(ExecArguments arguments, CancellationToken ctx = default)
{
var args = arguments.Args.NaivelyQuoteArguments();
var args = arguments.Args?.ToArray() ?? [];
var info = new ProcessStartInfo(arguments.Binary)
{
UseShellExecute = false
};
foreach (var arg in arguments.Args)
foreach (var arg in args)
info.ArgumentList.Add(arg);

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

var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
? $"\"{arguments.Binary}\""
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";

using var process = new Process { StartInfo = info };
if (!process.Start()) throw new ProcExecException($"Failed to start {printBinary}");
Expand Down
4 changes: 2 additions & 2 deletions src/Proc/Proc.StartLongRunning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public static LongRunningApplicationSubscription StartLongRunning(LongRunningArg
var completed = subscription.WaitHandle.WaitOne(waitForStartedConfirmation);
if (completed) return subscription;
var pwd = arguments.WorkingDirectory;
var args = arguments.Args.NaivelyQuoteArguments();
var args = arguments.Args;
var printBinary = arguments.OnlyPrintBinaryInExceptionMessage
? $"\"{arguments.Binary}\""
: $"\"{arguments.Binary} {args}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
: $"\"{arguments.Binary} {args.NaivelyQuoteArguments()}\"{(pwd == null ? string.Empty : $" pwd: {pwd}")}";
throw new ProcExecException($"Could not yield started confirmation after {waitForStartedConfirmation} while running {printBinary}");
}

Expand Down
1 change: 1 addition & 0 deletions src/Proc/Proc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<LangVersion>Latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<DefineConstants Condition="'$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'netstandard2.0'">STRING_ARGS</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions tests/Proc.Tests/PrintArgsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void ProcSendsAllArguments()
AssertOutput(testArgs);
}

#if NETSTANDARD2_1
[Fact]
public void ArgumentsWithSpaceAreNotSplit()
{
Expand All @@ -33,6 +34,20 @@ public void EscapedQuotes()
AssertOutput(testArgs);
}

#else
[Fact]
public void EscapedQuotes()
{
string[] testArgs = ["\"this argument has spaces\"", "hello", "world"];
var args = TestCaseArguments("PrintArgs", testArgs);
var outputWriter = new TestConsoleOutWriter(output);
args.ConsoleOutWriter = outputWriter;
var result = Proc.Start(args);
result.ExitCode.Should().Be(0);
result.ConsoleOut.Should().NotBeEmpty().And.HaveCount(testArgs.Length);
}
#endif

private void AssertOutput(string[] testArgs)
{
var args = TestCaseArguments("PrintArgs", testArgs);
Expand Down
Loading