aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted/test.zig
blob: 47e45d1bb03c72a559d1cfbc5838f90a4bbb63a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const std = @import("std");
const mem = std.mem;
const builtin = @import("builtin");
const Target = @import("target.zig").Target;
const Compilation = @import("compilation.zig").Compilation;
const introspect = @import("introspect.zig");
const assertOrPanic = std.debug.assertOrPanic;
const errmsg = @import("errmsg.zig");
const EventLoopLocal = @import("compilation.zig").EventLoopLocal;

var ctx: TestContext = undefined;

test "stage2" {
    try ctx.init();
    defer ctx.deinit();

    try @import("../test/stage2/compile_errors.zig").addCases(&ctx);
    try @import("../test/stage2/compare_output.zig").addCases(&ctx);

    try ctx.run();
}

const file1 = "1.zig";
const allocator = std.heap.c_allocator;

pub const TestContext = struct {
    loop: std.event.Loop,
    event_loop_local: EventLoopLocal,
    zig_lib_dir: []u8,
    file_index: std.atomic.Int(usize),
    group: std.event.Group(error!void),
    any_err: error!void,

    const tmp_dir_name = "stage2_test_tmp";

    fn init(self: *TestContext) !void {
        self.* = TestContext{
            .any_err = {},
            .loop = undefined,
            .event_loop_local = undefined,
            .zig_lib_dir = undefined,
            .group = undefined,
            .file_index = std.atomic.Int(usize).init(0),
        };

        try self.loop.initMultiThreaded(allocator);
        errdefer self.loop.deinit();

        self.event_loop_local = try EventLoopLocal.init(&self.loop);
        errdefer self.event_loop_local.deinit();

        self.group = std.event.Group(error!void).init(&self.loop);
        errdefer self.group.cancelAll();

        self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
        errdefer allocator.free(self.zig_lib_dir);

        try std.os.makePath(allocator, tmp_dir_name);
        errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {};
    }

    fn deinit(self: *TestContext) void {
        std.os.deleteTree(allocator, tmp_dir_name) catch {};
        allocator.free(self.zig_lib_dir);
        self.event_loop_local.deinit();
        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;
        const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
        const file1_path = try std.os.path.join(allocator, tmp_dir_name, file_index, file1);

        if (std.os.path.dirname(file1_path)) |dirname| {
            try std.os.makePath(allocator, dirname);
        }

        // TODO async I/O
        try std.io.writeFile(allocator, file1_path, source);

        var comp = try Compilation.create(
            &self.event_loop_local,
            "test",
            file1_path,
            Target.Native,
            Compilation.Kind.Obj,
            builtin.Mode.Debug,
            true, // is_static
            self.zig_lib_dir,
        );
        errdefer comp.destroy();

        try comp.build();

        try self.group.call(getModuleEvent, comp, source, path, line, column, msg);
    }

    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());
        const file1_path = try std.os.path.join(allocator, tmp_dir_name, file_index, file1);

        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
        try std.io.writeFile(allocator, file1_path, source);

        var comp = try Compilation.create(
            &self.event_loop_local,
            "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;
        try comp.build();

        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 => {
                const argv = []const []const u8{exe_file_2};
                // 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| {
                    defer msg.destroy();
                    try msg.printToFile(&stderr, errmsg.Color.Auto);
                }
            },
        }
    }

    async fn getModuleEvent(
        comp: *Compilation,
        source: []const u8,
        path: []const u8,
        line: usize,
        column: usize,
        text: []const u8,
    ) !void {
        defer comp.destroy();
        const build_event = await (async comp.events.get() catch unreachable);

        switch (build_event) {
            Compilation.Event.Ok => {
                @panic("build incorrectly succeeded");
            },
            Compilation.Event.Error => |err| {
                @panic("build incorrectly failed");
            },
            Compilation.Event.Fail => |msgs| {
                assertOrPanic(msgs.len != 0);
                for (msgs) |msg| {
                    if (mem.endsWith(u8, msg.getRealPath(), path) and mem.eql(u8, msg.text, text)) {
                        const first_token = msg.getTree().tokens.at(msg.span.first);
                        const last_token = msg.getTree().tokens.at(msg.span.first);
                        const start_loc = msg.getTree().tokenLocationPtr(0, first_token);
                        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| {
                    defer msg.destroy();
                    try msg.printToFile(&stderr, errmsg.Color.Auto);
                }
                std.debug.warn("============\n");
                return error.TestFailed;
            },
        }
    }
};