From e1fbb24d64caa956846405bd42aed59e5b9513f2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 11 Mar 2019 11:56:08 -0400 Subject: [PATCH] add test for spawning child process with empty environment thanks to BenoitJGirard for pointing out the child process implementation needs 3 extra null bytes in #2031 --- std/build.zig | 6 ++++++ test/build_examples.zig | 1 + test/standalone/empty_env/build.zig | 12 ++++++++++++ test/standalone/empty_env/main.zig | 6 ++++++ 4 files changed, 25 insertions(+) create mode 100644 test/standalone/empty_env/build.zig create mode 100644 test/standalone/empty_env/main.zig diff --git a/std/build.zig b/std/build.zig index e7d73b37c0..8840aef6be 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1530,6 +1530,12 @@ pub const RunStep = struct { } } + pub fn clearEnvironment(self: *RunStep) void { + const new_env_map = self.builder.allocator.create(BufMap) catch unreachable; + new_env_map.* = BufMap.init(self.builder.allocator); + self.env_map = new_env_map; + } + pub fn addPathDir(self: *RunStep, search_path: []const u8) void { const PATH = if (builtin.os == builtin.Os.windows) "Path" else "PATH"; const env_map = self.getEnvMap(); diff --git a/test/build_examples.zig b/test/build_examples.zig index c51fdfdf89..e4524a0188 100644 --- a/test/build_examples.zig +++ b/test/build_examples.zig @@ -19,6 +19,7 @@ pub fn addCases(cases: *tests.BuildExamplesContext) void { cases.addBuildFile("test/standalone/pkg_import/build.zig"); cases.addBuildFile("test/standalone/use_alias/build.zig"); cases.addBuildFile("test/standalone/brace_expansion/build.zig"); + cases.addBuildFile("test/standalone/empty_env/build.zig"); if (false) { // TODO this test is disabled because it is failing on the CI server's linux. when this is fixed // enable it for at least linux diff --git a/test/standalone/empty_env/build.zig b/test/standalone/empty_env/build.zig new file mode 100644 index 0000000000..2a184dcd2e --- /dev/null +++ b/test/standalone/empty_env/build.zig @@ -0,0 +1,12 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const main = b.addExecutable("main", "main.zig"); + main.setBuildMode(b.standardReleaseOptions()); + + const run = main.run(); + run.clearEnvironment(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); +} diff --git a/test/standalone/empty_env/main.zig b/test/standalone/empty_env/main.zig new file mode 100644 index 0000000000..20b45c3137 --- /dev/null +++ b/test/standalone/empty_env/main.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +pub fn main() void { + const env_map = std.os.getEnvMap(std.debug.global_allocator) catch @panic("unable to get env map"); + std.testing.expect(env_map.count() == 0); +}