std.testing: remove tight coupling with executing zig as child process

This tight coupling causes problems for various targets, requires
hacky "get args" functionality, and bungles relative file system paths,
making invalid assumptions about the zig-cache directory.

In short, these are not unit tests; these should be standalone tests
instead.

Reverts e5d4a694ea
Reverts d976456ef6
Reverts dbbda0f41a
Closes #11542
This commit is contained in:
Andrew Kelley 2022-05-26 16:22:47 -07:00
parent ba426f0a54
commit 67d5bfefba
2 changed files with 0 additions and 139 deletions

View File

@ -1323,91 +1323,3 @@ test "createNullDelimitedEnvMap" {
}
}
}
const childstr =
\\ const std = @import("std");
\\ const builtin = @import("builtin");
\\ pub fn main() !void {
\\ var it = try std.process.argsWithAllocator(std.testing.allocator);
\\ defer it.deinit(); // no-op unless WASI or Windows
\\ _ = it.next() orelse unreachable; // skip binary name
\\ const input = it.next() orelse unreachable;
\\ var expect_helloworld = "hello world".*;
\\ try std.testing.expect(std.mem.eql(u8, &expect_helloworld, input));
\\ try std.testing.expect(it.next() == null);
\\ try std.testing.expect(!it.skip());
\\ }
;
test "build and call child_process" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
const testing = std.testing;
const allocator = testing.allocator;
var it = try std.process.argsWithAllocator(allocator);
defer it.deinit(); // no-op unless WASI or Windows
const testargs = try testing.getTestArgs(&it);
var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
defer tmp.cleanup();
const tmpdirpath = try tmp.getFullPath(allocator);
defer allocator.free(tmpdirpath);
const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin'
const suffix_zig = ".zig";
const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name });
defer allocator.free(child_path);
const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig });
defer allocator.free(child_zig);
try tmp.dir.writeFile("child.zig", childstr);
try testing.buildExe(testargs.zigexec, child_zig, child_path);
// spawn compiled file as child_process with argument 'hello world' + expect success
const args = [_][]const u8{ child_path, "hello world" };
var child_proc = ChildProcess.init(&args, allocator);
const ret_val = try child_proc.spawnAndWait();
try testing.expectEqual(ret_val, .{ .Exited = 0 });
}
test "creating a child process with stdin and stdout behavior set to StdIo.Pipe" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
const testing = std.testing;
const allocator = testing.allocator;
var child_process = std.ChildProcess.init(
&[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" },
allocator,
);
child_process.stdin_behavior = .Pipe;
child_process.stdout_behavior = .Pipe;
try child_process.spawn();
const input_program =
\\ const std = @import("std");
\\ pub fn main() void {
\\ std.debug.print("Hello World", .{});
\\ }
;
try child_process.stdin.?.writer().writeAll(input_program);
child_process.stdin.?.close();
child_process.stdin = null;
const out_bytes = try child_process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(out_bytes);
switch (try child_process.wait()) {
.Exited => |code| if (code == 0) {
const expected_program =
\\const std = @import("std");
\\pub fn main() void {
\\ std.debug.print("Hello World", .{});
\\}
\\
;
try testing.expectEqualStrings(expected_program, out_bytes);
},
else => unreachable,
}
}

View File

@ -355,19 +355,6 @@ pub const TmpDir = struct {
const random_bytes_count = 12;
const sub_path_len = std.fs.base64_encoder.calcSize(random_bytes_count);
/// caller owns memory
pub fn getFullPath(self: *TmpDir, alloc: std.mem.Allocator) ![]u8 {
const cwd_str = try std.process.getCwdAlloc(alloc);
defer alloc.free(cwd_str);
const path = try std.fs.path.join(alloc, &[_][]const u8{
cwd_str,
"zig-cache",
"tmp",
&self.sub_path,
});
return path;
}
pub fn cleanup(self: *TmpDir) void {
self.dir.close();
self.parent_dir.deleteTree(&self.sub_path) catch {};
@ -413,44 +400,6 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
};
}
const TestArgs = struct {
testexec: [:0]const u8 = undefined,
zigexec: [:0]const u8 = undefined,
};
/// Get test arguments inside test block by regular test runner ('zig test file.zig')
/// Caller must provide backing ArgIterator
pub fn getTestArgs(it: *std.process.ArgIterator) !TestArgs {
var testargs = TestArgs{};
testargs.testexec = it.next() orelse unreachable;
testargs.zigexec = it.next() orelse unreachable;
try expect(!it.skip());
return testargs;
}
test "getTestArgs" {
var it = try std.process.argsWithAllocator(allocator);
const testargs = try getTestArgs(&it);
defer it.deinit(); // no-op unless WASI or Windows
try expect(testargs.testexec.len > 0); // zig compiler executable path
try expect(testargs.zigexec.len > 0); // test runner executable path
}
/// Spawns child process with 'zigexec build-exe zigfile -femit-bin=binfile'
/// and expects success
pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) !void {
const flag_emit = "-femit-bin=";
const cmd_emit = try std.mem.concat(allocator, u8, &[_][]const u8{ flag_emit, binfile });
defer allocator.free(cmd_emit);
const args = [_][]const u8{ zigexec, "build-exe", zigfile, cmd_emit };
var procCompileChild = std.ChildProcess.init(&args, allocator);
try procCompileChild.spawn();
const ret_val = try procCompileChild.wait();
try expectEqual(ret_val, .{ .Exited = 0 });
}
test "expectEqual nested array" {
const a = [2][2]f32{
[_]f32{ 1.0, 0.0 },