zig/lib/std/Build/CheckFileStep.zig
Andrew Kelley 0b8736f5ed re-enable CLI tests
CLI tests are now ported over to the new std.Build API and thus work
properly with concurrency.

 * add `std.Build.addCheckFile` for creating a
   `std.Build.CheckFileStep`.
 * add `std.Build.makeTempPath`. This function is intended to be called
   in the `configure` phase only. It returns an absolute directory path,
   which is potentially going to be a source of API breakage in the
   future, so keep that in mind when using this function.
 * add `std.Build.CheckFileStep.setName`.
 * `std.Build.CheckFileStep`: better error message when reading the
   input file fails.
 * `std.Build.RunStep`: add a `has_side_effects` flag for when you need
   to override the autodetection.
 * `std.Build.RunStep`: add the ability to obtain a FileSource for the
   directory that contains the written files.
 * `std.Build.WriteFileStep`: add a way to write bytes to an arbitrary
   path - absolute or relative to the package root. Be careful with this
   because it updates source files. This should not be used as part of
   the normal build process, but as a utility occasionally run by a
   developer with intent to modify source files and then commit those
   changes to version control. A file added this way is not available
   with `getFileSource`.
2023-03-15 10:48:13 -07:00

67 lines
1.8 KiB
Zig

const std = @import("../std.zig");
const Step = std.Build.Step;
const fs = std.fs;
const mem = std.mem;
const CheckFileStep = @This();
pub const base_id = .check_file;
step: Step,
expected_matches: []const []const u8,
source: std.Build.FileSource,
max_bytes: usize = 20 * 1024 * 1024,
pub const Options = struct {
expected_matches: []const []const u8,
};
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),
};
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 });
}
}
}