master
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//! CheckFile step produce those helpful diagnostics when there is not a match.
5const CheckFile = @This();
6const std = @import("std");
7const Step = std.Build.Step;
8const fs = std.fs;
9const mem = std.mem;
10
11step: Step,
12expected_matches: []const []const u8,
13expected_exact: ?[]const u8,
14source: std.Build.LazyPath,
15max_bytes: usize = 20 * 1024 * 1024,
16
17pub const base_id: Step.Id = .check_file;
18
19pub const Options = struct {
20 expected_matches: []const []const u8 = &.{},
21 expected_exact: ?[]const u8 = null,
22};
23
24pub fn create(
25 owner: *std.Build,
26 source: std.Build.LazyPath,
27 options: Options,
28) *CheckFile {
29 const check_file = owner.allocator.create(CheckFile) catch @panic("OOM");
30 check_file.* = .{
31 .step = Step.init(.{
32 .id = base_id,
33 .name = "CheckFile",
34 .owner = owner,
35 .makeFn = make,
36 }),
37 .source = source.dupe(owner),
38 .expected_matches = owner.dupeStrings(options.expected_matches),
39 .expected_exact = options.expected_exact,
40 };
41 check_file.source.addStepDependencies(&check_file.step);
42 return check_file;
43}
44
45pub fn setName(check_file: *CheckFile, name: []const u8) void {
46 check_file.step.name = name;
47}
48
49fn make(step: *Step, options: Step.MakeOptions) !void {
50 _ = options;
51 const b = step.owner;
52 const check_file: *CheckFile = @fieldParentPtr("step", step);
53 try step.singleUnchangingWatchInput(check_file.source);
54
55 const src_path = check_file.source.getPath2(b, step);
56 const contents = fs.cwd().readFileAlloc(src_path, b.allocator, .limited(check_file.max_bytes)) catch |err| {
57 return step.fail("unable to read '{s}': {s}", .{
58 src_path, @errorName(err),
59 });
60 };
61
62 for (check_file.expected_matches) |expected_match| {
63 if (mem.indexOf(u8, contents, expected_match) == null) {
64 return step.fail(
65 \\
66 \\========= expected to find: ===================
67 \\{s}
68 \\========= but file does not contain it: =======
69 \\{s}
70 \\===============================================
71 , .{ expected_match, contents });
72 }
73 }
74
75 if (check_file.expected_exact) |expected_exact| {
76 if (!mem.eql(u8, expected_exact, contents)) {
77 return step.fail(
78 \\
79 \\========= expected: =====================
80 \\{s}
81 \\========= but found: ====================
82 \\{s}
83 \\========= from the following file: ======
84 \\{s}
85 , .{ expected_exact, contents, src_path });
86 }
87 }
88}