authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-03-16 17:27:41-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-03-22 15:44:27-07:00
log752e7c0fd06d96450898913f6207bbe245ca12fc
tree06cf8665a47ed76fca2355f2ef123a41e65b06b2
parentb2cc408a3e5ae0f8c2550c8828f6d388c6969b5a

Add standalone test for environment variables

Tests all environment variable APIs in std.process

3 files changed, 209 insertions(+), 0 deletions(-)

test/standalone/build.zig.zon+3
...@@ -96,6 +96,9 @@...@@ -96,6 +96,9 @@
96 .empty_env = .{96 .empty_env = .{
97 .path = "empty_env",97 .path = "empty_env",
98 },98 },
99 .env_vars = .{
100 .path = "env_vars",
101 },
99 .issue_11595 = .{102 .issue_11595 = .{
100 .path = "issue_11595",103 .path = "issue_11595",
101 },104 },
test/standalone/env_vars/build.zig created+33
...@@ -0,0 +1,33 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4pub fn build(b: *std.Build) void {
5 const test_step = b.step("test", "Test it");
6 b.default_step = test_step;
7
8 const optimize: std.builtin.OptimizeMode = .Debug;
9
10 const main = b.addExecutable(.{
11 .name = "main",
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("main.zig"),
14 .target = b.graph.host,
15 .optimize = optimize,
16 }),
17 });
18
19 const run = b.addRunArtifact(main);
20 run.clearEnvironment();
21 run.setEnvironmentVariable("FOO", "123");
22 run.setEnvironmentVariable("EQUALS", "ABC=123");
23 run.setEnvironmentVariable("NO_VALUE", "");
24 run.setEnvironmentVariable("КИРиллИЦА", "non-ascii አማርኛ \u{10FFFF}");
25 if (b.graph.host.result.os.tag == .windows) {
26 run.setEnvironmentVariable("=Hidden", "hi");
27 // \xed\xa0\x80 is a WTF-8 encoded unpaired surrogate code point
28 run.setEnvironmentVariable("INVALID_UTF16_\xed\xa0\x80", "\xed\xa0\x80");
29 }
30 run.disable_zig_progress = true;
31
32 test_step.dependOn(&run.step);
33}
test/standalone/env_vars/main.zig created+173
...@@ -0,0 +1,173 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4// Note: the environment variables under test are set by the build.zig
5pub fn main() !void {
6 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
7 defer _ = gpa.deinit();
8 const allocator = gpa.allocator();
9
10 var arena_state = std.heap.ArenaAllocator.init(allocator);
11 defer arena_state.deinit();
12 const arena = arena_state.allocator();
13
14 // hasNonEmptyEnvVar
15 {
16 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "FOO"));
17 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FOO=")));
18 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FO")));
19 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "FOOO")));
20 if (builtin.os.tag == .windows) {
21 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "foo"));
22 }
23 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "EQUALS"));
24 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "EQUALS=ABC")));
25 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "КИРиллИЦА"));
26 if (builtin.os.tag == .windows) {
27 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "кирИЛЛица"));
28 }
29 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "NO_VALUE")));
30 try std.testing.expect(!(try std.process.hasNonEmptyEnvVar(allocator, "NOT_SET")));
31 if (builtin.os.tag == .windows) {
32 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "=HIDDEN"));
33 try std.testing.expect(try std.process.hasNonEmptyEnvVar(allocator, "INVALID_UTF16_\xed\xa0\x80"));
34 }
35 }
36
37 // hasNonEmptyEnvVarContstant
38 {
39 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("FOO"));
40 try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FOO="));
41 try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FO"));
42 try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("FOOO"));
43 if (builtin.os.tag == .windows) {
44 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("foo"));
45 }
46 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("EQUALS"));
47 try std.testing.expect(!std.process.hasNonEmptyEnvVarConstant("EQUALS=ABC"));
48 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("КИРиллИЦА"));
49 if (builtin.os.tag == .windows) {
50 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("кирИЛЛица"));
51 }
52 try std.testing.expect(!(std.process.hasNonEmptyEnvVarConstant("NO_VALUE")));
53 try std.testing.expect(!(std.process.hasNonEmptyEnvVarConstant("NOT_SET")));
54 if (builtin.os.tag == .windows) {
55 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("=HIDDEN"));
56 try std.testing.expect(std.process.hasNonEmptyEnvVarConstant("INVALID_UTF16_\xed\xa0\x80"));
57 }
58 }
59
60 // hasEnvVar
61 {
62 try std.testing.expect(try std.process.hasEnvVar(allocator, "FOO"));
63 try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FOO=")));
64 try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FO")));
65 try std.testing.expect(!(try std.process.hasEnvVar(allocator, "FOOO")));
66 if (builtin.os.tag == .windows) {
67 try std.testing.expect(try std.process.hasEnvVar(allocator, "foo"));
68 }
69 try std.testing.expect(try std.process.hasEnvVar(allocator, "EQUALS"));
70 try std.testing.expect(!(try std.process.hasEnvVar(allocator, "EQUALS=ABC")));
71 try std.testing.expect(try std.process.hasEnvVar(allocator, "КИРиллИЦА"));
72 if (builtin.os.tag == .windows) {
73 try std.testing.expect(try std.process.hasEnvVar(allocator, "кирИЛЛица"));
74 }
75 try std.testing.expect(try std.process.hasEnvVar(allocator, "NO_VALUE"));
76 try std.testing.expect(!(try std.process.hasEnvVar(allocator, "NOT_SET")));
77 if (builtin.os.tag == .windows) {
78 try std.testing.expect(try std.process.hasEnvVar(allocator, "=HIDDEN"));
79 try std.testing.expect(try std.process.hasEnvVar(allocator, "INVALID_UTF16_\xed\xa0\x80"));
80 }
81 }
82
83 // hasEnvVarConstant
84 {
85 try std.testing.expect(std.process.hasEnvVarConstant("FOO"));
86 try std.testing.expect(!std.process.hasEnvVarConstant("FOO="));
87 try std.testing.expect(!std.process.hasEnvVarConstant("FO"));
88 try std.testing.expect(!std.process.hasEnvVarConstant("FOOO"));
89 if (builtin.os.tag == .windows) {
90 try std.testing.expect(std.process.hasEnvVarConstant("foo"));
91 }
92 try std.testing.expect(std.process.hasEnvVarConstant("EQUALS"));
93 try std.testing.expect(!std.process.hasEnvVarConstant("EQUALS=ABC"));
94 try std.testing.expect(std.process.hasEnvVarConstant("КИРиллИЦА"));
95 if (builtin.os.tag == .windows) {
96 try std.testing.expect(std.process.hasEnvVarConstant("кирИЛЛица"));
97 }
98 try std.testing.expect(std.process.hasEnvVarConstant("NO_VALUE"));
99 try std.testing.expect(!(std.process.hasEnvVarConstant("NOT_SET")));
100 if (builtin.os.tag == .windows) {
101 try std.testing.expect(std.process.hasEnvVarConstant("=HIDDEN"));
102 try std.testing.expect(std.process.hasEnvVarConstant("INVALID_UTF16_\xed\xa0\x80"));
103 }
104 }
105
106 // getEnvVarOwned
107 {
108 try std.testing.expectEqualSlices(u8, "123", try std.process.getEnvVarOwned(arena, "FOO"));
109 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FOO="));
110 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FO"));
111 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "FOOO"));
112 if (builtin.os.tag == .windows) {
113 try std.testing.expectEqualSlices(u8, "123", try std.process.getEnvVarOwned(arena, "foo"));
114 }
115 try std.testing.expectEqualSlices(u8, "ABC=123", try std.process.getEnvVarOwned(arena, "EQUALS"));
116 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "EQUALS=ABC"));
117 try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try std.process.getEnvVarOwned(arena, "КИРиллИЦА"));
118 if (builtin.os.tag == .windows) {
119 try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try std.process.getEnvVarOwned(arena, "кирИЛЛица"));
120 }
121 try std.testing.expectEqualSlices(u8, "", try std.process.getEnvVarOwned(arena, "NO_VALUE"));
122 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.getEnvVarOwned(arena, "NOT_SET"));
123 if (builtin.os.tag == .windows) {
124 try std.testing.expectEqualSlices(u8, "hi", try std.process.getEnvVarOwned(arena, "=HIDDEN"));
125 try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", try std.process.getEnvVarOwned(arena, "INVALID_UTF16_\xed\xa0\x80"));
126 }
127 }
128
129 // parseEnvVarInt
130 {
131 try std.testing.expectEqual(123, try std.process.parseEnvVarInt("FOO", u32, 10));
132 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("FO", u32, 10));
133 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("FOOO", u32, 10));
134 try std.testing.expectEqual(0x123, try std.process.parseEnvVarInt("FOO", u32, 16));
135 if (builtin.os.tag == .windows) {
136 try std.testing.expectEqual(123, try std.process.parseEnvVarInt("foo", u32, 10));
137 }
138 try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("EQUALS", u32, 10));
139 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("EQUALS=ABC", u32, 10));
140 try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("КИРиллИЦА", u32, 10));
141 try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("NO_VALUE", u32, 10));
142 try std.testing.expectError(error.EnvironmentVariableNotFound, std.process.parseEnvVarInt("NOT_SET", u32, 10));
143 if (builtin.os.tag == .windows) {
144 try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("=HIDDEN", u32, 10));
145 try std.testing.expectError(error.InvalidCharacter, std.process.parseEnvVarInt("INVALID_UTF16_\xed\xa0\x80", u32, 10));
146 }
147 }
148
149 // EnvMap
150 {
151 var env_map = try std.process.getEnvMap(allocator);
152 defer env_map.deinit();
153
154 try std.testing.expectEqualSlices(u8, "123", env_map.get("FOO").?);
155 try std.testing.expectEqual(null, env_map.get("FO"));
156 try std.testing.expectEqual(null, env_map.get("FOOO"));
157 if (builtin.os.tag == .windows) {
158 try std.testing.expectEqualSlices(u8, "123", env_map.get("foo").?);
159 }
160 try std.testing.expectEqualSlices(u8, "ABC=123", env_map.get("EQUALS").?);
161 try std.testing.expectEqual(null, env_map.get("EQUALS=ABC"));
162 try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", env_map.get("КИРиллИЦА").?);
163 if (builtin.os.tag == .windows) {
164 try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", env_map.get("кирИЛЛица").?);
165 }
166 try std.testing.expectEqualSlices(u8, "", env_map.get("NO_VALUE").?);
167 try std.testing.expectEqual(null, env_map.get("NOT_SET"));
168 if (builtin.os.tag == .windows) {
169 try std.testing.expectEqualSlices(u8, "hi", env_map.get("=HIDDEN").?);
170 try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", env_map.get("INVALID_UTF16_\xed\xa0\x80").?);
171 }
172 }
173}