zig build test: support sudo command

This commit is contained in:
Motiejus Jakštys 2024-10-15 20:09:44 +03:00
parent c3858b01a2
commit 4946963af3

View File

@ -1,34 +1,8 @@
const std = @import("std"); const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "nyotun",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "nyotun", .name = "nyotun",
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
@ -36,6 +10,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
const sudo = b.option([]const u8, "sudo", "sudo command") orelse null;
// This declares intent for the executable to be installed into the // This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default // standard location when the user invokes the "install" step (the default
// step when running `zig build`). // step when running `zig build`).
@ -64,17 +40,25 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{ const unit_tests = b.addTest(.{
.root_source_file = b.path("src/test_all.zig"), .root_source_file = b.path("src/test_all.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); const set_cap = b.addSystemCommand(if (sudo) |cmd|
&.{ cmd, "setcap", "cap_net_admin=+ep" }
else
&.{ "setcap", "cap_net_admin=+ep" });
set_cap.addArtifactArg(unit_tests);
const run_unit_tests = b.addRunArtifact(unit_tests);
run_unit_tests.step.dependOn(&set_cap.step);
// Similar to creating the run step earlier, this exposes a `test` step to // Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request // the `zig build --help` menu, providing a way for the user to request
// running the unit tests. // running the unit tests.
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step); test_step.dependOn(&run_unit_tests.step);
} }