| ... | @@ -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 | } |