Skip to content

Commit

Permalink
fix std.os.getAppDataDir test on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jul 18, 2018
1 parent a8a1b5a commit cd488c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
27 changes: 18 additions & 9 deletions std/os/get_app_data_dir.zig
Expand Up @@ -35,15 +35,21 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
else => return error.AppDataDirUnavailable,
}
},
// TODO for macos it should be "~/Library/Application Support/<APPNAME>"
else => {
const home_dir = os.getEnvVarOwned(allocator, "HOME") catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.EnvironmentVariableNotFound => return error.AppDataDirUnavailable, // TODO look in /etc/passwd
builtin.Os.macosx => {
const home_dir = os.getEnvPosix("HOME") orelse {
// TODO look in /etc/passwd
return error.AppDataDirUnavailable;
};
return os.path.join(allocator, home_dir, "Library", "Application Support", appname);
},
builtin.Os.linux => {
const home_dir = os.getEnvPosix("HOME") orelse {
// TODO look in /etc/passwd
return error.AppDataDirUnavailable;
};
defer allocator.free(home_dir);
return os.path.join(allocator, home_dir, ".local", "share", appname);
},
else => @compileError("Unsupported OS"),
}
}

Expand All @@ -53,8 +59,11 @@ fn utf16lePtrSlice(ptr: [*]const u16) []const u16 {
return ptr[0..index];
}

test "getAppDataDir" {
const result = try getAppDataDir(std.debug.global_allocator, "zig");
std.debug.warn("{}...", result);
test "std.os.getAppDataDir" {
var buf: [512]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;

// We can't actually validate the result
_ = getAppDataDir(allocator, "zig") catch return;
}

3 changes: 3 additions & 0 deletions std/os/index.zig
Expand Up @@ -498,6 +498,7 @@ pub var linux_aux_raw = []usize{0} ** 38;
pub var posix_environ_raw: [][*]u8 = undefined;

/// Caller must free result when done.
/// TODO make this go through libc when we have it
pub fn getEnvMap(allocator: *Allocator) !BufMap {
var result = BufMap.init(allocator);
errdefer result.deinit();
Expand Down Expand Up @@ -541,6 +542,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
}
}

/// TODO make this go through libc when we have it
pub fn getEnvPosix(key: []const u8) ?[]const u8 {
for (posix_environ_raw) |ptr| {
var line_i: usize = 0;
Expand All @@ -563,6 +565,7 @@ pub const GetEnvVarOwnedError = error{
};

/// Caller must free returned memory.
/// TODO make this go through libc when we have it
pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
if (is_windows) {
const key_with_null = try cstr.addNullByte(allocator, key);
Expand Down

0 comments on commit cd488c9

Please sign in to comment.