From cdf0dd15e90996db24f88a2104fc9c798b8d4cbf Mon Sep 17 00:00:00 2001 From: Jeffrey C. Ollie Date: Fri, 27 Feb 2026 16:13:03 +0000 Subject: [PATCH] testing: use std.Build.TranslateC instead of @cImport --- build.zig | 29 ++++++----------------------- include/test.zig | 5 ----- src/renderer.zig | 2 +- src/apprt/action.zig | 36 ++++++++++++++++++------------------ src/apprt/ipc.zig | 4 ++-- src/lib/enum.zig | 12 ++++++------ src/terminal/mouse_shape.zig | 2 +- src/terminal/osc.zig | 2 +- 8 file(s) changed, 35 insertion(s)(+), 57 deletion(s)(-) diff --git a/build.zig b/build.zig --- a/build.zig +++ b/build.zig @@ -54,10 +54,6 @@ "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 @@ // 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); @@ -302,24 +303,6 @@ // 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(&.{ diff --git a/include/test.zig b/include/test.zig deleted file mode 100644 --- a/include/test.zig +++ /dev/null @@ -1,5 +0,0 @@ -test "ghostty_h" { - _ = @cImport({ - @cInclude("ghostty.h"); - }); -} diff --git a/src/renderer.zig b/src/renderer.zig --- a/src/renderer.zig +++ b/src/renderer.zig @@ -48,7 +48,7 @@ healthy, unhealthy, - test "ghostty_h Health" { + test "ghostty.h Health" { try lib.checkGhosttyHEnum(Health, "GHOSTTY_RENDERER_HEALTH_"); } }; diff --git a/src/apprt/action.zig b/src/apprt/action.zig --- a/src/apprt/action.zig +++ b/src/apprt/action.zig @@ -21,7 +21,7 @@ app, surface, - test "ghostty_h Target.Key" { + test "ghostty.h Target.Key" { try lib.checkGhosttyHEnum(Key, "GHOSTTY_TARGET_"); } }; @@ -407,7 +407,7 @@ 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 @@ left, up, - test "ghostty_h SplitDirection" { + test "ghostty.h SplitDirection" { try lib.checkGhosttyHEnum(SplitDirection, "GHOSTTY_SPLIT_DIRECTION_"); } }; @@ -508,7 +508,7 @@ down, right, - test "ghostty_h GotoSplit" { + test "ghostty.h GotoSplit" { try lib.checkGhosttyHEnum(GotoSplit, "GHOSTTY_GOTO_SPLIT_"); } }; @@ -519,7 +519,7 @@ previous, next, - test "ghostty_h GotoWindow" { + test "ghostty.h GotoWindow" { try lib.checkGhosttyHEnum(GotoWindow, "GHOSTTY_GOTO_WINDOW_"); } }; @@ -535,7 +535,7 @@ left, right, - test "ghostty_h ResizeSplit.Direction" { + test "ghostty.h ResizeSplit.Direction" { try lib.checkGhosttyHEnum(Direction, "GHOSTTY_RESIZE_SPLIT_"); } }; @@ -555,7 +555,7 @@ _, // TODO: check non-exhaustive enums - // test "ghostty_h GotoTab" { + // test "ghostty.h GotoTab" { // try lib.checkGhosttyHEnum(GotoTab, "GHOSTTY_GOTO_TAB_"); // } }; @@ -570,7 +570,7 @@ 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 @@ off, toggle, - test "ghostty_h FloatWindow" { + test "ghostty.h FloatWindow" { try lib.checkGhosttyHEnum(FloatWindow, "GHOSTTY_FLOAT_WINDOW_"); } }; @@ -590,7 +590,7 @@ off, toggle, - test "ghostty_h SecureInput" { + test "ghostty.h SecureInput" { try lib.checkGhosttyHEnum(SecureInput, "GHOSTTY_SECURE_INPUT_"); } }; @@ -601,7 +601,7 @@ show, hide, - test "ghostty_h Inspector" { + test "ghostty.h Inspector" { try lib.checkGhosttyHEnum(Inspector, "GHOSTTY_INSPECTOR_"); } }; @@ -610,7 +610,7 @@ start, stop, - test "ghostty_h QuitTimer" { + test "ghostty.h QuitTimer" { try lib.checkGhosttyHEnum(QuitTimer, "GHOSTTY_QUIT_TIMER_"); } }; @@ -619,7 +619,7 @@ off, on, - test "ghostty_h Readonly" { + test "ghostty.h Readonly" { try lib.checkGhosttyHEnum(Readonly, "GHOSTTY_READONLY_"); } }; @@ -628,7 +628,7 @@ visible, hidden, - test "ghostty_h MouseVisibility" { + test "ghostty.h MouseVisibility" { try lib.checkGhosttyHEnum(MouseVisibility, "GHOSTTY_MOUSE_"); } }; @@ -638,7 +638,7 @@ surface, tab, - test "ghostty_h PromptTitle" { + test "ghostty.h PromptTitle" { try lib.checkGhosttyHEnum(PromptTitle, "GHOSTTY_PROMPT_TITLE_"); } }; @@ -846,7 +846,7 @@ _, // 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 @@ /// 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 @@ /// 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_"); } }; diff --git a/src/apprt/ipc.zig b/src/apprt/ipc.zig --- a/src/apprt/ipc.zig +++ b/src/apprt/ipc.zig @@ -24,7 +24,7 @@ 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 Key = enum(c_int) { new_window, - test "ghostty_h Action.Key" { + test "ghostty.h Action.Key" { try lib.checkGhosttyHEnum(Key, "GHOSTTY_IPC_ACTION_"); } }; diff --git a/src/lib/enum.zig b/src/lib/enum.zig --- a/src/lib/enum.zig +++ b/src/lib/enum.zig @@ -93,18 +93,18 @@ } /// 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 @@ 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; }; diff --git a/src/terminal/mouse_shape.zig b/src/terminal/mouse_shape.zig --- a/src/terminal/mouse_shape.zig +++ b/src/terminal/mouse_shape.zig @@ -65,7 +65,7 @@ }; }; - test "ghostty_h MouseShape" { + test "ghostty.h MouseShape" { try lib.checkGhosttyHEnum(MouseShape, "GHOSTTY_MOUSE_SHAPE_"); } }; diff --git a/src/terminal/osc.zig b/src/terminal/osc.zig --- a/src/terminal/osc.zig +++ b/src/terminal/osc.zig @@ -199,7 +199,7 @@ indeterminate, pause, - test "ghostty_h Command.ProgressReport.State" { + test "ghostty.h Command.ProgressReport.State" { try lib.checkGhosttyHEnum(State, "GHOSTTY_PROGRESS_STATE_"); } }; -- tangled.sh