From 1590ed9d6aea95e5a21e3455e8edba4cdb374f2c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 29 Dec 2020 00:33:08 -0700 Subject: [PATCH] stage2 tests: pass cwd to child process to fix exe path Previous commit broke the tests for non-Windows because we were intending to change the cwd when running the child process. However, for Windows we don't support passing a directory handle for cwd when spawning child processes yet. However on Linux we do. This commit reverts the previous one but then fixes things for all systems by passing both cwd_dir and cwd to the child process. --- src/test.zig | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/test.zig b/src/test.zig index 519cc4bf50..5681468e10 100644 --- a/src/test.zig +++ b/src/test.zig @@ -561,10 +561,10 @@ pub const TestContext = struct { var cache_dir = try tmp.dir.makeOpenPath("zig-cache", .{}); defer cache_dir.close(); - const tmp_path = try std.fs.path.join(arena, &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path }); + const tmp_dir_path = try std.fs.path.join(arena, &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path }); const zig_cache_directory: Compilation.Directory = .{ .handle = cache_dir, - .path = try std.fs.path.join(arena, &[_][]const u8{ tmp_path, "zig-cache" }), + .path = try std.fs.path.join(arena, &[_][]const u8{ tmp_dir_path, "zig-cache" }), }; const tmp_src_path = switch (case.extension) { @@ -573,7 +573,7 @@ pub const TestContext = struct { }; var root_pkg: Package = .{ - .root_src_directory = .{ .path = tmp_path, .handle = tmp.dir }, + .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir }, .root_src_path = tmp_src_path, }; @@ -585,7 +585,7 @@ pub const TestContext = struct { }); const emit_directory: Compilation.Directory = .{ - .path = tmp_path, + .path = tmp_dir_path, .handle = tmp.dir, }; const emit_bin: Compilation.EmitLoc = .{ @@ -771,7 +771,9 @@ pub const TestContext = struct { exec_node.activate(); defer exec_node.end(); - const exe_path = try emit_directory.join(arena, &[_][]const u8{bin_name}); + // We use relative to cwd here because we pass a new cwd to the + // child process. + const exe_path = try std.fmt.allocPrint(arena, "." ++ std.fs.path.sep_str ++ "{s}", .{bin_name}); if (case.object_format != null and case.object_format.? == .c) { try argv.appendSlice(&[_][]const u8{ std.testing.zig_exe_path, "run", exe_path, "-lc", @@ -824,11 +826,18 @@ pub const TestContext = struct { try comp.makeBinFileExecutable(); - break :x try std.ChildProcess.exec(.{ + break :x std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv.items, .cwd_dir = tmp.dir, - }); + .cwd = tmp_dir_path, + }) catch |err| { + std.debug.print("\nThe following command failed with {s}:\n", .{ + @errorName(err), + }); + dumpArgs(argv.items); + return error.ZigTestFailed; + }; }; var test_node = update_node.start("test", 0); test_node.activate(); @@ -976,4 +985,4 @@ fn dumpArgs(argv: []const []const u8) void { std.debug.print("{s} ", .{arg}); } std.debug.print("\n", .{}); -} \ No newline at end of file +}