2018-09-17 22:08:56 +01:00
|
|
|
const std = @import("std");
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2019-05-26 18:17:34 +01:00
|
|
|
const process = std.process;
|
|
|
|
const fs = std.fs;
|
|
|
|
const ChildProcess = std.ChildProcess;
|
2018-09-17 22:08:56 +01:00
|
|
|
|
|
|
|
var a: *std.mem.Allocator = undefined;
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2020-01-30 06:26:54 +00:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
2018-09-17 22:08:56 +01:00
|
|
|
defer arena.deinit();
|
|
|
|
|
2019-05-26 18:17:34 +01:00
|
|
|
var arg_it = process.args();
|
2018-09-17 22:08:56 +01:00
|
|
|
|
|
|
|
// skip my own exe name
|
|
|
|
_ = arg_it.skip();
|
|
|
|
|
|
|
|
a = &arena.allocator;
|
|
|
|
|
|
|
|
const zig_exe_rel = try (arg_it.next(a) orelse {
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("Expected first argument to be path to zig compiler\n", .{});
|
2018-09-17 22:08:56 +01:00
|
|
|
return error.InvalidArgs;
|
|
|
|
});
|
|
|
|
const cache_root = try (arg_it.next(a) orelse {
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("Expected second argument to be cache root directory path\n", .{});
|
2018-09-17 22:08:56 +01:00
|
|
|
return error.InvalidArgs;
|
|
|
|
});
|
2019-11-30 04:04:19 +00:00
|
|
|
const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});
|
2018-09-17 22:08:56 +01:00
|
|
|
|
2019-11-30 04:04:19 +00:00
|
|
|
const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
|
2021-03-14 17:12:42 +00:00
|
|
|
defer fs.cwd().deleteTree(dir_path) catch {};
|
2021-05-04 19:23:22 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const TestFn = fn ([]const u8, []const u8) anyerror!void;
|
2019-06-10 00:24:24 +01:00
|
|
|
const test_fns = [_]TestFn{
|
2018-09-24 16:12:21 +01:00
|
|
|
testZigInitLib,
|
|
|
|
testZigInitExe,
|
|
|
|
testGodboltApi,
|
2019-09-11 05:25:10 +01:00
|
|
|
testMissingOutputPath,
|
2020-06-21 04:49:45 +01:00
|
|
|
testZigFmt,
|
2018-09-24 16:12:21 +01:00
|
|
|
};
|
|
|
|
for (test_fns) |testFn| {
|
2020-03-18 20:09:06 +00:00
|
|
|
try fs.cwd().deleteTree(dir_path);
|
2020-03-03 20:58:14 +00:00
|
|
|
try fs.cwd().makeDir(dir_path);
|
2018-09-24 16:12:21 +01:00
|
|
|
try testFn(zig_exe, dir_path);
|
|
|
|
}
|
2018-09-17 22:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
|
|
|
|
return arg catch |err| {
|
2019-12-09 03:53:51 +00:00
|
|
|
warn("Unable to parse command line: {}\n", .{err});
|
2018-09-17 22:08:56 +01:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn printCmd(cwd: []const u8, argv: []const []const u8) void {
|
2021-01-03 09:20:37 +00:00
|
|
|
std.debug.warn("cd {s} && ", .{cwd});
|
2018-09-17 22:08:56 +01:00
|
|
|
for (argv) |arg| {
|
2021-01-03 09:20:37 +00:00
|
|
|
std.debug.warn("{s} ", .{arg});
|
2018-09-17 22:08:56 +01:00
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("\n", .{});
|
2018-09-17 22:08:56 +01:00
|
|
|
}
|
|
|
|
|
2020-09-26 20:42:07 +01:00
|
|
|
fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess.ExecResult {
|
2018-09-17 22:08:56 +01:00
|
|
|
const max_output_size = 100 * 1024;
|
2020-03-30 19:23:22 +01:00
|
|
|
const result = ChildProcess.exec(.{
|
|
|
|
.allocator = a,
|
|
|
|
.argv = argv,
|
|
|
|
.cwd = cwd,
|
|
|
|
.max_output_bytes = max_output_size,
|
|
|
|
}) catch |err| {
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("The following command failed:\n", .{});
|
2018-09-17 22:08:56 +01:00
|
|
|
printCmd(cwd, argv);
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
switch (result.term) {
|
2019-05-26 18:17:34 +01:00
|
|
|
.Exited => |code| {
|
2020-09-26 20:42:07 +01:00
|
|
|
if ((code != 0) == expect_0) {
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("The following command exited with error code {}:\n", .{code});
|
2018-09-17 22:08:56 +01:00
|
|
|
printCmd(cwd, argv);
|
2021-01-03 09:20:37 +00:00
|
|
|
std.debug.warn("stderr:\n{s}\n", .{result.stderr});
|
2018-09-17 22:08:56 +01:00
|
|
|
return error.CommandFailed;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("The following command terminated unexpectedly:\n", .{});
|
2018-09-17 22:08:56 +01:00
|
|
|
printCmd(cwd, argv);
|
2021-01-03 09:20:37 +00:00
|
|
|
std.debug.warn("stderr:\n{s}\n", .{result.stderr});
|
2018-09-17 22:08:56 +01:00
|
|
|
return error.CommandFailed;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:12:21 +01:00
|
|
|
fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
|
2020-09-26 20:42:07 +01:00
|
|
|
_ = 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" });
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
|
2018-09-17 22:08:56 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 16:12:21 +01:00
|
|
|
fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
|
2020-09-26 20:42:07 +01:00
|
|
|
_ = 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" });
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
|
2018-09-17 22:08:56 +01:00
|
|
|
}
|
2018-09-24 16:12:21 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
|
2020-02-27 18:32:35 +00:00
|
|
|
if (std.Target.current.os.tag != .linux or std.Target.current.cpu.arch != .x86_64) return;
|
2018-09-24 16:12:21 +01:00
|
|
|
|
2019-11-30 04:04:19 +00:00
|
|
|
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" });
|
2018-09-24 16:12:21 +01:00
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
try fs.cwd().writeFile(example_zig_path,
|
2018-09-24 16:12:21 +01:00
|
|
|
\\// Type your code here, or load an example.
|
|
|
|
\\export fn square(num: i32) i32 {
|
|
|
|
\\ return num * num;
|
|
|
|
\\}
|
2018-09-24 16:14:39 +01:00
|
|
|
\\extern fn zig_panic() noreturn;
|
2021-05-17 22:36:51 +01:00
|
|
|
\\pub fn panic(msg: []const u8, error_return_trace: ?*@import("std").builtin.StackTrace) noreturn {
|
2018-09-24 16:14:39 +01:00
|
|
|
\\ zig_panic();
|
|
|
|
\\}
|
2018-09-24 16:12:21 +01:00
|
|
|
);
|
|
|
|
|
2020-09-26 09:42:54 +01:00
|
|
|
var args = std.ArrayList([]const u8).init(a);
|
|
|
|
try args.appendSlice(&[_][]const u8{
|
2019-05-26 18:17:34 +01:00
|
|
|
zig_exe, "build-obj",
|
|
|
|
"--cache-dir", dir_path,
|
|
|
|
"--name", "example",
|
2020-09-26 09:42:54 +01:00
|
|
|
"-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);
|
|
|
|
|
2020-09-26 20:42:07 +01:00
|
|
|
_ = try exec(dir_path, true, args.items);
|
2018-09-24 16:12:21 +01:00
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
|
|
|
|
try testing.expect(std.mem.indexOf(u8, out_asm, "mov\teax, edi") != null);
|
|
|
|
try testing.expect(std.mem.indexOf(u8, out_asm, "imul\teax, edi") != null);
|
2018-09-24 16:12:21 +01:00
|
|
|
}
|
2019-09-11 05:25:10 +01:00
|
|
|
|
|
|
|
fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
|
2020-09-26 20:42:07 +01:00
|
|
|
_ = 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});
|
2019-11-30 04:04:19 +00:00
|
|
|
const source_path = try fs.path.join(a, &[_][]const u8{ "src", "main.zig" });
|
2020-09-26 20:42:07 +01:00
|
|
|
const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
|
2020-09-30 06:31:08 +01:00
|
|
|
const s = std.fs.path.sep_str;
|
|
|
|
const expected: []const u8 = "error: unable to open output directory 'does" ++ s ++ "not" ++ s ++ "exist': FileNotFound\n";
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expectEqualStrings(expected, result.stderr);
|
2019-09-11 05:25:10 +01:00
|
|
|
}
|
2020-06-21 04:49:45 +01:00
|
|
|
|
|
|
|
fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
|
2020-09-26 20:42:07 +01:00
|
|
|
_ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
|
2020-06-21 04:49:45 +01:00
|
|
|
|
2020-06-21 06:20:23 +01:00
|
|
|
const unformatted_code = " // no reason for indent";
|
2020-06-21 04:49:45 +01:00
|
|
|
|
|
|
|
const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" });
|
|
|
|
try fs.cwd().writeFile(fmt1_zig_path, unformatted_code);
|
|
|
|
|
2020-09-26 20:42:07 +01:00
|
|
|
const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
|
2020-06-21 04:49:45 +01:00
|
|
|
// stderr should be file path + \n
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expect(std.mem.startsWith(u8, run_result1.stdout, fmt1_zig_path));
|
|
|
|
try testing.expect(run_result1.stdout.len == fmt1_zig_path.len + 1 and run_result1.stdout[run_result1.stdout.len - 1] == '\n');
|
2020-06-21 04:49:45 +01:00
|
|
|
|
|
|
|
const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
|
|
|
|
try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
|
|
|
|
|
2020-09-26 20:42:07 +01:00
|
|
|
const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
|
2020-06-21 04:49:45 +01:00
|
|
|
// running it on the dir, only the new file should be changed
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expect(std.mem.startsWith(u8, run_result2.stdout, fmt2_zig_path));
|
|
|
|
try testing.expect(run_result2.stdout.len == fmt2_zig_path.len + 1 and run_result2.stdout[run_result2.stdout.len - 1] == '\n');
|
2020-06-21 06:21:23 +01:00
|
|
|
|
2020-09-26 20:42:07 +01:00
|
|
|
const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
|
2020-06-21 06:21:23 +01:00
|
|
|
// both files have been formatted, nothing should change now
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expect(run_result3.stdout.len == 0);
|
2021-03-14 06:03:22 +00:00
|
|
|
|
|
|
|
// Check UTF-16 decoding
|
|
|
|
const fmt4_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt4.zig" });
|
|
|
|
var unformatted_code_utf16 = "\xff\xfe \x00 \x00 \x00 \x00/\x00/\x00 \x00n\x00o\x00 \x00r\x00e\x00a\x00s\x00o\x00n\x00";
|
|
|
|
try fs.cwd().writeFile(fmt4_zig_path, unformatted_code_utf16);
|
|
|
|
|
|
|
|
const run_result4 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
|
2021-05-04 19:23:22 +01:00
|
|
|
try testing.expect(std.mem.startsWith(u8, run_result4.stdout, fmt4_zig_path));
|
|
|
|
try testing.expect(run_result4.stdout.len == fmt4_zig_path.len + 1 and run_result4.stdout[run_result4.stdout.len - 1] == '\n');
|
2020-06-21 04:49:45 +01:00
|
|
|
}
|