| ... | @@ -1,140 +0,0 @@ |
| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); |
| 3 | |
| 4 | pub fn build(b: *std.Build) void { |
| 5 | const test_step = b.step("test", "Test it"); |
| 6 | b.default_step = test_step; |
| 7 | |
| 8 | if (builtin.os.tag == .windows) return; // https://codeberg.org/ziglang/zig/issues/31564 |
| 9 | |
| 10 | const target = b.standardTargetOptions(.{}); |
| 11 | const optimize = b.standardOptimizeOption(.{}); |
| 12 | |
| 13 | const exe = b.addExecutable(.{ |
| 14 | .name = "create-file", |
| 15 | .root_module = b.createModule(.{ |
| 16 | .root_source_file = b.path("main.zig"), |
| 17 | .target = target, |
| 18 | .optimize = optimize, |
| 19 | }), |
| 20 | }); |
| 21 | |
| 22 | { |
| 23 | const run_random_with_sideeffects_first = b.addRunArtifact(exe); |
| 24 | run_random_with_sideeffects_first.setName("run with side-effects (first)"); |
| 25 | run_random_with_sideeffects_first.has_side_effects = true; |
| 26 | |
| 27 | const run_random_with_sideeffects_second = b.addRunArtifact(exe); |
| 28 | run_random_with_sideeffects_second.setName("run with side-effects (second)"); |
| 29 | run_random_with_sideeffects_second.has_side_effects = true; |
| 30 | |
| 31 | // ensure that "second" runs after "first" |
| 32 | run_random_with_sideeffects_second.step.dependOn(&run_random_with_sideeffects_first.step); |
| 33 | |
| 34 | const first_output = run_random_with_sideeffects_first.addOutputFileArg("a.txt"); |
| 35 | const second_output = run_random_with_sideeffects_second.addOutputFileArg("a.txt"); |
| 36 | |
| 37 | const expect_uncached_dependencies = CheckOutputCaching.init(b, false, &.{ first_output, second_output }); |
| 38 | test_step.dependOn(&expect_uncached_dependencies.step); |
| 39 | |
| 40 | const expect_unequal_output = CheckPathEquality.init(b, true, &.{ first_output, second_output }); |
| 41 | test_step.dependOn(&expect_unequal_output.step); |
| 42 | |
| 43 | const check_first_output = b.addCheckFile(first_output, .{ .expected_matches = &.{"a.txt"} }); |
| 44 | test_step.dependOn(&check_first_output.step); |
| 45 | const check_second_output = b.addCheckFile(second_output, .{ .expected_matches = &.{"a.txt"} }); |
| 46 | test_step.dependOn(&check_second_output.step); |
| 47 | } |
| 48 | |
| 49 | { |
| 50 | const run_random_without_sideeffects_1 = b.addRunArtifact(exe); |
| 51 | run_random_without_sideeffects_1.setName("run without side-effects (A)"); |
| 52 | |
| 53 | const run_random_without_sideeffects_2 = b.addRunArtifact(exe); |
| 54 | run_random_without_sideeffects_2.setName("run without side-effects (B)"); |
| 55 | |
| 56 | run_random_without_sideeffects_2.step.dependOn(&run_random_without_sideeffects_1.step); |
| 57 | |
| 58 | const first_output = run_random_without_sideeffects_1.addOutputFileArg("a.txt"); |
| 59 | const second_output = run_random_without_sideeffects_2.addOutputFileArg("a.txt"); |
| 60 | |
| 61 | const expect_cached_dependencies = CheckOutputCaching.init(b, true, &.{second_output}); |
| 62 | test_step.dependOn(&expect_cached_dependencies.step); |
| 63 | |
| 64 | const expect_equal_output = CheckPathEquality.init(b, true, &.{ first_output, second_output }); |
| 65 | test_step.dependOn(&expect_equal_output.step); |
| 66 | |
| 67 | const check_first_output = b.addCheckFile(first_output, .{ .expected_matches = &.{"a.txt"} }); |
| 68 | test_step.dependOn(&check_first_output.step); |
| 69 | const check_second_output = b.addCheckFile(second_output, .{ .expected_matches = &.{"a.txt"} }); |
| 70 | test_step.dependOn(&check_second_output.step); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const CheckOutputCaching = struct { |
| 75 | step: std.Build.Step, |
| 76 | expect_caching: bool, |
| 77 | |
| 78 | pub fn init(owner: *std.Build, expect_caching: bool, output_paths: []const std.Build.LazyPath) *CheckOutputCaching { |
| 79 | const check = owner.allocator.create(CheckOutputCaching) catch @panic("OOM"); |
| 80 | check.* = .{ |
| 81 | .step = std.Build.Step.init(.{ |
| 82 | .id = .custom, |
| 83 | .name = "check output caching", |
| 84 | .owner = owner, |
| 85 | .makeFn = make, |
| 86 | }), |
| 87 | .expect_caching = expect_caching, |
| 88 | }; |
| 89 | for (output_paths) |output_path| { |
| 90 | output_path.addStepDependencies(&check.step); |
| 91 | } |
| 92 | return check; |
| 93 | } |
| 94 | |
| 95 | fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) !void { |
| 96 | const check: *CheckOutputCaching = @fieldParentPtr("step", step); |
| 97 | |
| 98 | for (step.dependencies.items) |dependency| { |
| 99 | if (check.expect_caching) { |
| 100 | if (dependency.result_cached) continue; |
| 101 | return step.fail("expected '{s}' step to be cached, but it was not", .{dependency.name}); |
| 102 | } else { |
| 103 | if (!dependency.result_cached) continue; |
| 104 | return step.fail("expected '{s}' step to not be cached, but it was", .{dependency.name}); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | const CheckPathEquality = struct { |
| 111 | step: std.Build.Step, |
| 112 | expected_equality: bool, |
| 113 | output_paths: []const std.Build.LazyPath, |
| 114 | |
| 115 | pub fn init(owner: *std.Build, expected_equality: bool, output_paths: []const std.Build.LazyPath) *CheckPathEquality { |
| 116 | const check = owner.allocator.create(CheckPathEquality) catch @panic("OOM"); |
| 117 | check.* = .{ |
| 118 | .step = std.Build.Step.init(.{ |
| 119 | .id = .custom, |
| 120 | .name = "check output path equality", |
| 121 | .owner = owner, |
| 122 | .makeFn = make, |
| 123 | }), |
| 124 | .expected_equality = expected_equality, |
| 125 | .output_paths = owner.allocator.dupe(std.Build.LazyPath, output_paths) catch @panic("OOM"), |
| 126 | }; |
| 127 | for (output_paths) |output_path| { |
| 128 | output_path.addStepDependencies(&check.step); |
| 129 | } |
| 130 | return check; |
| 131 | } |
| 132 | |
| 133 | fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) !void { |
| 134 | const check: *CheckPathEquality = @fieldParentPtr("step", step); |
| 135 | std.debug.assert(check.output_paths.len != 0); |
| 136 | for (check.output_paths[0 .. check.output_paths.len - 1], check.output_paths[1..]) |a, b| { |
| 137 | try std.testing.expectEqual(check.expected_equality, std.mem.eql(u8, a.getPath(step.owner), b.getPath(step.owner))); |
| 138 | } |
| 139 | } |
| 140 | }; |