Use $HOME/Library/Caches on macOS instead of $HOME/.cache

This commit is contained in:
Bryan Lee
2024-12-28 00:02:28 +08:00
parent 80c3833031
commit 1941a440d8

View File

@ -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;