Merge pull request #275 from perillo/heal-only-in-test

Heal only in test
This commit is contained in:
Chris Boesch 2023-05-02 21:30:29 +02:00 committed by GitHub
commit 72a6287a5f
3 changed files with 68 additions and 32 deletions

View file

@ -27,21 +27,21 @@ jobs:
- name: Check compatibility with old Zig compilers - name: Check compatibility with old Zig compilers
run: ci/compat.sh run: ci/compat.sh
# test: test:
# name: Unit Tests name: Unit Tests
# strategy: strategy:
# matrix: matrix:
# os: [ubuntu-latest, windows-latest, macos-latest] os: [ubuntu-latest, windows-latest, macos-latest]
# runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
# timeout-minutes: 30 timeout-minutes: 30
# steps: steps:
# - name: Checkout - name: Checkout
# uses: actions/checkout@v3 uses: actions/checkout@v3
#
# - name: Setup Zig - name: Setup Zig
# uses: goto-bus-stop/setup-zig@v2 uses: goto-bus-stop/setup-zig@v2
# with: with:
# version: master version: master
#
# - name: Run unit tests - name: Run unit tests
# run: zig build test run: zig build test

View file

@ -210,9 +210,8 @@ pub fn build(b: *Build) !void {
} }
ziglings_step.dependOn(prev_step); ziglings_step.dependOn(prev_step);
// Disabled, see issue 272 const test_step = b.step("test", "Run all the tests");
// const test_step = b.step("test", "Run all the tests"); test_step.dependOn(tests.addCliTests(b, &exercises));
// // test_step.dependOn(tests.addCliTests(b, &exercises));
} }
var use_color_escapes = false; var use_color_escapes = false;

View file

@ -20,15 +20,14 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
// We should use a temporary path, but it will make the implementation of // We should use a temporary path, but it will make the implementation of
// `build.zig` more complex. // `build.zig` more complex.
const outdir = "patches/healed"; const work_path = "patches/healed";
fs.cwd().makePath(outdir) catch |err| { fs.cwd().makePath(work_path) catch |err| {
return fail(step, "unable to make '{s}': {s}\n", .{ outdir, @errorName(err) }); return fail(step, "unable to make '{s}': {s}\n", .{ work_path, @errorName(err) });
};
heal(b.allocator, exercises, outdir) catch |err| {
return fail(step, "unable to heal exercises: {s}\n", .{@errorName(err)});
}; };
const heal_step = HealStep.create(b, exercises, work_path);
{ {
// Test that `zig build -Dhealed -Dn=n test` selects the nth exercise. // Test that `zig build -Dhealed -Dn=n test` selects the nth exercise.
const case_step = createCase(b, "case-1"); const case_step = createCase(b, "case-1");
@ -49,6 +48,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
else else
expectStdErrMatch(cmd, ex.output); expectStdErrMatch(cmd, ex.output);
cmd.step.dependOn(&heal_step.step);
case_step.dependOn(&cmd.step); case_step.dependOn(&cmd.step);
} }
@ -72,6 +73,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
cmd.expectStdOutEqual(""); cmd.expectStdOutEqual("");
expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file})); expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));
cmd.step.dependOn(&heal_step.step);
case_step.dependOn(&cmd.step); case_step.dependOn(&cmd.step);
} }
@ -86,6 +89,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" }); const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" });
cmd.setName("zig build -Dhealed"); cmd.setName("zig build -Dhealed");
cmd.expectExitCode(0); cmd.expectExitCode(0);
cmd.step.dependOn(&heal_step.step);
const stderr = cmd.captureStdErr(); const stderr = cmd.captureStdErr();
const verify = CheckStep.create(b, exercises, stderr, true); const verify = CheckStep.create(b, exercises, stderr, true);
@ -107,6 +111,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
); );
cmd.setName("zig build -Dhealed -Dn=1 start"); cmd.setName("zig build -Dhealed -Dn=1 start");
cmd.expectExitCode(0); cmd.expectExitCode(0);
cmd.step.dependOn(&heal_step.step);
const stderr = cmd.captureStdErr(); const stderr = cmd.captureStdErr();
const verify = CheckStep.create(b, exercises, stderr, false); const verify = CheckStep.create(b, exercises, stderr, false);
@ -126,14 +131,16 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
cmd.expectExitCode(1); cmd.expectExitCode(1);
expectStdErrMatch(cmd, exercises[0].hint); expectStdErrMatch(cmd, exercises[0].hint);
cmd.step.dependOn(&heal_step.step);
case_step.dependOn(&cmd.step); case_step.dependOn(&cmd.step);
step.dependOn(case_step); step.dependOn(case_step);
} }
// Don't add the cleanup step, since it may delete outdir while a test case // Don't add the cleanup step, since it may delete work_path while a test
// is running. // case is running.
//const cleanup = b.addRemoveDirTree(outdir); //const cleanup = b.addRemoveDirTree(work_path);
//step.dependOn(&cleanup.step); //step.dependOn(&cleanup.step);
return step; return step;
@ -315,8 +322,38 @@ fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
return step; return step;
} }
// A step that heals 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, _: *std.Progress.Node) !void {
const b = step.owner;
const self = @fieldParentPtr(HealStep, "step", step);
return heal(b.allocator, self.exercises, self.work_path);
}
};
// Heals all the exercises. // Heals all the exercises.
fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !void { fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void {
const join = fs.path.join; const join = fs.path.join;
const exercises_path = "exercises"; const exercises_path = "exercises";
@ -331,7 +368,7 @@ fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !
const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name}); const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
break :b try join(allocator, &.{ patches_path, patch_name }); break :b try join(allocator, &.{ patches_path, patch_name });
}; };
const output = try join(allocator, &.{ outdir, ex.main_file }); const output = try join(allocator, &.{ work_path, ex.main_file });
const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file }; const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file };