-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Windows: Faster getenvW
and a standalone environment variable test
#23272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
423761b
createWindowsEnvBlock: Reduce NUL terminator count to only what's req…
squeek502 b2cc408
std.process: Allow WTF-8 in env var functions with comptime-known keys
squeek502 752e7c0
Add standalone test for environment variables
squeek502 78ecf3b
windows: Document Environment pointer
squeek502 66dcebc
getenvW: Take advantage of sliceTo/indexOfScalarPos optimizations
squeek502 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const test_step = b.step("test", "Test it"); | ||
b.default_step = test_step; | ||
|
||
const optimize: std.builtin.OptimizeMode = .Debug; | ||
|
||
const main = b.addExecutable(.{ | ||
.name = "main", | ||
.root_module = b.createModule(.{ | ||
.root_source_file = b.path("main.zig"), | ||
.target = b.graph.host, | ||
.optimize = optimize, | ||
}), | ||
}); | ||
|
||
const run = b.addRunArtifact(main); | ||
run.clearEnvironment(); | ||
run.setEnvironmentVariable("FOO", "123"); | ||
run.setEnvironmentVariable("EQUALS", "ABC=123"); | ||
run.setEnvironmentVariable("NO_VALUE", ""); | ||
run.setEnvironmentVariable("КИРиллИЦА", "non-ascii አማርኛ \u{10FFFF}"); | ||
if (b.graph.host.result.os.tag == .windows) { | ||
run.setEnvironmentVariable("=Hidden", "hi"); | ||
// \xed\xa0\x80 is a WTF-8 encoded unpaired surrogate code point | ||
run.setEnvironmentVariable("INVALID_UTF16_\xed\xa0\x80", "\xed\xa0\x80"); | ||
} | ||
run.disable_zig_progress = true; | ||
|
||
test_step.dependOn(&run.step); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"))); | ||
squeek502 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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").?); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.