aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Build/Step/CheckFile.zig
diff options
context:
space:
mode:
authorNicolas Sterchele <nicolas@sterchelen.net>2023-03-20 09:23:10 +0100
committerVeikka Tuominen <git@vexu.eu>2023-05-03 08:39:24 +0300
commit13eb7251d37759bd47403db304c6120c706fe353 (patch)
tree225d87ef968270968379e2d58b9791b0aa152aa7 /lib/std/Build/Step/CheckFile.zig
parent855493bb8b395970921494d3a11ccfeaac30c2dc (diff)
downloadzig-13eb7251d37759bd47403db304c6120c706fe353.tar.gz
zig-13eb7251d37759bd47403db304c6120c706fe353.zip
build: rename std.Build.*Step to std.Build.Step.*
Follow-up actions from #14647 Fixes #14947
Diffstat (limited to 'lib/std/Build/Step/CheckFile.zig')
-rw-r--r--lib/std/Build/Step/CheckFile.zig87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/std/Build/Step/CheckFile.zig b/lib/std/Build/Step/CheckFile.zig
new file mode 100644
index 0000000000..ad8b1a25f0
--- /dev/null
+++ b/lib/std/Build/Step/CheckFile.zig
@@ -0,0 +1,87 @@
+//! Fail the build step if a file does not match certain checks.
+//! TODO: make this more flexible, supporting more kinds of checks.
+//! TODO: generalize the code in std.testing.expectEqualStrings and make this
+//! CheckFileStep produce those helpful diagnostics when there is not a match.
+const CheckFileStep = @This();
+const std = @import("std");
+const Step = std.Build.Step;
+const fs = std.fs;
+const mem = std.mem;
+
+step: Step,
+expected_matches: []const []const u8,
+expected_exact: ?[]const u8,
+source: std.Build.FileSource,
+max_bytes: usize = 20 * 1024 * 1024,
+
+pub const base_id = .check_file;
+
+pub const Options = struct {
+ expected_matches: []const []const u8 = &.{},
+ expected_exact: ?[]const u8 = null,
+};
+
+pub fn create(
+ owner: *std.Build,
+ source: std.Build.FileSource,
+ options: Options,
+) *CheckFileStep {
+ const self = owner.allocator.create(CheckFileStep) catch @panic("OOM");
+ self.* = .{
+ .step = Step.init(.{
+ .id = .check_file,
+ .name = "CheckFile",
+ .owner = owner,
+ .makeFn = make,
+ }),
+ .source = source.dupe(owner),
+ .expected_matches = owner.dupeStrings(options.expected_matches),
+ .expected_exact = options.expected_exact,
+ };
+ self.source.addStepDependencies(&self.step);
+ return self;
+}
+
+pub fn setName(self: *CheckFileStep, name: []const u8) void {
+ self.step.name = name;
+}
+
+fn make(step: *Step, prog_node: *std.Progress.Node) !void {
+ _ = prog_node;
+ const b = step.owner;
+ const self = @fieldParentPtr(CheckFileStep, "step", step);
+
+ const src_path = self.source.getPath(b);
+ const contents = fs.cwd().readFileAlloc(b.allocator, src_path, self.max_bytes) catch |err| {
+ return step.fail("unable to read '{s}': {s}", .{
+ src_path, @errorName(err),
+ });
+ };
+
+ for (self.expected_matches) |expected_match| {
+ if (mem.indexOf(u8, contents, expected_match) == null) {
+ return step.fail(
+ \\
+ \\========= expected to find: ===================
+ \\{s}
+ \\========= but file does not contain it: =======
+ \\{s}
+ \\===============================================
+ , .{ expected_match, contents });
+ }
+ }
+
+ if (self.expected_exact) |expected_exact| {
+ if (!mem.eql(u8, expected_exact, contents)) {
+ return step.fail(
+ \\
+ \\========= expected: =====================
+ \\{s}
+ \\========= but found: ====================
+ \\{s}
+ \\========= from the following file: ======
+ \\{s}
+ , .{ expected_exact, contents, src_path });
+ }
+ }
+}