Add standalone test case for passing options to dependencies

This commit is contained in:
Carl Åstholm 2025-03-23 20:38:41 +01:00
parent b92b55ab8e
commit 00bc72b5ff
5 changed files with 140 additions and 0 deletions

View File

@ -181,6 +181,9 @@
.install_headers = .{
.path = "install_headers",
},
.dependency_options = .{
.path = "dependency_options",
},
.dependencyFromBuildZig = .{
.path = "dependencyFromBuildZig",
},

View File

@ -0,0 +1,63 @@
const std = @import("std");
pub const Enum = enum { alfa, bravo, charlie };
pub fn build(b: *std.Build) !void {
const test_step = b.step("test", "Test passing options to a dependency");
b.default_step = test_step;
const none_specified = b.dependency("other", .{});
const none_specified_mod = none_specified.module("dummy");
if (!none_specified_mod.resolved_target.?.query.eql(b.graph.host.query)) return error.TestFailed;
if (none_specified_mod.optimize.? != .Debug) return error.TestFailed;
const all_specified = b.dependency("other", .{
.target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
.optimize = @as(std.builtin.OptimizeMode, .ReleaseSafe),
.bool = @as(bool, true),
.int = @as(i64, 123),
.float = @as(f64, 0.5),
.string = @as([]const u8, "abc"),
.string_list = @as([]const []const u8, &.{ "a", "b", "c" }),
.lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
.lazy_path_list = @as([]const std.Build.LazyPath, &.{
.{ .cwd_relative = "a.txt" },
.{ .cwd_relative = "b.txt" },
.{ .cwd_relative = "c.txt" },
}),
.@"enum" = @as(Enum, .alfa),
//.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
//.build_id = @as(std.zig.BuildId, .uuid),
});
const all_specified_mod = all_specified.module("dummy");
if (all_specified_mod.resolved_target.?.result.cpu.arch != .x86_64) return error.TestFailed;
if (all_specified_mod.resolved_target.?.result.os.tag != .windows) return error.TestFailed;
if (all_specified_mod.resolved_target.?.result.abi != .gnu) return error.TestFailed;
if (all_specified_mod.optimize.? != .ReleaseSafe) return error.TestFailed;
// Most supported option types are serialized to a string representation,
// so alternative representations of the same option value should resolve
// to the same cached dependency instance.
const all_specified_alt = b.dependency("other", .{
.target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
.optimize = @as([]const u8, "ReleaseSafe"),
.bool = .true,
.int = @as([]const u8, "123"),
.float = @as(f16, 0.5),
.string = .abc,
.string_list = @as([]const []const u8, &.{ "a", "b", "c" }),
.lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
.lazy_path_list = @as([]const std.Build.LazyPath, &.{
.{ .cwd_relative = "a.txt" },
.{ .cwd_relative = "b.txt" },
.{ .cwd_relative = "c.txt" },
}),
.@"enum" = @as([]const u8, "alfa"),
//.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
//.build_id = @as(std.zig.BuildId, .uuid),
});
if (all_specified != all_specified_alt) return error.TestFailed;
}

View File

@ -0,0 +1,11 @@
.{
.name = .dependency_options,
.fingerprint = 0x3e3ce1c1f92ba47e,
.version = "0.0.0",
.dependencies = .{
.other = .{
.path = "other",
},
},
.paths = .{""},
}

View File

@ -0,0 +1,56 @@
const std = @import("std");
pub const Enum = enum { alfa, bravo, charlie };
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const expected_bool: bool = true;
const expected_int: i64 = 123;
const expected_float: f64 = 0.5;
const expected_string: []const u8 = "abc";
const expected_string_list: []const []const u8 = &.{ "a", "b", "c" };
const expected_lazy_path: std.Build.LazyPath = .{ .cwd_relative = "abc.txt" };
const expected_lazy_path_list: []const std.Build.LazyPath = &.{
.{ .cwd_relative = "a.txt" },
.{ .cwd_relative = "b.txt" },
.{ .cwd_relative = "c.txt" },
};
const expected_enum: Enum = .alfa;
const expected_enum_list: []const Enum = &.{ .alfa, .bravo, .charlie };
const expected_build_id: std.zig.BuildId = .uuid;
const @"bool" = b.option(bool, "bool", "bool") orelse expected_bool;
const int = b.option(i64, "int", "int") orelse expected_int;
const float = b.option(f64, "float", "float") orelse expected_float;
const string = b.option([]const u8, "string", "string") orelse expected_string;
const string_list = b.option([]const []const u8, "string_list", "string_list") orelse expected_string_list;
const lazy_path = b.option(std.Build.LazyPath, "lazy_path", "lazy_path") orelse expected_lazy_path;
const lazy_path_list = b.option([]const std.Build.LazyPath, "lazy_path_list", "lazy_path_list") orelse expected_lazy_path_list;
const @"enum" = b.option(Enum, "enum", "enum") orelse expected_enum;
const enum_list = b.option([]const Enum, "enum_list", "enum_list") orelse expected_enum_list;
const build_id = b.option(std.zig.BuildId, "build_id", "build_id") orelse expected_build_id;
if (@"bool" != expected_bool) return error.TestFailed;
if (int != expected_int) return error.TestFailed;
if (float != expected_float) return error.TestFailed;
if (!std.mem.eql(u8, string, expected_string)) return error.TestFailed;
if (string_list.len != expected_string_list.len) return error.TestFailed;
for (string_list, expected_string_list) |x, y| {
if (!std.mem.eql(u8, x, y)) return error.TestFailed;
}
if (!std.mem.eql(u8, lazy_path.cwd_relative, expected_lazy_path.cwd_relative)) return error.TestFailed;
for (lazy_path_list, expected_lazy_path_list) |x, y| {
if (!std.mem.eql(u8, x.cwd_relative, y.cwd_relative)) return error.TestFailed;
}
if (@"enum" != expected_enum) return error.TestFailed;
if (!std.mem.eql(Enum, enum_list, expected_enum_list)) return error.TestFailed;
if (!std.meta.eql(build_id, expected_build_id)) return error.TestFailed;
_ = b.addModule("dummy", .{
.root_source_file = b.path("build.zig"),
.target = target,
.optimize = optimize,
});
}

View File

@ -0,0 +1,7 @@
.{
.name = .other,
.fingerprint = 0xd95835207bc8b630,
.version = "0.0.0",
.dependencies = .{},
.paths = .{""},
}