mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
Merge pull request 'started fix for new build system' (#437) from build-system-fix into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/437
This commit is contained in:
@@ -193,6 +193,7 @@ Zig Standard Library
|
||||
|
||||
### Version Changes
|
||||
|
||||
* 2026-05-31 zig 0.16.0-dev.2915 - zig build: separate the maker process from the configurer process, see[#35428](https://codeberg.org/ziglang/zig/pulls/35428)
|
||||
* 2026-03-20 zig 0.16.0-dev.2915 - `GeneralPurposeAllocator` was changed to `DebugAllocator`
|
||||
* 2026-02-04 zig 0.16.0-dev.2471 - added process.Child.Cwd, see [#31090](https://codeberg.org/ziglang/zig/pulls/31090)
|
||||
* 2026-01-09 zig 0.16.0-dev.2075 - move randomness API to `std.Io`, see [#30709](https://codeberg.org/ziglang/zig/pulls/30709)
|
||||
|
||||
1234
src/elrond.zig
Normal file
1234
src/elrond.zig
Normal file
File diff suppressed because it is too large
Load Diff
413
test/tests.zig
413
test/tests.zig
@@ -1,413 +0,0 @@
|
||||
const std = @import("std");
|
||||
const root = @import("../build.zig");
|
||||
|
||||
const debug = std.debug;
|
||||
const fmt = std.fmt;
|
||||
const mem = std.mem;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Process = std.process;
|
||||
const Build = std.Build;
|
||||
const Step = Build.Step;
|
||||
const RunStep = Build.RunStep;
|
||||
const LazyPath = Build.LazyPath;
|
||||
|
||||
const Exercise = root.Exercise;
|
||||
|
||||
pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
|
||||
const step = b.step("test-cli", "Test the command line interface");
|
||||
|
||||
{
|
||||
// Test that `zig build -Dhealed -Dn=n` selects the nth exercise.
|
||||
const case_step = createCase(b, "case-1");
|
||||
|
||||
const tmp_path = createTempPath(b) catch |err| {
|
||||
return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)});
|
||||
};
|
||||
defer deleteTmpPath(b, tmp_path);
|
||||
|
||||
const heal_step = HealStep.create(b, exercises, tmp_path);
|
||||
|
||||
for (exercises[0 .. exercises.len - 1]) |ex| {
|
||||
const n = ex.number();
|
||||
|
||||
const cmd = b.addSystemCommand(&.{
|
||||
b.graph.zig_exe,
|
||||
"build",
|
||||
"-Dhealed",
|
||||
b.fmt("-Dhealed-path={s}", .{tmp_path}),
|
||||
b.fmt("-Dn={}", .{n}),
|
||||
});
|
||||
cmd.setName(b.fmt("zig build -Dhealed -Dn={}", .{n}));
|
||||
cmd.expectExitCode(0);
|
||||
cmd.step.dependOn(&heal_step.step);
|
||||
|
||||
const stderr = cmd.captureStdErr(.{});
|
||||
const verify = CheckNamedStep.create(b, ex, stderr);
|
||||
verify.step.dependOn(&cmd.step);
|
||||
|
||||
case_step.dependOn(&verify.step);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Test that `zig build -Dhealed` processes all the exercises in order.
|
||||
const case_step = createCase(b, "case-2");
|
||||
|
||||
const tmp_path = createTempPath(b) catch |err| {
|
||||
return fail(step, "unable to make tmp path: {s}\n", .{@errorName(err)});
|
||||
};
|
||||
defer deleteTmpPath(b, tmp_path);
|
||||
|
||||
const heal_step = HealStep.create(b, exercises, tmp_path);
|
||||
heal_step.step.dependOn(case_step);
|
||||
|
||||
// TODO: when an exercise is modified, the cache is not invalidated.
|
||||
const cmd = b.addSystemCommand(&.{
|
||||
b.graph.zig_exe,
|
||||
"build",
|
||||
"-Dhealed",
|
||||
b.fmt("-Dhealed-path={s}", .{tmp_path}),
|
||||
});
|
||||
cmd.setName("zig build -Dhealed");
|
||||
cmd.expectExitCode(0);
|
||||
cmd.step.dependOn(&heal_step.step);
|
||||
|
||||
const stderr = cmd.captureStdErr(.{});
|
||||
const verify = CheckStep.create(b, exercises, stderr);
|
||||
verify.step.dependOn(&cmd.step);
|
||||
}
|
||||
|
||||
{
|
||||
// Test that `zig build -Dn=n` prints the hint.
|
||||
const case_step = createCase(b, "case-3");
|
||||
|
||||
for (exercises[0 .. exercises.len - 1]) |ex| {
|
||||
if (ex.skip) continue;
|
||||
|
||||
if (ex.hint) |hint| {
|
||||
const n = ex.number();
|
||||
|
||||
const cmd = b.addSystemCommand(&.{
|
||||
b.graph.zig_exe,
|
||||
"build",
|
||||
b.fmt("-Dn={}", .{n}),
|
||||
});
|
||||
cmd.setName(b.fmt("zig build -Dn={}", .{n}));
|
||||
cmd.expectExitCode(2);
|
||||
cmd.addCheck(.{ .expect_stderr_match = hint });
|
||||
|
||||
case_step.dependOn(&cmd.step);
|
||||
}
|
||||
}
|
||||
|
||||
step.dependOn(case_step);
|
||||
}
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
fn createCase(b: *Build, name: []const u8) *Step {
|
||||
const case_step = b.allocator.create(Step) catch @panic("OOM");
|
||||
case_step.* = Step.init(.{
|
||||
.id = .custom,
|
||||
.name = name,
|
||||
.owner = b,
|
||||
});
|
||||
|
||||
return case_step;
|
||||
}
|
||||
|
||||
/// Checks the output of `zig build -Dn=n`.
|
||||
const CheckNamedStep = struct {
|
||||
step: Step,
|
||||
exercise: Exercise,
|
||||
stderr: LazyPath,
|
||||
|
||||
pub fn create(owner: *Build, exercise: Exercise, stderr: LazyPath) *CheckNamedStep {
|
||||
const self = owner.allocator.create(CheckNamedStep) catch @panic("OOM");
|
||||
self.* = .{
|
||||
.step = Step.init(.{
|
||||
.id = .custom,
|
||||
.name = "check-named",
|
||||
.owner = owner,
|
||||
.makeFn = make,
|
||||
}),
|
||||
.exercise = exercise,
|
||||
.stderr = stderr,
|
||||
};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
fn make(step: *Step, _: Step.MakeOptions) !void {
|
||||
const b = step.owner;
|
||||
const io = b.graph.io;
|
||||
const self: *CheckNamedStep = @alignCast(@fieldParentPtr("step", step));
|
||||
const ex = self.exercise;
|
||||
|
||||
const stderr_file = try std.Io.Dir.cwd().openFile(
|
||||
io,
|
||||
self.stderr.getPath(b),
|
||||
.{ .mode = .read_only },
|
||||
);
|
||||
defer stderr_file.close(io);
|
||||
|
||||
var stderr = stderr_file.readerStreaming(io, &.{});
|
||||
{
|
||||
// Skip the logo.
|
||||
const nlines = mem.count(u8, root.logo, "\n");
|
||||
var buf: [80]u8 = undefined;
|
||||
|
||||
var lineno: usize = 0;
|
||||
while (lineno < nlines) : (lineno += 1) {
|
||||
_ = try readLine(&stderr, &buf);
|
||||
}
|
||||
}
|
||||
try check_output(step, ex, &stderr);
|
||||
}
|
||||
};
|
||||
|
||||
/// Checks the output of `zig build`.
|
||||
const CheckStep = struct {
|
||||
step: Step,
|
||||
exercises: []const Exercise,
|
||||
stderr: LazyPath,
|
||||
|
||||
pub fn create(
|
||||
owner: *Build,
|
||||
exercises: []const Exercise,
|
||||
stderr: LazyPath,
|
||||
) *CheckStep {
|
||||
const self = owner.allocator.create(CheckStep) catch @panic("OOM");
|
||||
self.* = .{
|
||||
.step = Step.init(.{
|
||||
.id = .custom,
|
||||
.name = "check",
|
||||
.owner = owner,
|
||||
.makeFn = make,
|
||||
}),
|
||||
.exercises = exercises,
|
||||
.stderr = stderr,
|
||||
};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
fn make(step: *Step, _: Step.MakeOptions) !void {
|
||||
const b = step.owner;
|
||||
const io = b.graph.io;
|
||||
const self: *CheckStep = @alignCast(@fieldParentPtr("step", step));
|
||||
const exercises = self.exercises;
|
||||
|
||||
const stderr_file = try std.Io.Dir.cwd().openFile(
|
||||
io,
|
||||
self.stderr.getPath(b),
|
||||
.{ .mode = .read_only },
|
||||
);
|
||||
defer stderr_file.close(io);
|
||||
|
||||
var stderr = stderr_file.readerStreaming(io, &.{});
|
||||
for (exercises) |ex| {
|
||||
if (ex.number() == 1) {
|
||||
// Skip the logo.
|
||||
const nlines = mem.count(u8, root.logo, "\n");
|
||||
var buf: [80]u8 = undefined;
|
||||
|
||||
var lineno: usize = 0;
|
||||
while (lineno < nlines) : (lineno += 1) {
|
||||
_ = try readLine(&stderr, &buf);
|
||||
}
|
||||
}
|
||||
try check_output(step, ex, &stderr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn check_output(step: *Step, exercise: Exercise, reader: *std.Io.File.Reader) !void {
|
||||
const b = step.owner;
|
||||
|
||||
var buf: [1024]u8 = undefined;
|
||||
if (exercise.skip) {
|
||||
{
|
||||
const actual = try readLine(reader, &buf) orelse "EOF";
|
||||
const expect = b.fmt("Skipping {s}", .{exercise.main_file});
|
||||
try check(step, exercise, expect, actual);
|
||||
}
|
||||
|
||||
{
|
||||
const actual = try readLine(reader, &buf) orelse "EOF";
|
||||
try check(step, exercise, "", actual);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
const actual = try readLine(reader, &buf) orelse "EOF";
|
||||
const expect = b.fmt("Compiling {s}...", .{exercise.main_file});
|
||||
try check(step, exercise, expect, actual);
|
||||
}
|
||||
|
||||
{
|
||||
const actual = try readLine(reader, &buf) orelse "EOF";
|
||||
const expect = b.fmt("Checking {s}...", .{exercise.main_file});
|
||||
try check(step, exercise, expect, actual);
|
||||
}
|
||||
|
||||
{
|
||||
const actual = try readLine(reader, &buf) orelse "EOF";
|
||||
const expect = switch (exercise.kind) {
|
||||
.exe => "PASSED:",
|
||||
.@"test" => "PASSED",
|
||||
};
|
||||
try check(step, exercise, expect, actual);
|
||||
}
|
||||
|
||||
// Skip the exercise output.
|
||||
const nlines = switch (exercise.kind) {
|
||||
.exe => 1 + mem.count(u8, exercise.output, "\n") + 1,
|
||||
.@"test" => 1,
|
||||
};
|
||||
|
||||
var lineno: usize = 0;
|
||||
while (lineno < nlines) : (lineno += 1) {
|
||||
_ = try readLine(reader, &buf) orelse @panic("EOF");
|
||||
}
|
||||
}
|
||||
|
||||
fn check(
|
||||
step: *Step,
|
||||
exercise: Exercise,
|
||||
expect: []const u8,
|
||||
actual: []const u8,
|
||||
) !void {
|
||||
if (!mem.eql(u8, expect, actual)) {
|
||||
return step.fail("{s}: expected to see \"{s}\", found \"{s}\"", .{
|
||||
exercise.main_file,
|
||||
expect,
|
||||
actual,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn readLine(reader: *std.Io.File.Reader, buf: []u8) !?[]const u8 {
|
||||
try reader.interface.readSliceAll(buf);
|
||||
return mem.trimEnd(u8, buf, " \r\n");
|
||||
}
|
||||
|
||||
/// Fails with a custom error message.
|
||||
const FailStep = struct {
|
||||
step: Step,
|
||||
error_msg: []const u8,
|
||||
|
||||
pub fn create(owner: *Build, error_msg: []const u8) *FailStep {
|
||||
const self = owner.allocator.create(FailStep) catch @panic("OOM");
|
||||
self.* = .{
|
||||
.step = Step.init(.{
|
||||
.id = .custom,
|
||||
.name = "fail",
|
||||
.owner = owner,
|
||||
.makeFn = make,
|
||||
}),
|
||||
.error_msg = error_msg,
|
||||
};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
fn make(step: *Step, _: Step.MakeOptions) !void {
|
||||
const b = step.owner;
|
||||
const self: *FailStep = @alignCast(@fieldParentPtr("step", step));
|
||||
|
||||
try step.result_error_msgs.append(b.allocator, self.error_msg);
|
||||
return error.MakeFailed;
|
||||
}
|
||||
};
|
||||
|
||||
/// A variant of `std.Build.Step.fail` that does not return an error so that it
|
||||
/// can be used in the configuration phase. It returns a FailStep, so that the
|
||||
/// error will be cleanly handled by the build runner.
|
||||
fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
|
||||
const b = step.owner;
|
||||
|
||||
const fail_step = FailStep.create(b, b.fmt(format, args));
|
||||
step.dependOn(&fail_step.step);
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
/// Heals the exercises.
|
||||
const HealStep = struct {
|
||||
step: Step,
|
||||
exercises: []const Exercise,
|
||||
work_path: []const u8,
|
||||
|
||||
pub fn create(owner: *Build, exercises: []const Exercise, work_path: []const u8) *HealStep {
|
||||
const self = owner.allocator.create(HealStep) catch @panic("OOM");
|
||||
self.* = .{
|
||||
.step = Step.init(.{
|
||||
.id = .custom,
|
||||
.name = "heal",
|
||||
.owner = owner,
|
||||
.makeFn = make,
|
||||
}),
|
||||
.exercises = exercises,
|
||||
.work_path = work_path,
|
||||
};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
fn make(step: *Step, _: Step.MakeOptions) !void {
|
||||
const b = step.owner;
|
||||
const self: *HealStep = @alignCast(@fieldParentPtr("step", step));
|
||||
|
||||
return heal(b.allocator, self.exercises, self.work_path);
|
||||
}
|
||||
};
|
||||
|
||||
/// Heals all the exercises.
|
||||
fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void {
|
||||
const io = std.Options.debug_io;
|
||||
const sep = std.Io.Dir.path.sep_str;
|
||||
const join = std.Io.Dir.path.join;
|
||||
|
||||
const exercises_path = "exercises";
|
||||
const patches_path = "patches" ++ sep ++ "patches";
|
||||
|
||||
for (exercises) |ex| {
|
||||
const name = ex.name();
|
||||
|
||||
const file = try join(allocator, &.{ exercises_path, ex.main_file });
|
||||
const patch = b: {
|
||||
const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
|
||||
break :b try join(allocator, &.{ patches_path, patch_name });
|
||||
};
|
||||
const output = try join(allocator, &.{ work_path, ex.main_file });
|
||||
|
||||
const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file };
|
||||
|
||||
_ = try Process.run(allocator, io, .{ .argv = argv });
|
||||
}
|
||||
}
|
||||
|
||||
fn createTempPath(b: *Build) ![]const u8 {
|
||||
const io = b.graph.io;
|
||||
const rand_int = r: {
|
||||
var x: u64 = undefined;
|
||||
io.random(@ptrCast(&x));
|
||||
break :r x;
|
||||
};
|
||||
const tmp_dir_sub_path = "tmp" ++ std.Io.Dir.path.sep_str ++ std.fmt.hex(rand_int);
|
||||
const result_path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch @panic("OOM");
|
||||
try b.cache_root.handle.createDirPath(io, tmp_dir_sub_path);
|
||||
return result_path;
|
||||
}
|
||||
|
||||
fn deleteTmpPath(b: *Build, path: []const u8) void {
|
||||
const io = b.graph.io;
|
||||
std.Io.Dir.cwd().deleteTree(io, path) catch |err| {
|
||||
std.log.warn("failed to delete {s}: {t}", .{ path, err });
|
||||
};
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
const string = []const u8;
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
const Dir = std.fs.Dir;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const EXERCISES_PATH = "exercises";
|
||||
const HEALED_PATH = "patches/healed";
|
||||
const TEMP_PATH = "patches/healed/tmp";
|
||||
const PATCHES_PATH = "patches/patches";
|
||||
|
||||
// Heals all the exercises.
|
||||
fn heal(alloc: Allocator) !void {
|
||||
try cwd.makePath(HEALED_PATH);
|
||||
|
||||
const org_path = try cwd.realpathAlloc(alloc, EXERCISES_PATH);
|
||||
const patch_path = try cwd.realpathAlloc(alloc, PATCHES_PATH);
|
||||
const healed_path = try cwd.realpathAlloc(alloc, HEALED_PATH);
|
||||
|
||||
var idir = try cwd.openIterableDir(EXERCISES_PATH, Dir.OpenDirOptions{});
|
||||
defer idir.close();
|
||||
|
||||
var it = idir.iterate();
|
||||
while (try it.next()) |entry| {
|
||||
|
||||
// create filenames
|
||||
const healed_file = try concat(alloc, &.{ healed_path, "/", entry.name });
|
||||
const patch_file = try concat(alloc, &.{ patch_path, "/", try patch_name(alloc, entry.name) });
|
||||
|
||||
// patch file
|
||||
const result = try std.ChildProcess.exec(.{
|
||||
.allocator = alloc,
|
||||
.argv = &.{ "patch", "-i", patch_file, "-o", healed_file, entry.name },
|
||||
.cwd = org_path,
|
||||
});
|
||||
|
||||
print("{s}", .{result.stderr});
|
||||
}
|
||||
}
|
||||
|
||||
// Yields all the healed exercises that are not correctly formatted.
|
||||
fn check_healed(alloc: Allocator) !void {
|
||||
try cwd.makePath(TEMP_PATH);
|
||||
|
||||
const temp_path = try cwd.realpathAlloc(alloc, TEMP_PATH);
|
||||
const healed_path = try cwd.realpathAlloc(alloc, HEALED_PATH);
|
||||
|
||||
var idir = try cwd.openIterableDir(HEALED_PATH, Dir.OpenDirOptions{});
|
||||
defer idir.close();
|
||||
|
||||
var it = idir.iterate();
|
||||
while (try it.next()) |entry| {
|
||||
|
||||
// Check the healed file
|
||||
const result = try std.ChildProcess.exec(.{
|
||||
.allocator = alloc,
|
||||
.argv = &.{ "zig", "fmt", "--check", entry.name },
|
||||
.cwd = healed_path,
|
||||
});
|
||||
|
||||
// Is there something to fix?
|
||||
if (result.stdout.len > 0) {
|
||||
const temp_file = try concat(alloc, &.{ temp_path, "/", entry.name });
|
||||
const healed_file = try concat(alloc, &.{ healed_path, "/", entry.name });
|
||||
try std.fs.copyFileAbsolute(healed_file, temp_file, std.fs.CopyFileOptions{});
|
||||
|
||||
// Formats the temp file
|
||||
_ = try std.ChildProcess.exec(.{
|
||||
.allocator = alloc,
|
||||
.argv = &.{ "zig", "fmt", entry.name },
|
||||
.cwd = temp_path,
|
||||
});
|
||||
|
||||
// Show the differences
|
||||
const diff = try std.ChildProcess.exec(.{
|
||||
.allocator = alloc,
|
||||
.argv = &.{ "diff", "-c", healed_file, entry.name },
|
||||
.cwd = temp_path,
|
||||
});
|
||||
|
||||
print("{s}", .{diff.stdout});
|
||||
try std.fs.deleteFileAbsolute(temp_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn concat(alloc: Allocator, slices: []const string) !string {
|
||||
const buf = try std.mem.concat(alloc, u8, slices);
|
||||
return buf;
|
||||
}
|
||||
|
||||
fn patch_name(alloc: Allocator, path: string) !string {
|
||||
var filename = path;
|
||||
const index = std.mem.lastIndexOfScalar(u8, path, '.') orelse return path;
|
||||
if (index > 0) filename = path[0..index];
|
||||
return try concat(alloc, &.{ filename, ".patch" });
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
try heal(alloc);
|
||||
try check_healed(alloc);
|
||||
}
|
||||
Reference in New Issue
Block a user