mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
standalone posix test for env vars
This commit is contained in:
parent
020eb622ee
commit
aa1d2adffc
3 changed files with 40 additions and 19 deletions
|
|
@ -607,24 +607,6 @@ test "mmap" {
|
|||
}
|
||||
}
|
||||
|
||||
test "getenv" {
|
||||
if (native_os == .wasi and !builtin.link_libc) {
|
||||
// std.posix.getenv is not supported on WASI due to the need of allocation
|
||||
return error.SkipZigTest;
|
||||
}
|
||||
|
||||
if (native_os == .windows) {
|
||||
try expect(std.process.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null);
|
||||
} else {
|
||||
try expect(posix.getenv("") == null);
|
||||
try expect(posix.getenv("BOGUSDOESNOTEXISTENVVAR") == null);
|
||||
if (builtin.link_libc) {
|
||||
try testing.expectEqualStrings(posix.getenv("USER") orelse "", mem.span(std.c.getenv("USER") orelse ""));
|
||||
}
|
||||
try expect(posix.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null);
|
||||
}
|
||||
}
|
||||
|
||||
test "fcntl" {
|
||||
if (native_os == .windows or native_os == .wasi)
|
||||
return error.SkipZigTest;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ const builtin = @import("builtin");
|
|||
|
||||
const Case = struct {
|
||||
src_path: []const u8,
|
||||
set_env_vars: bool = false,
|
||||
};
|
||||
|
||||
const cases = [_]Case{
|
||||
|
|
@ -11,6 +12,7 @@ const cases = [_]Case{
|
|||
},
|
||||
.{
|
||||
.src_path = "getenv.zig",
|
||||
.set_env_vars = true,
|
||||
},
|
||||
.{
|
||||
.src_path = "sigaction.zig",
|
||||
|
|
@ -68,5 +70,11 @@ fn run_exe(b: *std.Build, optimize: std.builtin.OptimizeMode, case: *const Case,
|
|||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
|
||||
if (case.set_env_vars) {
|
||||
run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_1EQ", "test=variable");
|
||||
run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_3EQ", "=test=variable=");
|
||||
run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_EMPTY", "");
|
||||
}
|
||||
|
||||
return run_cmd;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,32 @@
|
|||
pub fn main() !void {}
|
||||
// test getting environment variables
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub fn main() !void {
|
||||
if (builtin.target.os.tag == .windows) {
|
||||
return; // Windows env strings are WTF-16, so not supported by Zig's std.posix.getenv()
|
||||
}
|
||||
|
||||
if (builtin.target.os.tag == .wasi and !builtin.link_libc) {
|
||||
return; // std.posix.getenv is not supported on WASI due to the need of allocation
|
||||
}
|
||||
|
||||
// Test some unset env vars:
|
||||
|
||||
try std.testing.expectEqual(std.posix.getenv(""), null);
|
||||
try std.testing.expectEqual(std.posix.getenv("BOGUSDOESNOTEXISTENVVAR"), null);
|
||||
try std.testing.expectEqual(std.posix.getenvZ("BOGUSDOESNOTEXISTENVVAR"), null);
|
||||
|
||||
if (builtin.link_libc) {
|
||||
// Test if USER matches what C library sees
|
||||
const expected = std.mem.span(std.c.getenv("USER") orelse "");
|
||||
const actual = std.posix.getenv("USER") orelse "";
|
||||
try std.testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
// env vars set by our build.zig run step:
|
||||
try std.testing.expectEqualStrings("", std.posix.getenv("ZIG_TEST_POSIX_EMPTY") orelse "invalid");
|
||||
try std.testing.expectEqualStrings("test=variable", std.posix.getenv("ZIG_TEST_POSIX_1EQ") orelse "invalid");
|
||||
try std.testing.expectEqualStrings("=test=variable=", std.posix.getenv("ZIG_TEST_POSIX_3EQ") orelse "invalid");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue