|
1 | 1 | using System.Globalization; |
| 2 | +using System.Text; |
2 | 3 | using System.Text.RegularExpressions; |
3 | 4 | using CodeIndex.Cli; |
4 | 5 |
|
@@ -117,6 +118,52 @@ public void TryStart_WritesInvariantUtcTimestampAndStackTrace() |
117 | 118 | } |
118 | 119 | } |
119 | 120 |
|
| 121 | + [Fact] |
| 122 | + public void TryStart_ErrorMirrorIgnoresDisposedOriginalConsoleWriter() |
| 123 | + { |
| 124 | + var logRoot = Path.Combine(Path.GetTempPath(), $"cdidx_global_log_disposed_{Guid.NewGuid():N}"); |
| 125 | + var originalError = Console.Error; |
| 126 | + try |
| 127 | + { |
| 128 | + using var env = EnvironmentVariableScope.Capture( |
| 129 | + "CDIDX_FORCE_GLOBAL_TOOL_LOG", |
| 130 | + "CDIDX_DISABLE_PERSISTENT_LOG", |
| 131 | + "CDIDX_GLOBAL_TOOL_LOG_DIR"); |
| 132 | + env.Set("CDIDX_FORCE_GLOBAL_TOOL_LOG", "1"); |
| 133 | + env.Set("CDIDX_DISABLE_PERSISTENT_LOG", null); |
| 134 | + env.Set("CDIDX_GLOBAL_TOOL_LOG_DIR", logRoot); |
| 135 | + |
| 136 | + using var session = GlobalToolLog.TryStartForTesting( |
| 137 | + ["status"], |
| 138 | + "test", |
| 139 | + afterWriterCreated: () => Console.SetError(new ThrowingTextWriter())); |
| 140 | + |
| 141 | + var exception = Record.Exception(() => Console.Error.WriteLine("mirrored error")); |
| 142 | + |
| 143 | + Assert.NotNull(session); |
| 144 | + Assert.Null(exception); |
| 145 | + } |
| 146 | + finally |
| 147 | + { |
| 148 | + Console.SetError(originalError); |
| 149 | + if (Directory.Exists(logRoot)) |
| 150 | + Directory.Delete(logRoot, recursive: true); |
| 151 | + } |
| 152 | + } |
| 153 | + |
120 | 154 | private static void ThrowForGlobalToolLogTest() => |
121 | 155 | throw new InvalidOperationException("global log stack trace test"); |
| 156 | + |
| 157 | + private sealed class ThrowingTextWriter : TextWriter |
| 158 | + { |
| 159 | + public override Encoding Encoding => Encoding.UTF8; |
| 160 | + |
| 161 | + public override void Flush() => throw new ObjectDisposedException(nameof(ThrowingTextWriter)); |
| 162 | + |
| 163 | + public override void Write(char value) => throw new ObjectDisposedException(nameof(ThrowingTextWriter)); |
| 164 | + |
| 165 | + public override void Write(string? value) => throw new ObjectDisposedException(nameof(ThrowingTextWriter)); |
| 166 | + |
| 167 | + public override void WriteLine(string? value) => throw new ObjectDisposedException(nameof(ThrowingTextWriter)); |
| 168 | + } |
122 | 169 | } |
0 commit comments