Repository navigation
Expand file tree
/
Copy pathAnsiOptionTests.cs
More file actions
289 lines (252 loc) · 14.4 KB
/
Copy pathAnsiOptionTests.cs
File metadata and controls
289 lines (252 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;
[TestClass]
public sealed class AnsiOptionTests : AcceptanceTestBase<AnsiOptionTests.TestAssetFixture>
{
private const string AssetName = "AnsiOptionTest";
// ANSI escape character (ESC, 0x1b). Its presence in captured stdout proves the test host emitted
// raw ANSI escape sequences even though stdout was redirected by the test runner.
private const string EscapeCharacter = "\u001b";
[TestMethod]
[DataRow("on")]
[DataRow("true")]
[DataRow("enable")]
[DataRow("1")]
public async Task AnsiOption_OnAliases_ForceAnsiOutputEvenWhenRedirected(string value)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync($"--ansi {value}", cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
// TestHost.ExecuteAsync prepends `--no-ansi --progress off`. `--ansi <on-alias>` must take precedence
// over `--no-ansi` and re-enable ANSI escape codes, even though stdout is being redirected to a pipe.
Assert.IsTrue(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to contain ANSI escape characters when '--ansi {value}' is specified, but got:\n{result.StandardOutput}");
}
[TestMethod]
[DataRow("off")]
[DataRow("false")]
[DataRow("disable")]
[DataRow("0")]
public async Task AnsiOption_OffAliases_DisableAnsiOutput(string value)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync($"--ansi {value}", cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsFalse(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to NOT contain ANSI escape characters when '--ansi {value}' is specified, but got:\n{result.StandardOutput}");
}
[TestMethod]
public async Task AnsiOption_Auto_OverridesNoAnsi_AndFollowsEnvironmentDetection()
{
// TestHost.ExecuteAsync auto-injects `--no-ansi --progress off`. Asserting "no ESC" with
// `--ansi auto` would not actually prove `auto` won over `--no-ansi`: stdout-redirection
// alone also produces no ESC. To deterministically prove the override, we force the platform
// into a CI environment (which makes `auto` map to `SimpleAnsi`, which emits ESC) and clear
// known LLM env vars (which would otherwise force `NoAnsi`). With `--no-ansi` still on the
// command line, the only way ESC can appear in the output is if `--ansi auto` overrode it.
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
"--ansi auto",
environmentVariables: InCIEnvironmentVariablesWithLLMCleared,
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsTrue(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to contain ANSI escape characters proving '--ansi auto' overrode the auto-injected '--no-ansi' (SimpleAnsi mode in CI), but got:\n{result.StandardOutput}");
}
[TestMethod]
public async Task AnsiOption_OnExplicit_AfterNoAnsi_StillForcesAnsiOutput()
{
// Regression guard for the rule "any explicit --ansi value overrides --no-ansi", order-independent.
// We use --ansi on rather than --ansi auto because ForceOn short-circuits both the CI check AND the
// LLM env-var check in TerminalOutputDevice.InitializeAsync, making the assertion immune to whatever
// CI / LLM environment a contributor or CI agent might be running from.
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
"--no-ansi --ansi on",
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsTrue(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to contain ANSI escape characters proving '--ansi on' overrode the preceding '--no-ansi', but got:\n{result.StandardOutput}");
}
[TestMethod]
public async Task AnsiOption_InvalidArgument_FailsCommandLineValidation()
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync("--ansi invalid", cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.InvalidCommandLine);
result.AssertOutputContains("--ansi expects a single parameter");
}
[TestMethod]
public async Task AnsiOption_MissingArgument_FailsCommandLineValidation()
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync("--ansi", cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.InvalidCommandLine);
// When `--ansi` is supplied without a value, the platform's arity check
// (ArgumentArity.ExactlyOne) rejects it before the per-occurrence validator runs, so the
// user sees the generic "expects at least N arguments" message rather than the custom
// TerminalAnsiOptionInvalidArgument text used for invalid values.
result.AssertOutputContains("Option '--ansi' from provider 'Terminal test reporter' (UID: TerminalTestReporterCommandLineOptionsProvider) expects at least 1 arguments");
}
[TestMethod]
public async Task NoColorEnvVar_NonEmpty_DisablesAnsiOutput()
{
// The de-facto NO_COLOR convention (https://no-color.org): when present with any non-empty
// value, color (and in our implementation, all ANSI escape codes) must be suppressed.
// TestHost.ExecuteAsync auto-injects `--no-ansi --progress off` which would already suppress ESC,
// so we use `--ansi auto` together with the CI env (which would otherwise produce SimpleAnsi
// ESC) to deterministically prove NO_COLOR alone forces NoAnsi. With NO_COLOR set the output
// must contain no ESC even though `--ansi auto` is opted in.
Dictionary<string, string?> environmentVariables = new(InCIEnvironmentVariablesWithLLMCleared)
{
["NO_COLOR"] = "1",
};
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
"--ansi auto",
environmentVariables: environmentVariables,
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsFalse(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to NOT contain ANSI escape characters when NO_COLOR is set, but got:\n{result.StandardOutput}");
}
[TestMethod]
public async Task NoColorEnvVar_Empty_IsIgnored()
{
// Per the NO_COLOR spec, ONLY a non-empty value disables color. An empty NO_COLOR must be a no-op.
// With `--ansi auto` in a CI environment (and LLM detection cleared), MTP picks SimpleAnsi which
// does emit color ESC sequences. Asserting ESC presence proves empty NO_COLOR did not force NoAnsi.
Dictionary<string, string?> environmentVariables = new(InCIEnvironmentVariablesWithLLMCleared)
{
["NO_COLOR"] = string.Empty,
};
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
"--ansi auto",
environmentVariables: environmentVariables,
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsTrue(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to contain ANSI escape characters when NO_COLOR is empty (spec: only non-empty disables color), but got:\n{result.StandardOutput}");
}
[TestMethod]
public async Task NoColorEnvVar_OverriddenByExplicitAnsiOn()
{
// Precedence rule documented in TerminalOutputDevice: `--ansi on` short-circuits CI / LLM /
// NO_COLOR detection. NO_COLOR is honored "unless the user explicitly opted in via --ansi on".
Dictionary<string, string?> environmentVariables = new(InCIEnvironmentVariablesWithLLMCleared)
{
["NO_COLOR"] = "1",
};
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, TargetFrameworks.NetCurrent);
TestHostResult result = await testHost.ExecuteAsync(
"--ansi on",
environmentVariables: environmentVariables,
cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCode.Success);
Assert.IsTrue(
result.StandardOutput.Contains(EscapeCharacter, StringComparison.Ordinal),
$"Expected output to contain ANSI escape characters because `--ansi on` overrides NO_COLOR, but got:\n{result.StandardOutput}");
}
public TestContext TestContext { get; set; } = null!;
// Env vars that force the platform to consider it to be in CI while clearing the known LLM env
// vars (which would otherwise force NoAnsi mode and break tests relying on SimpleAnsi). Only
// GITHUB_ACTIONS is set to "true" because that is sufficient for CIEnvironmentDetector.
private static Dictionary<string, string?> InCIEnvironmentVariablesWithLLMCleared => new()
{
["GITHUB_ACTIONS"] = "true",
["NO_COLOR"] = string.Empty,
["CLAUDECODE"] = string.Empty,
["CLAUDE_CODE_ENTRYPOINT"] = string.Empty,
["CURSOR_EDITOR"] = string.Empty,
["CURSOR_AI"] = string.Empty,
["GEMINI_CLI"] = string.Empty,
["GITHUB_COPILOT_CLI_MODE"] = string.Empty,
["GH_COPILOT_WORKING_DIRECTORY"] = string.Empty,
["COPILOT_CLI"] = string.Empty,
["CODEX_CLI"] = string.Empty,
["CODEX_SANDBOX"] = string.Empty,
["OR_APP_NAME"] = string.Empty,
["AMP_HOME"] = string.Empty,
["QWEN_CODE"] = string.Empty,
["DROID_CLI"] = string.Empty,
["OPENCODE"] = string.Empty,
["ZED_ENVIRONMENT"] = string.Empty,
["ZED_TERM"] = string.Empty,
["KIMI_CLI"] = string.Empty,
["GOOSE_TERMINAL"] = string.Empty,
["CLINE_TASK_ID"] = string.Empty,
["ROO_CODE_TASK_ID"] = string.Empty,
["WINDSURF_SESSION"] = string.Empty,
["AGENT_CLI"] = string.Empty,
};
public sealed class TestAssetFixture() : TestAssetFixtureBase()
{
private const string TestCode = """
#file AnsiOptionTest.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<UseAppHost>true</UseAppHost>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>
</Project>
#file Program.cs
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
public class Program
{
public static async Task<int> Main(string[] args)
{
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
builder.RegisterTestFramework(
sp => new TestFrameworkCapabilities(),
(_, __) => new DummyTestFramework());
using ITestApplication app = await builder.BuildAsync();
return await app.RunAsync();
}
}
public class DummyTestFramework : ITestFramework, IDataProducer
{
public string Uid => nameof(DummyTestFramework);
public string Version => "1.0.0";
public string DisplayName => nameof(DummyTestFramework);
public string Description => nameof(DummyTestFramework);
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
public Type[] DataTypesProduced => [typeof(TestNodeUpdateMessage)];
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });
public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
// Emit one passing test so the summary line is rendered with color (DarkGreen "Passed!").
// The presence of a color-bearing line is what gives us a reliable signal for ANSI vs non-ANSI output.
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "0", DisplayName = "Test1", Properties = new(PassedTestNodeStateProperty.CachedInstance) }));
context.Complete();
}
}
""";
public string TargetAssetPath => GetAssetPath(AssetName);
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (AssetName, AssetName,
TestCode
.PatchTargetFrameworks([TargetFrameworks.NetCurrent])
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion));
}
}