aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorMatthew Lugg <mlugg@mlugg.co.uk>2025-10-28 12:42:05 +0000
committerMatthew Lugg <mlugg@mlugg.co.uk>2025-10-30 09:31:28 +0000
commit74931fe25cdd94e1cd08b5ece9dcce19959bc079 (patch)
tree75449b1d594d55a872aeb03f2dd3be16ac396274 /lib/std/testing.zig
parent74c23a237ef5245b63eb06b832a511aabeb715c0 (diff)
downloadzig-74931fe25cdd94e1cd08b5ece9dcce19959bc079.tar.gz
zig-74931fe25cdd94e1cd08b5ece9dcce19959bc079.zip
std.debug.lockStderrWriter: also return ttyconf
`std.Io.tty.Config.detect` may be an expensive check (e.g. involving syscalls), and doing it every time we need to print isn't really necessary; under normal usage, we can compute the value once and cache it for the whole program's execution. Since anyone outputting to stderr may reasonably want this information (in fact they are very likely to), it makes sense to cache it and return it from `lockStderrWriter`. Call sites who do not need it will experience no significant overhead, and can just ignore the TTY config with a `const w, _` destructure.
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig8
1 files changed, 4 insertions, 4 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 7cef6f9c58..b99542e7e5 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -355,7 +355,7 @@ test expectApproxEqRel {
/// This function is intended to be used only in tests. When the two slices are not
/// equal, prints diagnostics to stderr to show exactly how they are not equal (with
/// the differences highlighted in red), then returns a test failure error.
-/// The colorized output is optional and controlled by the return of `std.Io.tty.detectConfig()`.
+/// The colorized output is optional and controlled by the return of `std.Io.tty.Config.detect`.
/// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead.
pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) !void {
const diff_index: usize = diff_index: {
@@ -367,9 +367,9 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const
break :diff_index if (expected.len == actual.len) return else shortest;
};
if (!backend_can_print) return error.TestExpectedEqual;
- const stderr_w = std.debug.lockStderrWriter(&.{});
+ const stderr_w, const ttyconf = std.debug.lockStderrWriter(&.{});
defer std.debug.unlockStderrWriter();
- failEqualSlices(T, expected, actual, diff_index, stderr_w) catch {};
+ failEqualSlices(T, expected, actual, diff_index, stderr_w, ttyconf) catch {};
return error.TestExpectedEqual;
}
@@ -379,6 +379,7 @@ fn failEqualSlices(
actual: []const T,
diff_index: usize,
w: *std.Io.Writer,
+ ttyconf: std.Io.tty.Config,
) !void {
try w.print("slices differ. first difference occurs at index {d} (0x{X})\n", .{ diff_index, diff_index });
@@ -398,7 +399,6 @@ fn failEqualSlices(
const actual_window = actual[window_start..@min(actual.len, window_start + max_window_size)];
const actual_truncated = window_start + actual_window.len < actual.len;
- const ttyconf = std.Io.tty.detectConfig(.stderr());
var differ = if (T == u8) BytesDiffer{
.expected = expected_window,
.actual = actual_window,