aboutsummaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
authorManlio Perillo <[email protected]>2023-04-09 18:33:59 +0200
committerManlio Perillo <[email protected]>2023-04-11 12:45:55 +0200
commitc656536d3f322be31fffd88daa9a43683b11f693 (patch)
treed5f99539ec516fb56a92fa6f19bcb0dc8f5caccf /build.zig
parentd43f19ded87b4419d5fd5845bc34a0a18819a599 (diff)
downloadziglings-c656536d3f322be31fffd88daa9a43683b11f693.tar.xz
ziglings-c656536d3f322be31fffd88daa9a43683b11f693.zip
build: simplify code and add tests
Simplify the code finding the exercise number from the exercise index, when the -Dn option is set. This is now possible since the exercise numbers have no holes. Add the validate_exercises function to check that exercise number are in the correct order, and call it at the start of the build function. Add tests, with 2 test cases.
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig36
1 files changed, 29 insertions, 7 deletions
diff --git a/build.zig b/build.zig
index dbda63e..3281ed4 100644
--- a/build.zig
+++ b/build.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const compat = @import("src/compat.zig");
+const tests = @import("test/tests.zig");
const Build = compat.Build;
const Step = compat.build.Step;
@@ -8,7 +9,7 @@ const Step = compat.build.Step;
const assert = std.debug.assert;
const print = std.debug.print;
-const Exercise = struct {
+pub const Exercise = struct {
/// main_file must have the format key_name.zig.
/// The key will be used as a shorthand to build
/// just one example.
@@ -511,6 +512,7 @@ const exercises = [_]Exercise{
pub fn build(b: *Build) !void {
if (!compat.is_compatible) compat.die();
+ if (!validate_exercises()) std.os.exit(1);
use_color_escapes = false;
if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
@@ -558,15 +560,12 @@ pub fn build(b: *Build) !void {
const header_step = PrintStep.create(b, logo, std.io.getStdErr());
if (exno) |i| {
- const ex = blk: {
- for (exercises) |ex| {
- if (ex.number() == i) break :blk ex;
- }
-
+ if (i == 0 or i > exercises.len - 1) {
print("unknown exercise number: {}\n", .{i});
std.os.exit(1);
- };
+ }
+ const ex = exercises[i - 1];
const base_name = ex.baseName();
const file_path = std.fs.path.join(b.allocator, &[_][]const u8{
if (use_healed) "patches/healed" else "exercises", ex.main_file,
@@ -667,6 +666,9 @@ pub fn build(b: *Build) !void {
}
}
ziglings_step.dependOn(prev_step);
+
+ const test_step = b.step("test", "Run all the tests");
+ test_step.dependOn(tests.addCliTests(b, &exercises));
}
var use_color_escapes = false;
@@ -921,3 +923,23 @@ const SkipStep = struct {
}
}
};
+
+// Check that each exercise number, excluding the last, forms the sequence `[1, exercise.len)`.
+fn validate_exercises() bool {
+ // Don't use the "multi-object for loop" syntax, in order to avoid a syntax error with old Zig
+ // compilers.
+ var i: usize = 0;
+ for (exercises[0 .. exercises.len - 1]) |ex| {
+ i += 1;
+ if (ex.number() != i) {
+ print(
+ "exercise {s} has an incorrect number: expected {}, got {s}\n",
+ .{ ex.main_file, i, ex.key() },
+ );
+
+ return false;
+ }
+ }
+
+ return true;
+}