integrate tests into zig build API
This commit is contained in:
parent
29dc3ffdf4
commit
80ab6949d2
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -39,4 +39,4 @@ jobs:
|
|||||||
- name: Zig environment variables
|
- name: Zig environment variables
|
||||||
run: zig env
|
run: zig env
|
||||||
- name: Test
|
- name: Test
|
||||||
run: zig build test
|
run: zig build test -Dall --summary all
|
||||||
|
70
build.zig
70
build.zig
@ -1,14 +1,13 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
var all: bool = false;
|
||||||
|
|
||||||
pub fn build(b: *std.Build) void {
|
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 target = b.standardTargetOptions(.{});
|
const name = if (all) try std.mem.concat(b.allocator, u8, &.{ "nativity_", @tagName(optimization) }) else "nativity";
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "compiler",
|
.name = name,
|
||||||
.root_source_file = .{ .path = "src/main.zig" },
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimization,
|
||||||
});
|
});
|
||||||
|
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
@ -26,26 +25,29 @@ pub fn build(b: *std.Build) void {
|
|||||||
run_cmd.addArgs(args);
|
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);
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
const debug_command = addDebugCommand(b, exe);
|
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);
|
debug_step.dependOn(&debug_command.step);
|
||||||
|
|
||||||
const unit_tests = b.addTest(.{
|
const zig_tests = b.addTest(.{
|
||||||
.root_source_file = .{ .path = "src/main.zig" },
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimization,
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_unit_tests = b.addRunArtifact(unit_tests);
|
const run_zig_tests = b.addRunArtifact(zig_tests);
|
||||||
const test_step = b.step("test", "Run unit tests");
|
run_zig_tests.has_side_effects = true;
|
||||||
test_step.dependOn(&run_unit_tests.step);
|
test_step.dependOn(&run_zig_tests.step);
|
||||||
|
|
||||||
const debug_unit_tests_cmd = addDebugCommand(b, unit_tests);
|
for (unit_tests) |unit_test_main_source_file| {
|
||||||
const debug_test_step = b.step("debug_test", "Run the tests through the debugger");
|
const unit_test = b.addRunArtifact(exe);
|
||||||
debug_test_step.dependOn(&debug_unit_tests_cmd.step);
|
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 {
|
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"),
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
15
src/main.zig
15
src/main.zig
@ -8,11 +8,16 @@ pub const seed = std.math.maxInt(u64);
|
|||||||
const default_src_file = "src/test/main.nat";
|
const default_src_file = "src/test/main.nat";
|
||||||
|
|
||||||
pub fn main() !void {
|
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 {
|
fn singleCompilation(allocator: Allocator, main_file_path: []const u8) !void {
|
||||||
const allocator = std.heap.page_allocator;
|
|
||||||
const compilation = try Compilation.init(allocator);
|
const compilation = try Compilation.init(allocator);
|
||||||
|
|
||||||
try compilation.compileModule(.{
|
try compilation.compileModule(.{
|
||||||
@ -23,7 +28,3 @@ fn singleCompilation(main_file_path: []const u8) !void {
|
|||||||
test {
|
test {
|
||||||
_ = Compilation;
|
_ = Compilation;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "basic" {
|
|
||||||
try singleCompilation(default_src_file);
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user