aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/posix/getenv.zig
blob: 8b28d09ae76d0d91cf60a41a6f0a4d6a94083378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// test getting environment variables

const std = @import("std");
const builtin = @import("builtin");

pub fn main() !void {
    if (builtin.target.os.tag == .windows) {
        return; // Windows env strings are WTF-16, so not supported by Zig's std.posix.getenv()
    }

    if (builtin.target.os.tag == .wasi and !builtin.link_libc) {
        return; // std.posix.getenv is not supported on WASI due to the need of allocation
    }

    // Test some unset env vars:

    try std.testing.expectEqual(std.posix.getenv(""), null);
    try std.testing.expectEqual(std.posix.getenv("BOGUSDOESNOTEXISTENVVAR"), null);
    try std.testing.expectEqual(std.posix.getenvZ("BOGUSDOESNOTEXISTENVVAR"), null);

    if (builtin.link_libc) {
        // Test if USER matches what C library sees
        const expected = std.mem.span(std.c.getenv("USER") orelse "");
        const actual = std.posix.getenv("USER") orelse "";
        try std.testing.expectEqualStrings(expected, actual);
    }

    // env vars set by our build.zig run step:
    try std.testing.expectEqualStrings("", std.posix.getenv("ZIG_TEST_POSIX_EMPTY") orelse "invalid");
    try std.testing.expectEqualStrings("test=variable", std.posix.getenv("ZIG_TEST_POSIX_1EQ") orelse "invalid");
    try std.testing.expectEqualStrings("=test=variable=", std.posix.getenv("ZIG_TEST_POSIX_3EQ") orelse "invalid");
}