-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathTerminalTestReporterTests.cs
More file actions
758 lines (634 loc) · 35.2 KB
/
Copy pathTerminalTestReporterTests.cs
File metadata and controls
758 lines (634 loc) · 35.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.OutputDevice.Terminal;
using Microsoft.Testing.Platform.Services;
namespace Microsoft.Testing.Platform.UnitTests;
[TestClass]
public sealed class TerminalTestReporterTests
{
[TestMethod]
public void AppendStackFrameFormatsStackTraceLineCorrectly()
{
var terminal = new StringBuilderTerminal();
Exception err;
try
{
throw new Exception();
}
catch (Exception ex)
{
err = ex;
}
string firstStackTraceLine = err.StackTrace!.Replace("\r", string.Empty).Split('\n')[0];
TerminalTestReporter.AppendStackFrame(terminal, firstStackTraceLine);
#if NETCOREAPP
Assert.Contains(" at Microsoft.Testing.Platform.UnitTests.TerminalTestReporterTests.AppendStackFrameFormatsStackTraceLineCorrectly() in ", terminal.Output);
#else
// This is caused by us using portable symbols, and .NET Framework 4.6.2, once we update to .NET Framework 4.7.2 the path to file will be included in the stacktrace and this won't be necessary.
// See first point here: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/symbols#support-for-portable-pdbs
Assert.Contains(" at Microsoft.Testing.Platform.UnitTests.TerminalTestReporterTests.AppendStackFrameFormatsStackTraceLineCorrectly()", terminal.Output);
#endif
// Line number without the respective file
Assert.DoesNotContain(" :0", terminal.Output);
}
// Code with line when we have symbols
[DataRow(
" at MicrosoftTestingPlatformEntryPoint.Main(String[]) in /_/TUnit.TestProject/obj/Release/net8.0/osx-x64/MicrosoftTestingPlatformEntryPoint.cs:line 16",
" at MicrosoftTestingPlatformEntryPoint.Main(String[]) in /_/TUnit.TestProject/obj/Release/net8.0/osx-x64/MicrosoftTestingPlatformEntryPoint.cs:16")]
// code without line when we don't have symbols
[DataRow(
" at TestingPlatformEntryPoint.<Main>(String[])",
" at TestingPlatformEntryPoint.<Main>(String[])")]
// stack trace when published as NativeAOT
[DataRow(
" at BenchmarkTest.ExceptionThrower.<Nested1>d__2.MoveNext() + 0x9d",
" at BenchmarkTest.ExceptionThrower.<Nested1>d__2.MoveNext() + 0x9d")]
// spanners that we want to keep, to not lose information
[DataRow(
"--- End of stack trace from previous location ---",
" --- End of stack trace from previous location ---")]
[TestMethod]
public void StackTraceRegexCapturesLines(string stackTraceLine, string expected)
{
var terminal = new StringBuilderTerminal();
TerminalTestReporter.AppendStackFrame(terminal, stackTraceLine);
// We add newline after every, but it is hard to put it in the attribute.
expected += Environment.NewLine;
Assert.AreEqual(expected, terminal.Output);
}
[TestMethod]
public void NonAnsiTerminal_OutputFormattingIsCorrect()
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
// Like --no-ansi in commandline, should disable ANSI altogether.
UseAnsi = false,
ShowProgress = () => false,
});
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: false);
string folder = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\" : "/mnt/work/";
string folderNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work" : "/mnt/work";
string folderLink = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work/" : "mnt/work/";
string folderLinkNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work" : "mnt/work";
terminalReporter.AssemblyRunStarted();
string standardOutput = "Hello!";
string errorOutput = "Oh no!";
terminalReporter.TestCompleted(testNodeUid: "PassedTest1", "PassedTest1", TestOutcome.Passed, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "SkippedTest1", "SkippedTest1", TestOutcome.Skipped, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
// timed out + canceled + failed should all report as failed in summary
terminalReporter.TestCompleted(testNodeUid: "TimedoutTest1", "TimedoutTest1", TestOutcome.Timeout, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "CanceledTest1", "CanceledTest1", TestOutcome.Canceled, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "FailedTest1", "FailedTest1", TestOutcome.Fail, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: "Tests failed", exception: new StackTraceException(@$" at FailingTest() in {folder}codefile.cs:line 10"), expected: "ABC", actual: "DEF", standardOutput, errorOutput);
terminalReporter.ArtifactAdded(outOfProcess: true, testName: null, @$"{folder}artifact1.txt");
terminalReporter.ArtifactAdded(outOfProcess: false, testName: null, @$"{folder}artifact2.txt");
terminalReporter.AssemblyRunCompleted();
terminalReporter.TestExecutionCompleted(endTime);
string output = stringBuilderConsole.Output;
string expected = $"""
passed PassedTest1 (10s 000ms)
Standard output
Hello!
Error output
Oh no!
skipped SkippedTest1 (10s 000ms)
Standard output
Hello!
Error output
Oh no!
failed (canceled) TimedoutTest1 (10s 000ms)
Standard output
Hello!
Error output
Oh no!
failed (canceled) CanceledTest1 (10s 000ms)
Standard output
Hello!
Error output
Oh no!
failed FailedTest1 (10s 000ms)
Tests failed
Expected
ABC
Actual
DEF
at FailingTest() in {folder}codefile.cs:10
Standard output
Hello!
Error output
Oh no!
Out of process file artifacts produced:
- {folder}artifact1.txt
In process file artifacts produced:
- {folder}artifact2.txt
Test run summary: Failed! - {assembly} (net8.0|x64)
total: 5
failed: 3
succeeded: 1
skipped: 1
duration: 3652058d 23h 59m 59s 999ms
""";
Assert.AreEqual(expected, ShowEscape(output));
}
[TestMethod]
public void SimpleAnsiTerminal_OutputFormattingIsCorrect()
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
// Like if we autodetect that we are in CI (e.g. by looking at TF_BUILD, and we don't disable ANSI.
UseAnsi = true,
UseCIAnsi = true,
ForceAnsi = true,
ShowProgress = () => false,
});
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: false);
string folder = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\" : "/mnt/work/";
string folderNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work" : "/mnt/work";
string folderLink = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work/" : "mnt/work/";
string folderLinkNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work" : "mnt/work";
terminalReporter.AssemblyRunStarted();
string standardOutput = "Hello!";
string errorOutput = "Oh no!";
terminalReporter.TestCompleted(testNodeUid: "PassedTest1", "PassedTest1", TestOutcome.Passed, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "SkippedTest1", "SkippedTest1", TestOutcome.Skipped, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
// timed out + canceled + failed should all report as failed in summary
terminalReporter.TestCompleted(testNodeUid: "TimedoutTest1", "TimedoutTest1", TestOutcome.Timeout, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "CanceledTest1", "CanceledTest1", TestOutcome.Canceled, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "FailedTest1", "FailedTest1", TestOutcome.Fail, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: "Tests failed", exception: new StackTraceException(@$" at FailingTest() in {folder}codefile.cs:line 10"), expected: "ABC", actual: "DEF", standardOutput, errorOutput);
terminalReporter.ArtifactAdded(outOfProcess: true, testName: null, @$"{folder}artifact1.txt");
terminalReporter.ArtifactAdded(outOfProcess: false, testName: null, @$"{folder}artifact2.txt");
terminalReporter.AssemblyRunCompleted();
terminalReporter.TestExecutionCompleted(endTime);
string output = stringBuilderConsole.Output;
string expected = $"""
␛[32mpassed␛[m PassedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
␛[90m Hello!
␛[90m Error output
␛[90m Oh no!
␛[m␛[33mskipped␛[m SkippedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
␛[90m Hello!
␛[90m Error output
␛[90m Oh no!
␛[m␛[31mfailed (canceled)␛[m TimedoutTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
␛[90m Hello!
␛[90m Error output
␛[90m Oh no!
␛[m␛[31mfailed (canceled)␛[m CanceledTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
␛[90m Hello!
␛[90m Error output
␛[90m Oh no!
␛[m␛[31mfailed␛[m FailedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[31m Tests failed
␛[m␛[31m Expected
␛[31m ABC
␛[31m Actual
␛[31m DEF
␛[m␛[90m at FailingTest() in {folder}codefile.cs:10␛[90m
␛[m␛[90m Standard output
␛[90m Hello!
␛[90m Error output
␛[90m Oh no!
␛[m
Out of process file artifacts produced:
- {folder}artifact1.txt
In process file artifacts produced:
- {folder}artifact2.txt
␛[31mTest run summary: Failed!␛[90m - ␛[m{folder}assembly.dll (net8.0|x64)
␛[m total: 5
␛[31m failed: 3
␛[m succeeded: 1
skipped: 1
duration: 3652058d 23h 59m 59s 999ms
""";
Assert.AreEqual(expected, ShowEscape(output));
}
[TestMethod]
public void AnsiTerminal_OutputFormattingIsCorrect()
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
// Like if we autodetect that we are in ANSI capable terminal.
UseAnsi = true,
UseCIAnsi = false,
ForceAnsi = true,
ShowProgress = () => false,
});
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: false);
string folder = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\" : "/mnt/work/";
string folderNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work" : "/mnt/work";
string folderLink = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work/" : "mnt/work/";
string folderLinkNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work" : "mnt/work";
terminalReporter.AssemblyRunStarted();
string standardOutput = "Hello!";
string errorOutput = "Oh no!";
terminalReporter.TestCompleted(testNodeUid: "PassedTest1", "PassedTest1", TestOutcome.Passed, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "SkippedTest1", "SkippedTest1", TestOutcome.Skipped, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
// timed out + canceled + failed should all report as failed in summary
terminalReporter.TestCompleted(testNodeUid: "TimedoutTest1", "TimedoutTest1", TestOutcome.Timeout, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "CanceledTest1", "CanceledTest1", TestOutcome.Canceled, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "FailedTest1", "FailedTest1", TestOutcome.Fail, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: "Tests failed", exception: new StackTraceException(@$" at FailingTest() in {folder}codefile.cs:line 10"), expected: "ABC", actual: "DEF", standardOutput, errorOutput);
terminalReporter.ArtifactAdded(outOfProcess: true, testName: null, @$"{folder}artifact1.txt");
terminalReporter.ArtifactAdded(outOfProcess: false, testName: null, @$"{folder}artifact2.txt");
terminalReporter.AssemblyRunCompleted();
terminalReporter.TestExecutionCompleted(endTime);
string output = stringBuilderConsole.Output;
string expected = $"""
␛[32mpassed␛[m PassedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m␛[33mskipped␛[m SkippedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m␛[31mfailed (canceled)␛[m TimedoutTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m␛[31mfailed (canceled)␛[m CanceledTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m␛[31mfailed␛[m FailedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[31m Tests failed
␛[m␛[31m Expected
ABC
Actual
DEF
␛[m␛[90m at FailingTest() in ␛[90m␛]8;;file:///{folderLink}codefile.cs␛\{folder}codefile.cs:10␛]8;;␛\␛[m␛[90m
␛[m␛[90m Standard output
Hello!
Error output
Oh no!
␛[m
Out of process file artifacts produced:
- ␛[90m␛]8;;file:///{folderLink}artifact1.txt␛\{folder}artifact1.txt␛]8;;␛\␛[m
In process file artifacts produced:
- ␛[90m␛]8;;file:///{folderLink}artifact2.txt␛\{folder}artifact2.txt␛]8;;␛\␛[m
␛[31mTest run summary: Failed!␛[90m - ␛[m␛[90m␛]8;;file:///{folderLinkNoSlash}␛\{folder}assembly.dll␛]8;;␛\␛[m (net8.0|x64)
␛[m total: 5
␛[31m failed: 3
␛[m succeeded: 1
skipped: 1
duration: 3652058d 23h 59m 59s 999ms
""";
Assert.AreEqual(expected, ShowEscape(output));
}
[TestMethod]
public void AnsiTerminal_OutputProgressFrameIsCorrect()
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var stopwatchFactory = new StopwatchFactory();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
// Like if we autodetect that we are in ANSI capable terminal.
UseAnsi = true,
UseCIAnsi = false,
ForceAnsi = true,
ShowActiveTests = true,
ShowProgress = () => true,
})
{
CreateStopwatch = stopwatchFactory.CreateStopwatch,
};
var startHandle = new AutoResetEvent(initialState: false);
var stopHandle = new AutoResetEvent(initialState: false);
// Note: Ensure that we disable the timer updates, so that we can have deterministic output.
terminalReporter.OnProgressStartUpdate += (sender, args) => startHandle.WaitOne();
terminalReporter.OnProgressStopUpdate += (sender, args) => stopHandle.Set();
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: false);
string folder = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\" : "/mnt/work/";
string folderNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work" : "/mnt/work";
string folderLink = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work/" : "mnt/work/";
string folderLinkNoSlash = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:/work" : "mnt/work";
terminalReporter.AssemblyRunStarted();
string standardOutput = "Hello!";
string errorOutput = "Oh no!";
// Note: Add 1ms to make the order of the progress frame deterministic.
// Otherwise all tests that run for 1m31s could show in any order.
terminalReporter.TestInProgress(testNodeUid: "PassedTest1", displayName: "PassedTest1");
stopwatchFactory.AddTime(TimeSpan.FromMilliseconds(1));
terminalReporter.TestInProgress(testNodeUid: "SkippedTest1", displayName: "SkippedTest1");
stopwatchFactory.AddTime(TimeSpan.FromMilliseconds(1));
terminalReporter.TestInProgress(testNodeUid: "InProgressTest1", displayName: "InProgressTest1");
stopwatchFactory.AddTime(TimeSpan.FromMinutes(1));
terminalReporter.TestInProgress(testNodeUid: "InProgressTest2", displayName: "InProgressTest2");
stopwatchFactory.AddTime(TimeSpan.FromSeconds(30));
terminalReporter.TestInProgress(testNodeUid: "InProgressTest3", displayName: "InProgressTest3");
stopwatchFactory.AddTime(TimeSpan.FromSeconds(1));
terminalReporter.TestCompleted(testNodeUid: "PassedTest1", "PassedTest1", TestOutcome.Passed, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
terminalReporter.TestCompleted(testNodeUid: "SkippedTest1", "SkippedTest1", TestOutcome.Skipped, TimeSpan.FromSeconds(10),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput, errorOutput);
string output = stringBuilderConsole.Output;
startHandle.Set();
stopHandle.WaitOne();
// Note: On MacOS the busy indicator is not rendered.
bool useBusyIndicator = !RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
string busyIndicatorString = useBusyIndicator ? "␛]9;4;3;␛\\" : string.Empty;
// Note: The progress is drawn after each completed event.
string expected = $"""
{busyIndicatorString}␛[?25l␛[32mpassed␛[m PassedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m
[␛[32m✓1␛[m/␛[31mx0␛[m/␛[33m↓0␛[m] assembly.dll (net8.0|x64)␛[2147483640G(1m 31s)
SkippedTest1␛[2147483640G(1m 31s)
InProgressTest1␛[2147483640G(1m 31s)
InProgressTest2␛[2147483643G(31s)
InProgressTest3␛[2147483644G(1s)
␛[7F
␛[J␛[33mskipped␛[m SkippedTest1␛[90m ␛[90m(10s 000ms)␛[m
␛[90m Standard output
Hello!
Error output
Oh no!
␛[m
[␛[32m✓1␛[m/␛[31mx0␛[m/␛[33m↓1␛[m] assembly.dll (net8.0|x64)␛[2147483640G(1m 31s)
InProgressTest1␛[2147483640G(1m 31s)
InProgressTest2␛[2147483643G(31s)
InProgressTest3␛[2147483644G(1s)
""";
Assert.AreEqual(expected, ShowEscape(output));
}
private static string? ShowEscape(string? text)
{
string visibleEsc = "\x241b";
return text?.Replace(AnsiCodes.Esc, visibleEsc);
}
internal sealed class StopwatchFactory
{
private TimeSpan _currentTime = TimeSpan.Zero;
public void AddTime(TimeSpan time) => _currentTime += time;
public IStopwatch CreateStopwatch() => new MockStopwatch(this, _currentTime);
internal sealed class MockStopwatch : IStopwatch
{
private readonly StopwatchFactory _factory;
public MockStopwatch(StopwatchFactory factory, TimeSpan startTime)
{
_factory = factory;
StartTime = startTime;
}
private TimeSpan StartTime { get; }
public TimeSpan Elapsed => _factory._currentTime - StartTime;
public void Start()
{
}
public void Stop()
{
}
}
}
internal class StringBuilderConsole : IConsole
{
private readonly StringBuilder _output = new();
public int BufferHeight => int.MaxValue;
public int BufferWidth => int.MinValue;
public bool IsOutputRedirected => false;
public string Output => _output.ToString();
public event ConsoleCancelEventHandler? CancelKeyPress = (sender, e) => { };
public void Clear() => throw new NotImplementedException();
public ConsoleColor GetForegroundColor() => ConsoleColor.White;
public void SetForegroundColor(ConsoleColor color)
{
// do nothing
}
public void Write(string? value) => _output.Append(value);
public void Write(char value) => _output.Append(value);
public void WriteLine() => _output.AppendLine();
public void WriteLine(string? value) => _output.AppendLine(value);
}
internal class StringBuilderTerminal : ITerminal
{
private readonly StringBuilder _stringBuilder;
public StringBuilderTerminal()
=> _stringBuilder = new();
public string Output => _stringBuilder.ToString();
public int Width => throw new NotImplementedException();
public int Height => throw new NotImplementedException();
public void Append(char value) => _stringBuilder.Append(value);
public void Append(string value) => _stringBuilder.Append(value);
public void AppendLine() => _stringBuilder.AppendLine();
public void AppendLine(string value) => _stringBuilder.AppendLine(value);
public void AppendLink(string path, int? lineNumber)
{
_stringBuilder.Append(path);
_stringBuilder.Append(':');
_stringBuilder.Append(lineNumber);
}
public void EraseProgress() => throw new NotImplementedException();
public void HideCursor() => throw new NotImplementedException();
public void RenderProgress(TestProgressState?[] progress) => throw new NotImplementedException();
public void ResetColor()
{
// do nothing
}
public void SetColor(TerminalColor color)
{
// do nothing
}
public void ShowCursor() => throw new NotImplementedException();
public void StartBusyIndicator() => throw new NotImplementedException();
public void StartUpdate() => throw new NotImplementedException();
public void StopBusyIndicator() => throw new NotImplementedException();
public void StopUpdate() => throw new NotImplementedException();
}
private class StackTraceException : Exception
{
public StackTraceException(string stackTrace) => StackTrace = stackTrace;
public override string? StackTrace { get; }
}
// Test data for all C0 control characters (U+0000-U+001F) that are normalized
[DataRow('\x0000', '\x2400', "NULL")]
[DataRow('\x0001', '\x2401', "START OF HEADING")]
[DataRow('\x0002', '\x2402', "START OF TEXT")]
[DataRow('\x0003', '\x2403', "END OF TEXT")]
[DataRow('\x0004', '\x2404', "END OF TRANSMISSION")]
[DataRow('\x0005', '\x2405', "ENQUIRY")]
[DataRow('\x0006', '\x2406', "ACKNOWLEDGE")]
[DataRow('\x0007', '\x2407', "BELL")]
[DataRow('\x0008', '\x2408', "BACKSPACE")]
[DataRow('\t', '\x2409', "TAB")]
[DataRow('\n', '\x240A', "LINE FEED")]
[DataRow('\x000B', '\x240B', "VERTICAL TAB")]
[DataRow('\x000C', '\x240C', "FORM FEED")]
[DataRow('\r', '\x240D', "CARRIAGE RETURN")]
[DataRow('\x000E', '\x240E', "SHIFT OUT")]
[DataRow('\x000F', '\x240F', "SHIFT IN")]
[DataRow('\x0010', '\x2410', "DATA LINK ESCAPE")]
[DataRow('\x0011', '\x2411', "DEVICE CONTROL ONE")]
[DataRow('\x0012', '\x2412', "DEVICE CONTROL TWO")]
[DataRow('\x0013', '\x2413', "DEVICE CONTROL THREE")]
[DataRow('\x0014', '\x2414', "DEVICE CONTROL FOUR")]
[DataRow('\x0015', '\x2415', "NEGATIVE ACKNOWLEDGE")]
[DataRow('\x0016', '\x2416', "SYNCHRONOUS IDLE")]
[DataRow('\x0017', '\x2417', "END OF TRANSMISSION BLOCK")]
[DataRow('\x0018', '\x2418', "CANCEL")]
[DataRow('\x0019', '\x2419', "END OF MEDIUM")]
[DataRow('\x001A', '\x241A', "SUBSTITUTE")]
[DataRow('\x001B', '\x241B', "ESCAPE")]
[DataRow('\x001C', '\x241C', "FILE SEPARATOR")]
[DataRow('\x001D', '\x241D', "GROUP SEPARATOR")]
[DataRow('\x001E', '\x241E', "RECORD SEPARATOR")]
[DataRow('\x001F', '\x241F', "UNIT SEPARATOR")]
[TestMethod]
public void TestDisplayNames_WithControlCharacters_AreNormalized(char controlChar, char expectedChar, string charName)
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
UseAnsi = false,
ShowProgress = () => false,
});
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: false);
terminalReporter.AssemblyRunStarted();
// Test display name with the specific control character
string testDisplayName = $"Test{controlChar}Name";
terminalReporter.TestCompleted(testNodeUid: "Test1", testDisplayName, TestOutcome.Passed, TimeSpan.FromSeconds(1),
informativeMessage: null, errorMessage: null, exception: null, expected: null, actual: null, standardOutput: null, errorOutput: null);
terminalReporter.AssemblyRunCompleted();
terminalReporter.TestExecutionCompleted(endTime);
string output = stringBuilderConsole.Output;
// Verify that the control character is replaced with its Unicode control picture
string normalizedDisplayName = $"Test{expectedChar}Name";
Assert.Contains(normalizedDisplayName, output, $"{charName} should be replaced with {expectedChar}");
// Verify that the literal control character is not present in the test display name
// Note: We skip this assertion for whitespace characters (\t, \n, \r) because these
// characters naturally appear in console output formatting (e.g., line breaks between tests)
// and asserting their complete absence would cause false positives
string literalDisplayName = $"Test{controlChar}Name";
bool isWhitespaceChar = controlChar is '\t' or '\n' or '\r';
if (!isWhitespaceChar)
{
Assert.DoesNotContain(literalDisplayName, output, $"Literal {charName} should not be present in test display name");
}
}
// Test data for all C0 control characters (U+0000-U+001F) that are normalized
[DataRow('\x0000', '\x2400', "NULL")]
[DataRow('\x0001', '\x2401', "START OF HEADING")]
[DataRow('\x0002', '\x2402', "START OF TEXT")]
[DataRow('\x0003', '\x2403', "END OF TEXT")]
[DataRow('\x0004', '\x2404', "END OF TRANSMISSION")]
[DataRow('\x0005', '\x2405', "ENQUIRY")]
[DataRow('\x0006', '\x2406', "ACKNOWLEDGE")]
[DataRow('\x0007', '\x2407', "BELL")]
[DataRow('\x0008', '\x2408', "BACKSPACE")]
[DataRow('\t', '\x2409', "TAB")]
[DataRow('\n', '\x240A', "LINE FEED")]
[DataRow('\x000B', '\x240B', "VERTICAL TAB")]
[DataRow('\x000C', '\x240C', "FORM FEED")]
[DataRow('\r', '\x240D', "CARRIAGE RETURN")]
[DataRow('\x000E', '\x240E', "SHIFT OUT")]
[DataRow('\x000F', '\x240F', "SHIFT IN")]
[DataRow('\x0010', '\x2410', "DATA LINK ESCAPE")]
[DataRow('\x0011', '\x2411', "DEVICE CONTROL ONE")]
[DataRow('\x0012', '\x2412', "DEVICE CONTROL TWO")]
[DataRow('\x0013', '\x2413', "DEVICE CONTROL THREE")]
[DataRow('\x0014', '\x2414', "DEVICE CONTROL FOUR")]
[DataRow('\x0015', '\x2415', "NEGATIVE ACKNOWLEDGE")]
[DataRow('\x0016', '\x2416', "SYNCHRONOUS IDLE")]
[DataRow('\x0017', '\x2417', "END OF TRANSMISSION BLOCK")]
[DataRow('\x0018', '\x2418', "CANCEL")]
[DataRow('\x0019', '\x2419', "END OF MEDIUM")]
[DataRow('\x001A', '\x241A', "SUBSTITUTE")]
[DataRow('\x001B', '\x241B', "ESCAPE")]
[DataRow('\x001C', '\x241C', "FILE SEPARATOR")]
[DataRow('\x001D', '\x241D', "GROUP SEPARATOR")]
[DataRow('\x001E', '\x241E', "RECORD SEPARATOR")]
[DataRow('\x001F', '\x241F', "UNIT SEPARATOR")]
[TestMethod]
public void TestDiscovery_WithControlCharacters_AreNormalized(char controlChar, char expectedChar, string charName)
{
string targetFramework = "net8.0";
string architecture = "x64";
string assembly = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"C:\work\assembly.dll" : "/mnt/work/assembly.dll";
var stringBuilderConsole = new StringBuilderConsole();
var terminalReporter = new TerminalTestReporter(assembly, targetFramework, architecture, stringBuilderConsole, new CTRLPlusCCancellationTokenSource(), new TerminalTestReporterOptions
{
ShowPassedTests = () => true,
UseAnsi = false,
ShowProgress = () => false,
});
DateTimeOffset startTime = DateTimeOffset.MinValue;
DateTimeOffset endTime = DateTimeOffset.MaxValue;
terminalReporter.TestExecutionStarted(startTime, 1, isDiscovery: true);
terminalReporter.AssemblyRunStarted();
// Test discovery with the specific control character
string testDisplayName = $"Test{controlChar}Name";
terminalReporter.TestDiscovered(testDisplayName);
terminalReporter.AssemblyRunCompleted();
terminalReporter.TestExecutionCompleted(endTime);
string output = stringBuilderConsole.Output;
// Verify that the control character is replaced with its Unicode control picture
string normalizedDisplayName = $"Test{expectedChar}Name";
Assert.Contains(normalizedDisplayName, output, $"{charName} should be replaced with {expectedChar} in discovery");
// Verify that the literal control character is not present in the test display name
// Note: We skip this assertion for whitespace characters (\t, \n, \r) because these
// characters naturally appear in console output formatting (e.g., line breaks between tests)
// and asserting their complete absence would cause false positives
string literalDisplayName = $"Test{controlChar}Name";
bool isWhitespaceChar = controlChar is '\t' or '\n' or '\r';
if (!isWhitespaceChar)
{
Assert.DoesNotContain(literalDisplayName, output, $"Literal {charName} should not be present in test display name");
}
}
}