authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-09-08 22:08:04-07:00
committergravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-09-09 22:07:44-07:00
logaa1d2adffc78167b38b34afeb683c152ad5767f4
treec35d1c84febb58f345f1af8df8e7994efbd909b6
parent020eb622ee344d211946738ac1013deb60697381

standalone posix test for env vars


3 files changed, 40 insertions(+), 19 deletions(-)

lib/std/posix/test.zig-18
...@@ -607,24 +607,6 @@ test "mmap" {...@@ -607,24 +607,6 @@ test "mmap" {
607 }607 }
608}608}
609609
610test "getenv" {
611 if (native_os == .wasi and !builtin.link_libc) {
612 // std.posix.getenv is not supported on WASI due to the need of allocation
613 return error.SkipZigTest;
614 }
615
616 if (native_os == .windows) {
617 try expect(std.process.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null);
618 } else {
619 try expect(posix.getenv("") == null);
620 try expect(posix.getenv("BOGUSDOESNOTEXISTENVVAR") == null);
621 if (builtin.link_libc) {
622 try testing.expectEqualStrings(posix.getenv("USER") orelse "", mem.span(std.c.getenv("USER") orelse ""));
623 }
624 try expect(posix.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null);
625 }
626}
627
628test "fcntl" {610test "fcntl" {
629 if (native_os == .windows or native_os == .wasi)611 if (native_os == .windows or native_os == .wasi)
630 return error.SkipZigTest;612 return error.SkipZigTest;
test/standalone/posix/build.zig+8
...@@ -3,6 +3,7 @@ const builtin = @import("builtin");...@@ -3,6 +3,7 @@ const builtin = @import("builtin");
33
4const Case = struct {4const Case = struct {
5 src_path: []const u8,5 src_path: []const u8,
6 set_env_vars: bool = false,
6};7};
78
8const cases = [_]Case{9const cases = [_]Case{
...@@ -11,6 +12,7 @@ const cases = [_]Case{...@@ -11,6 +12,7 @@ const cases = [_]Case{
11 },12 },
12 .{13 .{
13 .src_path = "getenv.zig",14 .src_path = "getenv.zig",
15 .set_env_vars = true,
14 },16 },
15 .{17 .{
16 .src_path = "sigaction.zig",18 .src_path = "sigaction.zig",
...@@ -68,5 +70,11 @@ fn run_exe(b: *std.Build, optimize: std.builtin.OptimizeMode, case: *const Case,...@@ -68,5 +70,11 @@ fn run_exe(b: *std.Build, optimize: std.builtin.OptimizeMode, case: *const Case,
6870
69 const run_cmd = b.addRunArtifact(exe);71 const run_cmd = b.addRunArtifact(exe);
7072
73 if (case.set_env_vars) {
74 run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_1EQ", "test=variable");
75 run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_3EQ", "=test=variable=");
76 run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_EMPTY", "");
77 }
78
71 return run_cmd;79 return run_cmd;
72}80}
test/standalone/posix/getenv.zig+32-1
...@@ -1 +1,32 @@...@@ -1 +1,32 @@
1pub fn main() !void {}1// test getting environment variables
2
3const std = @import("std");
4const builtin = @import("builtin");
5
6pub fn main() !void {
7 if (builtin.target.os.tag == .windows) {
8 return; // Windows env strings are WTF-16, so not supported by Zig's std.posix.getenv()
9 }
10
11 if (builtin.target.os.tag == .wasi and !builtin.link_libc) {
12 return; // std.posix.getenv is not supported on WASI due to the need of allocation
13 }
14
15 // Test some unset env vars:
16
17 try std.testing.expectEqual(std.posix.getenv(""), null);
18 try std.testing.expectEqual(std.posix.getenv("BOGUSDOESNOTEXISTENVVAR"), null);
19 try std.testing.expectEqual(std.posix.getenvZ("BOGUSDOESNOTEXISTENVVAR"), null);
20
21 if (builtin.link_libc) {
22 // Test if USER matches what C library sees
23 const expected = std.mem.span(std.c.getenv("USER") orelse "");
24 const actual = std.posix.getenv("USER") orelse "";
25 try std.testing.expectEqualStrings(expected, actual);
26 }
27
28 // env vars set by our build.zig run step:
29 try std.testing.expectEqualStrings("", std.posix.getenv("ZIG_TEST_POSIX_EMPTY") orelse "invalid");
30 try std.testing.expectEqualStrings("test=variable", std.posix.getenv("ZIG_TEST_POSIX_1EQ") orelse "invalid");
31 try std.testing.expectEqualStrings("=test=variable=", std.posix.getenv("ZIG_TEST_POSIX_3EQ") orelse "invalid");
32}