diff --git a/src/os/xdg.zig b/src/os/xdg.zig index 6c7655c226..1d60374efc 100644 --- a/src/os/xdg.zig +++ b/src/os/xdg.zig @@ -30,6 +30,22 @@ pub fn config(alloc: Allocator, opts: Options) ![]u8 { /// Get the XDG cache directory. The returned value is allocated. pub fn cache(alloc: Allocator, opts: Options) ![]u8 { + // On macOS we should use ~/Library/Caches instead of ~/.cache + if (builtin.os.tag == .macos) { + // Get our home dir if not provided + const home = if (opts.home) |h| h else blk: { + var buf: [1024]u8 = undefined; + break :blk try homedir.home(&buf) orelse return error.NoHomeDir; + }; + + return try std.fs.path.join(alloc, &[_][]const u8{ + home, + "Library", + "Caches", + opts.subdir orelse "", + }); + } + return try dir(alloc, opts, .{ .env = "XDG_CACHE_HOME", .windows_env = "LOCALAPPDATA", @@ -143,6 +159,52 @@ test { } } +test "cache directory paths" { + const testing = std.testing; + const alloc = testing.allocator; + const mock_home = "/Users/test"; + + // Test macOS path + if (builtin.os.tag == .macos) { + // Test base path + { + const cache_path = try cache(alloc, .{ .home = mock_home }); + defer alloc.free(cache_path); + try testing.expectEqualStrings("/Users/test/Library/Caches", cache_path); + } + + // Test with subdir + { + const cache_path = try cache(alloc, .{ + .home = mock_home, + .subdir = "ghostty", + }); + defer alloc.free(cache_path); + try testing.expectEqualStrings("/Users/test/Library/Caches/ghostty", cache_path); + } + } + + // Test Linux path (when XDG_CACHE_HOME is not set) + if (builtin.os.tag == .linux) { + // Test base path + { + const cache_path = try cache(alloc, .{ .home = mock_home }); + defer alloc.free(cache_path); + try testing.expectEqualStrings("/Users/test/.cache", cache_path); + } + + // Test with subdir + { + const cache_path = try cache(alloc, .{ + .home = mock_home, + .subdir = "ghostty", + }); + defer alloc.free(cache_path); + try testing.expectEqualStrings("/Users/test/.cache/ghostty", cache_path); + } + } +} + test parseTerminalExec { const testing = std.testing;