| 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(init: std.process.Init) !void { |
| 6 | @setEvalBranchQuota(10000); |
| 7 | |
| 8 | const allocator = init.gpa; |
| 9 | const arena = init.arena.allocator(); |
| 10 | const environ = init.minimal.environ; |
| 11 | |
| 12 | // containsUnempty |
| 13 | { |
| 14 | try std.testing.expect(try environ.containsUnempty(allocator, "FOO")); |
| 15 | try std.testing.expect(!(try environ.containsUnempty(allocator, "FOO="))); |
| 16 | try std.testing.expect(!(try environ.containsUnempty(allocator, "FO"))); |
| 17 | try std.testing.expect(!(try environ.containsUnempty(allocator, "FOOO"))); |
| 18 | if (builtin.os.tag == .windows) { |
| 19 | try std.testing.expect(try environ.containsUnempty(allocator, "foo")); |
| 20 | } |
| 21 | try std.testing.expect(try environ.containsUnempty(allocator, "EQUALS")); |
| 22 | try std.testing.expect(!(try environ.containsUnempty(allocator, "EQUALS=ABC"))); |
| 23 | try std.testing.expect(try environ.containsUnempty(allocator, "КИРиллИЦА")); |
| 24 | if (builtin.os.tag == .windows) { |
| 25 | try std.testing.expect(try environ.containsUnempty(allocator, "кирИЛЛица")); |
| 26 | } |
| 27 | try std.testing.expect(!(try environ.containsUnempty(allocator, "NO_VALUE"))); |
| 28 | try std.testing.expect(!(try environ.containsUnempty(allocator, "NOT_SET"))); |
| 29 | if (builtin.os.tag == .windows) { |
| 30 | try std.testing.expect(try environ.containsUnempty(allocator, "=HIDDEN")); |
| 31 | try std.testing.expect(try environ.containsUnempty(allocator, "INVALID_UTF16_\xed\xa0\x80")); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // containsUnemptyConstant |
| 36 | { |
| 37 | try std.testing.expect(environ.containsUnemptyConstant("FOO")); |
| 38 | try std.testing.expect(!environ.containsUnemptyConstant("FOO=")); |
| 39 | try std.testing.expect(!environ.containsUnemptyConstant("FO")); |
| 40 | try std.testing.expect(!environ.containsUnemptyConstant("FOOO")); |
| 41 | if (builtin.os.tag == .windows) { |
| 42 | try std.testing.expect(environ.containsUnemptyConstant("foo")); |
| 43 | } |
| 44 | try std.testing.expect(environ.containsUnemptyConstant("EQUALS")); |
| 45 | try std.testing.expect(!environ.containsUnemptyConstant("EQUALS=ABC")); |
| 46 | try std.testing.expect(environ.containsUnemptyConstant("КИРиллИЦА")); |
| 47 | if (builtin.os.tag == .windows) { |
| 48 | try std.testing.expect(environ.containsUnemptyConstant("кирИЛЛица")); |
| 49 | } |
| 50 | try std.testing.expect(!(environ.containsUnemptyConstant("NO_VALUE"))); |
| 51 | try std.testing.expect(!(environ.containsUnemptyConstant("NOT_SET"))); |
| 52 | if (builtin.os.tag == .windows) { |
| 53 | try std.testing.expect(environ.containsUnemptyConstant("=HIDDEN")); |
| 54 | try std.testing.expect(environ.containsUnemptyConstant("INVALID_UTF16_\xed\xa0\x80")); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // contains |
| 59 | { |
| 60 | try std.testing.expect(try environ.contains(allocator, "FOO")); |
| 61 | try std.testing.expect(!(try environ.contains(allocator, "FOO="))); |
| 62 | try std.testing.expect(!(try environ.contains(allocator, "FO"))); |
| 63 | try std.testing.expect(!(try environ.contains(allocator, "FOOO"))); |
| 64 | if (builtin.os.tag == .windows) { |
| 65 | try std.testing.expect(try environ.contains(allocator, "foo")); |
| 66 | } |
| 67 | try std.testing.expect(try environ.contains(allocator, "EQUALS")); |
| 68 | try std.testing.expect(!(try environ.contains(allocator, "EQUALS=ABC"))); |
| 69 | try std.testing.expect(try environ.contains(allocator, "КИРиллИЦА")); |
| 70 | if (builtin.os.tag == .windows) { |
| 71 | try std.testing.expect(try environ.contains(allocator, "кирИЛЛица")); |
| 72 | } |
| 73 | try std.testing.expect(try environ.contains(allocator, "NO_VALUE")); |
| 74 | try std.testing.expect(!(try environ.contains(allocator, "NOT_SET"))); |
| 75 | if (builtin.os.tag == .windows) { |
| 76 | try std.testing.expect(try environ.contains(allocator, "=HIDDEN")); |
| 77 | try std.testing.expect(try environ.contains(allocator, "INVALID_UTF16_\xed\xa0\x80")); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // containsConstant |
| 82 | { |
| 83 | try std.testing.expect(environ.containsConstant("FOO")); |
| 84 | try std.testing.expect(!environ.containsConstant("FOO=")); |
| 85 | try std.testing.expect(!environ.containsConstant("FO")); |
| 86 | try std.testing.expect(!environ.containsConstant("FOOO")); |
| 87 | if (builtin.os.tag == .windows) { |
| 88 | try std.testing.expect(environ.containsConstant("foo")); |
| 89 | } |
| 90 | try std.testing.expect(environ.containsConstant("EQUALS")); |
| 91 | try std.testing.expect(!environ.containsConstant("EQUALS=ABC")); |
| 92 | try std.testing.expect(environ.containsConstant("КИРиллИЦА")); |
| 93 | if (builtin.os.tag == .windows) { |
| 94 | try std.testing.expect(environ.containsConstant("кирИЛЛица")); |
| 95 | } |
| 96 | try std.testing.expect(environ.containsConstant("NO_VALUE")); |
| 97 | try std.testing.expect(!(environ.containsConstant("NOT_SET"))); |
| 98 | if (builtin.os.tag == .windows) { |
| 99 | try std.testing.expect(environ.containsConstant("=HIDDEN")); |
| 100 | try std.testing.expect(environ.containsConstant("INVALID_UTF16_\xed\xa0\x80")); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // getAlloc |
| 105 | { |
| 106 | try std.testing.expectEqualSlices(u8, "123", try environ.getAlloc(arena, "FOO")); |
| 107 | try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FOO=")); |
| 108 | try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FO")); |
| 109 | try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FOOO")); |
| 110 | if (builtin.os.tag == .windows) { |
| 111 | try std.testing.expectEqualSlices(u8, "123", try environ.getAlloc(arena, "foo")); |
| 112 | } |
| 113 | try std.testing.expectEqualSlices(u8, "ABC=123", try environ.getAlloc(arena, "EQUALS")); |
| 114 | try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "EQUALS=ABC")); |
| 115 | try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try environ.getAlloc(arena, "КИРиллИЦА")); |
| 116 | if (builtin.os.tag == .windows) { |
| 117 | try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try environ.getAlloc(arena, "кирИЛЛица")); |
| 118 | } |
| 119 | try std.testing.expectEqualSlices(u8, "", try environ.getAlloc(arena, "NO_VALUE")); |
| 120 | try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "NOT_SET")); |
| 121 | if (builtin.os.tag == .windows) { |
| 122 | try std.testing.expectEqualSlices(u8, "hi", try environ.getAlloc(arena, "=HIDDEN")); |
| 123 | try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", try environ.getAlloc(arena, "INVALID_UTF16_\xed\xa0\x80")); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Environ.Map |
| 128 | { |
| 129 | var environ_map = try environ.createMap(allocator); |
| 130 | defer environ_map.deinit(); |
| 131 | |
| 132 | try std.testing.expectEqualSlices(u8, "123", environ_map.get("FOO").?); |
| 133 | try std.testing.expectEqual(null, environ_map.get("FO")); |
| 134 | try std.testing.expectEqual(null, environ_map.get("FOOO")); |
| 135 | if (builtin.os.tag == .windows) { |
| 136 | try std.testing.expectEqualSlices(u8, "123", environ_map.get("foo").?); |
| 137 | } |
| 138 | try std.testing.expectEqualSlices(u8, "ABC=123", environ_map.get("EQUALS").?); |
| 139 | try std.testing.expectEqual(null, environ_map.get("EQUALS=ABC")); |
| 140 | try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", environ_map.get("КИРиллИЦА").?); |
| 141 | if (builtin.os.tag == .windows) { |
| 142 | try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", environ_map.get("кирИЛЛица").?); |
| 143 | } |
| 144 | try std.testing.expectEqualSlices(u8, "", environ_map.get("NO_VALUE").?); |
| 145 | try std.testing.expectEqual(null, environ_map.get("NOT_SET")); |
| 146 | if (builtin.os.tag == .windows) { |
| 147 | try std.testing.expectEqualSlices(u8, "hi", environ_map.get("=HIDDEN").?); |
| 148 | try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", environ_map.get("INVALID_UTF16_\xed\xa0\x80").?); |
| 149 | } |
| 150 | } |
| 151 | } |