aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/child_process/child.zig
blob: 2e74f30882fd76684c80303287cc19a29c6cfb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const std = @import("std");
const Io = std.Io;

// 42 is expected by parent; other values result in test failure
var exit_code: u8 = 42;

pub fn main() !void {
    var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    const arena = arena_state.allocator();

    var threaded: std.Io.Threaded = .init(arena);
    defer threaded.deinit();
    const io = threaded.io();

    try run(arena, io);
    arena_state.deinit();
    std.process.exit(exit_code);
}

fn run(allocator: std.mem.Allocator, io: Io) !void {
    var args = try std.process.argsWithAllocator(allocator);
    defer args.deinit();
    _ = args.next() orelse unreachable; // skip binary name

    // test cmd args
    const hello_arg = "hello arg";
    const a1 = args.next() orelse unreachable;
    if (!std.mem.eql(u8, a1, hello_arg)) {
        testError("first arg: '{s}'; want '{s}'", .{ a1, hello_arg });
    }
    if (args.next()) |a2| {
        testError("expected only one arg; got more: {s}", .{a2});
    }

    // test stdout pipe; parent verifies
    try std.fs.File.stdout().writeAll("hello from stdout");

    // test stdin pipe from parent
    const hello_stdin = "hello from stdin";
    var buf: [hello_stdin.len]u8 = undefined;
    const stdin: std.fs.File = .stdin();
    var reader = stdin.reader(io, &.{});
    const n = try reader.interface.readSliceShort(&buf);
    if (!std.mem.eql(u8, buf[0..n], hello_stdin)) {
        testError("stdin: '{s}'; want '{s}'", .{ buf[0..n], hello_stdin });
    }
}

fn testError(comptime fmt: []const u8, args: anytype) void {
    var stderr_writer = std.fs.File.stderr().writer(&.{});
    const stderr = &stderr_writer.interface;
    stderr.print("CHILD TEST ERROR: ", .{}) catch {};
    stderr.print(fmt, args) catch {};
    if (fmt[fmt.len - 1] != '\n') {
        stderr.writeByte('\n') catch {};
    }
    exit_code = 1;
}