| 1 | //! test getting environment variables |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const builtin = @import("builtin"); |
| 5 | |
| 6 | pub fn main(init: std.process.Init.Minimal) !void { |
| 7 | if (builtin.target.os.tag == .windows) return; |
| 8 | if (builtin.target.os.tag == .wasi and !builtin.link_libc) return; |
| 9 | |
| 10 | const environ = init.environ; |
| 11 | |
| 12 | // Test some unset env vars: |
| 13 | try std.testing.expectEqual(environ.getPosix(""), null); |
| 14 | try std.testing.expectEqual(environ.getPosix("BOGUSDOESNOTEXISTENVVAR"), null); |
| 15 | try std.testing.expectEqual(environ.getPosix("BOGUSDOESNOTEXISTENVVAR"), null); |
| 16 | |
| 17 | if (builtin.link_libc) { |
| 18 | // Test if USER matches what C library sees |
| 19 | const expected = std.mem.span(std.c.getenv("USER") orelse ""); |
| 20 | const actual = environ.getPosix("USER") orelse ""; |
| 21 | try std.testing.expectEqualStrings(expected, actual); |
| 22 | } |
| 23 | |
| 24 | // env vars set by our build.zig run step: |
| 25 | try std.testing.expectEqualStrings("", environ.getPosix("ZIG_TEST_POSIX_EMPTY") orelse "invalid"); |
| 26 | try std.testing.expectEqualStrings("test=variable", environ.getPosix("ZIG_TEST_POSIX_1EQ") orelse "invalid"); |
| 27 | try std.testing.expectEqualStrings("=test=variable=", environ.getPosix("ZIG_TEST_POSIX_3EQ") orelse "invalid"); |
| 28 | } |