Skip to content

Commit 752e7c0

Browse files
committed
Add standalone test for environment variables
Tests all environment variable APIs in std.process
1 parent b2cc408 commit 752e7c0

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

test/standalone/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
.empty_env = .{
9797
.path = "empty_env",
9898
},
99+
.env_vars = .{
100+
.path = "env_vars",
101+
},
99102
.issue_11595 = .{
100103
.path = "issue_11595",
101104
},

test/standalone/env_vars/build.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
4+
pub 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

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
4+
// Note: the environment variables under test are set by the build.zig
5+
pub 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+
}

0 commit comments

Comments
 (0)