authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-23 00:35:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-23 00:35:53-04:00
logd767fae47e89aef53505191cf11ce7e592a784b2
treed72db72137e11c77f58b3407b4ed3399e428a328
parent93e78ee72259b98840f63db0ad87fdddb071e384

self-hosted: add first compare-output test


6 files changed, 101 insertions(+), 28 deletions(-)

src-self-hosted/compilation.zig+2-4
......@@ -128,7 +128,6 @@ pub const Compilation = struct {
128128 version_patch: u32,
129129
130130 linker_script: ?[]const u8,
131 cache_dir: []const u8,
132131 out_h_path: ?[]const u8,
133132
134133 is_test: bool,
......@@ -326,7 +325,6 @@ pub const Compilation = struct {
326325 build_mode: builtin.Mode,
327326 is_static: bool,
328327 zig_lib_dir: []const u8,
329 cache_dir: []const u8,
330328 ) !*Compilation {
331329 const loop = event_loop_local.loop;
332330 const comp = try event_loop_local.loop.allocator.create(Compilation{
......@@ -341,7 +339,6 @@ pub const Compilation = struct {
341339 .build_mode = build_mode,
342340 .zig_lib_dir = zig_lib_dir,
343341 .zig_std_dir = undefined,
344 .cache_dir = cache_dir,
345342 .tmp_dir = event.Future(BuildError![]u8).init(loop),
346343
347344 .name = undefined,
......@@ -777,7 +774,8 @@ pub const Compilation = struct {
777774 defer decls.base.deref(self);
778775
779776 var decl_group = event.Group(BuildError!void).init(self.loop);
780 errdefer decl_group.cancelAll();
777 // TODO https://github.com/ziglang/zig/issues/1261
778 //errdefer decl_group.cancelAll();
781779
782780 var it = tree.root_node.decls.iterator(0);
783781 while (it.next()) |decl_ptr| {
src-self-hosted/main.zig+1-11
......@@ -138,7 +138,6 @@ const usage_build_generic =
138138 \\Compile Options:
139139 \\ --libc [file] Provide a file which specifies libc paths
140140 \\ --assembly [source] Add assembly file to build
141 \\ --cache-dir [path] Override the cache directory
142141 \\ --emit [filetype] Emit a specific file format as compilation output
143142 \\ --enable-timing-info Print timing diagnostics
144143 \\ --name [name] Override output name
......@@ -204,7 +203,6 @@ const args_build_generic = []Flag{
204203 }),
205204
206205 Flag.ArgMergeN("--assembly", 1),
207 Flag.Arg1("--cache-dir"),
208206 Flag.Option("--emit", []const []const u8{
209207 "asm",
210208 "bin",
......@@ -373,13 +371,6 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
373371 os.exit(1);
374372 }
375373
376 const rel_cache_dir = flags.single("cache-dir") orelse "zig-cache"[0..];
377 const full_cache_dir = os.path.resolve(allocator, ".", rel_cache_dir) catch {
378 try stderr.print("invalid cache dir: {}\n", rel_cache_dir);
379 os.exit(1);
380 };
381 defer allocator.free(full_cache_dir);
382
383374 const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
384375 defer allocator.free(zig_lib_dir);
385376
......@@ -401,7 +392,6 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
401392 build_mode,
402393 is_static,
403394 zig_lib_dir,
404 full_cache_dir,
405395 );
406396 defer comp.destroy();
407397
......@@ -472,7 +462,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
472462
473463 comp.emit_file_type = emit_type;
474464 comp.assembly_files = assembly_files;
475 comp.link_out_file = flags.single("out-file");
465 comp.link_out_file = flags.single("output");
476466 comp.link_objects = link_objects;
477467
478468 try comp.build();
src-self-hosted/test.zig+81-10
......@@ -8,13 +8,14 @@ const assertOrPanic = std.debug.assertOrPanic;
88const errmsg = @import("errmsg.zig");
99const EventLoopLocal = @import("compilation.zig").EventLoopLocal;
1010
11test "compile errors" {
12 var ctx: TestContext = undefined;
11var ctx: TestContext = undefined;
12
13test "stage2" {
1314 try ctx.init();
1415 defer ctx.deinit();
1516
1617 try @import("../test/stage2/compile_errors.zig").addCases(&ctx);
17 //try @import("../test/stage2/compare_output.zig").addCases(&ctx);
18 try @import("../test/stage2/compare_output.zig").addCases(&ctx);
1819
1920 try ctx.run();
2021}
......@@ -26,7 +27,6 @@ pub const TestContext = struct {
2627 loop: std.event.Loop,
2728 event_loop_local: EventLoopLocal,
2829 zig_lib_dir: []u8,
29 zig_cache_dir: []u8,
3030 file_index: std.atomic.Int(usize),
3131 group: std.event.Group(error!void),
3232 any_err: error!void,
......@@ -39,7 +39,6 @@ pub const TestContext = struct {
3939 .loop = undefined,
4040 .event_loop_local = undefined,
4141 .zig_lib_dir = undefined,
42 .zig_cache_dir = undefined,
4342 .group = undefined,
4443 .file_index = std.atomic.Int(usize).init(0),
4544 };
......@@ -56,16 +55,12 @@ pub const TestContext = struct {
5655 self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
5756 errdefer allocator.free(self.zig_lib_dir);
5857
59 self.zig_cache_dir = try introspect.resolveZigCacheDir(allocator);
60 errdefer allocator.free(self.zig_cache_dir);
61
6258 try std.os.makePath(allocator, tmp_dir_name);
6359 errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {};
6460 }
6561
6662 fn deinit(self: *TestContext) void {
6763 std.os.deleteTree(allocator, tmp_dir_name) catch {};
68 allocator.free(self.zig_cache_dir);
6964 allocator.free(self.zig_lib_dir);
7065 self.event_loop_local.deinit();
7166 self.loop.deinit();
......@@ -110,7 +105,6 @@ pub const TestContext = struct {
110105 builtin.Mode.Debug,
111106 true, // is_static
112107 self.zig_lib_dir,
113 self.zig_cache_dir,
114108 );
115109 errdefer comp.destroy();
116110
......@@ -119,6 +113,83 @@ pub const TestContext = struct {
119113 try self.group.call(getModuleEvent, comp, source, path, line, column, msg);
120114 }
121115
116 fn testCompareOutputLibC(
117 self: *TestContext,
118 source: []const u8,
119 expected_output: []const u8,
120 ) !void {
121 var file_index_buf: [20]u8 = undefined;
122 const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
123 const file1_path = try std.os.path.join(allocator, tmp_dir_name, file_index, file1);
124
125 const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, Target(Target.Native).exeFileExt());
126 if (std.os.path.dirname(file1_path)) |dirname| {
127 try std.os.makePath(allocator, dirname);
128 }
129
130 // TODO async I/O
131 try std.io.writeFile(allocator, file1_path, source);
132
133 var comp = try Compilation.create(
134 &self.event_loop_local,
135 "test",
136 file1_path,
137 Target.Native,
138 Compilation.Kind.Exe,
139 builtin.Mode.Debug,
140 false,
141 self.zig_lib_dir,
142 );
143 errdefer comp.destroy();
144
145 _ = try comp.addLinkLib("c", true);
146 comp.link_out_file = output_file;
147 try comp.build();
148
149 try self.group.call(getModuleEventSuccess, comp, output_file, expected_output);
150 }
151
152 async fn getModuleEventSuccess(
153 comp: *Compilation,
154 exe_file: []const u8,
155 expected_output: []const u8,
156 ) !void {
157 // TODO this should not be necessary
158 const exe_file_2 = try std.mem.dupe(allocator, u8, exe_file);
159
160 defer comp.destroy();
161 const build_event = await (async comp.events.get() catch unreachable);
162
163 switch (build_event) {
164 Compilation.Event.Ok => {
165 const argv = []const []const u8{exe_file_2};
166 // TODO use event loop
167 const child = try std.os.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
168 switch (child.term) {
169 std.os.ChildProcess.Term.Exited => |code| {
170 if (code != 0) {
171 return error.BadReturnCode;
172 }
173 },
174 else => {
175 return error.Crashed;
176 },
177 }
178 if (!mem.eql(u8, child.stdout, expected_output)) {
179 return error.OutputMismatch;
180 }
181 },
182 Compilation.Event.Error => |err| return err,
183 Compilation.Event.Fail => |msgs| {
184 var stderr = try std.io.getStdErr();
185 try stderr.write("build incorrectly failed:\n");
186 for (msgs) |msg| {
187 try errmsg.printToFile(&stderr, msg, errmsg.Color.Auto);
188 }
189 },
190 }
191 }
192
122193 async fn getModuleEvent(
123194 comp: *Compilation,
124195 source: []const u8,
src/windows_sdk.h+2
......@@ -14,6 +14,8 @@
1414#define ZIG_EXTERN_C
1515#endif
1616
17#include <stddef.h>
18
1719struct ZigWindowsSDK {
1820 const char *path10_ptr;
1921 size_t path10_len;
std/os/test.zig+3-3
......@@ -23,14 +23,14 @@ test "makePath, put some files in it, deleteTree" {
2323
2424test "access file" {
2525 try os.makePath(a, "os_test_tmp");
26 if (os.File.access(a, "os_test_tmp/file.txt", os.default_file_mode)) |ok| {
27 unreachable;
26 if (os.File.access(a, "os_test_tmp/file.txt")) |ok| {
27 @panic("expected error");
2828 } else |err| {
2929 assert(err == error.NotFound);
3030 }
3131
3232 try io.writeFile(a, "os_test_tmp/file.txt", "");
33 assert((try os.File.access(a, "os_test_tmp/file.txt", os.default_file_mode)) == true);
33 try os.File.access(a, "os_test_tmp/file.txt");
3434 try os.deleteTree(a, "os_test_tmp");
3535}
3636
test/stage2/compare_output.zig created+12
......@@ -0,0 +1,12 @@
1const std = @import("std");
2const TestContext = @import("../../src-self-hosted/test.zig").TestContext;
3
4pub fn addCases(ctx: *TestContext) !void {
5 try ctx.testCompareOutputLibC(
6 \\extern fn puts([*]const u8) void;
7 \\export fn main() c_int {
8 \\ puts(c"Hello, world!");
9 \\ return 0;
10 \\}
11 , "Hello, world!" ++ std.cstr.line_sep);
12}