2017-04-19 11:00:12 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const debug = std.debug;
|
2017-10-31 01:47:55 -07:00
|
|
|
const warn = debug.warn;
|
2017-04-19 11:00:12 -07:00
|
|
|
const build = std.build;
|
|
|
|
const os = std.os;
|
|
|
|
const StdIo = os.ChildProcess.StdIo;
|
|
|
|
const Term = os.ChildProcess.Term;
|
2017-05-04 11:05:06 -07:00
|
|
|
const Buffer = std.Buffer;
|
2017-04-19 11:00:12 -07:00
|
|
|
const io = std.io;
|
|
|
|
const mem = std.mem;
|
|
|
|
const fmt = std.fmt;
|
2017-05-04 11:05:06 -07:00
|
|
|
const ArrayList = std.ArrayList;
|
2017-08-30 11:55:26 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Mode = builtin.Mode;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-19 13:59:20 -07:00
|
|
|
const compare_output = @import("compare_output.zig");
|
|
|
|
const build_examples = @import("build_examples.zig");
|
|
|
|
const compile_errors = @import("compile_errors.zig");
|
|
|
|
const assemble_and_link = @import("assemble_and_link.zig");
|
2018-01-24 22:46:12 -08:00
|
|
|
const runtime_safety = @import("runtime_safety.zig");
|
2017-11-24 11:56:05 -08:00
|
|
|
const translate_c = @import("translate_c.zig");
|
2018-01-22 19:24:07 -08:00
|
|
|
const gen_h = @import("gen_h.zig");
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TestTarget = struct {
|
2017-08-30 11:55:26 -07:00
|
|
|
os: builtin.Os,
|
|
|
|
arch: builtin.Arch,
|
|
|
|
environ: builtin.Environ,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const test_targets = []TestTarget{
|
|
|
|
TestTarget{
|
2017-08-30 11:55:26 -07:00
|
|
|
.os = builtin.Os.linux,
|
|
|
|
.arch = builtin.Arch.x86_64,
|
|
|
|
.environ = builtin.Environ.gnu,
|
|
|
|
},
|
2018-11-13 05:08:37 -08:00
|
|
|
TestTarget{
|
2018-01-06 20:10:53 -08:00
|
|
|
.os = builtin.Os.macosx,
|
2017-08-30 11:55:26 -07:00
|
|
|
.arch = builtin.Arch.x86_64,
|
|
|
|
.environ = builtin.Environ.unknown,
|
|
|
|
},
|
2018-11-13 05:08:37 -08:00
|
|
|
TestTarget{
|
2017-08-31 08:41:58 -07:00
|
|
|
.os = builtin.Os.windows,
|
|
|
|
.arch = builtin.Arch.x86_64,
|
|
|
|
.environ = builtin.Environ.msvc,
|
|
|
|
},
|
2017-08-30 11:55:26 -07:00
|
|
|
};
|
|
|
|
|
2017-10-31 01:47:55 -07:00
|
|
|
const max_stdout_size = 1 * 1024 * 1024; // 1 MB
|
|
|
|
|
2018-07-11 16:38:01 -07:00
|
|
|
pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
|
|
|
|
cases.* = CompareOutputContext{
|
2017-04-19 11:00:12 -07:00
|
|
|
.b = b,
|
|
|
|
.step = b.step("test-compare-output", "Run the compare output tests"),
|
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2018-07-11 16:38:01 -07:00
|
|
|
.modes = modes,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
compare_output.addCases(cases);
|
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:38:01 -07:00
|
|
|
pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
|
|
|
|
cases.* = CompareOutputContext{
|
2017-04-19 11:41:59 -07:00
|
|
|
.b = b,
|
2018-01-24 22:46:12 -08:00
|
|
|
.step = b.step("test-runtime-safety", "Run the runtime safety tests"),
|
2017-04-19 11:41:59 -07:00
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2018-07-11 16:38:01 -07:00
|
|
|
.modes = modes,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:41:59 -07:00
|
|
|
|
2018-01-24 22:46:12 -08:00
|
|
|
runtime_safety.addCases(cases);
|
2017-04-19 11:41:59 -07:00
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:38:01 -07:00
|
|
|
pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(CompileErrorContext) catch unreachable;
|
|
|
|
cases.* = CompileErrorContext{
|
2017-04-19 11:00:12 -07:00
|
|
|
.b = b,
|
|
|
|
.step = b.step("test-compile-errors", "Run the compile error tests"),
|
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2018-07-11 16:38:01 -07:00
|
|
|
.modes = modes,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
compile_errors.addCases(cases);
|
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-07-18 01:28:14 -07:00
|
|
|
pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(BuildExamplesContext) catch unreachable;
|
|
|
|
cases.* = BuildExamplesContext{
|
2017-04-19 11:00:12 -07:00
|
|
|
.b = b,
|
|
|
|
.step = b.step("test-build-examples", "Build the examples"),
|
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2018-07-18 01:28:14 -07:00
|
|
|
.modes = modes,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
build_examples.addCases(cases);
|
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-09-17 14:08:56 -07:00
|
|
|
pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
|
|
|
const step = b.step("test-cli", "Test the command line interface");
|
|
|
|
|
|
|
|
const exe = b.addExecutable("test-cli", "test/cli.zig");
|
2018-11-13 05:08:37 -08:00
|
|
|
const run_cmd = b.addCommand(null, b.env_map, [][]const u8{
|
2018-09-17 14:08:56 -07:00
|
|
|
b.pathFromRoot(exe.getOutputPath()),
|
|
|
|
os.path.realAlloc(b.allocator, b.zig_exe) catch unreachable,
|
|
|
|
b.pathFromRoot(b.cache_root),
|
|
|
|
});
|
|
|
|
run_cmd.step.dependOn(&exe.step);
|
|
|
|
|
|
|
|
step.dependOn(&run_cmd.step);
|
|
|
|
return step;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:38:01 -07:00
|
|
|
pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
|
|
|
|
cases.* = CompareOutputContext{
|
2017-04-19 11:00:12 -07:00
|
|
|
.b = b,
|
|
|
|
.step = b.step("test-asm-link", "Run the assemble and link tests"),
|
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2018-07-11 16:38:01 -07:00
|
|
|
.modes = modes,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
assemble_and_link.addCases(cases);
|
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(TranslateCContext) catch unreachable;
|
|
|
|
cases.* = TranslateCContext{
|
2017-04-19 13:59:20 -07:00
|
|
|
.b = b,
|
2018-01-22 19:24:07 -08:00
|
|
|
.step = b.step("test-translate-c", "Run the C transation tests"),
|
2017-04-19 13:59:20 -07:00
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2017-11-24 11:56:05 -08:00
|
|
|
translate_c.addCases(cases);
|
2017-04-19 13:59:20 -07:00
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
|
2019-02-03 13:13:28 -08:00
|
|
|
const cases = b.allocator.create(GenHContext) catch unreachable;
|
|
|
|
cases.* = GenHContext{
|
2018-01-22 19:24:07 -08:00
|
|
|
.b = b,
|
|
|
|
.step = b.step("test-gen-h", "Run the C header file generation tests"),
|
|
|
|
.test_index = 0,
|
|
|
|
.test_filter = test_filter,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-01-22 19:24:07 -08:00
|
|
|
|
|
|
|
gen_h.addCases(cases);
|
|
|
|
|
|
|
|
return cases.step;
|
|
|
|
}
|
|
|
|
|
2018-07-11 11:09:05 -07:00
|
|
|
pub fn addPkgTests(b: *build.Builder, test_filter: ?[]const u8, root_src: []const u8, name: []const u8, desc: []const u8, modes: []const Mode) *build.Step {
|
2017-04-19 23:26:36 -07:00
|
|
|
const step = b.step(b.fmt("test-{}", name), desc);
|
2017-08-30 11:55:26 -07:00
|
|
|
for (test_targets) |test_target| {
|
|
|
|
const is_native = (test_target.os == builtin.os and test_target.arch == builtin.arch);
|
2018-07-11 11:09:05 -07:00
|
|
|
for (modes) |mode| {
|
2019-02-01 14:49:29 -08:00
|
|
|
for ([]bool{ false, true }) |link_libc| {
|
|
|
|
for ([]bool{ false, true }) |single_threaded| {
|
|
|
|
if (link_libc and !is_native) {
|
|
|
|
// don't assume we have a cross-compiling libc set up
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const these_tests = b.addTest(root_src);
|
|
|
|
these_tests.setNamePrefix(b.fmt(
|
|
|
|
"{}-{}-{}-{}-{}-{} ",
|
|
|
|
name,
|
|
|
|
@tagName(test_target.os),
|
|
|
|
@tagName(test_target.arch),
|
|
|
|
@tagName(mode),
|
|
|
|
if (link_libc) "c" else "bare",
|
|
|
|
if (single_threaded) "single" else "multi",
|
|
|
|
));
|
|
|
|
these_tests.setFilter(test_filter);
|
|
|
|
these_tests.setBuildMode(mode);
|
|
|
|
if (!is_native) {
|
|
|
|
these_tests.setTarget(test_target.arch, test_target.os, test_target.environ);
|
|
|
|
}
|
|
|
|
if (link_libc) {
|
|
|
|
these_tests.linkSystemLibrary("c");
|
|
|
|
}
|
2019-02-06 11:32:20 -08:00
|
|
|
if (mem.eql(u8, name, "std")) {
|
|
|
|
these_tests.overrideStdDir("std");
|
|
|
|
}
|
2019-02-01 14:49:29 -08:00
|
|
|
step.dependOn(&these_tests.step);
|
2017-08-30 11:55:26 -07:00
|
|
|
}
|
2017-04-19 23:26:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return step;
|
|
|
|
}
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const CompareOutputContext = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
b: *build.Builder,
|
|
|
|
step: *build.Step,
|
2017-04-19 11:00:12 -07:00
|
|
|
test_index: usize,
|
|
|
|
test_filter: ?[]const u8,
|
2018-07-11 16:38:01 -07:00
|
|
|
modes: []const Mode,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Special = enum {
|
2017-04-19 11:41:59 -07:00
|
|
|
None,
|
|
|
|
Asm,
|
2018-01-24 22:46:12 -08:00
|
|
|
RuntimeSafety,
|
2017-04-19 11:41:59 -07:00
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TestCase = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
name: []const u8,
|
2017-05-04 11:05:06 -07:00
|
|
|
sources: ArrayList(SourceFile),
|
2017-04-19 11:00:12 -07:00
|
|
|
expected_output: []const u8,
|
|
|
|
link_libc: bool,
|
2017-04-19 11:41:59 -07:00
|
|
|
special: Special,
|
2017-12-06 15:12:05 -08:00
|
|
|
cli_args: []const []const u8,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const SourceFile = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
filename: []const u8,
|
|
|
|
source: []const u8,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
2018-11-13 05:08:37 -08:00
|
|
|
self.sources.append(SourceFile{
|
2017-04-19 11:00:12 -07:00
|
|
|
.filename = filename,
|
|
|
|
.source = source,
|
2018-01-08 21:07:01 -08:00
|
|
|
}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
2017-12-06 15:12:05 -08:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn setCommandLineArgs(self: *TestCase, args: []const []const u8) void {
|
2017-12-06 15:12:05 -08:00
|
|
|
self.cli_args = args;
|
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const RunCompareOutputStep = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
step: build.Step,
|
2018-05-31 07:56:59 -07:00
|
|
|
context: *CompareOutputContext,
|
2017-04-19 11:00:12 -07:00
|
|
|
exe_path: []const u8,
|
|
|
|
name: []const u8,
|
|
|
|
expected_output: []const u8,
|
|
|
|
test_index: usize,
|
2017-12-06 15:12:05 -08:00
|
|
|
cli_args: []const []const u8,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) *RunCompareOutputStep {
|
2017-04-19 11:00:12 -07:00
|
|
|
const allocator = context.b.allocator;
|
2019-02-03 13:13:28 -08:00
|
|
|
const ptr = allocator.create(RunCompareOutputStep) catch unreachable;
|
|
|
|
ptr.* = RunCompareOutputStep{
|
2017-04-19 11:00:12 -07:00
|
|
|
.context = context,
|
|
|
|
.exe_path = exe_path,
|
|
|
|
.name = name,
|
|
|
|
.expected_output = expected_output,
|
|
|
|
.test_index = context.test_index,
|
|
|
|
.step = build.Step.init("RunCompareOutput", allocator, make),
|
2017-12-06 15:12:05 -08:00
|
|
|
.cli_args = cli_args,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2017-04-19 11:00:12 -07:00
|
|
|
context.test_index += 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn make(step: *build.Step) !void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const self = @fieldParentPtr(RunCompareOutputStep, "step", step);
|
|
|
|
const b = self.context.b;
|
|
|
|
|
|
|
|
const full_exe_path = b.pathFromRoot(self.exe_path);
|
2017-12-06 15:12:05 -08:00
|
|
|
var args = ArrayList([]const u8).init(b.allocator);
|
|
|
|
defer args.deinit();
|
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
args.append(full_exe_path) catch unreachable;
|
2017-12-06 15:12:05 -08:00
|
|
|
for (self.cli_args) |arg| {
|
2018-01-08 21:07:01 -08:00
|
|
|
args.append(arg) catch unreachable;
|
2017-12-06 15:12:05 -08:00
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const child = os.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
defer child.deinit();
|
|
|
|
|
|
|
|
child.stdin_behavior = StdIo.Ignore;
|
|
|
|
child.stdout_behavior = StdIo.Pipe;
|
|
|
|
child.stderr_behavior = StdIo.Pipe;
|
2018-10-15 15:23:47 -07:00
|
|
|
child.env_map = b.env_map;
|
2017-09-25 22:01:49 -07:00
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err));
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-22 08:36:42 -07:00
|
|
|
var stdout = Buffer.initNull(b.allocator);
|
|
|
|
var stderr = Buffer.initNull(b.allocator);
|
|
|
|
|
2018-09-30 14:23:42 -07:00
|
|
|
var stdout_file_in_stream = child.stdout.?.inStream();
|
|
|
|
var stderr_file_in_stream = child.stderr.?.inStream();
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;
|
|
|
|
stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;
|
2017-04-22 08:36:42 -07:00
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const term = child.wait() catch |err| {
|
2017-04-19 11:00:12 -07:00
|
|
|
debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err));
|
|
|
|
};
|
|
|
|
switch (term) {
|
2017-09-07 20:10:23 -07:00
|
|
|
Term.Exited => |code| {
|
2017-04-19 11:00:12 -07:00
|
|
|
if (code != 0) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Process {} exited with error code {}\n", full_exe_path, code);
|
2017-04-19 11:00:12 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Process {} terminated unexpectedly\n", full_exe_path);
|
2017-04-19 11:00:12 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
2017-12-21 21:50:30 -08:00
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
if (!mem.eql(u8, self.expected_output, stdout.toSliceConst())) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn(
|
2017-04-19 11:00:12 -07:00
|
|
|
\\
|
|
|
|
\\========= Expected this output: =========
|
|
|
|
\\{}
|
|
|
|
\\================================================
|
|
|
|
\\{}
|
|
|
|
\\
|
|
|
|
, self.expected_output, stdout.toSliceConst());
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("OK\n");
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const RuntimeSafetyRunStep = struct {
|
2017-04-19 11:41:59 -07:00
|
|
|
step: build.Step,
|
2018-05-31 07:56:59 -07:00
|
|
|
context: *CompareOutputContext,
|
2017-04-19 11:41:59 -07:00
|
|
|
exe_path: []const u8,
|
|
|
|
name: []const u8,
|
|
|
|
test_index: usize,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8) *RuntimeSafetyRunStep {
|
2017-04-19 11:41:59 -07:00
|
|
|
const allocator = context.b.allocator;
|
2019-02-03 13:13:28 -08:00
|
|
|
const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable;
|
|
|
|
ptr.* = RuntimeSafetyRunStep{
|
2017-04-19 11:41:59 -07:00
|
|
|
.context = context,
|
|
|
|
.exe_path = exe_path,
|
|
|
|
.name = name,
|
|
|
|
.test_index = context.test_index,
|
2018-01-24 22:46:12 -08:00
|
|
|
.step = build.Step.init("RuntimeSafetyRun", allocator, make),
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2017-04-19 11:41:59 -07:00
|
|
|
context.test_index += 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn make(step: *build.Step) !void {
|
2018-01-24 22:46:12 -08:00
|
|
|
const self = @fieldParentPtr(RuntimeSafetyRunStep, "step", step);
|
2017-04-19 11:41:59 -07:00
|
|
|
const b = self.context.b;
|
|
|
|
|
|
|
|
const full_exe_path = b.pathFromRoot(self.exe_path);
|
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
|
2017-04-19 11:41:59 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const child = os.ChildProcess.init([][]u8{full_exe_path}, b.allocator) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
defer child.deinit();
|
2017-04-19 11:41:59 -07:00
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
child.env_map = b.env_map;
|
2017-09-25 22:01:49 -07:00
|
|
|
child.stdin_behavior = StdIo.Ignore;
|
|
|
|
child.stdout_behavior = StdIo.Ignore;
|
|
|
|
child.stderr_behavior = StdIo.Ignore;
|
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const term = child.spawnAndWait() catch |err| {
|
2017-04-19 11:41:59 -07:00
|
|
|
debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err));
|
|
|
|
};
|
|
|
|
|
2017-10-15 21:20:51 -07:00
|
|
|
const expected_exit_code: i32 = 126;
|
2017-04-19 11:41:59 -07:00
|
|
|
switch (term) {
|
2017-09-07 20:10:23 -07:00
|
|
|
Term.Exited => |code| {
|
2017-10-15 21:20:51 -07:00
|
|
|
if (code != expected_exit_code) {
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("\nProgram expected to exit with code {} " ++ "but exited with code {}\n", expected_exit_code, code);
|
2017-04-19 11:41:59 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
},
|
2017-10-15 21:20:51 -07:00
|
|
|
Term.Signal => |sig| {
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("\nProgram expected to exit with code {} " ++ "but instead signaled {}\n", expected_exit_code, sig);
|
2017-10-15 21:20:51 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
2017-04-19 11:41:59 -07:00
|
|
|
else => {
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("\nProgram expected to exit with code {}" ++ " but exited in an unexpected way\n", expected_exit_code);
|
2017-04-19 11:41:59 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("OK\n");
|
2017-04-19 11:41:59 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn createExtra(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
|
2018-11-13 05:08:37 -08:00
|
|
|
var tc = TestCase{
|
2017-04-19 11:00:12 -07:00
|
|
|
.name = name,
|
2017-05-04 11:05:06 -07:00
|
|
|
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
2017-04-19 11:00:12 -07:00
|
|
|
.expected_output = expected_output,
|
|
|
|
.link_libc = false,
|
2017-04-19 11:41:59 -07:00
|
|
|
.special = special,
|
2018-11-13 05:08:37 -08:00
|
|
|
.cli_args = []const []const u8{},
|
2017-04-19 11:00:12 -07:00
|
|
|
};
|
2017-04-19 11:41:59 -07:00
|
|
|
const root_src_name = if (special == Special.Asm) "source.s" else "source.zig";
|
2017-04-19 11:00:12 -07:00
|
|
|
tc.addSourceFile(root_src_name, source);
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
|
2017-04-19 11:41:59 -07:00
|
|
|
return createExtra(self, name, source, expected_output, Special.None);
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addC(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
var tc = self.create(name, source, expected_output);
|
|
|
|
tc.link_libc = true;
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn add(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const tc = self.create(name, source, expected_output);
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addAsm(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
2017-04-19 11:41:59 -07:00
|
|
|
const tc = self.createExtra(name, source, expected_output, Special.Asm);
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addRuntimeSafety(self: *CompareOutputContext, name: []const u8, source: []const u8) void {
|
2018-01-24 22:46:12 -08:00
|
|
|
const tc = self.createExtra(name, source, undefined, Special.RuntimeSafety);
|
2017-04-19 11:00:12 -07:00
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
pub fn addCase(self: *CompareOutputContext, case: TestCase) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const b = self.b;
|
|
|
|
|
2018-11-29 07:37:01 -08:00
|
|
|
const root_src = os.path.join(b.allocator, [][]const u8{b.cache_root, case.sources.items[0].filename}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-19 11:41:59 -07:00
|
|
|
switch (case.special) {
|
|
|
|
Special.Asm => {
|
2018-01-08 21:07:01 -08:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "assemble-and-link {}", case.name) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
2017-04-19 11:41:59 -07:00
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-26 16:17:05 -07:00
|
|
|
const exe = b.addExecutable("test", null);
|
|
|
|
exe.addAssemblyFile(root_src);
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-19 11:41:59 -07:00
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2017-04-19 11:41:59 -07:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
2017-04-26 16:17:05 -07:00
|
|
|
exe.step.dependOn(&write_src.step);
|
2017-04-19 11:41:59 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(), annotated_case_name, case.expected_output, case.cli_args);
|
2017-04-19 11:41:59 -07:00
|
|
|
run_and_cmp_output.step.dependOn(&exe.step);
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-19 11:41:59 -07:00
|
|
|
self.step.dependOn(&run_and_cmp_output.step);
|
|
|
|
},
|
|
|
|
Special.None => {
|
2018-07-11 16:38:01 -07:00
|
|
|
for (self.modes) |mode| {
|
2018-05-17 20:21:44 -07:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {} ({})", "compare-output", case.name, @tagName(mode)) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
|
2017-04-19 11:41:59 -07:00
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-19 11:41:59 -07:00
|
|
|
const exe = b.addExecutable("test", root_src);
|
2017-05-02 14:34:21 -07:00
|
|
|
exe.setBuildMode(mode);
|
2017-04-19 11:41:59 -07:00
|
|
|
if (case.link_libc) {
|
2017-04-20 22:56:12 -07:00
|
|
|
exe.linkSystemLibrary("c");
|
2017-04-19 11:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2017-04-19 11:41:59 -07:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
|
|
|
exe.step.dependOn(&write_src.step);
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(), annotated_case_name, case.expected_output, case.cli_args);
|
2017-04-19 11:41:59 -07:00
|
|
|
run_and_cmp_output.step.dependOn(&exe.step);
|
|
|
|
|
|
|
|
self.step.dependOn(&run_and_cmp_output.step);
|
|
|
|
}
|
|
|
|
},
|
2018-01-24 22:46:12 -08:00
|
|
|
Special.RuntimeSafety => {
|
2018-01-08 21:07:01 -08:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "safety {}", case.name) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const exe = b.addExecutable("test", root_src);
|
|
|
|
if (case.link_libc) {
|
2017-04-20 22:56:12 -07:00
|
|
|
exe.linkSystemLibrary("c");
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
|
|
|
exe.step.dependOn(&write_src.step);
|
|
|
|
}
|
|
|
|
|
2018-01-24 22:46:12 -08:00
|
|
|
const run_and_cmp_output = RuntimeSafetyRunStep.create(self, exe.getOutputPath(), annotated_case_name);
|
2017-04-19 11:00:12 -07:00
|
|
|
run_and_cmp_output.step.dependOn(&exe.step);
|
|
|
|
|
|
|
|
self.step.dependOn(&run_and_cmp_output.step);
|
2017-04-19 11:41:59 -07:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const CompileErrorContext = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
b: *build.Builder,
|
|
|
|
step: *build.Step,
|
2017-04-19 11:00:12 -07:00
|
|
|
test_index: usize,
|
|
|
|
test_filter: ?[]const u8,
|
2018-07-11 16:38:01 -07:00
|
|
|
modes: []const Mode,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TestCase = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
name: []const u8,
|
2017-05-04 11:05:06 -07:00
|
|
|
sources: ArrayList(SourceFile),
|
|
|
|
expected_errors: ArrayList([]const u8),
|
2017-04-19 11:00:12 -07:00
|
|
|
link_libc: bool,
|
|
|
|
is_exe: bool,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const SourceFile = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
filename: []const u8,
|
|
|
|
source: []const u8,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
2018-11-13 05:08:37 -08:00
|
|
|
self.sources.append(SourceFile{
|
2017-04-19 11:00:12 -07:00
|
|
|
.filename = filename,
|
|
|
|
.source = source,
|
2018-01-08 21:07:01 -08:00
|
|
|
}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addExpectedError(self: *TestCase, text: []const u8) void {
|
2018-01-08 21:07:01 -08:00
|
|
|
self.expected_errors.append(text) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const CompileCmpOutputStep = struct {
|
2017-04-19 11:00:12 -07:00
|
|
|
step: build.Step,
|
2018-05-31 07:56:59 -07:00
|
|
|
context: *CompileErrorContext,
|
2017-04-19 11:00:12 -07:00
|
|
|
name: []const u8,
|
|
|
|
test_index: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
case: *const TestCase,
|
2017-05-02 14:34:21 -07:00
|
|
|
build_mode: Mode,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep {
|
2017-04-19 11:00:12 -07:00
|
|
|
const allocator = context.b.allocator;
|
2019-02-03 13:13:28 -08:00
|
|
|
const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
|
|
|
|
ptr.* = CompileCmpOutputStep{
|
2017-04-19 11:00:12 -07:00
|
|
|
.step = build.Step.init("CompileCmpOutput", allocator, make),
|
|
|
|
.context = context,
|
|
|
|
.name = name,
|
|
|
|
.test_index = context.test_index,
|
|
|
|
.case = case,
|
2017-05-02 14:34:21 -07:00
|
|
|
.build_mode = build_mode,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2017-04-19 11:00:12 -07:00
|
|
|
context.test_index += 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn make(step: *build.Step) !void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const self = @fieldParentPtr(CompileCmpOutputStep, "step", step);
|
|
|
|
const b = self.context.b;
|
|
|
|
|
2018-11-29 07:37:01 -08:00
|
|
|
const root_src = os.path.join(b.allocator, [][]const u8{b.cache_root, self.case.sources.items[0].filename}) catch unreachable;
|
|
|
|
const obj_path = os.path.join(b.allocator, [][]const u8{b.cache_root, "test.o"}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-05-04 11:05:06 -07:00
|
|
|
var zig_args = ArrayList([]const u8).init(b.allocator);
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append(b.zig_exe) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append(if (self.case.is_exe) "build-exe" else "build-obj") catch unreachable;
|
|
|
|
zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("--name") catch unreachable;
|
|
|
|
zig_args.append("test") catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("--output") catch unreachable;
|
|
|
|
zig_args.append(b.pathFromRoot(obj_path)) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-05-02 14:34:21 -07:00
|
|
|
switch (self.build_mode) {
|
|
|
|
Mode.Debug => {},
|
2018-01-08 21:07:01 -08:00
|
|
|
Mode.ReleaseSafe => zig_args.append("--release-safe") catch unreachable,
|
|
|
|
Mode.ReleaseFast => zig_args.append("--release-fast") catch unreachable,
|
2018-04-15 18:06:00 -07:00
|
|
|
Mode.ReleaseSmall => zig_args.append("--release-small") catch unreachable,
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
if (b.verbose) {
|
2017-10-15 13:03:32 -07:00
|
|
|
printInvocation(zig_args.toSliceConst());
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
defer child.deinit();
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
child.env_map = b.env_map;
|
2017-09-25 22:01:49 -07:00
|
|
|
child.stdin_behavior = StdIo.Ignore;
|
|
|
|
child.stdout_behavior = StdIo.Pipe;
|
|
|
|
child.stderr_behavior = StdIo.Pipe;
|
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err));
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2017-04-22 08:36:42 -07:00
|
|
|
var stdout_buf = Buffer.initNull(b.allocator);
|
|
|
|
var stderr_buf = Buffer.initNull(b.allocator);
|
|
|
|
|
2018-09-30 14:23:42 -07:00
|
|
|
var stdout_file_in_stream = child.stdout.?.inStream();
|
|
|
|
var stderr_file_in_stream = child.stderr.?.inStream();
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
stdout_file_in_stream.stream.readAllBuffer(&stdout_buf, max_stdout_size) catch unreachable;
|
|
|
|
stderr_file_in_stream.stream.readAllBuffer(&stderr_buf, max_stdout_size) catch unreachable;
|
2017-04-22 08:36:42 -07:00
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const term = child.wait() catch |err| {
|
2017-09-25 22:01:49 -07:00
|
|
|
debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err));
|
2017-04-19 11:00:12 -07:00
|
|
|
};
|
|
|
|
switch (term) {
|
2017-09-07 20:10:23 -07:00
|
|
|
Term.Exited => |code| {
|
2017-04-19 11:00:12 -07:00
|
|
|
if (code == 0) {
|
2018-01-14 21:01:02 -08:00
|
|
|
return error.CompilationIncorrectlySucceeded;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Process {} terminated unexpectedly\n", b.zig_exe);
|
2017-04-19 11:00:12 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
2017-12-21 21:50:30 -08:00
|
|
|
}
|
2017-04-19 11:00:12 -07:00
|
|
|
|
|
|
|
const stdout = stdout_buf.toSliceConst();
|
|
|
|
const stderr = stderr_buf.toSliceConst();
|
|
|
|
|
|
|
|
if (stdout.len != 0) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn(
|
2017-04-19 11:00:12 -07:00
|
|
|
\\
|
|
|
|
\\Expected empty stdout, instead found:
|
|
|
|
\\================================================
|
|
|
|
\\{}
|
|
|
|
\\================================================
|
|
|
|
\\
|
|
|
|
, stdout);
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (self.case.expected_errors.toSliceConst()) |expected_error| {
|
|
|
|
if (mem.indexOf(u8, stderr, expected_error) == null) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn(
|
2017-04-19 11:00:12 -07:00
|
|
|
\\
|
|
|
|
\\========= Expected this compile error: =========
|
|
|
|
\\{}
|
|
|
|
\\================================================
|
|
|
|
\\{}
|
|
|
|
\\
|
|
|
|
, expected_error, stderr);
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("OK\n");
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn printInvocation(args: []const []const u8) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
for (args) |arg| {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("{} ", arg);
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("\n");
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
|
2019-02-03 13:13:28 -08:00
|
|
|
const tc = self.b.allocator.create(TestCase) catch unreachable;
|
|
|
|
tc.* = TestCase{
|
2017-04-19 11:00:12 -07:00
|
|
|
.name = name,
|
2017-05-04 11:05:06 -07:00
|
|
|
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
|
|
|
.expected_errors = ArrayList([]const u8).init(self.b.allocator),
|
2017-04-19 11:00:12 -07:00
|
|
|
.link_libc = false,
|
|
|
|
.is_exe = false,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2017-04-19 11:00:12 -07:00
|
|
|
tc.addSourceFile(".tmp_source.zig", source);
|
|
|
|
comptime var arg_i = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
|
2017-05-26 20:31:38 -07:00
|
|
|
tc.addExpectedError(expected_lines[arg_i]);
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addC(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
var tc = self.create(name, source, expected_lines);
|
|
|
|
tc.link_libc = true;
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addExe(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
var tc = self.create(name, source, expected_lines);
|
|
|
|
tc.is_exe = true;
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn add(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const tc = self.create(name, source, expected_lines);
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const b = self.b;
|
|
|
|
|
2018-07-11 16:38:01 -07:00
|
|
|
for (self.modes) |mode| {
|
2018-05-17 20:21:44 -07:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {} ({})", case.name, @tagName(mode)) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2017-05-02 14:34:21 -07:00
|
|
|
const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, mode);
|
2017-04-19 11:00:12 -07:00
|
|
|
self.step.dependOn(&compile_and_cmp_errors.step);
|
|
|
|
|
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2017-04-19 11:00:12 -07:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
|
|
|
compile_and_cmp_errors.step.dependOn(&write_src.step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const BuildExamplesContext = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
b: *build.Builder,
|
|
|
|
step: *build.Step,
|
2017-04-19 11:00:12 -07:00
|
|
|
test_index: usize,
|
|
|
|
test_filter: ?[]const u8,
|
2018-07-18 01:28:14 -07:00
|
|
|
modes: []const Mode,
|
2017-04-19 11:00:12 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addC(self: *BuildExamplesContext, root_src: []const u8) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
self.addAllArgs(root_src, true);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn add(self: *BuildExamplesContext, root_src: []const u8) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
self.addAllArgs(root_src, false);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addBuildFile(self: *BuildExamplesContext, build_file: []const u8) void {
|
2017-04-20 22:56:12 -07:00
|
|
|
const b = self.b;
|
|
|
|
|
2017-05-02 14:34:21 -07:00
|
|
|
const annotated_case_name = b.fmt("build {} (Debug)", build_file);
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2017-05-04 11:05:06 -07:00
|
|
|
var zig_args = ArrayList([]const u8).init(b.allocator);
|
2018-01-08 21:07:01 -08:00
|
|
|
const rel_zig_exe = os.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable;
|
|
|
|
zig_args.append(rel_zig_exe) catch unreachable;
|
|
|
|
zig_args.append("build") catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("--build-file") catch unreachable;
|
|
|
|
zig_args.append(b.pathFromRoot(build_file)) catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("test") catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
|
|
|
|
if (b.verbose) {
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("--verbose") catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2017-09-25 22:01:49 -07:00
|
|
|
const run_cmd = b.addCommand(null, b.env_map, zig_args.toSliceConst());
|
2017-04-20 22:56:12 -07:00
|
|
|
|
|
|
|
const log_step = b.addLog("PASS {}\n", annotated_case_name);
|
|
|
|
log_step.step.dependOn(&run_cmd.step);
|
|
|
|
|
|
|
|
self.step.dependOn(&log_step.step);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addAllArgs(self: *BuildExamplesContext, root_src: []const u8, link_libc: bool) void {
|
2017-04-19 11:00:12 -07:00
|
|
|
const b = self.b;
|
|
|
|
|
2018-07-18 01:28:14 -07:00
|
|
|
for (self.modes) |mode| {
|
2018-05-17 20:21:44 -07:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {} ({})", root_src, @tagName(mode)) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const exe = b.addExecutable("test", root_src);
|
2017-05-02 14:34:21 -07:00
|
|
|
exe.setBuildMode(mode);
|
2017-04-19 11:00:12 -07:00
|
|
|
if (link_libc) {
|
2017-04-20 22:56:12 -07:00
|
|
|
exe.linkSystemLibrary("c");
|
2017-04-19 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const log_step = b.addLog("PASS {}\n", annotated_case_name);
|
|
|
|
log_step.step.dependOn(&exe.step);
|
|
|
|
|
|
|
|
self.step.dependOn(&log_step.step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const TranslateCContext = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
b: *build.Builder,
|
|
|
|
step: *build.Step,
|
2017-04-19 13:59:20 -07:00
|
|
|
test_index: usize,
|
|
|
|
test_filter: ?[]const u8,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TestCase = struct {
|
2017-04-19 13:59:20 -07:00
|
|
|
name: []const u8,
|
2017-05-04 11:05:06 -07:00
|
|
|
sources: ArrayList(SourceFile),
|
|
|
|
expected_lines: ArrayList([]const u8),
|
2017-04-19 13:59:20 -07:00
|
|
|
allow_warnings: bool,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const SourceFile = struct {
|
2017-04-19 13:59:20 -07:00
|
|
|
filename: []const u8,
|
|
|
|
source: []const u8,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
2018-11-13 05:08:37 -08:00
|
|
|
self.sources.append(SourceFile{
|
2017-04-19 13:59:20 -07:00
|
|
|
.filename = filename,
|
|
|
|
.source = source,
|
2018-01-08 21:07:01 -08:00
|
|
|
}) catch unreachable;
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
|
2018-01-08 21:07:01 -08:00
|
|
|
self.expected_lines.append(text) catch unreachable;
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TranslateCCmpOutputStep = struct {
|
2017-04-19 13:59:20 -07:00
|
|
|
step: build.Step,
|
2018-05-31 07:56:59 -07:00
|
|
|
context: *TranslateCContext,
|
2017-04-19 13:59:20 -07:00
|
|
|
name: []const u8,
|
|
|
|
test_index: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
case: *const TestCase,
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(context: *TranslateCContext, name: []const u8, case: *const TestCase) *TranslateCCmpOutputStep {
|
2017-04-19 13:59:20 -07:00
|
|
|
const allocator = context.b.allocator;
|
2019-02-03 13:13:28 -08:00
|
|
|
const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable;
|
|
|
|
ptr.* = TranslateCCmpOutputStep{
|
2017-09-05 19:55:03 -07:00
|
|
|
.step = build.Step.init("ParseCCmpOutput", allocator, make),
|
2017-04-19 13:59:20 -07:00
|
|
|
.context = context,
|
|
|
|
.name = name,
|
|
|
|
.test_index = context.test_index,
|
|
|
|
.case = case,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2017-04-19 13:59:20 -07:00
|
|
|
context.test_index += 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn make(step: *build.Step) !void {
|
2017-11-24 11:56:05 -08:00
|
|
|
const self = @fieldParentPtr(TranslateCCmpOutputStep, "step", step);
|
2017-04-19 13:59:20 -07:00
|
|
|
const b = self.context.b;
|
|
|
|
|
2018-11-29 07:37:01 -08:00
|
|
|
const root_src = os.path.join(b.allocator, [][]const u8{b.cache_root, self.case.sources.items[0].filename}) catch unreachable;
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2017-05-04 11:05:06 -07:00
|
|
|
var zig_args = ArrayList([]const u8).init(b.allocator);
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append(b.zig_exe) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
zig_args.append("translate-c") catch unreachable;
|
|
|
|
zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
|
2017-04-19 13:59:20 -07:00
|
|
|
|
|
|
|
if (b.verbose) {
|
2017-10-15 13:03:32 -07:00
|
|
|
printInvocation(zig_args.toSliceConst());
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable;
|
2017-09-25 22:01:49 -07:00
|
|
|
defer child.deinit();
|
|
|
|
|
2018-10-15 15:23:47 -07:00
|
|
|
child.env_map = b.env_map;
|
2017-09-25 22:01:49 -07:00
|
|
|
child.stdin_behavior = StdIo.Ignore;
|
|
|
|
child.stdout_behavior = StdIo.Pipe;
|
|
|
|
child.stderr_behavior = StdIo.Pipe;
|
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err));
|
2017-04-19 13:59:20 -07:00
|
|
|
|
2017-04-22 08:36:42 -07:00
|
|
|
var stdout_buf = Buffer.initNull(b.allocator);
|
|
|
|
var stderr_buf = Buffer.initNull(b.allocator);
|
|
|
|
|
2018-09-30 14:23:42 -07:00
|
|
|
var stdout_file_in_stream = child.stdout.?.inStream();
|
|
|
|
var stderr_file_in_stream = child.stderr.?.inStream();
|
2017-11-07 00:22:27 -08:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
stdout_file_in_stream.stream.readAllBuffer(&stdout_buf, max_stdout_size) catch unreachable;
|
|
|
|
stderr_file_in_stream.stream.readAllBuffer(&stderr_buf, max_stdout_size) catch unreachable;
|
2017-04-22 08:36:42 -07:00
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const term = child.wait() catch |err| {
|
2017-09-25 22:01:49 -07:00
|
|
|
debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err));
|
2017-04-19 13:59:20 -07:00
|
|
|
};
|
|
|
|
switch (term) {
|
2017-09-07 20:10:23 -07:00
|
|
|
Term.Exited => |code| {
|
2017-04-19 13:59:20 -07:00
|
|
|
if (code != 0) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Compilation failed with exit code {}\n", code);
|
2017-04-19 13:59:20 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Term.Signal => |code| {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Compilation failed with signal {}\n", code);
|
2017-04-19 13:59:20 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
|
|
|
else => {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("Compilation terminated unexpectedly\n");
|
2017-04-19 13:59:20 -07:00
|
|
|
return error.TestFailed;
|
|
|
|
},
|
2017-12-21 21:50:30 -08:00
|
|
|
}
|
2017-04-19 13:59:20 -07:00
|
|
|
|
|
|
|
const stdout = stdout_buf.toSliceConst();
|
|
|
|
const stderr = stderr_buf.toSliceConst();
|
|
|
|
|
|
|
|
if (stderr.len != 0 and !self.case.allow_warnings) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn(
|
2017-11-24 11:56:05 -08:00
|
|
|
\\====== translate-c emitted warnings: =======
|
2017-04-19 13:59:20 -07:00
|
|
|
\\{}
|
|
|
|
\\============================================
|
|
|
|
\\
|
|
|
|
, stderr);
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (self.case.expected_lines.toSliceConst()) |expected_line| {
|
|
|
|
if (mem.indexOf(u8, stdout, expected_line) == null) {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn(
|
2017-04-19 13:59:20 -07:00
|
|
|
\\
|
|
|
|
\\========= Expected this output: ================
|
|
|
|
\\{}
|
|
|
|
\\================================================
|
|
|
|
\\{}
|
|
|
|
\\
|
|
|
|
, expected_line, stdout);
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("OK\n");
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn printInvocation(args: []const []const u8) void {
|
2017-04-19 13:59:20 -07:00
|
|
|
for (args) |arg| {
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("{} ", arg);
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
warn("\n");
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(self: *TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
|
2019-02-03 13:13:28 -08:00
|
|
|
const tc = self.b.allocator.create(TestCase) catch unreachable;
|
|
|
|
tc.* = TestCase{
|
2017-04-19 13:59:20 -07:00
|
|
|
.name = name,
|
2017-05-04 11:05:06 -07:00
|
|
|
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
|
|
|
.expected_lines = ArrayList([]const u8).init(self.b.allocator),
|
2017-04-19 13:59:20 -07:00
|
|
|
.allow_warnings = allow_warnings,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2017-10-01 18:42:33 -07:00
|
|
|
tc.addSourceFile(filename, source);
|
2017-04-19 13:59:20 -07:00
|
|
|
comptime var arg_i = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
|
2017-09-11 19:58:06 -07:00
|
|
|
tc.addExpectedLine(expected_lines[arg_i]);
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn add(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-10-01 18:42:33 -07:00
|
|
|
const tc = self.create(false, "source.h", name, source, expected_lines);
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addC(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-10-01 18:42:33 -07:00
|
|
|
const tc = self.create(false, "source.c", name, source, expected_lines);
|
2017-04-19 13:59:20 -07:00
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addAllowWarnings(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2017-10-01 18:42:33 -07:00
|
|
|
const tc = self.create(true, "source.h", name, source, expected_lines);
|
2017-04-19 13:59:20 -07:00
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addCase(self: *TranslateCContext, case: *const TestCase) void {
|
2017-04-19 13:59:20 -07:00
|
|
|
const b = self.b;
|
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "translate-c {}", case.name) catch unreachable;
|
2017-05-03 14:23:11 -07:00
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
|
2017-11-24 11:56:05 -08:00
|
|
|
const translate_c_and_cmp = TranslateCCmpOutputStep.create(self, annotated_case_name, case);
|
|
|
|
self.step.dependOn(&translate_c_and_cmp.step);
|
2017-04-19 13:59:20 -07:00
|
|
|
|
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2017-04-19 13:59:20 -07:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
2017-11-24 11:56:05 -08:00
|
|
|
translate_c_and_cmp.step.dependOn(&write_src.step);
|
2017-04-19 13:59:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-01-22 19:24:07 -08:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const GenHContext = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
b: *build.Builder,
|
|
|
|
step: *build.Step,
|
2018-01-22 19:24:07 -08:00
|
|
|
test_index: usize,
|
|
|
|
test_filter: ?[]const u8,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const TestCase = struct {
|
2018-01-22 19:24:07 -08:00
|
|
|
name: []const u8,
|
|
|
|
sources: ArrayList(SourceFile),
|
|
|
|
expected_lines: ArrayList([]const u8),
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const SourceFile = struct {
|
2018-01-22 19:24:07 -08:00
|
|
|
filename: []const u8,
|
|
|
|
source: []const u8,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
2018-11-13 05:08:37 -08:00
|
|
|
self.sources.append(SourceFile{
|
2018-01-22 19:24:07 -08:00
|
|
|
.filename = filename,
|
|
|
|
.source = source,
|
|
|
|
}) catch unreachable;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
|
2018-01-22 19:24:07 -08:00
|
|
|
self.expected_lines.append(text) catch unreachable;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const GenHCmpOutputStep = struct {
|
2018-01-22 19:24:07 -08:00
|
|
|
step: build.Step,
|
2018-05-31 07:56:59 -07:00
|
|
|
context: *GenHContext,
|
2018-01-22 19:24:07 -08:00
|
|
|
h_path: []const u8,
|
|
|
|
name: []const u8,
|
|
|
|
test_index: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
case: *const TestCase,
|
2018-01-22 19:24:07 -08:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(context: *GenHContext, h_path: []const u8, name: []const u8, case: *const TestCase) *GenHCmpOutputStep {
|
2018-01-22 19:24:07 -08:00
|
|
|
const allocator = context.b.allocator;
|
2019-02-03 13:13:28 -08:00
|
|
|
const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
|
|
|
|
ptr.* = GenHCmpOutputStep{
|
2018-01-22 19:24:07 -08:00
|
|
|
.step = build.Step.init("ParseCCmpOutput", allocator, make),
|
|
|
|
.context = context,
|
|
|
|
.h_path = h_path,
|
|
|
|
.name = name,
|
|
|
|
.test_index = context.test_index,
|
|
|
|
.case = case,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2018-01-22 19:24:07 -08:00
|
|
|
context.test_index += 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn make(step: *build.Step) !void {
|
2018-01-22 19:24:07 -08:00
|
|
|
const self = @fieldParentPtr(GenHCmpOutputStep, "step", step);
|
|
|
|
const b = self.context.b;
|
|
|
|
|
2018-05-17 20:21:44 -07:00
|
|
|
warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
|
2018-01-22 19:24:07 -08:00
|
|
|
|
|
|
|
const full_h_path = b.pathFromRoot(self.h_path);
|
2018-02-09 15:27:50 -08:00
|
|
|
const actual_h = try io.readFileAlloc(b.allocator, full_h_path);
|
2018-01-22 19:24:07 -08:00
|
|
|
|
|
|
|
for (self.case.expected_lines.toSliceConst()) |expected_line| {
|
|
|
|
if (mem.indexOf(u8, actual_h, expected_line) == null) {
|
|
|
|
warn(
|
|
|
|
\\
|
|
|
|
\\========= Expected this output: ================
|
|
|
|
\\{}
|
|
|
|
\\================================================
|
|
|
|
\\{}
|
|
|
|
\\
|
|
|
|
, expected_line, actual_h);
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
warn("OK\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn printInvocation(args: []const []const u8) void {
|
2018-01-22 19:24:07 -08:00
|
|
|
for (args) |arg| {
|
|
|
|
warn("{} ", arg);
|
|
|
|
}
|
|
|
|
warn("\n");
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn create(self: *GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
|
2019-02-03 13:13:28 -08:00
|
|
|
const tc = self.b.allocator.create(TestCase) catch unreachable;
|
|
|
|
tc.* = TestCase{
|
2018-01-22 19:24:07 -08:00
|
|
|
.name = name,
|
|
|
|
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
|
|
|
.expected_lines = ArrayList([]const u8).init(self.b.allocator),
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-06-20 08:40:21 -07:00
|
|
|
|
2018-01-22 19:24:07 -08:00
|
|
|
tc.addSourceFile(filename, source);
|
|
|
|
comptime var arg_i = 0;
|
|
|
|
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
|
|
|
|
tc.addExpectedLine(expected_lines[arg_i]);
|
|
|
|
}
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn add(self: *GenHContext, name: []const u8, source: []const u8, expected_lines: ...) void {
|
2018-01-22 19:24:07 -08:00
|
|
|
const tc = self.create("test.zig", name, source, expected_lines);
|
|
|
|
self.addCase(tc);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addCase(self: *GenHContext, case: *const TestCase) void {
|
2018-01-22 19:24:07 -08:00
|
|
|
const b = self.b;
|
2018-11-29 07:37:01 -08:00
|
|
|
const root_src = os.path.join(b.allocator, [][]const u8{b.cache_root, case.sources.items[0].filename}) catch unreachable;
|
2018-01-22 19:24:07 -08:00
|
|
|
|
|
|
|
const mode = builtin.Mode.Debug;
|
|
|
|
const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {} ({})", case.name, @tagName(mode)) catch unreachable;
|
|
|
|
if (self.test_filter) |filter| {
|
2018-05-17 20:21:44 -07:00
|
|
|
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
2018-01-22 19:24:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const obj = b.addObject("test", root_src);
|
|
|
|
obj.setBuildMode(mode);
|
|
|
|
|
|
|
|
for (case.sources.toSliceConst()) |src_file| {
|
2018-11-29 07:37:01 -08:00
|
|
|
const expanded_src_path = os.path.join(b.allocator, [][]const u8{b.cache_root, src_file.filename}) catch unreachable;
|
2018-01-22 19:24:07 -08:00
|
|
|
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
|
|
|
obj.step.dependOn(&write_src.step);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmp_h = GenHCmpOutputStep.create(self, obj.getOutputHPath(), annotated_case_name, case);
|
|
|
|
cmp_h.step.dependOn(&obj.step);
|
|
|
|
|
|
|
|
self.step.dependOn(&cmp_h.step);
|
|
|
|
}
|
|
|
|
};
|