rework and improve some of the zig build steps
* `RunStep` moved to lib/std/build/run.zig and gains ability to compare output and exit code against expected values. Multiple redundant locations in the test harness code are replaced to use `RunStep`. * `WriteFileStep` moved to lib/std/build/write_file.zig and gains ability to write more than one file into the cache directory, for when the files need to be relative to each other. This makes usage of `WriteFileStep` no longer problematic when parallelizing zig build. * Added `CheckFileStep`, which can be used to validate that the output of another step produced a valid file. Multiple redundant locations in the test harness code are replaced to use `CheckFileStep`. * Added `TranslateCStep`. This exposes `zig translate-c` to the build system, which is likely to be rarely useful by most Zig users; however Zig's own test suite uses it both for translate-c tests and for run-translated-c tests. * Refactored ad-hoc code to handle source files coming from multiple kinds of sources, into `std.build.FileSource`. * Added `std.build.Builder.addExecutableFromWriteFileStep`. * Added `std.build.Builder.addExecutableSource`. * Added `std.build.Builder.addWriteFiles`. * Added `std.build.Builder.addTranslateC`. * Added `std.build.LibExeObjStep.addCSourceFileSource`. * Added `std.build.LibExeObjStep.addAssemblyFileFromWriteFileStep`. * Added `std.build.LibExeObjStep.addAssemblyFileSource`. * Exposed `std.fs.base64_encoder`.
This commit is contained in:
165
test/src/compare_output.zig
Normal file
165
test/src/compare_output.zig
Normal file
@@ -0,0 +1,165 @@
|
||||
// This is the implementation of the test harness.
|
||||
// For the actual test cases, see test/compare_output.zig.
|
||||
const std = @import("std");
|
||||
const builtin = std.builtin;
|
||||
const build = std.build;
|
||||
const ArrayList = std.ArrayList;
|
||||
const fmt = std.fmt;
|
||||
const mem = std.mem;
|
||||
const fs = std.fs;
|
||||
const warn = std.debug.warn;
|
||||
const Mode = builtin.Mode;
|
||||
|
||||
pub const CompareOutputContext = struct {
|
||||
b: *build.Builder,
|
||||
step: *build.Step,
|
||||
test_index: usize,
|
||||
test_filter: ?[]const u8,
|
||||
modes: []const Mode,
|
||||
|
||||
const Special = enum {
|
||||
None,
|
||||
Asm,
|
||||
RuntimeSafety,
|
||||
};
|
||||
|
||||
const TestCase = struct {
|
||||
name: []const u8,
|
||||
sources: ArrayList(SourceFile),
|
||||
expected_output: []const u8,
|
||||
link_libc: bool,
|
||||
special: Special,
|
||||
cli_args: []const []const u8,
|
||||
|
||||
const SourceFile = struct {
|
||||
filename: []const u8,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
||||
self.sources.append(SourceFile{
|
||||
.filename = filename,
|
||||
.source = source,
|
||||
}) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn setCommandLineArgs(self: *TestCase, args: []const []const u8) void {
|
||||
self.cli_args = args;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn createExtra(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
|
||||
var tc = TestCase{
|
||||
.name = name,
|
||||
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
||||
.expected_output = expected_output,
|
||||
.link_libc = false,
|
||||
.special = special,
|
||||
.cli_args = &[_][]const u8{},
|
||||
};
|
||||
const root_src_name = if (special == Special.Asm) "source.s" else "source.zig";
|
||||
tc.addSourceFile(root_src_name, source);
|
||||
return tc;
|
||||
}
|
||||
|
||||
pub fn create(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
|
||||
return createExtra(self, name, source, expected_output, Special.None);
|
||||
}
|
||||
|
||||
pub fn addC(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
||||
var tc = self.create(name, source, expected_output);
|
||||
tc.link_libc = true;
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn add(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
||||
const tc = self.create(name, source, expected_output);
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn addAsm(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
|
||||
const tc = self.createExtra(name, source, expected_output, Special.Asm);
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn addRuntimeSafety(self: *CompareOutputContext, name: []const u8, source: []const u8) void {
|
||||
const tc = self.createExtra(name, source, undefined, Special.RuntimeSafety);
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn addCase(self: *CompareOutputContext, case: TestCase) void {
|
||||
const b = self.b;
|
||||
|
||||
const write_src = b.addWriteFiles();
|
||||
for (case.sources.toSliceConst()) |src_file| {
|
||||
write_src.add(src_file.filename, src_file.source);
|
||||
}
|
||||
|
||||
switch (case.special) {
|
||||
Special.Asm => {
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "assemble-and-link {}", .{
|
||||
case.name,
|
||||
}) catch unreachable;
|
||||
if (self.test_filter) |filter| {
|
||||
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
||||
}
|
||||
|
||||
const exe = b.addExecutable("test", null);
|
||||
exe.addAssemblyFileFromWriteFileStep(write_src, case.sources.toSliceConst()[0].filename);
|
||||
|
||||
const run = exe.run();
|
||||
run.addArgs(case.cli_args);
|
||||
run.expectStdErrEqual("");
|
||||
run.expectStdOutEqual(case.expected_output);
|
||||
|
||||
self.step.dependOn(&run.step);
|
||||
},
|
||||
Special.None => {
|
||||
for (self.modes) |mode| {
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {} ({})", .{
|
||||
"compare-output",
|
||||
case.name,
|
||||
@tagName(mode),
|
||||
}) catch unreachable;
|
||||
if (self.test_filter) |filter| {
|
||||
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
|
||||
}
|
||||
|
||||
const basename = case.sources.toSliceConst()[0].filename;
|
||||
const exe = b.addExecutableFromWriteFileStep("test", write_src, basename);
|
||||
exe.setBuildMode(mode);
|
||||
if (case.link_libc) {
|
||||
exe.linkSystemLibrary("c");
|
||||
}
|
||||
|
||||
const run = exe.run();
|
||||
run.addArgs(case.cli_args);
|
||||
run.expectStdErrEqual("");
|
||||
run.expectStdOutEqual(case.expected_output);
|
||||
|
||||
self.step.dependOn(&run.step);
|
||||
}
|
||||
},
|
||||
Special.RuntimeSafety => {
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "safety {}", .{case.name}) catch unreachable;
|
||||
if (self.test_filter) |filter| {
|
||||
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
||||
}
|
||||
|
||||
const basename = case.sources.toSliceConst()[0].filename;
|
||||
const exe = b.addExecutableFromWriteFileStep("test", write_src, basename);
|
||||
if (case.link_libc) {
|
||||
exe.linkSystemLibrary("c");
|
||||
}
|
||||
|
||||
const run = exe.run();
|
||||
run.addArgs(case.cli_args);
|
||||
run.stderr_action = .ignore;
|
||||
run.stdout_action = .ignore;
|
||||
run.expected_exit_code = 126;
|
||||
|
||||
self.step.dependOn(&run.step);
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -33,89 +33,6 @@ pub const RunTranslatedCContext = struct {
|
||||
}
|
||||
};
|
||||
|
||||
const DoEverythingStep = struct {
|
||||
step: build.Step,
|
||||
context: *RunTranslatedCContext,
|
||||
name: []const u8,
|
||||
case: *const TestCase,
|
||||
test_index: usize,
|
||||
|
||||
pub fn create(
|
||||
context: *RunTranslatedCContext,
|
||||
name: []const u8,
|
||||
case: *const TestCase,
|
||||
) *DoEverythingStep {
|
||||
const allocator = context.b.allocator;
|
||||
const ptr = allocator.create(DoEverythingStep) catch unreachable;
|
||||
ptr.* = DoEverythingStep{
|
||||
.context = context,
|
||||
.name = name,
|
||||
.case = case,
|
||||
.test_index = context.test_index,
|
||||
.step = build.Step.init("RunTranslatedC", allocator, make),
|
||||
};
|
||||
context.test_index += 1;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
fn make(step: *build.Step) !void {
|
||||
const self = @fieldParentPtr(DoEverythingStep, "step", step);
|
||||
const b = self.context.b;
|
||||
|
||||
warn("Test {}/{} {}...", .{ self.test_index + 1, self.context.test_index, self.name });
|
||||
// translate from c to zig
|
||||
const translated_c_code = blk: {
|
||||
var zig_args = ArrayList([]const u8).init(b.allocator);
|
||||
defer zig_args.deinit();
|
||||
|
||||
const rel_c_filename = try fs.path.join(b.allocator, &[_][]const u8{
|
||||
b.cache_root,
|
||||
self.case.sources.toSliceConst()[0].filename,
|
||||
});
|
||||
|
||||
try zig_args.append(b.zig_exe);
|
||||
try zig_args.append("translate-c");
|
||||
try zig_args.append("-lc");
|
||||
try zig_args.append(b.pathFromRoot(rel_c_filename));
|
||||
|
||||
break :blk try b.exec(zig_args.toSliceConst());
|
||||
};
|
||||
|
||||
// write stdout to a file
|
||||
|
||||
const translated_c_path = try fs.path.join(b.allocator,
|
||||
&[_][]const u8{ b.cache_root, "translated_c.zig" });
|
||||
try fs.cwd().writeFile(translated_c_path, translated_c_code);
|
||||
|
||||
// zig run the result
|
||||
const run_stdout = blk: {
|
||||
var zig_args = ArrayList([]const u8).init(b.allocator);
|
||||
defer zig_args.deinit();
|
||||
|
||||
try zig_args.append(b.zig_exe);
|
||||
try zig_args.append("-lc");
|
||||
try zig_args.append("run");
|
||||
try zig_args.append(translated_c_path);
|
||||
|
||||
break :blk try b.exec(zig_args.toSliceConst());
|
||||
};
|
||||
// compare stdout
|
||||
if (!mem.eql(u8, self.case.expected_stdout, run_stdout)) {
|
||||
warn(
|
||||
\\
|
||||
\\========= Expected this output: =========
|
||||
\\{}
|
||||
\\========= But found: ====================
|
||||
\\{}
|
||||
\\
|
||||
, .{ self.case.expected_stdout, run_stdout });
|
||||
return error.TestFailed;
|
||||
}
|
||||
|
||||
warn("OK\n", .{});
|
||||
}
|
||||
};
|
||||
|
||||
pub fn create(
|
||||
self: *RunTranslatedCContext,
|
||||
allow_warnings: bool,
|
||||
@@ -159,22 +76,29 @@ pub const RunTranslatedCContext = struct {
|
||||
pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {
|
||||
const b = self.b;
|
||||
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "run-translated-c {}", .{ case.name }) catch unreachable;
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "run-translated-c {}", .{case.name}) catch unreachable;
|
||||
if (self.test_filter) |filter| {
|
||||
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
||||
}
|
||||
|
||||
const do_everything_step = DoEverythingStep.create(self, annotated_case_name, case);
|
||||
self.step.dependOn(&do_everything_step.step);
|
||||
|
||||
const write_src = b.addWriteFiles();
|
||||
for (case.sources.toSliceConst()) |src_file| {
|
||||
const expanded_src_path = fs.path.join(
|
||||
b.allocator,
|
||||
&[_][]const u8{ b.cache_root, src_file.filename },
|
||||
) catch unreachable;
|
||||
const write_src = b.addWriteFile(expanded_src_path, src_file.source);
|
||||
do_everything_step.step.dependOn(&write_src.step);
|
||||
write_src.add(src_file.filename, src_file.source);
|
||||
}
|
||||
const translate_c = b.addTranslateC(.{
|
||||
.write_file = .{
|
||||
.step = write_src,
|
||||
.basename = case.sources.toSliceConst()[0].filename,
|
||||
},
|
||||
});
|
||||
const exe = translate_c.addExecutable();
|
||||
exe.linkLibC();
|
||||
const run = exe.run();
|
||||
if (!case.allow_warnings) {
|
||||
run.expectStdErrEqual("");
|
||||
}
|
||||
run.expectStdOutEqual(case.expected_stdout);
|
||||
|
||||
self.step.dependOn(&run.step);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
109
test/src/translate_c.zig
Normal file
109
test/src/translate_c.zig
Normal file
@@ -0,0 +1,109 @@
|
||||
// This is the implementation of the test harness.
|
||||
// For the actual test cases, see test/translate_c.zig.
|
||||
const std = @import("std");
|
||||
const build = std.build;
|
||||
const ArrayList = std.ArrayList;
|
||||
const fmt = std.fmt;
|
||||
const mem = std.mem;
|
||||
const fs = std.fs;
|
||||
const warn = std.debug.warn;
|
||||
|
||||
pub const TranslateCContext = struct {
|
||||
b: *build.Builder,
|
||||
step: *build.Step,
|
||||
test_index: usize,
|
||||
test_filter: ?[]const u8,
|
||||
|
||||
const TestCase = struct {
|
||||
name: []const u8,
|
||||
sources: ArrayList(SourceFile),
|
||||
expected_lines: ArrayList([]const u8),
|
||||
allow_warnings: bool,
|
||||
|
||||
const SourceFile = struct {
|
||||
filename: []const u8,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
|
||||
self.sources.append(SourceFile{
|
||||
.filename = filename,
|
||||
.source = source,
|
||||
}) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
|
||||
self.expected_lines.append(text) catch unreachable;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn create(
|
||||
self: *TranslateCContext,
|
||||
allow_warnings: bool,
|
||||
filename: []const u8,
|
||||
name: []const u8,
|
||||
source: []const u8,
|
||||
expected_lines: []const []const u8,
|
||||
) *TestCase {
|
||||
const tc = self.b.allocator.create(TestCase) catch unreachable;
|
||||
tc.* = TestCase{
|
||||
.name = name,
|
||||
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
|
||||
.expected_lines = ArrayList([]const u8).init(self.b.allocator),
|
||||
.allow_warnings = allow_warnings,
|
||||
};
|
||||
|
||||
tc.addSourceFile(filename, source);
|
||||
var arg_i: usize = 0;
|
||||
while (arg_i < expected_lines.len) : (arg_i += 1) {
|
||||
tc.addExpectedLine(expected_lines[arg_i]);
|
||||
}
|
||||
return tc;
|
||||
}
|
||||
|
||||
pub fn add(
|
||||
self: *TranslateCContext,
|
||||
name: []const u8,
|
||||
source: []const u8,
|
||||
expected_lines: []const []const u8,
|
||||
) void {
|
||||
const tc = self.create(false, "source.h", name, source, expected_lines);
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn addAllowWarnings(
|
||||
self: *TranslateCContext,
|
||||
name: []const u8,
|
||||
source: []const u8,
|
||||
expected_lines: []const []const u8,
|
||||
) void {
|
||||
const tc = self.create(true, "source.h", name, source, expected_lines);
|
||||
self.addCase(tc);
|
||||
}
|
||||
|
||||
pub fn addCase(self: *TranslateCContext, case: *const TestCase) void {
|
||||
const b = self.b;
|
||||
|
||||
const translate_c_cmd = "translate-c";
|
||||
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {}", .{ translate_c_cmd, case.name }) catch unreachable;
|
||||
if (self.test_filter) |filter| {
|
||||
if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
|
||||
}
|
||||
|
||||
const write_src = b.addWriteFiles();
|
||||
for (case.sources.toSliceConst()) |src_file| {
|
||||
write_src.add(src_file.filename, src_file.source);
|
||||
}
|
||||
|
||||
const translate_c = b.addTranslateC(.{
|
||||
.write_file = .{
|
||||
.step = write_src,
|
||||
.basename = case.sources.toSliceConst()[0].filename,
|
||||
},
|
||||
});
|
||||
|
||||
const check_file = translate_c.addCheckFile(case.expected_lines.toSliceConst());
|
||||
|
||||
self.step.dependOn(&check_file.step);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user