Skip to content

Commit

Permalink
Use $HOME/Library/Caches on macOS instead of $HOME/.cache
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Dec 30, 2024
1 parent d5703a5 commit c4a3780
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/os/xdg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit c4a3780

Please sign in to comment.