2018-07-10 17:18:43 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const mem = std.mem;
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Target = @import("target.zig").Target;
|
2018-07-14 13:12:41 -07:00
|
|
|
const Compilation = @import("compilation.zig").Compilation;
|
2018-07-10 17:18:43 -07:00
|
|
|
const introspect = @import("introspect.zig");
|
|
|
|
const assertOrPanic = std.debug.assertOrPanic;
|
|
|
|
const errmsg = @import("errmsg.zig");
|
2018-08-10 09:28:20 -07:00
|
|
|
const ZigCompiler = @import("compilation.zig").ZigCompiler;
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-22 21:35:53 -07:00
|
|
|
var ctx: TestContext = undefined;
|
|
|
|
|
|
|
|
test "stage2" {
|
2018-07-10 17:18:43 -07:00
|
|
|
try ctx.init();
|
|
|
|
defer ctx.deinit();
|
|
|
|
|
2018-07-11 12:58:48 -07:00
|
|
|
try @import("../test/stage2/compile_errors.zig").addCases(&ctx);
|
2018-07-22 21:35:53 -07:00
|
|
|
try @import("../test/stage2/compare_output.zig").addCases(&ctx);
|
2018-07-10 22:26:46 -07:00
|
|
|
|
2018-07-10 17:18:43 -07:00
|
|
|
try ctx.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
const file1 = "1.zig";
|
2018-07-10 17:27:15 -07:00
|
|
|
const allocator = std.heap.c_allocator;
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const TestContext = struct {
|
2018-07-10 17:18:43 -07:00
|
|
|
loop: std.event.Loop,
|
2018-08-10 09:28:20 -07:00
|
|
|
zig_compiler: ZigCompiler,
|
2018-07-10 17:18:43 -07:00
|
|
|
zig_lib_dir: []u8,
|
|
|
|
file_index: std.atomic.Int(usize),
|
2018-11-13 05:08:37 -08:00
|
|
|
group: std.event.Group(anyerror!void),
|
|
|
|
any_err: anyerror!void,
|
2018-07-10 17:18:43 -07:00
|
|
|
|
|
|
|
const tmp_dir_name = "stage2_test_tmp";
|
|
|
|
|
|
|
|
fn init(self: *TestContext) !void {
|
2018-11-13 05:08:37 -08:00
|
|
|
self.* = TestContext{
|
2018-07-10 17:18:43 -07:00
|
|
|
.any_err = {},
|
|
|
|
.loop = undefined,
|
2018-08-10 09:28:20 -07:00
|
|
|
.zig_compiler = undefined,
|
2018-07-10 17:18:43 -07:00
|
|
|
.zig_lib_dir = undefined,
|
|
|
|
.group = undefined,
|
|
|
|
.file_index = std.atomic.Int(usize).init(0),
|
|
|
|
};
|
|
|
|
|
2018-08-10 10:19:07 -07:00
|
|
|
try self.loop.initSingleThreaded(allocator);
|
2018-07-10 17:18:43 -07:00
|
|
|
errdefer self.loop.deinit();
|
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
self.zig_compiler = try ZigCompiler.init(&self.loop);
|
|
|
|
errdefer self.zig_compiler.deinit();
|
2018-07-14 12:45:15 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
self.group = std.event.Group(anyerror!void).init(&self.loop);
|
2018-08-02 14:04:17 -07:00
|
|
|
errdefer self.group.deinit();
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-10 17:27:15 -07:00
|
|
|
self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
|
|
|
|
errdefer allocator.free(self.zig_lib_dir);
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-10 17:27:15 -07:00
|
|
|
try std.os.makePath(allocator, tmp_dir_name);
|
|
|
|
errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {};
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deinit(self: *TestContext) void {
|
2018-07-10 17:27:15 -07:00
|
|
|
std.os.deleteTree(allocator, tmp_dir_name) catch {};
|
|
|
|
allocator.free(self.zig_lib_dir);
|
2018-08-10 09:28:20 -07:00
|
|
|
self.zig_compiler.deinit();
|
2018-07-10 17:18:43 -07:00
|
|
|
self.loop.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(self: *TestContext) !void {
|
|
|
|
const handle = try self.loop.call(waitForGroup, self);
|
|
|
|
defer cancel handle;
|
|
|
|
self.loop.run();
|
|
|
|
return self.any_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn waitForGroup(self: *TestContext) void {
|
|
|
|
self.any_err = await (async self.group.wait() catch unreachable);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testCompileError(
|
|
|
|
self: *TestContext,
|
|
|
|
source: []const u8,
|
|
|
|
path: []const u8,
|
|
|
|
line: usize,
|
|
|
|
column: usize,
|
|
|
|
msg: []const u8,
|
|
|
|
) !void {
|
|
|
|
var file_index_buf: [20]u8 = undefined;
|
2018-07-14 12:45:15 -07:00
|
|
|
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
|
2018-11-29 08:51:31 -08:00
|
|
|
const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
|
2018-07-10 17:18:43 -07:00
|
|
|
|
|
|
|
if (std.os.path.dirname(file1_path)) |dirname| {
|
2018-07-10 17:27:15 -07:00
|
|
|
try std.os.makePath(allocator, dirname);
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO async I/O
|
2018-08-21 13:07:28 -07:00
|
|
|
try std.io.writeFile(file1_path, source);
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
var comp = try Compilation.create(
|
2018-08-10 09:28:20 -07:00
|
|
|
&self.zig_compiler,
|
2018-07-10 17:18:43 -07:00
|
|
|
"test",
|
|
|
|
file1_path,
|
|
|
|
Target.Native,
|
2018-07-14 13:12:41 -07:00
|
|
|
Compilation.Kind.Obj,
|
2018-07-10 17:18:43 -07:00
|
|
|
builtin.Mode.Debug,
|
2018-07-16 17:52:50 -07:00
|
|
|
true, // is_static
|
2018-07-10 17:18:43 -07:00
|
|
|
self.zig_lib_dir,
|
|
|
|
);
|
2018-07-14 13:12:41 -07:00
|
|
|
errdefer comp.destroy();
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
comp.start();
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
try self.group.call(getModuleEvent, comp, source, path, line, column, msg);
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
|
|
|
|
2018-07-22 21:35:53 -07:00
|
|
|
fn testCompareOutputLibC(
|
|
|
|
self: *TestContext,
|
|
|
|
source: []const u8,
|
|
|
|
expected_output: []const u8,
|
|
|
|
) !void {
|
|
|
|
var file_index_buf: [20]u8 = undefined;
|
|
|
|
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
|
2018-11-29 08:51:31 -08:00
|
|
|
const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
|
2018-07-22 21:35:53 -07:00
|
|
|
|
|
|
|
const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, Target(Target.Native).exeFileExt());
|
|
|
|
if (std.os.path.dirname(file1_path)) |dirname| {
|
|
|
|
try std.os.makePath(allocator, dirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO async I/O
|
2018-08-21 13:07:28 -07:00
|
|
|
try std.io.writeFile(file1_path, source);
|
2018-07-22 21:35:53 -07:00
|
|
|
|
|
|
|
var comp = try Compilation.create(
|
2018-08-10 09:28:20 -07:00
|
|
|
&self.zig_compiler,
|
2018-07-22 21:35:53 -07:00
|
|
|
"test",
|
|
|
|
file1_path,
|
|
|
|
Target.Native,
|
|
|
|
Compilation.Kind.Exe,
|
|
|
|
builtin.Mode.Debug,
|
|
|
|
false,
|
|
|
|
self.zig_lib_dir,
|
|
|
|
);
|
|
|
|
errdefer comp.destroy();
|
|
|
|
|
|
|
|
_ = try comp.addLinkLib("c", true);
|
|
|
|
comp.link_out_file = output_file;
|
2018-08-10 09:28:20 -07:00
|
|
|
comp.start();
|
2018-07-22 21:35:53 -07:00
|
|
|
|
|
|
|
try self.group.call(getModuleEventSuccess, comp, output_file, expected_output);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn getModuleEventSuccess(
|
|
|
|
comp: *Compilation,
|
|
|
|
exe_file: []const u8,
|
|
|
|
expected_output: []const u8,
|
|
|
|
) !void {
|
|
|
|
// TODO this should not be necessary
|
|
|
|
const exe_file_2 = try std.mem.dupe(allocator, u8, exe_file);
|
|
|
|
|
|
|
|
defer comp.destroy();
|
|
|
|
const build_event = await (async comp.events.get() catch unreachable);
|
|
|
|
|
|
|
|
switch (build_event) {
|
|
|
|
Compilation.Event.Ok => {
|
2018-11-13 05:08:37 -08:00
|
|
|
const argv = []const []const u8{exe_file_2};
|
2018-07-22 21:35:53 -07:00
|
|
|
// TODO use event loop
|
|
|
|
const child = try std.os.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
|
|
|
|
switch (child.term) {
|
|
|
|
std.os.ChildProcess.Term.Exited => |code| {
|
|
|
|
if (code != 0) {
|
|
|
|
return error.BadReturnCode;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return error.Crashed;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if (!mem.eql(u8, child.stdout, expected_output)) {
|
|
|
|
return error.OutputMismatch;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Compilation.Event.Error => |err| return err,
|
|
|
|
Compilation.Event.Fail => |msgs| {
|
|
|
|
var stderr = try std.io.getStdErr();
|
|
|
|
try stderr.write("build incorrectly failed:\n");
|
|
|
|
for (msgs) |msg| {
|
2018-07-23 11:28:14 -07:00
|
|
|
defer msg.destroy();
|
2018-09-02 15:35:32 -07:00
|
|
|
try msg.printToFile(stderr, errmsg.Color.Auto);
|
2018-07-22 21:35:53 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:18:43 -07:00
|
|
|
async fn getModuleEvent(
|
2018-07-14 13:12:41 -07:00
|
|
|
comp: *Compilation,
|
2018-07-10 17:18:43 -07:00
|
|
|
source: []const u8,
|
|
|
|
path: []const u8,
|
|
|
|
line: usize,
|
|
|
|
column: usize,
|
|
|
|
text: []const u8,
|
|
|
|
) !void {
|
2018-07-14 13:12:41 -07:00
|
|
|
defer comp.destroy();
|
|
|
|
const build_event = await (async comp.events.get() catch unreachable);
|
2018-07-10 17:18:43 -07:00
|
|
|
|
|
|
|
switch (build_event) {
|
2018-07-14 13:12:41 -07:00
|
|
|
Compilation.Event.Ok => {
|
2018-07-10 17:18:43 -07:00
|
|
|
@panic("build incorrectly succeeded");
|
|
|
|
},
|
2018-07-14 13:12:41 -07:00
|
|
|
Compilation.Event.Error => |err| {
|
2018-07-10 17:18:43 -07:00
|
|
|
@panic("build incorrectly failed");
|
|
|
|
},
|
2018-07-14 13:12:41 -07:00
|
|
|
Compilation.Event.Fail => |msgs| {
|
2018-07-10 17:18:43 -07:00
|
|
|
assertOrPanic(msgs.len != 0);
|
|
|
|
for (msgs) |msg| {
|
2018-08-03 14:22:17 -07:00
|
|
|
if (mem.endsWith(u8, msg.realpath, path) and mem.eql(u8, msg.text, text)) {
|
|
|
|
const span = msg.getSpan();
|
|
|
|
const first_token = msg.getTree().tokens.at(span.first);
|
|
|
|
const last_token = msg.getTree().tokens.at(span.first);
|
2018-07-23 11:28:14 -07:00
|
|
|
const start_loc = msg.getTree().tokenLocationPtr(0, first_token);
|
2018-07-10 17:18:43 -07:00
|
|
|
if (start_loc.line + 1 == line and start_loc.column + 1 == column) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std.debug.warn(
|
|
|
|
"\n=====source:=======\n{}\n====expected:========\n{}:{}:{}: error: {}\n",
|
|
|
|
source,
|
|
|
|
path,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
text,
|
|
|
|
);
|
|
|
|
std.debug.warn("\n====found:========\n");
|
|
|
|
var stderr = try std.io.getStdErr();
|
|
|
|
for (msgs) |msg| {
|
2018-07-23 11:28:14 -07:00
|
|
|
defer msg.destroy();
|
2018-09-02 15:35:32 -07:00
|
|
|
try msg.printToFile(stderr, errmsg.Color.Auto);
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
|
|
|
std.debug.warn("============\n");
|
|
|
|
return error.TestFailed;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|