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 @@...@@ -1,19 +1,19 @@
1const std = @import("../std.zig");1//! Fail the build step if a file does not match certain checks.
2const Step = std.Build.Step;2//! TODO: make this more flexible, supporting more kinds of checks.
3const fs = std.fs;3//! TODO: generalize the code in std.testing.expectEqualStrings and make this
4const mem = std.mem;4//! CheckFileStep produce those helpful diagnostics when there is not a match.
5
6const CheckFileStep = @This();
7
8pub const base_id = .check_file;
95
10step: Step,6step: Step,
11expected_matches: []const []const u8,7expected_matches: []const []const u8,
8expected_exact: ?[]const u8,
12source: std.Build.FileSource,9source: std.Build.FileSource,
13max_bytes: usize = 20 * 1024 * 1024,10max_bytes: usize = 20 * 1024 * 1024,
1411
12pub const base_id = .check_file;
13
15pub const Options = struct {14pub const Options = struct {
16 expected_matches: []const []const u8,15 expected_matches: []const []const u8 = &.{},
16 expected_exact: ?[]const u8 = null,
17};17};
1818
19pub fn create(19pub 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
84const CheckFileStep = @This();
85const std = @import("../std.zig");
86const Step = std.Build.Step;
87const fs = std.fs;
88const mem = std.mem;