authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-29 00:33:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-29 00:33:08-07:00
log1590ed9d6aea95e5a21e3455e8edba4cdb374f2c
tree509e9a697be358d82553937943fdf71841e7c0a2
parent7e1e771f0260b5fd0a5cd2f7e3d669979d8f141b

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.

1 files changed, 17 insertions(+), 8 deletions(-)

src/test.zig+17-8
...@@ -561,10 +561,10 @@ pub const TestContext = struct {...@@ -561,10 +561,10 @@ pub const TestContext = struct {
561561
562 var cache_dir = try tmp.dir.makeOpenPath("zig-cache", .{});562 var cache_dir = try tmp.dir.makeOpenPath("zig-cache", .{});
563 defer cache_dir.close();563 defer cache_dir.close();
564 const tmp_path = try std.fs.path.join(arena, &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path });564 const tmp_dir_path = try std.fs.path.join(arena, &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path });
565 const zig_cache_directory: Compilation.Directory = .{565 const zig_cache_directory: Compilation.Directory = .{
566 .handle = cache_dir,566 .handle = cache_dir,
567 .path = try std.fs.path.join(arena, &[_][]const u8{ tmp_path, "zig-cache" }),567 .path = try std.fs.path.join(arena, &[_][]const u8{ tmp_dir_path, "zig-cache" }),
568 };568 };
569569
570 const tmp_src_path = switch (case.extension) {570 const tmp_src_path = switch (case.extension) {
...@@ -573,7 +573,7 @@ pub const TestContext = struct {...@@ -573,7 +573,7 @@ pub const TestContext = struct {
573 };573 };
574574
575 var root_pkg: Package = .{575 var root_pkg: Package = .{
576 .root_src_directory = .{ .path = tmp_path, .handle = tmp.dir },576 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
577 .root_src_path = tmp_src_path,577 .root_src_path = tmp_src_path,
578 };578 };
579579
...@@ -585,7 +585,7 @@ pub const TestContext = struct {...@@ -585,7 +585,7 @@ pub const TestContext = struct {
585 });585 });
586586
587 const emit_directory: Compilation.Directory = .{587 const emit_directory: Compilation.Directory = .{
588 .path = tmp_path,588 .path = tmp_dir_path,
589 .handle = tmp.dir,589 .handle = tmp.dir,
590 };590 };
591 const emit_bin: Compilation.EmitLoc = .{591 const emit_bin: Compilation.EmitLoc = .{
...@@ -771,7 +771,9 @@ pub const TestContext = struct {...@@ -771,7 +771,9 @@ pub const TestContext = struct {
771 exec_node.activate();771 exec_node.activate();
772 defer exec_node.end();772 defer exec_node.end();
773773
774 const exe_path = try emit_directory.join(arena, &[_][]const u8{bin_name});774 // We use relative to cwd here because we pass a new cwd to the
775 // child process.
776 const exe_path = try std.fmt.allocPrint(arena, "." ++ std.fs.path.sep_str ++ "{s}", .{bin_name});
775 if (case.object_format != null and case.object_format.? == .c) {777 if (case.object_format != null and case.object_format.? == .c) {
776 try argv.appendSlice(&[_][]const u8{778 try argv.appendSlice(&[_][]const u8{
777 std.testing.zig_exe_path, "run", exe_path, "-lc",779 std.testing.zig_exe_path, "run", exe_path, "-lc",
...@@ -824,11 +826,18 @@ pub const TestContext = struct {...@@ -824,11 +826,18 @@ pub const TestContext = struct {
824826
825 try comp.makeBinFileExecutable();827 try comp.makeBinFileExecutable();
826828
827 break :x try std.ChildProcess.exec(.{829 break :x std.ChildProcess.exec(.{
828 .allocator = allocator,830 .allocator = allocator,
829 .argv = argv.items,831 .argv = argv.items,
830 .cwd_dir = tmp.dir,832 .cwd_dir = tmp.dir,
831 });833 .cwd = tmp_dir_path,
834 }) catch |err| {
835 std.debug.print("\nThe following command failed with {s}:\n", .{
836 @errorName(err),
837 });
838 dumpArgs(argv.items);
839 return error.ZigTestFailed;
840 };
832 };841 };
833 var test_node = update_node.start("test", 0);842 var test_node = update_node.start("test", 0);
834 test_node.activate();843 test_node.activate();
...@@ -976,4 +985,4 @@ fn dumpArgs(argv: []const []const u8) void {...@@ -976,4 +985,4 @@ fn dumpArgs(argv: []const []const u8) void {
976 std.debug.print("{s} ", .{arg});985 std.debug.print("{s} ", .{arg});
977 }986 }
978 std.debug.print("\n", .{});987 std.debug.print("\n", .{});
979}
\ No newline at end of file
988}