|
| 1 | +using Asynkron.TestRunner.Models; |
| 2 | + |
| 3 | +namespace Asynkron.TestRunner; |
| 4 | + |
| 5 | +public static class ChartRenderer |
| 6 | +{ |
| 7 | + private const int BarWidth = 30; |
| 8 | + |
| 9 | + // Detect if we can use colors |
| 10 | + private static readonly bool UseColor = DetectColorSupport(); |
| 11 | + |
| 12 | + // ANSI color codes (empty strings if no color support) |
| 13 | + private static string Reset => UseColor ? "\x1b[0m" : ""; |
| 14 | + private static string Green => UseColor ? "\x1b[32m" : ""; |
| 15 | + private static string Red => UseColor ? "\x1b[31m" : ""; |
| 16 | + private static string Yellow => UseColor ? "\x1b[33m" : ""; |
| 17 | + private static string Dim => UseColor ? "\x1b[2m" : ""; |
| 18 | + private static string Bold => UseColor ? "\x1b[1m" : ""; |
| 19 | + |
| 20 | + // Bar characters - use distinct chars when no color |
| 21 | + private static char PassedChar => UseColor ? '█' : '='; |
| 22 | + private static char FailedChar => UseColor ? '█' : 'X'; |
| 23 | + private static char SkippedChar => UseColor ? '░' : '-'; |
| 24 | + private static char EmptyChar => UseColor ? '░' : '.'; |
| 25 | + |
| 26 | + private static bool DetectColorSupport() |
| 27 | + { |
| 28 | + // NO_COLOR is a standard env var to disable colors |
| 29 | + if (Environment.GetEnvironmentVariable("NO_COLOR") != null) |
| 30 | + return false; |
| 31 | + |
| 32 | + // If output is redirected (piped), don't use colors |
| 33 | + if (Console.IsOutputRedirected) |
| 34 | + return false; |
| 35 | + |
| 36 | + // Check for dumb terminal |
| 37 | + var term = Environment.GetEnvironmentVariable("TERM"); |
| 38 | + if (term == "dumb") |
| 39 | + return false; |
| 40 | + |
| 41 | + // Check CI environments that may not support colors |
| 42 | + if (Environment.GetEnvironmentVariable("CI") != null && |
| 43 | + Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == null) // GitHub Actions supports colors |
| 44 | + return false; |
| 45 | + |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + public static void RenderHistory(List<TestRunResult> results, bool showLatestFirst = true) |
| 50 | + { |
| 51 | + if (results.Count == 0) |
| 52 | + { |
| 53 | + Console.WriteLine($"{Dim}No test history found.{Reset}"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + var ordered = showLatestFirst |
| 58 | + ? results.OrderByDescending(r => r.Timestamp).ToList() |
| 59 | + : results.OrderBy(r => r.Timestamp).ToList(); |
| 60 | + |
| 61 | + var maxTotal = ordered.Max(r => r.Total); |
| 62 | + |
| 63 | + Console.WriteLine(); |
| 64 | + Console.WriteLine($"{Bold}Test History ({results.Count} runs){Reset}"); |
| 65 | + Console.WriteLine(new string('─', 70)); |
| 66 | + |
| 67 | + foreach (var result in ordered) |
| 68 | + { |
| 69 | + RenderBar(result, maxTotal); |
| 70 | + } |
| 71 | + |
| 72 | + Console.WriteLine(); |
| 73 | + } |
| 74 | + |
| 75 | + public static void RenderSingleResult(TestRunResult result, TestRunResult? previousRun = null) |
| 76 | + { |
| 77 | + Console.WriteLine(); |
| 78 | + |
| 79 | + var statusColor = result.Failed > 0 ? Red : Green; |
| 80 | + var statusText = result.Failed > 0 ? "FAILED" : "PASSED"; |
| 81 | + |
| 82 | + Console.WriteLine($"{statusColor}{Bold}{statusText}{Reset}"); |
| 83 | + Console.WriteLine(new string('─', 50)); |
| 84 | + |
| 85 | + Console.WriteLine($" {Green}Passed:{Reset} {result.Passed}"); |
| 86 | + Console.WriteLine($" {Red}Failed:{Reset} {result.Failed}"); |
| 87 | + Console.WriteLine($" {Yellow}Skipped:{Reset} {result.Skipped}"); |
| 88 | + Console.WriteLine($" {Dim}Total:{Reset} {result.Total}"); |
| 89 | + Console.WriteLine($" {Dim}Duration:{Reset} {FormatDuration(result.Duration)}"); |
| 90 | + Console.WriteLine($" {Dim}Pass Rate:{Reset} {result.PassRate:F1}%"); |
| 91 | + |
| 92 | + // Show regressions if we have a previous run to compare |
| 93 | + if (previousRun != null) |
| 94 | + { |
| 95 | + RenderRegressions(result, previousRun); |
| 96 | + } |
| 97 | + |
| 98 | + Console.WriteLine(); |
| 99 | + } |
| 100 | + |
| 101 | + public static void RenderRegressions(TestRunResult current, TestRunResult previous) |
| 102 | + { |
| 103 | + var regressions = current.GetRegressions(previous); |
| 104 | + var fixes = current.GetFixes(previous); |
| 105 | + |
| 106 | + if (regressions.Count > 0) |
| 107 | + { |
| 108 | + Console.WriteLine(); |
| 109 | + Console.WriteLine($"{Red}{Bold}Regressions ({regressions.Count}):{Reset}"); |
| 110 | + foreach (var test in regressions.Take(20)) |
| 111 | + { |
| 112 | + Console.WriteLine($" {Red}✗{Reset} {test}"); |
| 113 | + } |
| 114 | + if (regressions.Count > 20) |
| 115 | + { |
| 116 | + Console.WriteLine($" {Dim}... and {regressions.Count - 20} more{Reset}"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (fixes.Count > 0) |
| 121 | + { |
| 122 | + Console.WriteLine(); |
| 123 | + Console.WriteLine($"{Green}{Bold}Fixed ({fixes.Count}):{Reset}"); |
| 124 | + foreach (var test in fixes.Take(20)) |
| 125 | + { |
| 126 | + Console.WriteLine($" {Green}✓{Reset} {test}"); |
| 127 | + } |
| 128 | + if (fixes.Count > 20) |
| 129 | + { |
| 130 | + Console.WriteLine($" {Dim}... and {fixes.Count - 20} more{Reset}"); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void RenderBar(TestRunResult result, int maxTotal) |
| 136 | + { |
| 137 | + var timestamp = result.Timestamp.ToString("yyyy-MM-dd HH:mm"); |
| 138 | + var passedWidth = maxTotal > 0 ? (int)((double)result.Passed / maxTotal * BarWidth) : 0; |
| 139 | + var failedWidth = maxTotal > 0 ? (int)((double)result.Failed / maxTotal * BarWidth) : 0; |
| 140 | + var skippedWidth = maxTotal > 0 ? (int)((double)result.Skipped / maxTotal * BarWidth) : 0; |
| 141 | + |
| 142 | + // Ensure at least 1 char for failed if there are failures |
| 143 | + if (result.Failed > 0 && failedWidth == 0) failedWidth = 1; |
| 144 | + |
| 145 | + var passedBar = new string(PassedChar, passedWidth); |
| 146 | + var failedBar = new string(FailedChar, failedWidth); |
| 147 | + var skippedBar = new string(SkippedChar, skippedWidth); |
| 148 | + var emptyWidth = Math.Max(0, BarWidth - passedWidth - failedWidth - skippedWidth); |
| 149 | + var emptyBar = new string(EmptyChar, emptyWidth); |
| 150 | + |
| 151 | + var failedIndicator = result.Failed > 0 |
| 152 | + ? $"{Red}✗{result.Failed}{Reset}" |
| 153 | + : $"{Green}✓{Reset}"; |
| 154 | + |
| 155 | + Console.Write($"{Dim}{timestamp}{Reset} "); |
| 156 | + Console.Write($"{Green}{passedBar}{Reset}"); |
| 157 | + Console.Write($"{Red}{failedBar}{Reset}"); |
| 158 | + Console.Write($"{Yellow}{skippedBar}{Reset}"); |
| 159 | + Console.Write($"{Dim}{emptyBar}{Reset}"); |
| 160 | + Console.Write($" {result.Passed}/{result.Total} ({result.PassRate:F1}%) "); |
| 161 | + Console.WriteLine(failedIndicator); |
| 162 | + } |
| 163 | + |
| 164 | + private static string FormatDuration(TimeSpan duration) |
| 165 | + { |
| 166 | + if (duration.TotalMinutes >= 1) |
| 167 | + return $"{duration.TotalMinutes:F1}m"; |
| 168 | + return $"{duration.TotalSeconds:F1}s"; |
| 169 | + } |
| 170 | +} |
0 commit comments