diff --git a/src/test.zig b/src/test.zig index 8ad11efa9c..c0ac56aa2d 100644 --- a/src/test.zig +++ b/src/test.zig @@ -56,6 +56,12 @@ pub const TestContext = struct { }, }; + pub const File = struct { + /// Contents of the importable file. Doesn't yet support incremental updates. + src: [:0]const u8, + path: []const u8, + }; + pub const TestType = enum { Zig, ZIR, @@ -78,6 +84,8 @@ pub const TestContext = struct { extension: TestType, cbe: bool = false, + files: std.ArrayList(File), + /// Adds a subcase in which the module is updated with `src`, and the /// resulting ZIR is validated against `result`. pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void { @@ -156,6 +164,7 @@ pub const TestContext = struct { .updates = std.ArrayList(Update).init(ctx.cases.allocator), .output_mode = .Exe, .extension = T, + .files = std.ArrayList(File).init(ctx.cases.allocator), }) catch unreachable; return &ctx.cases.items[ctx.cases.items.len - 1]; } @@ -182,6 +191,7 @@ pub const TestContext = struct { .updates = std.ArrayList(Update).init(ctx.cases.allocator), .output_mode = .Obj, .extension = T, + .files = std.ArrayList(File).init(ctx.cases.allocator), }) catch unreachable; return &ctx.cases.items[ctx.cases.items.len - 1]; } @@ -204,6 +214,7 @@ pub const TestContext = struct { .output_mode = .Obj, .extension = T, .cbe = true, + .files = std.ArrayList(File).init(ctx.cases.allocator), }) catch unreachable; return &ctx.cases.items[ctx.cases.items.len - 1]; } @@ -505,6 +516,10 @@ pub const TestContext = struct { }); defer comp.destroy(); + for (case.files.items) |file| { + try tmp.dir.writeFile(file.path, file.src); + } + for (case.updates.items) |update, update_index| { var update_node = root_node.start("update", 3); update_node.activate(); diff --git a/test/stage2/test.zig b/test/stage2/test.zig index a22dc23d36..618ed2f680 100644 --- a/test/stage2/test.zig +++ b/test/stage2/test.zig @@ -909,6 +909,44 @@ pub fn addCases(ctx: *TestContext) !void { ); } + { + var case = ctx.exe("basic import", linux_x64); + case.addCompareOutput( + \\export fn _start() noreturn { + \\ @import("print.zig").print(); + \\ exit(); + \\} + \\ + \\fn exit() noreturn { + \\ asm volatile ("syscall" + \\ : + \\ : [number] "{rax}" (231), + \\ [arg1] "{rdi}" (@as(usize, 0)) + \\ : "rcx", "r11", "memory" + \\ ); + \\ unreachable; + \\} + , + "Hello, World!\n", + ); + try case.files.append(.{ + .src = + \\pub fn print() void { + \\ asm volatile ("syscall" + \\ : + \\ : [number] "{rax}" (@as(usize, 1)), + \\ [arg1] "{rdi}" (@as(usize, 1)), + \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")), + \\ [arg3] "{rdx}" (@as(usize, 14)) + \\ : "rcx", "r11", "memory" + \\ ); + \\ return; + \\} + , + .path = "print.zig", + }); + } + { var case = ctx.exe("wasm function calls", wasi);