authorgravatar for sayedmurtazamuttahary@gmail.commurtaza <sayedmurtazamuttahary@gmail.com> 2026-01-15 23:34:56+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-17 00:40:22+01:00
logb5770541bdc10311125a06ab2ef1b7f3546cea7a
tree0fe86cadab60847fe058c008b3dc2493bc5c4db7
parent790d28d6cd9029805af857d56645a6ce191bfe91

testing: ability to read environment variables from unit tests


2 files changed, 5 insertions(+), 0 deletions(-)

lib/compiler/test_runner.zig+2
...@@ -129,6 +129,7 @@ fn mainServer(init: std.process.Init.Minimal) !void {...@@ -129,6 +129,7 @@ fn mainServer(init: std.process.Init.Minimal) !void {
129 },129 },
130130
131 .run_test => {131 .run_test => {
132 testing.environ = init.environ;
132 testing.allocator_instance = .{};133 testing.allocator_instance = .{};
133 testing.io_instance = .init(testing.allocator, .{134 testing.io_instance = .init(testing.allocator, .{
134 .argv0 = .init(init.args),135 .argv0 = .init(init.args),
...@@ -244,6 +245,7 @@ fn mainTerminal(init: std.process.Init.Minimal) void {...@@ -244,6 +245,7 @@ fn mainTerminal(init: std.process.Init.Minimal) void {
244 if (testing.allocator_instance.deinit() == .leak) leaks += 1;245 if (testing.allocator_instance.deinit() == .leak) leaks += 1;
245 }246 }
246 testing.log_level = .warn;247 testing.log_level = .warn;
248 testing.environ = init.environ;
247249
248 const test_node = root_node.start(test_fn.name, 0);250 const test_node = root_node.start(test_fn.name, 0);
249 if (!have_tty) {251 if (!have_tty) {
lib/std/testing.zig+3
...@@ -2,6 +2,7 @@ const builtin = @import("builtin");...@@ -2,6 +2,7 @@ const builtin = @import("builtin");
22
3const std = @import("std.zig");3const std = @import("std.zig");
4const Io = std.Io;4const Io = std.Io;
5const Environ = std.process.Environ;
5const assert = std.debug.assert;6const assert = std.debug.assert;
6const math = std.math;7const math = std.math;
78
...@@ -33,6 +34,8 @@ pub var allocator_instance: std.heap.GeneralPurposeAllocator(.{...@@ -33,6 +34,8 @@ pub var allocator_instance: std.heap.GeneralPurposeAllocator(.{
33pub var io_instance: Io.Threaded = undefined;34pub var io_instance: Io.Threaded = undefined;
34pub const io = if (builtin.is_test) io_instance.io() else @compileError("not testing");35pub const io = if (builtin.is_test) io_instance.io() else @compileError("not testing");
3536
37pub var environ: Environ = if (builtin.is_test) undefined else @compileError("not testing");
38
36/// TODO https://github.com/ziglang/zig/issues/573839/// TODO https://github.com/ziglang/zig/issues/5738
37pub var log_level = std.log.Level.warn;40pub var log_level = std.log.Level.warn;
3841