forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cs
More file actions
124 lines (105 loc) · 4.91 KB
/
Color.cs
File metadata and controls
124 lines (105 loc) · 4.91 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
public class Color
{
private const char Esc = (char)0x1B;
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void InvalidColors()
{
AssertExtensions.Throws<ArgumentException>(null, () => Console.BackgroundColor = (ConsoleColor)42);
AssertExtensions.Throws<ArgumentException>(null, () => Console.ForegroundColor = (ConsoleColor)42);
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void RoundtrippingColor()
{
Console.BackgroundColor = Console.BackgroundColor;
Console.ForegroundColor = Console.ForegroundColor;
// Changing color on Windows doesn't have effect in some testing environments
// when there is no associated console, such as when run under a profiler like
// our code coverage tools, so we don't assert that the change took place and
// simple ensure that getting/setting doesn't throw.
}
[Fact]
[PlatformSpecific(TestPlatforms.Browser)]
public static void ForegroundColor_Throws_PlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => Console.ForegroundColor);
Assert.Throws<PlatformNotSupportedException>(() => Console.ForegroundColor = ConsoleColor.Red);
}
[Fact]
[PlatformSpecific(TestPlatforms.Browser)]
public static void BackgroundColor_Throws_PlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => Console.BackgroundColor);
Assert.Throws<PlatformNotSupportedException>(() => Console.BackgroundColor = ConsoleColor.Red);
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void RedirectedOutputDoesNotUseAnsiSequences()
{
// Make sure that redirecting to a memory stream causes Console not to write out the ANSI sequences
Helpers.RunInRedirectedOutput((data) =>
{
Console.Write('1');
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write('2');
Console.BackgroundColor = ConsoleColor.Red;
Console.Write('3');
Console.ResetColor();
Console.Write('4');
Assert.Equal(0, Encoding.UTF8.GetString(data.ToArray()).ToCharArray().Count(c => c == Esc));
Assert.Equal("1234", Encoding.UTF8.GetString(data.ToArray()));
});
}
public static bool TermIsSet => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TERM"));
[ConditionalTheory(nameof(TermIsSet))]
[PlatformSpecific(TestPlatforms.AnyUnix)]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
[InlineData(null)]
[InlineData("1")]
[InlineData("true")]
[InlineData("tRuE")]
[InlineData("0")]
[InlineData("false")]
public static void RedirectedOutput_EnvVarSet_EmitsAnsiCodes(string envVar)
{
var psi = new ProcessStartInfo { RedirectStandardOutput = true };
psi.Environment["DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION"] = envVar;
for (int i = 0; i < 3; i++)
{
Action<string> main = i =>
{
Console.Write("SEPARATOR");
switch (i)
{
case "0":
Console.ForegroundColor = ConsoleColor.Blue;
break;
case "1":
Console.BackgroundColor = ConsoleColor.Red;
break;
case "2":
Console.ResetColor();
break;
}
Console.Write("SEPARATOR");
};
using RemoteInvokeHandle remote = RemoteExecutor.Invoke(main, i.ToString(CultureInfo.InvariantCulture), new RemoteInvokeOptions() { StartInfo = psi });
bool expectedEscapes = envVar is not null && (envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase));
string stdout = remote.Process.StandardOutput.ReadToEnd();
string[] parts = stdout.Split("SEPARATOR");
Assert.Equal(3, parts.Length);
Assert.Equal(expectedEscapes, parts[1].Contains(Esc));
}
}
}