authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-09 18:10:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:14-07:00
log3186658e602b13a98c388872bbdc0b5cc1eb9267
treecd028fe89e9d4b04173a3a3ea171ab0e4d02a3ca
parent3b00e341fd8d1f12a3a14ee79a675a36549e716b

std.Build.CheckFileStep: add a way to expect exact

This is done in a bit of a haphazard way. Eventually the API needs to break in favor of a "checks" system similar to how RunStep works.

1 files changed, 31 insertions(+), 10 deletions(-)

lib/std/Build/CheckFileStep.zig+31-10
......@@ -1,19 +1,19 @@
1const std = @import("../std.zig");
2const Step = std.Build.Step;
3const fs = std.fs;
4const mem = std.mem;
5
6const CheckFileStep = @This();
7
8pub const base_id = .check_file;
1//! Fail the build step if a file does not match certain checks.
2//! TODO: make this more flexible, supporting more kinds of checks.
3//! TODO: generalize the code in std.testing.expectEqualStrings and make this
4//! CheckFileStep produce those helpful diagnostics when there is not a match.
95
106step: Step,
117expected_matches: []const []const u8,
8expected_exact: ?[]const u8,
129source: std.Build.FileSource,
1310max_bytes: usize = 20 * 1024 * 1024,
1411
12pub const base_id = .check_file;
13
1514pub const Options = struct {
16 expected_matches: []const []const u8,
15 expected_matches: []const []const u8 = &.{},
16 expected_exact: ?[]const u8 = null,
1717};
1818
1919pub fn create(
......@@ -31,6 +31,7 @@ pub fn create(
3131 }),
3232 .source = source.dupe(owner),
3333 .expected_matches = owner.dupeStrings(options.expected_matches),
34 .expected_exact = options.expected_exact,
3435 };
3536 self.source.addStepDependencies(&self.step);
3637 return self;
......@@ -60,8 +61,28 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
6061 \\{s}
6162 \\========= but file does not contain it: =======
6263 \\{s}
63 \\
64 \\===============================================
6465 , .{ expected_match, contents });
6566 }
6667 }
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 }
6782}
83
84const CheckFileStep = @This();
85const std = @import("../std.zig");
86const Step = std.Build.Step;
87const fs = std.fs;
88const mem = std.mem;