diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 4196c28..ec4a668 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -30,6 +30,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: SetupBuildInfo id: SetupBuildInfo run: dotnet run --project _atom/_atom.csproj -- SetupBuildInfo --skip --headless @@ -48,6 +53,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: PackProjects id: PackProjects run: dotnet run --project _atom/_atom.csproj -- PackProjects --skip --headless @@ -62,7 +72,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - test-framework: [ net10.0 ] + test-framework: [ net10.0, net11.0 ] steps: - name: Checkout @@ -75,6 +85,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: TestProjects id: TestProjects run: dotnet run --project _atom/_atom.csproj -- TestProjects --skip --headless @@ -103,6 +118,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: Retrieve Invex.Process uses: actions/download-artifact@v8 with: @@ -134,6 +154,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: Retrieve Invex.Process uses: actions/download-artifact@v8 with: @@ -162,6 +187,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: BuildDocs id: BuildDocs run: dotnet run --project _atom/_atom.csproj -- BuildDocs --skip --headless @@ -190,6 +220,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: Retrieve GeneratedDocs uses: actions/download-artifact@v8 with: diff --git a/.github/workflows/Cleanup Prereleases.yml b/.github/workflows/Cleanup Prereleases.yml index 5c0899d..ed3b071 100644 --- a/.github/workflows/Cleanup Prereleases.yml +++ b/.github/workflows/Cleanup Prereleases.yml @@ -26,6 +26,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: UnlistOldPrereleases id: UnlistOldPrereleases run: dotnet run --project _atom/_atom.csproj -- UnlistOldPrereleases --skip --headless diff --git a/.github/workflows/Dependabot Enable auto-merge.yml b/.github/workflows/Dependabot Enable auto-merge.yml index f9563cf..0b43808 100644 --- a/.github/workflows/Dependabot Enable auto-merge.yml +++ b/.github/workflows/Dependabot Enable auto-merge.yml @@ -23,6 +23,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: ApproveDependabotPr id: ApproveDependabotPr run: dotnet run --project _atom/_atom.csproj -- ApproveDependabotPr --skip --headless diff --git a/.github/workflows/Validate.yml b/.github/workflows/Validate.yml index bb5809d..ffa9f27 100644 --- a/.github/workflows/Validate.yml +++ b/.github/workflows/Validate.yml @@ -28,6 +28,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: SetupBuildInfo id: SetupBuildInfo run: dotnet run --project _atom/_atom.csproj -- SetupBuildInfo --skip --headless @@ -46,6 +51,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: PackProjects id: PackProjects run: dotnet run --project _atom/_atom.csproj -- PackProjects --skip --headless @@ -54,7 +64,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - test-framework: [ net10.0 ] + test-framework: [ net10.0, net11.0 ] steps: - name: Checkout @@ -67,6 +77,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: TestProjects id: TestProjects run: dotnet run --project _atom/_atom.csproj -- TestProjects --skip --headless @@ -95,6 +110,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: CheckPrForBreakingChanges id: CheckPrForBreakingChanges run: dotnet run --project _atom/_atom.csproj -- CheckPrForBreakingChanges --skip --headless @@ -117,6 +137,11 @@ jobs: with: dotnet-version: 10.0.x + - name: Setup .NET 11.0.x + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + - name: WaitForCopilotReview id: WaitForCopilotReview run: dotnet run --project _atom/_atom.csproj -- WaitForCopilotReview --skip --headless diff --git a/AGENTS.md b/AGENTS.md index e5d6e48..9b6efc5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -18,8 +18,9 @@ and `toc.yml`. ## Build & language specifics -- **.NET 10 SDK** is required. Library and test projects target `net10.0`. -- C# `LangVersion` 14, `ImplicitUsings` and `Nullable` enabled, `TreatWarningsAsErrors` on. +- **.NET 11 SDK** is required, plus the .NET 10 runtime (or SDK) to run the `net10.0` tests and build project. Library and test projects target `net10.0` and `net11.0`; + `ProcessRunner` uses the .NET 11 `Process.ReadAllLines` APIs under `#if NET11_0_OR_GREATER`. +- C# `LangVersion` 15, `ImplicitUsings` and `Nullable` enabled, `TreatWarningsAsErrors` on. - Global usings live in each project's `_usings.cs` — add shared usings there, not per-file. - `GenerateDocumentationFile` is on and `CS1591` is **enforced** in `src/` — every public type and member needs XML doc comments. (`_atom` and the test project suppress `CS1591` locally; do diff --git a/Directory.Build.props b/Directory.Build.props index fc6cf48..d123f92 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -11,7 +11,7 @@ true true - 14 + 15 enable enable true diff --git a/README.md b/README.md index 836a4ac..babb1e7 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ full output capture, and fail-fast error handling. It wraps `System.Diagnostics. dotnet add package Invex.Process ``` -The package targets `net10.0`. It depends on +The package targets `net10.0` and `net11.0`. It depends on `Microsoft.Extensions.DependencyInjection.Abstractions` and `Microsoft.Extensions.Hosting.Abstractions`, and logs through `ILogger`, so configure logging on your host (it is by default with `Host.CreateApplicationBuilder`). diff --git a/_atom/IBuild.cs b/_atom/IBuild.cs index c8c83bc..cd18aa9 100644 --- a/_atom/IBuild.cs +++ b/_atom/IBuild.cs @@ -40,6 +40,7 @@ internal interface IBuild : IWorkflowBuildDefinition, static readonly string[] TestFrameworkNames = [ WorkflowLabels.Dotnet.Framework.Net_10_0, + "net11.0", ]; IEnumerable ICheckPrForBreakingChanges.BreakingChangeFilesToCheck => @@ -53,6 +54,8 @@ internal interface IBuild : IWorkflowBuildDefinition, BuildOptions.GitVersion.ProvideBuildId, BuildOptions.GitVersion.ProvideBuildVersion, BuildOptions.Steps.SetupDotnet.Dotnet100X(), + // .NET 11 SDK. The .NET 10 step above still provides the runtime for net10.0 tests. + new SetupDotnetStep("11.0.x"), ]; Target PackProjects => diff --git a/docs/getting-started.md b/docs/getting-started.md index 1222824..3cd2c4b 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -9,7 +9,7 @@ if you are adding `Invex.Process` to a host, tool or build for the first time. dotnet add package Invex.Process ``` -The package targets `net10.0`. +The package targets `net10.0` and `net11.0`. On .NET 11 it reads output with the new `Process.ReadAllLines` APIs, so stdout and stderr lines are logged as one stream in the order they become available (stderr first when both are ready, so not always the order the process wrote them), and a process killed by a signal is reported by signal name. ## Register the service diff --git a/global.json b/global.json index 2170bab..85ae47e 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { - "version": "10.0.403", - "rollForward": "latestMajor", + "version": "11.0.100", + "rollForward": "latestMinor", "allowPrerelease": false } } \ No newline at end of file diff --git a/src/Invex.Process/Invex.Process.csproj b/src/Invex.Process/Invex.Process.csproj index 574d735..aa87c76 100644 --- a/src/Invex.Process/Invex.Process.csproj +++ b/src/Invex.Process/Invex.Process.csproj @@ -1,7 +1,7 @@  - net10.0 + net10.0;net11.0 diff --git a/src/Invex.Process/ProcessRunner.cs b/src/Invex.Process/ProcessRunner.cs index 5e647a7..7c1e541 100644 --- a/src/Invex.Process/ProcessRunner.cs +++ b/src/Invex.Process/ProcessRunner.cs @@ -91,10 +91,13 @@ public interface IProcessRunner /// /// /// -/// stdout and stderr are both redirected and read asynchronously via +/// stdout and stderr are both redirected and streamed to the logger in real time rather than +/// buffered. On .NET 11 and later they are read as one interleaved stream of lines with +/// Process.ReadAllLines / Process.ReadAllLinesAsync, in the order lines become available +/// (stderr first when both are ready), which is not always the order the process wrote them; on .NET 10 +/// they are read via /// / -/// so that output is streamed -/// to the logger in real time rather than buffered. +/// . /// /// /// Each received line is passed through or @@ -169,6 +172,17 @@ public ProcessRunResult Run(ProcessRunOptions options) var outputBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); +#if NET11_0_OR_GREATER + process.Start(); + + // stdout and stderr arrive as one stream of lines, in the order they become available (not strictly write order). + foreach (var line in process.ReadAllLines()) + OnLine(line.Content, line.StandardError); + + var exitStatus = process.WaitForExitStatus(); + var exitCode = exitStatus.ExitCode; + var signal = exitStatus.Signal; +#else process.OutputDataReceived += OnProcessOnOutputDataReceived; process.ErrorDataReceived += OnProcessOnErrorDataReceived; @@ -185,10 +199,14 @@ public ProcessRunResult Run(ProcessRunOptions options) process.ErrorDataReceived -= OnProcessOnErrorDataReceived; } + var exitCode = process.ExitCode; + PosixSignal? signal = null; +#endif + var output = outputBuilder.ToString(); var error = errorBuilder.ToString(); - var result = new ProcessRunResult(options, process.ExitCode, output, error); + var result = new ProcessRunResult(options, exitCode, output, error); if (result.ExitCode is 0) return result; @@ -206,39 +224,43 @@ public ProcessRunResult Run(ProcessRunOptions options) return result; } - throw new ProcessException(BuildProcessErrorMessage(options, result.ExitCode, error), result); + throw new ProcessException(BuildProcessErrorMessage(options, result.ExitCode, signal, error), result); - void OnProcessOnErrorDataReceived(object _, DataReceivedEventArgs e) + void OnLine(string? data, bool isError) { - if (e.Data is null) + if (data is null) return; - var text = options.TransformError is not null - ? options.TransformError(e.Data) - : e.Data; + var transform = isError + ? options.TransformError + : options.TransformOutput; + + var text = transform is not null + ? transform(data) + : data; if (text is null) return; - errorBuilder.AppendLine(text); - logger.Log(options.ErrorLogLevel, "{Error}", text); + if (isError) + { + errorBuilder.AppendLine(text); + logger.Log(options.ErrorLogLevel, "{Error}", text); + } + else + { + outputBuilder.AppendLine(text); + logger.Log(options.OutputLogLevel, "{Output}", text); + } } - void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) - { - if (e.Data is null) - return; - - var text = options.TransformOutput is not null - ? options.TransformOutput(e.Data) - : e.Data; - - if (text is null) - return; +#if !NET11_0_OR_GREATER + void OnProcessOnErrorDataReceived(object _, DataReceivedEventArgs e) => + OnLine(e.Data, true); - outputBuilder.AppendLine(text); - logger.Log(options.OutputLogLevel, "{Output}", text); - } + void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) => + OnLine(e.Data, false); +#endif } /// @@ -296,6 +318,17 @@ public async Task RunAsync( var outputBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); +#if NET11_0_OR_GREATER + process.Start(); + + // stdout and stderr arrive as one stream of lines, in the order they become available (not strictly write order). + await foreach (var line in process.ReadAllLinesAsync(cancellationToken)) + OnLine(line.Content, line.StandardError); + + var exitStatus = await process.WaitForExitStatusAsync(cancellationToken); + var exitCode = exitStatus.ExitCode; + var signal = exitStatus.Signal; +#else process.OutputDataReceived += OnProcessOnOutputDataReceived; process.ErrorDataReceived += OnProcessOnErrorDataReceived; @@ -312,10 +345,14 @@ public async Task RunAsync( process.ErrorDataReceived -= OnProcessOnErrorDataReceived; } + var exitCode = process.ExitCode; + PosixSignal? signal = null; +#endif + var output = outputBuilder.ToString(); var error = errorBuilder.ToString(); - var result = new ProcessRunResult(options, process.ExitCode, output, error); + var result = new ProcessRunResult(options, exitCode, output, error); if (result.ExitCode is 0) return result; @@ -333,39 +370,43 @@ public async Task RunAsync( return result; } - throw new ProcessException(BuildProcessErrorMessage(options, result.ExitCode, error), result); + throw new ProcessException(BuildProcessErrorMessage(options, result.ExitCode, signal, error), result); - void OnProcessOnErrorDataReceived(object _, DataReceivedEventArgs e) + void OnLine(string? data, bool isError) { - if (e.Data is null) + if (data is null) return; - var text = options.TransformError is not null - ? options.TransformError(e.Data) - : e.Data; + var transform = isError + ? options.TransformError + : options.TransformOutput; + + var text = transform is not null + ? transform(data) + : data; if (text is null) return; - errorBuilder.AppendLine(text); - logger.Log(options.ErrorLogLevel, "{Error}", text); + if (isError) + { + errorBuilder.AppendLine(text); + logger.Log(options.ErrorLogLevel, "{Error}", text); + } + else + { + outputBuilder.AppendLine(text); + logger.Log(options.OutputLogLevel, "{Output}", text); + } } - void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) - { - if (e.Data is null) - return; - - var text = options.TransformOutput is not null - ? options.TransformOutput(e.Data) - : e.Data; +#if !NET11_0_OR_GREATER + void OnProcessOnErrorDataReceived(object _, DataReceivedEventArgs e) => + OnLine(e.Data, true); - if (text is null) - return; - - outputBuilder.AppendLine(text); - logger.Log(options.OutputLogLevel, "{Output}", text); - } + void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) => + OnLine(e.Data, false); +#endif } /// @@ -380,10 +421,17 @@ void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) private static string FormatEnvironmentVariableNames(ProcessRunOptions options) => string.Join(", ", options.EnvironmentVariables.Keys); - private static string BuildProcessErrorMessage(ProcessRunOptions options, int exitCode, string error) + private static string BuildProcessErrorMessage( + ProcessRunOptions options, + int exitCode, + PosixSignal? signal, + string error) { var errorMessage = new StringBuilder(); - errorMessage.AppendLine($"Process '{options.Name}' failed with exit code {exitCode}"); + + errorMessage.AppendLine(signal is { } terminatingSignal + ? $"Process '{options.Name}' was terminated by signal {terminatingSignal} (exit code {exitCode})" + : $"Process '{options.Name}' failed with exit code {exitCode}"); errorMessage.AppendLine($" Command: {options.Name} {options.Args}"); if (options.WorkingDirectory is { Length: > 0 }) diff --git a/src/Invex.Process/_usings.cs b/src/Invex.Process/_usings.cs index d6b71b3..a9fc0e4 100644 --- a/src/Invex.Process/_usings.cs +++ b/src/Invex.Process/_usings.cs @@ -1,6 +1,7 @@ global using System.Diagnostics; global using System.Diagnostics.CodeAnalysis; global using System.Runtime.CompilerServices; +global using System.Runtime.InteropServices; global using System.Text; global using JetBrains.Annotations; global using Microsoft.Extensions.DependencyInjection; diff --git a/tests/Invex.Process.Tests/Invex.Process.Tests.csproj b/tests/Invex.Process.Tests/Invex.Process.Tests.csproj index 2e13ec0..b1479b1 100644 --- a/tests/Invex.Process.Tests/Invex.Process.Tests.csproj +++ b/tests/Invex.Process.Tests/Invex.Process.Tests.csproj @@ -1,7 +1,7 @@ - net10.0 + net10.0;net11.0 false $(NoWarn);CS1591 diff --git a/tests/Invex.Process.Tests/ProcessRunnerTests.cs b/tests/Invex.Process.Tests/ProcessRunnerTests.cs index 41cbf9e..d84c0dc 100644 --- a/tests/Invex.Process.Tests/ProcessRunnerTests.cs +++ b/tests/Invex.Process.Tests/ProcessRunnerTests.cs @@ -44,6 +44,37 @@ private static ProcessRunOptions PrintCwdOptions() => ? new ProcessRunOptions("cmd", "/c cd") : new("pwd", string.Empty); + // Writes to both streams, ending with an stdout line that has no trailing newline. + private static ProcessRunOptions MixedStreamsOptions() => + new("sh", "-c \"echo out1; echo err1 >&2; echo out2; echo drop-err >&2; printf out3\"") + { + TransformOutput = line => line == "out2" + ? null + : $"[O]{line}", + TransformError = line => line.StartsWith("drop", StringComparison.Ordinal) + ? null + : $"[E]{line}", + OutputLogLevel = LogLevel.Information, + ErrorLogLevel = LogLevel.Warning, + }; + + private static void ShouldHaveMixedStreamsResult(ProcessRunResult result, RecordingLogger logger) + { + result.Output.ShouldBe($"[O]out1{Environment.NewLine}[O]out3{Environment.NewLine}"); + result.Error.ShouldBe($"[E]err1{Environment.NewLine}"); + + var entries = logger.Entries; + entries.Where(e => e.Level == LogLevel.Information && e.Message.StartsWith("[O]", StringComparison.Ordinal)) + .Select(e => e.Message) + .ShouldBe(["[O]out1", "[O]out3"], ignoreOrder: true); + entries.Where(e => e.Level == LogLevel.Warning && e.Message.StartsWith("[E]", StringComparison.Ordinal)) + .Select(e => e.Message) + .ShouldBe(["[E]err1"]); + // The invocation log line names the command, so skip it when checking that suppressed lines never reach the log. + entries.Where(e => !e.Message.Contains("echo", StringComparison.Ordinal)) + .ShouldNotContain(e => e.Message.Contains("out2") || e.Message.Contains("drop-err")); + } + private static ProcessRunOptions LongRunningOptions() => // ping sends one packet per second and works reliably with redirected stdout; // unlike `timeout`, it does not exit early when used in a non-interactive context. @@ -531,19 +562,101 @@ public async Task RunAsync_WhenCancelledMidRun_ThrowsOperationCanceledException( await Should.ThrowAsync(() => runner.RunAsync(LongRunningOptions(), cts.Token)); } + [Test] + public void Run_WithMixedStreams_CapturesAndLogsEachStreamSeparately() + { + if (OperatingSystem.IsWindows()) + Assert.Ignore("Uses sh to write to both streams."); + + // Arrange + var logger = new RecordingLogger(); + var runner = new ProcessRunner(logger); + + // Act + var result = runner.Run(MixedStreamsOptions()); + + // Assert + ShouldHaveMixedStreamsResult(result, logger); + } + + [Test] + public async Task RunAsync_WithMixedStreams_CapturesAndLogsEachStreamSeparately() + { + if (OperatingSystem.IsWindows()) + Assert.Ignore("Uses sh to write to both streams."); + + // Arrange + var logger = new RecordingLogger(); + var runner = new ProcessRunner(logger); + + // Act + var result = await runner.RunAsync(MixedStreamsOptions()); + + // Assert + ShouldHaveMixedStreamsResult(result, logger); + } + +#if NET11_0_OR_GREATER + private static ProcessRunOptions KilledBySigtermOptions() => + new("sh", "-c \"kill -TERM $$\""); + + [Test] + public void Run_WhenProcessIsKilledBySignal_ErrorMessageNamesTheSignal() + { + if (OperatingSystem.IsWindows()) + Assert.Ignore("POSIX signals are not available on Windows."); + + // Arrange + var runner = CreateRunner(); + + // Act + var exception = Should.Throw(() => runner.Run(KilledBySigtermOptions())); + + // Assert + exception.Message.ShouldContain("terminated by signal SIGTERM"); + exception.Result.ExitCode.ShouldNotBe(0); + } + + [Test] + public async Task RunAsync_WhenProcessIsKilledBySignal_ErrorMessageNamesTheSignal() + { + if (OperatingSystem.IsWindows()) + Assert.Ignore("POSIX signals are not available on Windows."); + + // Arrange + var runner = CreateRunner(); + + // Act + var exception = await Should.ThrowAsync(() => runner.RunAsync(KilledBySigtermOptions())); + + // Assert + exception.Message.ShouldContain("terminated by signal SIGTERM"); + exception.Result.ExitCode.ShouldNotBe(0); + } + +#endif /// /// A minimal that records every formatted message. /// private sealed class RecordingLogger : ILogger { - private readonly List _messages = []; + private readonly List<(LogLevel Level, string Message)> _entries = []; public IReadOnlyList Messages { get { - lock (_messages) - return [.. _messages]; + lock (_entries) + return [.. _entries.Select(e => e.Message)]; + } + } + + public IReadOnlyList<(LogLevel Level, string Message)> Entries + { + get + { + lock (_entries) + return [.. _entries]; } } @@ -561,8 +674,8 @@ public void Log( Exception? exception, Func formatter) { - lock (_messages) - _messages.Add(formatter(state, exception)); + lock (_entries) + _entries.Add((logLevel, formatter(state, exception))); } } }