authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 11:12:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-24 11:12:21-04:00
loga155f2973b681dd984ddaba7686dc7c07347120c
tree061255ffe93aa049f85e3ba5262d4eae65183f0e
parenta170a64776e703a8549e5ef16cc6009b642b617e
signaturelock-open Commit is signed but in an unrecognized format.

add test to cover the CLI API that godbolt is using

closes #1399

1 files changed, 45 insertions(+), 10 deletions(-)

test/cli.zig+45-10
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");
2const os = std.os;3const os = std.os;
3const assertOrPanic = std.debug.assertOrPanic;4const assertOrPanic = std.debug.assertOrPanic;
45
...@@ -28,8 +29,18 @@ pub fn main() !void {...@@ -28,8 +29,18 @@ pub fn main() !void {
28 });29 });
29 const zig_exe = try os.path.resolve(a, zig_exe_rel);30 const zig_exe = try os.path.resolve(a, zig_exe_rel);
3031
31 try testZigInitLib(zig_exe, cache_root);32 const dir_path = try os.path.join(a, cache_root, "clitest");
32 try testZigInitExe(zig_exe, cache_root);33 const TestFn = fn ([]const u8, []const u8) error!void;
34 const test_fns = []TestFn{
35 testZigInitLib,
36 testZigInitExe,
37 testGodboltApi,
38 };
39 for (test_fns) |testFn| {
40 try os.deleteTree(a, dir_path);
41 try os.makeDir(dir_path);
42 try testFn(zig_exe, dir_path);
43 }
33}44}
3445
35fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {46fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
...@@ -73,20 +84,44 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult {...@@ -73,20 +84,44 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult {
73 return result;84 return result;
74}85}
7586
76fn testZigInitLib(zig_exe: []const u8, cache_root: []const u8) !void {87fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
77 const dir_path = try os.path.join(a, cache_root, "clitest");
78 try os.deleteTree(a, dir_path);
79 try os.makeDir(dir_path);
80 _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" });88 _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" });
81 const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" });89 const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" });
82 assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n"));90 assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n"));
83}91}
8492
85fn testZigInitExe(zig_exe: []const u8, cache_root: []const u8) !void {93fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
86 const dir_path = try os.path.join(a, cache_root, "clitest");
87 try os.deleteTree(a, dir_path);
88 try os.makeDir(dir_path);
89 _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" });94 _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" });
90 const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" });95 const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" });
91 assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n"));96 assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n"));
92}97}
98
99fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) !void {
100 if (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64) return;
101
102 const example_zig_path = try os.path.join(a, dir_path, "example.zig");
103 const example_s_path = try os.path.join(a, dir_path, "example.s");
104
105 try std.io.writeFile(example_zig_path,
106 \\// Type your code here, or load an example.
107 \\export fn square(num: i32) i32 {
108 \\ return num * num;
109 \\}
110 );
111
112 const args = [][]const u8{
113 zig_exe, "build-obj",
114 "--cache-dir", dir_path,
115 "--output", example_s_path,
116 "--output-h", "/dev/null",
117 "--emit", "asm",
118 "-mllvm", "--x86-asm-syntax=intel",
119 "--strip", "--release-fast",
120 example_zig_path,
121 };
122 _ = try exec(dir_path, args);
123
124 const out_asm = try std.io.readFileAlloc(a, example_s_path);
125 assertOrPanic(std.mem.indexOf(u8, out_asm, "square:") != null);
126 assertOrPanic(std.mem.indexOf(u8, out_asm, "imul\tedi, edi") != null);
127}