diff --git a/src/libraries/System.Diagnostics.Process/tests/StartAndForget.cs b/src/libraries/System.Diagnostics.Process/tests/StartAndForget.cs index 0a3d26ec319365..dc20e3fd9a8a45 100644 --- a/src/libraries/System.Diagnostics.Process/tests/StartAndForget.cs +++ b/src/libraries/System.Diagnostics.Process/tests/StartAndForget.cs @@ -1,7 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; using System.IO; +using System.Text; using Microsoft.DotNet.RemoteExecutor; using Microsoft.Win32.SafeHandles; using Xunit; @@ -18,7 +20,7 @@ public void StartAndForget_StartsProcessAndReturnsValidPid(bool useProcessStartI using Process template = CreateSleepProcess((int)TimeSpan.FromHours(1).TotalMilliseconds); int pid = useProcessStartInfo ? Process.StartAndForget(template.StartInfo) - : Process.StartAndForget(template.StartInfo.FileName, template.StartInfo.ArgumentList); + : Process.StartAndForget(template.StartInfo.FileName, MapToArgumentList(template.StartInfo)); Assert.True(pid > 0); @@ -110,5 +112,49 @@ public void StartAndForget_WithUseShellExecute_ThrowsInvalidOperationException() Assert.Throws(() => Process.StartAndForget(startInfo)); } + + // RemoteExecutor populates ProcessStartInfo.Arguments, but StartAndForget(fileName, arguments) + // takes an argument list, so this helper maps the serialized argument string for this test. + private static List? MapToArgumentList(ProcessStartInfo startInfo) + { + string arguments = startInfo.Arguments; + if (string.IsNullOrEmpty(arguments)) + { + return null; + } + + List list = new(); + StringBuilder builder = new(); + bool isQuoted = false; + + foreach (char c in arguments) + { + switch (c) + { + case '"' when !isQuoted: + isQuoted = true; + break; + case ' ' when !isQuoted: + case '"' when isQuoted: + if (builder.Length > 0) + { + list.Add(builder.ToString()); + builder.Clear(); + } + isQuoted = false; + break; + default: + builder.Append(c); + break; + } + } + + if (builder.Length > 0) + { + list.Add(builder.ToString()); + } + + return list; + } } }