add test to cover the CLI API that godbolt is using

closes #1399
This commit is contained in:
Andrew Kelley 2018-09-24 11:12:21 -04:00
parent a170a64776
commit a155f2973b
No known key found for this signature in database
GPG key ID: 4E7CD66038A4D47C

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const os = std.os; const os = std.os;
const assertOrPanic = std.debug.assertOrPanic; const assertOrPanic = std.debug.assertOrPanic;
@ -28,8 +29,18 @@ pub fn main() !void {
}); });
const zig_exe = try os.path.resolve(a, zig_exe_rel); const zig_exe = try os.path.resolve(a, zig_exe_rel);
try testZigInitLib(zig_exe, cache_root); const dir_path = try os.path.join(a, cache_root, "clitest");
try testZigInitExe(zig_exe, cache_root); const TestFn = fn ([]const u8, []const u8) error!void;
const test_fns = []TestFn{
testZigInitLib,
testZigInitExe,
testGodboltApi,
};
for (test_fns) |testFn| {
try os.deleteTree(a, dir_path);
try os.makeDir(dir_path);
try testFn(zig_exe, dir_path);
}
} }
fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 { fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
@ -73,20 +84,44 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult {
return result; return result;
} }
fn testZigInitLib(zig_exe: []const u8, cache_root: []const u8) !void { fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
const dir_path = try os.path.join(a, cache_root, "clitest");
try os.deleteTree(a, dir_path);
try os.makeDir(dir_path);
_ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" }); _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" });
const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" }); const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" });
assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n")); assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n"));
} }
fn testZigInitExe(zig_exe: []const u8, cache_root: []const u8) !void { fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
const dir_path = try os.path.join(a, cache_root, "clitest");
try os.deleteTree(a, dir_path);
try os.makeDir(dir_path);
_ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" }); _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" });
const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" }); const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" });
assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n")); assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n"));
} }
fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) !void {
if (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64) return;
const example_zig_path = try os.path.join(a, dir_path, "example.zig");
const example_s_path = try os.path.join(a, dir_path, "example.s");
try std.io.writeFile(example_zig_path,
\\// Type your code here, or load an example.
\\export fn square(num: i32) i32 {
\\ return num * num;
\\}
);
const args = [][]const u8{
zig_exe, "build-obj",
"--cache-dir", dir_path,
"--output", example_s_path,
"--output-h", "/dev/null",
"--emit", "asm",
"-mllvm", "--x86-asm-syntax=intel",
"--strip", "--release-fast",
example_zig_path,
};
_ = try exec(dir_path, args);
const out_asm = try std.io.readFileAlloc(a, example_s_path);
assertOrPanic(std.mem.indexOf(u8, out_asm, "square:") != null);
assertOrPanic(std.mem.indexOf(u8, out_asm, "imul\tedi, edi") != null);
}