Merge pull request #9 from birth-software/zig-build-test

integrate tests into zig build API
This commit is contained in:
David 2023-10-02 19:13:42 -06:00 committed by GitHub
commit f63fcbcb62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 24 deletions

View File

@ -39,4 +39,4 @@ jobs:
- name: Zig environment variables
run: zig env
- name: Test
run: zig build test
run: zig build test -Dall --summary all

View File

@ -1,14 +1,13 @@
const std = @import("std");
var all: bool = false;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
fn everythingForTargetAndOptimization(b: *std.Build, target: std.zig.CrossTarget, optimization: std.builtin.OptimizeMode, unit_tests: []const []const u8, test_step: *std.Build.Step) !void {
const name = if (all) try std.mem.concat(b.allocator, u8, &.{ "nativity_", @tagName(optimization) }) else "nativity";
const exe = b.addExecutable(.{
.name = "compiler",
.name = name,
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
.optimize = optimization,
});
b.installArtifact(exe);
@ -26,26 +25,29 @@ pub fn build(b: *std.Build) void {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
const run_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "run_", @tagName(optimization) }) else "run", "Run the app");
run_step.dependOn(&run_cmd.step);
const debug_command = addDebugCommand(b, exe);
const debug_step = b.step("debug", "Debug the app");
const debug_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "debug_", @tagName(optimization) }) else "debug", "Debug the app");
debug_step.dependOn(&debug_command.step);
const unit_tests = b.addTest(.{
const zig_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
.optimize = optimization,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
const run_zig_tests = b.addRunArtifact(zig_tests);
run_zig_tests.has_side_effects = true;
test_step.dependOn(&run_zig_tests.step);
const debug_unit_tests_cmd = addDebugCommand(b, unit_tests);
const debug_test_step = b.step("debug_test", "Run the tests through the debugger");
debug_test_step.dependOn(&debug_unit_tests_cmd.step);
for (unit_tests) |unit_test_main_source_file| {
const unit_test = b.addRunArtifact(exe);
unit_test.has_side_effects = true;
unit_test.addArg(unit_test_main_source_file);
test_step.dependOn(&unit_test.step);
}
}
fn addDebugCommand(b: *std.Build, artifact: *std.Build.Step.Compile) *std.Build.Step.Run {
@ -76,3 +78,39 @@ fn addDebugCommand(b: *std.Build, artifact: *std.Build.Step.Compile) *std.Build.
else => @compileError("Operating system not supported"),
};
}
pub fn build(b: *std.Build) !void {
all = b.option(bool, "all", "All") orelse false;
var unit_test_list = std.ArrayList([]const u8).init(b.allocator);
var test_dir = try std.fs.cwd().openIterableDir("test", .{ .access_sub_paths = true });
defer test_dir.close();
var test_dir_iterator = test_dir.iterate();
while (try test_dir_iterator.next()) |entry| {
switch (entry.kind) {
.directory => {
const dir_name = entry.name;
const main_unit_test_source_file = try std.mem.concat(b.allocator, u8, &.{ "test/", dir_name, "/main.nat" });
try unit_test_list.append(main_unit_test_source_file);
},
.file => {},
else => @panic("Don't put crap on test directory"),
}
}
const target = b.standardTargetOptions(.{});
const unit_tests = unit_test_list.items;
const test_step = b.step("test", "Test the Nativity compiler");
if (all) {
inline for (@typeInfo(std.builtin.OptimizeMode).Enum.fields) |enum_field| {
const optimization = @field(std.builtin.OptimizeMode, enum_field.name);
try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step);
}
} else {
const optimization = b.standardOptimizeOption(.{});
_ = try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step);
}
}

View File

@ -8,11 +8,16 @@ pub const seed = std.math.maxInt(u64);
const default_src_file = "src/test/main.nat";
pub fn main() !void {
try singleCompilation(default_src_file);
const allocator = std.heap.page_allocator;
const arguments = try std.process.argsAlloc(allocator);
if (arguments.len == 2) {
try singleCompilation(allocator, arguments[1]);
} else {
@panic("Wrong arguments");
}
}
fn singleCompilation(main_file_path: []const u8) !void {
const allocator = std.heap.page_allocator;
fn singleCompilation(allocator: Allocator, main_file_path: []const u8) !void {
const compilation = try Compilation.init(allocator);
try compilation.compileModule(.{
@ -23,7 +28,3 @@ fn singleCompilation(main_file_path: []const u8) !void {
test {
_ = Compilation;
}
test "basic" {
try singleCompilation(default_src_file);
}