aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/env_vars/main.zig
blob: 12b911404a7641a7bef304fed888104ddec699db (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const std = @import("std");
const builtin = @import("builtin");

// Note: the environment variables under test are set by the build.zig
pub fn main() !void {
    var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var arena_state = std.heap.ArenaAllocator.init(allocator);
    defer arena_state.deinit();
    const arena = arena_state.allocator();

    // hasNonEmptyEnvVar
    {
        try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "FOO"));
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FOO=")));
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FO")));
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FOOO")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "foo"));
        }
        try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "EQUALS"));
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "EQUALS=ABC")));
        try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "КИРиллИЦА"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "кирИЛЛица"));
        }
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "NO_VALUE")));
        try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "NOT_SET")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "=HIDDEN"));
            try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "INVALID_UTF16_\xed\xa0\x80"));
        }
    }

    // hasNonEmptyEnvVarContstant
    {
        try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("FOO"));
        try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FOO="));
        try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FO"));
        try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FOOO"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("foo"));
        }
        try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("EQUALS"));
        try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("EQUALS=ABC"));
        try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("КИРиллИЦА"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("кирИЛЛица"));
        }
        try std.testing.expect(!(std.process.hasNonEmptyEnvVarConstant("NO_VALUE")));
        try std.testing.expect(!(std.process.hasNonEmptyEnvVarConstant("NOT_SET")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("=HIDDEN"));
            try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("INVALID_UTF16_\xed\xa0\x80"));
        }
    }

    // hasEnvVar
    {
        try std.testing.expect(try std.process.hasEnvVar(allocator, "FOO"));
        try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FOO=")));
        try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FO")));
        try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FOOO")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasEnvVar(allocator, "foo"));
        }
        try std.testing.expect(try std.process.hasEnvVar(allocator, "EQUALS"));
        try std.testing.expect(!(try std.process.hasEnvVar(allocator, "EQUALS=ABC")));
        try std.testing.expect(try std.process.hasEnvVar(allocator, "КИРиллИЦА"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasEnvVar(allocator, "кирИЛЛица"));
        }
        try std.testing.expect(try std.process.hasEnvVar(allocator, "NO_VALUE"));
        try std.testing.expect(!(try std.process.hasEnvVar(allocator, "NOT_SET")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(try std.process.hasEnvVar(allocator, "=HIDDEN"));
            try std.testing.expect(try std.process.hasEnvVar(allocator, "INVALID_UTF16_\xed\xa0\x80"));
        }
    }

    // hasEnvVarConstant
    {
        try std.testing.expect(std.process.hasEnvVarConstant("FOO"));
        try std.testing.expect(!std.process.hasEnvVarConstant("FOO="));
        try std.testing.expect(!std.process.hasEnvVarConstant("FO"));
        try std.testing.expect(!std.process.hasEnvVarConstant("FOOO"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasEnvVarConstant("foo"));
        }
        try std.testing.expect(std.process.hasEnvVarConstant("EQUALS"));
        try std.testing.expect(!std.process.hasEnvVarConstant("EQUALS=ABC"));
        try std.testing.expect(std.process.hasEnvVarConstant("КИРиллИЦА"));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasEnvVarConstant("кирИЛЛица"));
        }
        try std.testing.expect(std.process.hasEnvVarConstant("NO_VALUE"));
        try std.testing.expect(!(std.process.hasEnvVarConstant("NOT_SET")));
        if (builtin.os.tag == .windows) {
            try std.testing.expect(std.process.hasEnvVarConstant("=HIDDEN"));
            try std.testing.expect(std.process.hasEnvVarConstant("INVALID_UTF16_\xed\xa0\x80"));
        }
    }

    // getEnvVarOwned
    {
        try std.testing.expectEqualSlices(u8, "123", try std.process.getEnvVarOwned(arena, "FOO"));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FOO="));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FO"));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FOOO"));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "123", try std.process.getEnvVarOwned(arena, "foo"));
        }
        try std.testing.expectEqualSlices(u8, "ABC=123", try std.process.getEnvVarOwned(arena, "EQUALS"));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "EQUALS=ABC"));
        try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try std.process.getEnvVarOwned(arena, "КИРиллИЦА"));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try std.process.getEnvVarOwned(arena, "кирИЛЛица"));
        }
        try std.testing.expectEqualSlices(u8, "", try std.process.getEnvVarOwned(arena, "NO_VALUE"));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "NOT_SET"));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "hi", try std.process.getEnvVarOwned(arena, "=HIDDEN"));
            try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", try std.process.getEnvVarOwned(arena, "INVALID_UTF16_\xed\xa0\x80"));
        }
    }

    // parseEnvVarInt
    {
        try std.testing.expectEqual(123, try std.process.parseEnvVarInt("FOO", u32, 10));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("FO", u32, 10));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("FOOO", u32, 10));
        try std.testing.expectEqual(0x123, try std.process.parseEnvVarInt("FOO", u32, 16));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqual(123, try std.process.parseEnvVarInt("foo", u32, 10));
        }
        try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("EQUALS", u32, 10));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("EQUALS=ABC", u32, 10));
        try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("КИРиллИЦА", u32, 10));
        try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("NO_VALUE", u32, 10));
        try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("NOT_SET", u32, 10));
        if (builtin.os.tag == .windows) {
            try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("=HIDDEN", u32, 10));
            try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("INVALID_UTF16_\xed\xa0\x80", u32, 10));
        }
    }

    // EnvMap
    {
        var env_map = try std.process.getEnvMap(allocator);
        defer env_map.deinit();

        try std.testing.expectEqualSlices(u8, "123", env_map.get("FOO").?);
        try std.testing.expectEqual(null, env_map.get("FO"));
        try std.testing.expectEqual(null, env_map.get("FOOO"));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "123", env_map.get("foo").?);
        }
        try std.testing.expectEqualSlices(u8, "ABC=123", env_map.get("EQUALS").?);
        try std.testing.expectEqual(null, env_map.get("EQUALS=ABC"));
        try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", env_map.get("КИРиллИЦА").?);
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", env_map.get("кирИЛЛица").?);
        }
        try std.testing.expectEqualSlices(u8, "", env_map.get("NO_VALUE").?);
        try std.testing.expectEqual(null, env_map.get("NOT_SET"));
        if (builtin.os.tag == .windows) {
            try std.testing.expectEqualSlices(u8, "hi", env_map.get("=HIDDEN").?);
            try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", env_map.get("INVALID_UTF16_\xed\xa0\x80").?);
        }
    }
}