Problem
when running zig test, this line in my test, std.testing.environ.createMap(gpa), fails for me with an unusual stack trace that repeats itself over and over again (but not forever), and I’m not sure what’s going on.
This is also strange because I can successfully read environment variables from inside a test using std.testing.environ.contains(gpa,"PATH").
I will post a minimal reproducable test example below. (I can also post the stack trace if needed, but I will have to edit out my name from the home directory first.)
why I’m doing this
I have a function which takes init.environ_map, and reads an environment variable to find the home directory depending on the platform. This function works as expected, but I just wanted to add a test to before some refactoring for peace of mind, so I was looking for a way to pass the environment map to the function from inside a test.
code example
const std = @import("std");
test "read environment from test" {
// setup
const gpa = std.testing.allocator;
const environ = std.testing.environ;
// can read environment variable; SUCCEEDS
const path_is_visible = try environ.contains(gpa, "PATH");
try std.testing.expect(path_is_visible);
// try creating map; FAILS
const env_map = try environ.createMap(gpa); // the line that doesn't work
_ = env_map;
}
Can anyone explain why this code doesn’t work? From what I can tell in the stdlib docs, this seems like the correct way to do this, but perhaps I’m misusing the function?
zig version: 0.16.0, installed via scoop on x86_64-windows.win11_dt