aboutsummaryrefslogtreecommitdiff
path: root/test/cli.zig
blob: c0702fa54c03ec8f4329ed412212a95eb00c5574 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const std = @import("std");
const testing = std.testing;
const process = std.process;
const fs = std.fs;
const ChildProcess = std.ChildProcess;

var a: *std.mem.Allocator = undefined;

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();

    var arg_it = process.args();

    // skip my own exe name
    _ = arg_it.skip();

    a = &arena.allocator;

    const zig_exe_rel = try (arg_it.next(a) orelse {
        std.debug.warn("Expected first argument to be path to zig compiler\n", .{});
        return error.InvalidArgs;
    });
    const cache_root = try (arg_it.next(a) orelse {
        std.debug.warn("Expected second argument to be cache root directory path\n", .{});
        return error.InvalidArgs;
    });
    const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});

    const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
    const TestFn = fn ([]const u8, []const u8) anyerror!void;
    const test_fns = [_]TestFn{
        testZigInitLib,
        testZigInitExe,
        testGodboltApi,
        testMissingOutputPath,
        testZigFmt,
    };
    for (test_fns) |testFn| {
        try fs.cwd().deleteTree(dir_path);
        try fs.cwd().makeDir(dir_path);
        try testFn(zig_exe, dir_path);
    }
}

fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
    return arg catch |err| {
        warn("Unable to parse command line: {}\n", .{err});
        return err;
    };
}

fn printCmd(cwd: []const u8, argv: []const []const u8) void {
    std.debug.warn("cd {s} && ", .{cwd});
    for (argv) |arg| {
        std.debug.warn("{s} ", .{arg});
    }
    std.debug.warn("\n", .{});
}

fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess.ExecResult {
    const max_output_size = 100 * 1024;
    const result = ChildProcess.exec(.{
        .allocator = a,
        .argv = argv,
        .cwd = cwd,
        .max_output_bytes = max_output_size,
    }) catch |err| {
        std.debug.warn("The following command failed:\n", .{});
        printCmd(cwd, argv);
        return err;
    };
    switch (result.term) {
        .Exited => |code| {
            if ((code != 0) == expect_0) {
                std.debug.warn("The following command exited with error code {}:\n", .{code});
                printCmd(cwd, argv);
                std.debug.warn("stderr:\n{s}\n", .{result.stderr});
                return error.CommandFailed;
            }
        },
        else => {
            std.debug.warn("The following command terminated unexpectedly:\n", .{});
            printCmd(cwd, argv);
            std.debug.warn("stderr:\n{s}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }
    return result;
}

fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
    _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
    const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
    testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
}

fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
    _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
    const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
    testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
}

fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
    if (std.Target.current.os.tag != .linux or std.Target.current.cpu.arch != .x86_64) return;

    const example_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "example.zig" });
    const example_s_path = try fs.path.join(a, &[_][]const u8{ dir_path, "example.s" });

    try fs.cwd().writeFile(example_zig_path,
        \\// Type your code here, or load an example.
        \\export fn square(num: i32) i32 {
        \\    return num * num;
        \\}
        \\extern fn zig_panic() noreturn;
        \\pub fn panic(msg: []const u8, error_return_trace: ?*@import("builtin").StackTrace) noreturn {
        \\    zig_panic();
        \\}
    );

    var args = std.ArrayList([]const u8).init(a);
    try args.appendSlice(&[_][]const u8{
        zig_exe,          "build-obj",
        "--cache-dir",    dir_path,
        "--name",         "example",
        "-fno-emit-bin",  "-fno-emit-h",
        "--strip",        "-OReleaseFast",
        example_zig_path,
    });

    const emit_asm_arg = try std.fmt.allocPrint(a, "-femit-asm={s}", .{example_s_path});
    try args.append(emit_asm_arg);

    _ = try exec(dir_path, true, args.items);

    const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));
    testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
    testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
    testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
}

fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
    _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
    const output_path = try fs.path.join(a, &[_][]const u8{ "does", "not", "exist", "foo.exe" });
    const output_arg = try std.fmt.allocPrint(a, "-femit-bin={s}", .{output_path});
    const source_path = try fs.path.join(a, &[_][]const u8{ "src", "main.zig" });
    const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
    const s = std.fs.path.sep_str;
    const expected: []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n";
    testing.expectEqualStrings(expected, result.stderr);
}

fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
    _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });

    const unformatted_code = "    // no reason for indent";

    const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" });
    try fs.cwd().writeFile(fmt1_zig_path, unformatted_code);

    const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
    // stderr should be file path + \n
    testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
    testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');

    const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
    try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);

    const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
    // running it on the dir, only the new file should be changed
    testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
    testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');

    const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
    // both files have been formatted, nothing should change now
    testing.expect(run_result3.stdout.len == 0);
}