testing: ability to read environment variables from unit tests

This commit is contained in:
murtaza
2026-01-15 23:34:56 +01:00
committed by Andrew Kelley
parent 790d28d6cd
commit b5770541bd
2 changed files with 5 additions and 0 deletions
+2
View File
@@ -129,6 +129,7 @@ fn mainServer(init: std.process.Init.Minimal) !void {
},
.run_test => {
testing.environ = init.environ;
testing.allocator_instance = .{};
testing.io_instance = .init(testing.allocator, .{
.argv0 = .init(init.args),
@@ -244,6 +245,7 @@ fn mainTerminal(init: std.process.Init.Minimal) void {
if (testing.allocator_instance.deinit() == .leak) leaks += 1;
}
testing.log_level = .warn;
testing.environ = init.environ;
const test_node = root_node.start(test_fn.name, 0);
if (!have_tty) {
+3
View File
@@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("std.zig");
const Io = std.Io;
const Environ = std.process.Environ;
const assert = std.debug.assert;
const math = std.math;
@@ -33,6 +34,8 @@ pub var allocator_instance: std.heap.GeneralPurposeAllocator(.{
pub var io_instance: Io.Threaded = undefined;
pub const io = if (builtin.is_test) io_instance.io() else @compileError("not testing");
pub var environ: Environ = if (builtin.is_test) undefined else @compileError("not testing");
/// TODO https://github.com/ziglang/zig/issues/5738
pub var log_level = std.log.Level.warn;