Skip to content

Commit 017f286

Browse files
Copilotadamsitnik
andcommitted
Address review feedback: simplify test try/catch, remove manual Close() calls, add LeaveHandlesOpen test
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6c515d2e-34b7-483e-a1e5-2b0fae3caad8
1 parent 0aa6fcf commit 017f286

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/libraries/System.Diagnostics.Process/tests/ProcessHandlesTests.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,7 @@ public async Task CanRedirectOutputAndErrorToDifferentPipes(bool readAsync)
5353
: new("sh") { ArgumentList = { "-c", "echo 'Hello from stdout' && echo 'Error from stderr' >&2" } };
5454

5555
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle outputRead, out SafeFileHandle outputWrite, asyncRead: readAsync);
56-
57-
SafeFileHandle errorRead;
58-
SafeFileHandle errorWrite;
59-
try
60-
{
61-
SafeFileHandle.CreateAnonymousPipe(out errorRead, out errorWrite, asyncRead: readAsync);
62-
}
63-
catch
64-
{
65-
outputRead.Dispose();
66-
outputWrite.Dispose();
67-
throw;
68-
}
56+
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle errorRead, out SafeFileHandle errorWrite, asyncRead: readAsync);
6957

7058
startInfo.StandardOutput = outputWrite;
7159
startInfo.StandardError = errorWrite;
@@ -170,13 +158,8 @@ public async Task CanImplementPiping()
170158
consumerInfo.StandardOutput = outputHandle;
171159

172160
using Process producer = Process.Start(producerInfo)!;
173-
174-
writePipe.Close(); // close the parent copy of child handle
175-
176161
using Process consumer = Process.Start(consumerInfo)!;
177162

178-
readPipe.Close(); // close the parent copy of child handle
179-
180163
producer.WaitForExit();
181164
consumer.WaitForExit();
182165

@@ -195,6 +178,37 @@ public async Task CanImplementPiping()
195178
}
196179
}
197180

181+
[Fact]
182+
public void LeaveHandlesOpen_KeepsHandleOpen()
183+
{
184+
string tempFile = Path.GetTempFileName();
185+
186+
try
187+
{
188+
ProcessStartInfo startInfo = new("hostname");
189+
190+
SafeFileHandle outputHandle = File.OpenHandle(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
191+
192+
startInfo.StandardOutput = outputHandle;
193+
startInfo.LeaveHandlesOpen = true;
194+
195+
using Process process = Process.Start(startInfo)!;
196+
process.WaitForExit();
197+
198+
Assert.Equal(0, process.ExitCode);
199+
Assert.False(outputHandle.IsClosed);
200+
201+
outputHandle.Dispose();
202+
}
203+
finally
204+
{
205+
if (File.Exists(tempFile))
206+
{
207+
File.Delete(tempFile);
208+
}
209+
}
210+
}
211+
198212
[Fact]
199213
public void StandardInput_WithRedirectStandardInput_Throws()
200214
{

0 commit comments

Comments
 (0)