|
1 | 1 | const builtin = @import("builtin"); |
| 2 | +const color = @import("color.zig"); |
2 | 3 | const std = @import("std"); |
3 | 4 |
|
4 | 5 | const Allocator = std.mem.Allocator; |
5 | 6 | const Anvil = @import("zabi").clients.Anvil; |
| 7 | +const ColorWriter = color.ColorWriter; |
6 | 8 | const FileWriter = std.fs.File.Writer; |
7 | 9 | const Options = std.Options; |
8 | | -const TerminalColors = std.io.tty.Color; |
9 | 10 | const TestFn = std.builtin.TestFn; |
10 | | -const ZigColor = std.zig.Color; |
11 | 11 |
|
12 | 12 | pub const std_options: Options = .{ |
13 | 13 | .log_level = .info, |
@@ -52,11 +52,18 @@ const Runner = struct { |
52 | 52 | } |
53 | 53 | /// Writes the test module name. |
54 | 54 | pub fn writeModule(self: *Self, module: []const u8) ColorWriterStream.Error!void { |
55 | | - try self.color_stream.writeModule(module); |
| 55 | + self.color_stream.setNextColor(.yellow); |
| 56 | + try self.color_stream.writer().print(" |{s}|", .{module}); |
| 57 | + try self.color_stream.applyReset(); |
56 | 58 | } |
57 | 59 | /// Writes the test name. |
58 | 60 | pub fn writeTestName(self: *Self, name: []const u8) ColorWriterStream.Error!void { |
59 | | - try self.color_stream.writeTestName(name); |
| 61 | + self.color_stream.setNextColor(.dim); |
| 62 | + |
| 63 | + const index = std.mem.lastIndexOf(u8, name, "test.") orelse unreachable; |
| 64 | + |
| 65 | + try self.color_stream.writer().print(" Running {s}...", .{name[index + 5 ..]}); |
| 66 | + try self.color_stream.applyReset(); |
60 | 67 | } |
61 | 68 | /// Write a success result to the stream |
62 | 69 | pub fn writeSuccess(self: *Self) ColorWriterStream.Error!void { |
@@ -88,100 +95,28 @@ const Runner = struct { |
88 | 95 | } |
89 | 96 | /// Pretty print the test results. |
90 | 97 | pub fn writeResult(self: *Self) ColorWriterStream.Error!void { |
91 | | - try self.color_stream.printResult(self.result); |
92 | | - } |
93 | | -}; |
94 | | - |
95 | | -/// Custom writer that we use to write tests result and with specific tty colors. |
96 | | -fn ColorWriter(comptime UnderlayingWriter: type) type { |
97 | | - return struct { |
98 | | - /// Set of possible errors from this writer. |
99 | | - const Error = UnderlayingWriter.Error || std.os.windows.SetConsoleTextAttributeError; |
100 | | - |
101 | | - const Writer = std.io.Writer(*Self, Error, write); |
102 | | - const Self = @This(); |
103 | | - |
104 | | - /// Initial empty state. |
105 | | - pub const empty: Self = .{ |
106 | | - .color = .auto, |
107 | | - .underlaying_writer = std.io.getStdErr().writer(), |
108 | | - .next_color = .reset, |
109 | | - }; |
110 | | - |
111 | | - /// The writer that we will use to write to. |
112 | | - underlaying_writer: UnderlayingWriter, |
113 | | - /// Zig color tty config. |
114 | | - color: ZigColor, |
115 | | - /// Next tty color to apply in the stream. |
116 | | - next_color: TerminalColors, |
117 | | - |
118 | | - pub fn writer(self: *Self) Writer { |
119 | | - return .{ .context = self }; |
120 | | - } |
121 | | - /// Write function that will write to the stream with the `next_color`. |
122 | | - pub fn write(self: *Self, bytes: []const u8) Error!usize { |
123 | | - if (bytes.len == 0) |
124 | | - return bytes.len; |
| 98 | + self.color_stream.setNextColor(.reset); |
| 99 | + try self.color_stream.writer().writeAll("\n ZABI Tests: "); |
| 100 | + self.color_stream.setNextColor(.green); |
| 101 | + try self.color_stream.writer().print("{d} passed\n", .{self.result.passed}); |
| 102 | + self.color_stream.setNextColor(.reset); |
125 | 103 |
|
126 | | - try self.applyColor(self.next_color); |
127 | | - try self.writeNoColor(bytes); |
128 | | - try self.applyColor(.reset); |
| 104 | + try self.color_stream.writer().writeAll(" ZABI Tests: "); |
| 105 | + self.color_stream.setNextColor(.red); |
| 106 | + try self.color_stream.writer().print("{d} failed\n", .{self.result.failed}); |
| 107 | + self.color_stream.setNextColor(.reset); |
129 | 108 |
|
130 | | - return bytes.len; |
131 | | - } |
132 | | - /// Writes the test module to the stream. |
133 | | - pub fn writeModule(self: *Self, module: []const u8) !void { |
134 | | - self.setNextColor(.yellow); |
135 | | - try self.applyColor(self.next_color); |
136 | | - try self.underlaying_writer.print(" |{s}|", .{module}); |
137 | | - try self.applyColor(.reset); |
138 | | - } |
139 | | - /// Writes the test name with ansi `dim`. |
140 | | - pub fn writeTestName(self: *Self, test_name: []const u8) !void { |
141 | | - self.setNextColor(.dim); |
142 | | - try self.applyColor(self.next_color); |
143 | | - try self.underlaying_writer.print(" Running {s}...", .{test_name}); |
144 | | - try self.applyColor(.reset); |
145 | | - } |
146 | | - /// Sets the next color in the stream |
147 | | - pub fn setNextColor(self: *Self, next: TerminalColors) void { |
148 | | - self.next_color = next; |
149 | | - } |
150 | | - /// Writes the next color to the stream. |
151 | | - pub fn applyColor(self: *Self, color: TerminalColors) Error!void { |
152 | | - try self.color.renderOptions().ttyconf.setColor(self.underlaying_writer, color); |
153 | | - } |
154 | | - /// Writes to the stream without colors. |
155 | | - pub fn writeNoColor(self: *Self, bytes: []const u8) UnderlayingWriter.Error!void { |
156 | | - if (bytes.len == 0) |
157 | | - return; |
| 109 | + try self.color_stream.writer().writeAll(" ZABI Tests: "); |
| 110 | + self.color_stream.setNextColor(.yellow); |
| 111 | + try self.color_stream.writer().print("{d} skipped\n", .{self.result.skipped}); |
| 112 | + self.color_stream.setNextColor(.reset); |
158 | 113 |
|
159 | | - try self.underlaying_writer.writeAll(bytes); |
160 | | - } |
161 | | - /// Prints all of the test results. |
162 | | - pub fn printResult(self: *Self, results: TestResults) Error!void { |
163 | | - try self.underlaying_writer.writeAll("\n ZABI Tests: "); |
164 | | - try self.applyColor(.green); |
165 | | - try self.underlaying_writer.print("{d} passed\n", .{results.passed}); |
166 | | - try self.applyColor(.reset); |
167 | | - |
168 | | - try self.underlaying_writer.writeAll(" ZABI Tests: "); |
169 | | - try self.applyColor(.red); |
170 | | - try self.underlaying_writer.print("{d} failed\n", .{results.failed}); |
171 | | - try self.applyColor(.reset); |
172 | | - |
173 | | - try self.underlaying_writer.writeAll(" ZABI Tests: "); |
174 | | - try self.applyColor(.yellow); |
175 | | - try self.underlaying_writer.print("{d} skipped\n", .{results.skipped}); |
176 | | - try self.applyColor(.reset); |
177 | | - |
178 | | - try self.underlaying_writer.writeAll(" ZABI Tests: "); |
179 | | - try self.applyColor(.blue); |
180 | | - try self.underlaying_writer.print("{d} leaked\n", .{results.leaked}); |
181 | | - try self.applyColor(.reset); |
182 | | - } |
183 | | - }; |
184 | | -} |
| 114 | + try self.color_stream.writer().writeAll(" ZABI Tests: "); |
| 115 | + self.color_stream.setNextColor(.blue); |
| 116 | + try self.color_stream.writer().print("{d} leaked\n", .{self.result.leaked}); |
| 117 | + self.color_stream.setNextColor(.reset); |
| 118 | + } |
| 119 | +}; |
185 | 120 |
|
186 | 121 | /// Main test runner. |
187 | 122 | pub fn main() !void { |
|
0 commit comments