testing: use std.Build.TranslateC instead of @cImport

This commit is contained in:
Jeffrey C. Ollie
2026-02-27 10:13:03 -06:00
parent ea5b07d20f
commit cdf0dd15e9
8 changed files with 35 additions and 57 deletions

View File

@@ -54,10 +54,6 @@ pub fn build(b: *std.Build) !void {
"update-translations",
"Update translation files",
);
const test_include_ghostty_h_step = b.step(
"test-include-ghostty-h",
"Test include of ghostty.h",
);
// Ghostty resources like terminfo, shell integration, themes, etc.
const resources = try buildpkg.GhosttyResources.init(b, &config, &deps);
@@ -292,7 +288,12 @@ pub fn build(b: *std.Build) !void {
// Crash on x86_64 without this
.use_llvm = true,
});
test_exe.root_module.addIncludePath(b.path("include"));
const ghostty_h = b.addTranslateC(.{
.root_source_file = b.path("include/ghostty.h"),
.target = config.baselineTarget(),
.optimize = .Debug,
});
test_exe.root_module.addImport("ghostty.h", ghostty_h.createModule());
if (config.emit_test_exe) b.installArtifact(test_exe);
_ = try deps.add(test_exe);
@@ -303,24 +304,6 @@ pub fn build(b: *std.Build) !void {
// Normal tests always test our libghostty modules
//test_step.dependOn(test_lib_vt_step);
const test_include_ghostty_h_exe = b.addTest(.{
.name = "ghostty-include-ghostty-h",
.filters = test_filters,
.root_module = b.createModule(.{
.root_source_file = b.path("include/test.zig"),
.target = config.baselineTarget(),
.optimize = .Debug,
.strip = false,
.omit_frame_pointer = false,
.unwind_tables = .sync,
.link_libc = true,
}),
});
test_include_ghostty_h_exe.addIncludePath(b.path("include"));
const test_include_ghostty_h_run = b.addRunArtifact(test_include_ghostty_h_exe);
test_include_ghostty_h_step.dependOn(&test_include_ghostty_h_run.step);
test_step.dependOn(test_include_ghostty_h_step);
// Valgrind test running
const valgrind_run = b.addSystemCommand(&.{
"valgrind",

View File

@@ -1,5 +0,0 @@
test "ghostty_h" {
_ = @cImport({
@cInclude("ghostty.h");
});
}

View File

@@ -21,7 +21,7 @@ pub const Target = union(Key) {
app,
surface,
test "ghostty_h Target.Key" {
test "ghostty.h Target.Key" {
try lib.checkGhosttyHEnum(Key, "GHOSTTY_TARGET_");
}
};
@@ -407,7 +407,7 @@ pub const Action = union(Key) {
readonly,
copy_title_to_clipboard,
test "ghostty_h Action.Key" {
test "ghostty.h Action.Key" {
try lib.checkGhosttyHEnum(Key, "GHOSTTY_ACTION_");
}
};
@@ -492,7 +492,7 @@ pub const SplitDirection = enum(c_int) {
left,
up,
test "ghostty_h SplitDirection" {
test "ghostty.h SplitDirection" {
try lib.checkGhosttyHEnum(SplitDirection, "GHOSTTY_SPLIT_DIRECTION_");
}
};
@@ -508,7 +508,7 @@ pub const GotoSplit = enum(c_int) {
down,
right,
test "ghostty_h GotoSplit" {
test "ghostty.h GotoSplit" {
try lib.checkGhosttyHEnum(GotoSplit, "GHOSTTY_GOTO_SPLIT_");
}
};
@@ -519,7 +519,7 @@ pub const GotoWindow = enum(c_int) {
previous,
next,
test "ghostty_h GotoWindow" {
test "ghostty.h GotoWindow" {
try lib.checkGhosttyHEnum(GotoWindow, "GHOSTTY_GOTO_WINDOW_");
}
};
@@ -535,7 +535,7 @@ pub const ResizeSplit = extern struct {
left,
right,
test "ghostty_h ResizeSplit.Direction" {
test "ghostty.h ResizeSplit.Direction" {
try lib.checkGhosttyHEnum(Direction, "GHOSTTY_RESIZE_SPLIT_");
}
};
@@ -555,7 +555,7 @@ pub const GotoTab = enum(c_int) {
_,
// TODO: check non-exhaustive enums
// test "ghostty_h GotoTab" {
// test "ghostty.h GotoTab" {
// try lib.checkGhosttyHEnum(GotoTab, "GHOSTTY_GOTO_TAB_");
// }
};
@@ -570,7 +570,7 @@ pub const Fullscreen = enum(c_int) {
macos_non_native_visible_menu,
macos_non_native_padded_notch,
test "ghostty_h Fullscreen" {
test "ghostty.h Fullscreen" {
try lib.checkGhosttyHEnum(Fullscreen, "GHOSTTY_FULLSCREEN_");
}
};
@@ -580,7 +580,7 @@ pub const FloatWindow = enum(c_int) {
off,
toggle,
test "ghostty_h FloatWindow" {
test "ghostty.h FloatWindow" {
try lib.checkGhosttyHEnum(FloatWindow, "GHOSTTY_FLOAT_WINDOW_");
}
};
@@ -590,7 +590,7 @@ pub const SecureInput = enum(c_int) {
off,
toggle,
test "ghostty_h SecureInput" {
test "ghostty.h SecureInput" {
try lib.checkGhosttyHEnum(SecureInput, "GHOSTTY_SECURE_INPUT_");
}
};
@@ -601,7 +601,7 @@ pub const Inspector = enum(c_int) {
show,
hide,
test "ghostty_h Inspector" {
test "ghostty.h Inspector" {
try lib.checkGhosttyHEnum(Inspector, "GHOSTTY_INSPECTOR_");
}
};
@@ -610,7 +610,7 @@ pub const QuitTimer = enum(c_int) {
start,
stop,
test "ghostty_h QuitTimer" {
test "ghostty.h QuitTimer" {
try lib.checkGhosttyHEnum(QuitTimer, "GHOSTTY_QUIT_TIMER_");
}
};
@@ -619,7 +619,7 @@ pub const Readonly = enum(c_int) {
off,
on,
test "ghostty_h Readonly" {
test "ghostty.h Readonly" {
try lib.checkGhosttyHEnum(Readonly, "GHOSTTY_READONLY_");
}
};
@@ -628,7 +628,7 @@ pub const MouseVisibility = enum(c_int) {
visible,
hidden,
test "ghostty_h MouseVisibility" {
test "ghostty.h MouseVisibility" {
try lib.checkGhosttyHEnum(MouseVisibility, "GHOSTTY_MOUSE_");
}
};
@@ -638,7 +638,7 @@ pub const PromptTitle = enum(c_int) {
surface,
tab,
test "ghostty_h PromptTitle" {
test "ghostty.h PromptTitle" {
try lib.checkGhosttyHEnum(PromptTitle, "GHOSTTY_PROMPT_TITLE_");
}
};
@@ -846,7 +846,7 @@ pub const ColorKind = enum(c_int) {
_,
// TODO: check non-non-exhaustive enums
// test "ghostty_h ColorKind" {
// test "ghostty.h ColorKind" {
// try lib.checkGhosttyHEnum(ColorKind, "GHOSTTY_COLOR_KIND_");
// }
};
@@ -900,7 +900,7 @@ pub const OpenUrl = struct {
/// The URL is known to contain HTML content.
html,
test "ghostty_h OpenUrl.Kind" {
test "ghostty.h OpenUrl.Kind" {
try lib.checkGhosttyHEnum(Kind, "GHOSTTY_ACTION_OPEN_URL_KIND_");
}
};
@@ -930,7 +930,7 @@ pub const CloseTabMode = enum(c_int) {
/// Close all tabs to the right of the current tab.
right,
test "ghostty_h CloseTabMode" {
test "ghostty.h CloseTabMode" {
try lib.checkGhosttyHEnum(CloseTabMode, "GHOSTTY_ACTION_CLOSE_TAB_MODE_");
}
};

View File

@@ -24,7 +24,7 @@ pub const Target = union(Key) {
class,
detect,
test "ghostty_h Target.Key" {
test "ghostty.h Target.Key" {
try lib.checkGhosttyHEnum(Key, "GHOSTTY_IPC_TARGET_");
}
};
@@ -114,7 +114,7 @@ pub const Action = union(enum) {
pub const Key = enum(c_int) {
new_window,
test "ghostty_h Action.Key" {
test "ghostty.h Action.Key" {
try lib.checkGhosttyHEnum(Key, "GHOSTTY_IPC_ACTION_");
}
};

View File

@@ -93,18 +93,18 @@ test "abi by removing a key" {
}
/// Verify that for every key in enum T, there is a matching declaration in
/// `ghostty.h` with the correct value.
/// `ghostty.h` with the correct value. This should only ever be called inside a `test`
/// because the `ghostty.h` module is only available then.
pub fn checkGhosttyHEnum(comptime T: type, comptime prefix: []const u8) !void {
const info = @typeInfo(T);
try std.testing.expect(info == .@"enum");
try std.testing.expect(info.@"enum".tag_type == c_int);
try std.testing.expect(info.@"enum".is_exhaustive == true);
@setEvalBranchQuota(1000000);
const c = @cImport({
@cInclude("ghostty.h");
});
const c = @import("ghostty.h");
var set: std.EnumSet(T) = .initFull();
@@ -137,8 +137,8 @@ pub fn checkGhosttyHEnum(comptime T: type, comptime prefix: []const u8) !void {
var it = set.iterator();
while (it.next()) |v| {
var buf: [128]u8 = undefined;
const n = std.ascii.upperString(&buf, @tagName(v));
std.log.err("ghostty.h is missing value for {s}{s}, {t}", .{ prefix, n, v });
const upper_string = std.ascii.upperString(&buf, @tagName(v));
std.log.err("ghostty.h is missing value for {s}{s}", .{ prefix, upper_string });
}
return e;
};

View File

@@ -48,7 +48,7 @@ pub const Health = enum(c_int) {
healthy,
unhealthy,
test "ghostty_h Health" {
test "ghostty.h Health" {
try lib.checkGhosttyHEnum(Health, "GHOSTTY_RENDERER_HEALTH_");
}
};

View File

@@ -65,7 +65,7 @@ pub const MouseShape = enum(c_int) {
};
};
test "ghostty_h MouseShape" {
test "ghostty.h MouseShape" {
try lib.checkGhosttyHEnum(MouseShape, "GHOSTTY_MOUSE_SHAPE_");
}
};

View File

@@ -199,7 +199,7 @@ pub const Command = union(Key) {
indeterminate,
pause,
test "ghostty_h Command.ProgressReport.State" {
test "ghostty.h Command.ProgressReport.State" {
try lib.checkGhosttyHEnum(State, "GHOSTTY_PROGRESS_STATE_");
}
};