From 5c8b92db7f6e99d075af1cf87d48a5af0c748603 Mon Sep 17 00:00:00 2001 From: mlugg Date: Thu, 5 Jun 2025 12:13:31 +0100 Subject: [PATCH] tests: do not require absolute paths from the build system File arguments added to `std.Build.Step.Run` with e.g. `addFileArg` are not necessarily passed as absolute paths. It used to be the case that they were as a consequence of an unnecessary path conversion done by the frontend, but this no longer happens, at least not always, so these tests were sometimes failing when run locally. Therefore, the standalone tests must handle cwd-relative CLI paths correctly. --- test/standalone/dirname/exists_in.zig | 7 +------ test/standalone/dirname/has_basename.zig | 5 ----- test/standalone/dirname/touch.zig | 7 +------ test/standalone/run_output_caching/main.zig | 2 +- test/standalone/self_exe_symlink/create-symlink.zig | 5 ++++- 5 files changed, 7 insertions(+), 19 deletions(-) diff --git a/test/standalone/dirname/exists_in.zig b/test/standalone/dirname/exists_in.zig index 6730200b3ff5750e70b275460e7bb824122db3fe..7aec1f423dce8f1d61fc67d2036b00ccab36223b 100644 --- a/test/standalone/dirname/exists_in.zig +++ b/test/standalone/dirname/exists_in.zig @@ -29,17 +29,12 @@ fn run(allocator: std.mem.Allocator) !void { return error.BadUsage; }; - if (!std.fs.path.isAbsolute(dir_path)) { - std.log.err("expected to be an absolute path", .{}); - return error.BadUsage; - } - const relpath = args.next() orelse { std.log.err("missing argument", .{}); return error.BadUsage; }; - var dir = try std.fs.openDirAbsolute(dir_path, .{}); + var dir = try std.fs.cwd().openDir(dir_path, .{}); defer dir.close(); _ = try dir.statFile(relpath); diff --git a/test/standalone/dirname/has_basename.zig b/test/standalone/dirname/has_basename.zig index 84f473a07c03ec7ecc259f7f61c1e33decff0569..49eefa3b48e9b79b5794c17a1334602465a07090 100644 --- a/test/standalone/dirname/has_basename.zig +++ b/test/standalone/dirname/has_basename.zig @@ -31,11 +31,6 @@ fn run(allocator: std.mem.Allocator) !void { return error.BadUsage; }; - if (!std.fs.path.isAbsolute(path)) { - std.log.err("path must be absolute", .{}); - return error.BadUsage; - } - const basename = args.next() orelse { std.log.err("missing argument", .{}); return error.BadUsage; diff --git a/test/standalone/dirname/touch.zig b/test/standalone/dirname/touch.zig index 3ca714a3af247e3bfb33e682879959bf23c7a01e..43fcabf91efab37c8975c9f432dcdb21ae12a8e1 100644 --- a/test/standalone/dirname/touch.zig +++ b/test/standalone/dirname/touch.zig @@ -26,15 +26,10 @@ fn run(allocator: std.mem.Allocator) !void { return error.BadUsage; }; - if (!std.fs.path.isAbsolute(path)) { - std.log.err("path must be absolute: {s}", .{path}); - return error.BadUsage; - } - const dir_path = std.fs.path.dirname(path) orelse unreachable; const basename = std.fs.path.basename(path); - var dir = try std.fs.openDirAbsolute(dir_path, .{}); + var dir = try std.fs.cwd().openDir(dir_path, .{}); defer dir.close(); _ = dir.statFile(basename) catch { diff --git a/test/standalone/run_output_caching/main.zig b/test/standalone/run_output_caching/main.zig index f801971e832721746bbdbf0b064b939cc2d37eda..e4e6332f1128095edfe3b77927dc81dbd170f7a1 100644 --- a/test/standalone/run_output_caching/main.zig +++ b/test/standalone/run_output_caching/main.zig @@ -4,7 +4,7 @@ pub fn main() !void { var args = try std.process.argsWithAllocator(std.heap.page_allocator); _ = args.skip(); const filename = args.next().?; - const file = try std.fs.createFileAbsolute(filename, .{}); + const file = try std.fs.cwd().createFile(filename, .{}); defer file.close(); try file.writeAll(filename); } diff --git a/test/standalone/self_exe_symlink/create-symlink.zig b/test/standalone/self_exe_symlink/create-symlink.zig index 7bc36df8fe3091235da3d4b88933731f2ce3391d..dac5891ba89ff396a1e0517485ff427b44bd90c2 100644 --- a/test/standalone/self_exe_symlink/create-symlink.zig +++ b/test/standalone/self_exe_symlink/create-symlink.zig @@ -11,5 +11,8 @@ pub fn main() anyerror!void { const exe_path = it.next() orelse unreachable; const symlink_path = it.next() orelse unreachable; - try std.fs.cwd().symLink(exe_path, symlink_path, .{}); + // If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`. + const exe_rel_path = try std.fs.path.relative(allocator, std.fs.path.dirname(symlink_path) orelse ".", exe_path); + defer allocator.free(exe_rel_path); + try std.fs.cwd().symLink(exe_rel_path, symlink_path, .{}); } -- 2.54.0