Skip to content

Commit 3853095

Browse files
committed
Add ProcessX.AcceptableExitCodes
1 parent e2f097f commit 3853095

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![GitHub Actions](https://github.com/Cysharp/ProcessX/workflows/Build-Debug/badge.svg)](https://github.com/Cysharp/ProcessX/actions) [![Releases](https://img.shields.io/github/release/Cysharp/ProcessX.svg)](https://github.com/Cysharp/ProcessX/releases)
1+
[![GitHub Actions](https://github.com/Cysharp/ProcessX/workflows/Build-Debug/badge.svg)](https://github.com/Cysharp/ProcessX/actions)
22

33
ProcessX
44
===
@@ -133,6 +133,10 @@ If stdout is binary data, you can use `StartReadBinaryAsync` to read `byte[]`.
133133
byte[] bin = await ProcessX.StartReadBinaryAsync($"...");
134134
```
135135

136+
Change acceptable exit codes
137+
---
138+
In default, ExitCode is not 0 throws ProcessErrorException. You can change acceptable exit codes globally by `ProcessX.AcceptableExitCodes` property. Default is `[0]`.
139+
136140
Reference
137141
---
138142
`ProcessX.StartAsync` overloads, you can set workingDirectory, environmentVariable, encoding.

sandbox/ConsoleApp/Program.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Cysharp.Diagnostics; // using namespace
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Threading;
56
using System.Threading.Tasks;
67

@@ -10,9 +11,9 @@ class Program
1011
static async Task Main(string[] args)
1112
{
1213
var path = @"..\..\..\..\ReturnMessage\ReturnMessage.csproj";
13-
// await ProcessX.StartAsync($"dotnet run --project {path} -- str -m foo -c 10").WriteLineAllAsync();
14+
await ProcessX.StartAsync($"dotnet run --project {path} -- str -m foo -c 10").WriteLineAllAsync();
1415

15-
var bin = await ProcessX.StartReadBinaryAsync($"dotnet run --project {path} -- bin -s 999 -c 10 -w 10");
16+
//var bin = await ProcessX.StartReadBinaryAsync($"dotnet run --project {path} -- bin -s 999 -c 10 -w 10");
1617

1718
//// first argument is Process, if you want to know ProcessID, use StandardInput, use it.
1819
//var (_, stdOut, stdError) = ProcessX.GetDualAsyncEnumerable("dotnet --foo --bar");
@@ -47,6 +48,21 @@ static async Task Main(string[] args)
4748
// // ex.ErrorOutput is empty, if you want to use it, buffer yourself.
4849
// // Console.WriteLine(string.Join(Environment.NewLine, errorBuffered));
4950
//}
50-
Console.WriteLine(bin.Length);
51+
//Console.WriteLine(bin.Length);
52+
53+
Console.WriteLine(IsInvalidExitCode(0));
54+
AcceptableExitCodes = new[] { 1 };
55+
Console.WriteLine(IsInvalidExitCode(0));
56+
Console.WriteLine(IsInvalidExitCode(1));
57+
AcceptableExitCodes = new[] { 0, 1 };
58+
Console.WriteLine(IsInvalidExitCode(0));
59+
Console.WriteLine(IsInvalidExitCode(1));
60+
}
61+
62+
public static IReadOnlyList<int> AcceptableExitCodes { get; set; } = new[] { 0 };
63+
64+
static bool IsInvalidExitCode(int processExitCode)
65+
{
66+
return !AcceptableExitCodes.Any(x => x == processExitCode);
5167
}
5268
}

src/ProcessX/ProcessX.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Channels;
@@ -11,6 +12,13 @@ namespace Cysharp.Diagnostics
1112
{
1213
public static class ProcessX
1314
{
15+
public static IReadOnlyList<int> AcceptableExitCodes { get; set; } = new[] { 0 };
16+
17+
static bool IsInvalidExitCode(Process process)
18+
{
19+
return !AcceptableExitCodes.Any(x => x == process.ExitCode);
20+
}
21+
1422
static (string fileName, string? arguments) ParseCommand(string command)
1523
{
1624
var cmdBegin = command.IndexOf(' ');
@@ -132,7 +140,7 @@ public static ProcessAsyncEnumerable StartAsync(ProcessStartInfo processStartInf
132140
await waitOutputDataCompleted.Task.ConfigureAwait(false);
133141
}
134142

135-
if (process.ExitCode != 0)
143+
if (IsInvalidExitCode(process))
136144
{
137145
outputChannel.Writer.TryComplete(new ProcessErrorException(process.ExitCode, errorList.ToArray()));
138146
}
@@ -245,7 +253,7 @@ public static (Process Process, ProcessAsyncEnumerable StdOut, ProcessAsyncEnume
245253
await waitErrorDataCompleted.Task.ConfigureAwait(false);
246254
await waitOutputDataCompleted.Task.ConfigureAwait(false);
247255

248-
if (process.ExitCode != 0)
256+
if (IsInvalidExitCode(process))
249257
{
250258
errorChannel.Writer.TryComplete();
251259
outputChannel.Writer.TryComplete(new ProcessErrorException(process.ExitCode, Array.Empty<string>()));
@@ -337,7 +345,7 @@ public static Task<byte[]> StartReadBinaryAsync(ProcessStartInfo processStartInf
337345
{
338346
await waitErrorDataCompleted.Task.ConfigureAwait(false);
339347

340-
if (errorList.Count == 0 && process.ExitCode == 0)
348+
if (errorList.Count == 0 && !IsInvalidExitCode(process))
341349
{
342350
var resultBin = await readTask.Task.ConfigureAwait(false);
343351
if (resultBin != null)

0 commit comments

Comments
 (0)