From 056a4d6b0d67582a6a0044c4233e9a74113e773d Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Wed, 21 Oct 2026 15:54:18 +0200 Subject: [PATCH] Only disable color when NO_COLOR is non-empty. Closes #1967 Per https://no-color.org, NO_COLOR applies only when present and not empty. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 09b222bb-b264-4461-926d-32f935613554 --- DevProxy.Tests/DevProxyCommandNoColorTests.cs | 37 +++++++++++++++++++ DevProxy/Commands/DevProxyCommand.cs | 14 +++++-- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 DevProxy.Tests/DevProxyCommandNoColorTests.cs diff --git a/DevProxy.Tests/DevProxyCommandNoColorTests.cs b/DevProxy.Tests/DevProxyCommandNoColorTests.cs new file mode 100644 index 00000000..3596fe72 --- /dev/null +++ b/DevProxy.Tests/DevProxyCommandNoColorTests.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using DevProxy.Commands; + +using Xunit; + +namespace DevProxy.Tests; + +public class DevProxyCommandNoColorTests +{ + [Theory] + [InlineData(null, false)] + [InlineData("", false)] + [InlineData("1", true)] + [InlineData("true", true)] + public void ShouldDisableColor_RespectsNoColorEnv(string? noColor, bool expected) + { + Assert.Equal(expected, DevProxyCommand.ShouldDisableColor([], noColor, null)); + } + + [Fact] + public void ShouldDisableColor_NoColorOption_DisablesColor() + { + Assert.True(DevProxyCommand.ShouldDisableColor(["devproxy", "--no-color"], "", null)); + } + + [Theory] + [InlineData("dumb", true)] + [InlineData("DUMB", true)] + [InlineData("xterm-256color", false)] + public void ShouldDisableColor_RespectsTerm(string term, bool expected) + { + Assert.Equal(expected, DevProxyCommand.ShouldDisableColor([], null, term)); + } +} diff --git a/DevProxy/Commands/DevProxyCommand.cs b/DevProxy/Commands/DevProxyCommand.cs index dfc9b763..ce6834d5 100644 --- a/DevProxy/Commands/DevProxyCommand.cs +++ b/DevProxy/Commands/DevProxyCommand.cs @@ -231,15 +231,21 @@ public static bool NoColor return field; } - var args = Environment.GetCommandLineArgs(); - field = args.Contains(NoColorOptionName) || - Environment.GetEnvironmentVariable("NO_COLOR") is not null || - string.Equals(Environment.GetEnvironmentVariable("TERM"), "dumb", StringComparison.OrdinalIgnoreCase); + field = ShouldDisableColor( + Environment.GetCommandLineArgs(), + Environment.GetEnvironmentVariable("NO_COLOR"), + Environment.GetEnvironmentVariable("TERM")); _noColorResolved = true; return field; } } + // Per https://no-color.org, NO_COLOR disables color only when present and not empty. + internal static bool ShouldDisableColor(string[] args, string? noColorEnv, string? termEnv) => + args.Contains(NoColorOptionName) || + !string.IsNullOrEmpty(noColorEnv) || + string.Equals(termEnv, "dumb", StringComparison.OrdinalIgnoreCase); + public DevProxyCommand( IEnumerable plugins, ISet urlsToWatch,