authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-18 23:44:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-18 23:45:32-07:00
log07c03f85a8e062923b07572a68acc0191c84f001
treed1cb78c94e593a47a28b47cf65cda0b53e8df1ef
parent9f9f215305389c08a21730859982b68bf2681932

zig test: fix test runner detection of tty

Before, `std.Progress` was printing unwanted stuff to stderr. Now, the test runner's logic to detect whether we should print each test as a separate line to stderr is properly activated.

1 files changed, 9 insertions(+), 6 deletions(-)

lib/std/special/test_runner.zig+9-6
...@@ -31,11 +31,14 @@ pub fn main() void {...@@ -31,11 +31,14 @@ pub fn main() void {
31 var ok_count: usize = 0;31 var ok_count: usize = 0;
32 var skip_count: usize = 0;32 var skip_count: usize = 0;
33 var fail_count: usize = 0;33 var fail_count: usize = 0;
34 var progress = std.Progress{};34 var progress = std.Progress{
35 .dont_print_on_dumb = true,
36 };
35 const root_node = progress.start("Test", test_fn_list.len) catch |err| switch (err) {37 const root_node = progress.start("Test", test_fn_list.len) catch |err| switch (err) {
36 // TODO still run tests in this case38 // TODO still run tests in this case
37 error.TimerUnsupported => @panic("timer unsupported"),39 error.TimerUnsupported => @panic("timer unsupported"),
38 };40 };
41 const have_tty = progress.terminal != null and progress.supports_ansi_escape_codes;
3942
40 var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;43 var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;
41 // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly44 // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly
...@@ -55,7 +58,7 @@ pub fn main() void {...@@ -55,7 +58,7 @@ pub fn main() void {
55 var test_node = root_node.start(test_fn.name, 0);58 var test_node = root_node.start(test_fn.name, 0);
56 test_node.activate();59 test_node.activate();
57 progress.refresh();60 progress.refresh();
58 if (progress.terminal == null) {61 if (!have_tty) {
59 std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });62 std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });
60 }63 }
61 const result = if (test_fn.async_frame_size) |size| switch (io_mode) {64 const result = if (test_fn.async_frame_size) |size| switch (io_mode) {
...@@ -71,26 +74,26 @@ pub fn main() void {...@@ -71,26 +74,26 @@ pub fn main() void {
71 skip_count += 1;74 skip_count += 1;
72 test_node.end();75 test_node.end();
73 progress.log("{s}... SKIP (async test)\n", .{test_fn.name});76 progress.log("{s}... SKIP (async test)\n", .{test_fn.name});
74 if (progress.terminal == null) std.debug.print("SKIP (async test)\n", .{});77 if (!have_tty) std.debug.print("SKIP (async test)\n", .{});
75 continue;78 continue;
76 },79 },
77 } else test_fn.func();80 } else test_fn.func();
78 if (result) |_| {81 if (result) |_| {
79 ok_count += 1;82 ok_count += 1;
80 test_node.end();83 test_node.end();
81 if (progress.terminal == null) std.debug.print("OK\n", .{});84 if (!have_tty) std.debug.print("OK\n", .{});
82 } else |err| switch (err) {85 } else |err| switch (err) {
83 error.SkipZigTest => {86 error.SkipZigTest => {
84 skip_count += 1;87 skip_count += 1;
85 test_node.end();88 test_node.end();
86 progress.log("{s}... SKIP\n", .{test_fn.name});89 progress.log("{s}... SKIP\n", .{test_fn.name});
87 if (progress.terminal == null) std.debug.print("SKIP\n", .{});90 if (!have_tty) std.debug.print("SKIP\n", .{});
88 },91 },
89 else => {92 else => {
90 fail_count += 1;93 fail_count += 1;
91 test_node.end();94 test_node.end();
92 progress.log("{s}... FAIL ({s})\n", .{ test_fn.name, @errorName(err) });95 progress.log("{s}... FAIL ({s})\n", .{ test_fn.name, @errorName(err) });
93 if (progress.terminal == null) std.debug.print("FAIL ({s})\n", .{@errorName(err)});96 if (!have_tty) std.debug.print("FAIL ({s})\n", .{@errorName(err)});
94 if (@errorReturnTrace()) |trace| {97 if (@errorReturnTrace()) |trace| {
95 std.debug.dumpStackTrace(trace.*);98 std.debug.dumpStackTrace(trace.*);
96 }99 }