Skip to content

unit tests that write files to cwd have racy failures #14968

@andrewrk

Description

@andrewrk

Extracted from #14647.

zig/lib/std/os/test.zig

Lines 24 to 29 in b4d58e9

test "chdir smoke test" {
if (native_os == .wasi) return error.SkipZigTest;
// Get current working directory path
var old_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
const old_cwd = try os.getcwd(old_cwd_buf[0..]);

zig/lib/std/os/test.zig

Lines 173 to 181 in b4d58e9

test "symlink with relative paths" {
if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
const cwd = fs.cwd();
cwd.deleteFile("file.txt") catch {};
cwd.deleteFile("symlinked") catch {};
// First, try relative paths in cwd
try cwd.writeFile("file.txt", "nonsense");

zig/lib/std/os/test.zig

Lines 263 to 276 in b4d58e9

test "linkat with different directories" {
if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
switch (native_os) {
.wasi, .linux, .solaris => {},
else => return error.SkipZigTest,
}
var cwd = fs.cwd();
var tmp = tmpDir(.{});
cwd.deleteFile("example.txt") catch {};
tmp.dir.deleteFile("new.txt") catch {};
try cwd.writeFile("example.txt", "example");

There are more in this file too.

These tests are all incorrect because they write garbage to the current working directory. This is racy because multiple instances of unit tests may be running at once (with different build options, or for different targets, for example).

An alternative solution to this would be to have the test runner create a new temporary directory for each independent unit test, and change to that directory as the current working directory before each unit test run. But that seems like a waste of (the computer's) time, so probably the tests should just be modified to not do problematic things.

Example failure:

run test std-native-Debug-bare-multi-default: error: 'test.symlink with relative paths' failed: C:\actions-runner\_work\zig\zig\lib\std\os\windows.zig:138:39: 0x7ff61b94f337 in OpenFile (test.exe.obj)
            .OBJECT_NAME_COLLISION => return error.PathAlreadyExists,
                                      ^
C:\actions-runner\_work\zig\zig\lib\std\os\windows.zig:738:21: 0x7ff61bd7c633 in CreateSymbolicLink (test.exe.obj)
        else => |e| return e,
                    ^
C:\actions-runner\_work\zig\zig\lib\std\os\test.zig:196:21: 0x7ff61bd7c07f in test.symlink with relative paths (test.exe.obj)
            else => return err,
                    ^
run test std-native-Debug-bare-multi-default: error: the following test command failed:
C:\actions-runner\_work\zig\zig\build-release\zig-local-cache\o\32dc1975e40afd93fbbf8a70ff27fd84\test.exe --listen=- 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions