From 5c20e2b55338e829b2c2db2bbd5b1fe410b656af Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 18:48:52 +0200 Subject: [PATCH] build: rename Exercise.baseName to name The name "baseName" is confusing, since it refers to the UNIX basename command, and not to the basename function in some programming languages including Zig. Use the std.fs.path.stem function to remove the file extension, instead of slicing. Remove the use of the assertion, since it is no longer necessary. Instead, add a check to ensure that the exercise must be a Zig source file in the validate_exercises function. Update the validate_exercises function to check the last exercise, too. --- build.zig | 20 ++++++++++++++------ test/tests.zig | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index b9c59d6..e1f5556 100644 --- a/build.zig +++ b/build.zig @@ -39,9 +39,8 @@ pub const Exercise = struct { skip: bool = false, /// Returns the name of the main file with .zig stripped. - pub fn baseName(self: Exercise) []const u8 { - assert(std.mem.endsWith(u8, self.main_file, ".zig")); - return self.main_file[0 .. self.main_file.len - 4]; + pub fn name(self: Exercise) []const u8 { + return std.fs.path.stem(self.main_file); } /// Returns the key of the main file, the string before the '_' with @@ -68,7 +67,7 @@ pub const Exercise = struct { @panic("OOM"); return b.addExecutable(.{ - .name = self.baseName(), + .name = self.name(), .root_source_file = .{ .path = file_path }, .link_libc = self.link_libc, }); @@ -598,9 +597,12 @@ 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| { + for (exercises[0..]) |ex| { + const exno = ex.number(); + const last = 999; i += 1; - if (ex.number() != i) { + + if (exno != i and exno != last) { print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{ ex.main_file, i, @@ -618,6 +620,12 @@ fn validate_exercises() bool { return false; } + + if (!std.mem.endsWith(u8, ex.main_file, ".zig")) { + print("exercise {s} is not a zig source file\n", .{ex.main_file}); + + return false; + } } return true; diff --git a/test/tests.zig b/test/tests.zig index f9ad9c4..8786d91 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -323,7 +323,7 @@ fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) ! const patches_path = "patches/patches"; for (exercises) |ex| { - const name = ex.baseName(); + const name = ex.name(); // Use the POSIX patch variant. const file = try join(allocator, &.{ exercises_path, ex.main_file });