| ... | @@ -1,19 +1,19 @@ | ... | @@ -1,19 +1,19 @@ |
| 1 | const std = @import("../std.zig"); | 1 | //! Fail the build step if a file does not match certain checks. |
| 2 | const Step = std.Build.Step; | 2 | //! TODO: make this more flexible, supporting more kinds of checks. |
| 3 | const fs = std.fs; | 3 | //! TODO: generalize the code in std.testing.expectEqualStrings and make this |
| 4 | const mem = std.mem; | 4 | //! CheckFileStep produce those helpful diagnostics when there is not a match. |
| 5 | | | |
| 6 | const CheckFileStep = @This(); | | |
| 7 | | | |
| 8 | pub const base_id = .check_file; | | |
| 9 | | 5 | |
| 10 | step: Step, | 6 | step: Step, |
| 11 | expected_matches: []const []const u8, | 7 | expected_matches: []const []const u8, |
| | 8 | expected_exact: ?[]const u8, |
| 12 | source: std.Build.FileSource, | 9 | source: std.Build.FileSource, |
| 13 | max_bytes: usize = 20 * 1024 * 1024, | 10 | max_bytes: usize = 20 * 1024 * 1024, |
| 14 | | 11 | |
| | 12 | pub const base_id = .check_file; |
| | 13 | |
| 15 | pub const Options = struct { | 14 | pub const Options = struct { |
| 16 | expected_matches: []const []const u8, | 15 | expected_matches: []const []const u8 = &.{}, |
| | 16 | expected_exact: ?[]const u8 = null, |
| 17 | }; | 17 | }; |
| 18 | | 18 | |
| 19 | pub fn create( | 19 | pub fn create( |
| ... | @@ -31,6 +31,7 @@ pub fn create( | ... | @@ -31,6 +31,7 @@ pub fn create( |
| 31 | }), | 31 | }), |
| 32 | .source = source.dupe(owner), | 32 | .source = source.dupe(owner), |
| 33 | .expected_matches = owner.dupeStrings(options.expected_matches), | 33 | .expected_matches = owner.dupeStrings(options.expected_matches), |
| | 34 | .expected_exact = options.expected_exact, |
| 34 | }; | 35 | }; |
| 35 | self.source.addStepDependencies(&self.step); | 36 | self.source.addStepDependencies(&self.step); |
| 36 | return self; | 37 | return self; |
| ... | @@ -60,8 +61,28 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { | ... | @@ -60,8 +61,28 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { |
| 60 | \\{s} | 61 | \\{s} |
| 61 | \\========= but file does not contain it: ======= | 62 | \\========= but file does not contain it: ======= |
| 62 | \\{s} | 63 | \\{s} |
| 63 | \\ | 64 | \\=============================================== |
| 64 | , .{ expected_match, contents }); | 65 | , .{ expected_match, contents }); |
| 65 | } | 66 | } |
| 66 | } | 67 | } |
| | 68 | |
| | 69 | if (self.expected_exact) |expected_exact| { |
| | 70 | if (!mem.eql(u8, expected_exact, contents)) { |
| | 71 | return step.fail( |
| | 72 | \\ |
| | 73 | \\========= expected: ===================== |
| | 74 | \\{s} |
| | 75 | \\========= but found: ==================== |
| | 76 | \\{s} |
| | 77 | \\========= from the following file: ====== |
| | 78 | \\{s} |
| | 79 | , .{ expected_exact, contents, src_path }); |
| | 80 | } |
| | 81 | } |
| 67 | } | 82 | } |
| | 83 | |
| | 84 | const CheckFileStep = @This(); |
| | 85 | const std = @import("../std.zig"); |
| | 86 | const Step = std.Build.Step; |
| | 87 | const fs = std.fs; |
| | 88 | const mem = std.mem; |