Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make "default" command fail for non-existent compilers #32

Merged
merged 1 commit into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ pub fn main() !void {
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "master"));
}
{
const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"default", "0.5.0"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
dumpExecResult(result);
switch (result.term) {
.Exited => |code| try testing.expectEqual(@as(u8, 1), code),
else => |term| std.debug.panic("unexpected exit {}", .{term}),
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Error: compiler '0.5.0' is not installed\n"));
}
try runNoCapture(".", zigup_args ++ &[_][]const u8 {"0.5.0"});
{
const result = try runCaptureOuts(allocator, ".", zigup_args ++ &[_][]const u8 {"default"});
Expand Down
21 changes: 18 additions & 3 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub fn main2() !u8 {
if (std.mem.eql(u8, version_string, "master")) {
@panic("set default to master not implemented");
} else {
try setDefaultCompiler(allocator, compiler_dir);
try setDefaultCompiler(allocator, compiler_dir, .verify_existence);
}
return 0;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ fn fetchCompiler(allocator: *Allocator, version_arg: []const u8, set_default: Se
}
}
if (set_default == .set_default) {
try setDefaultCompiler(allocator, compiler_dir);
try setDefaultCompiler(allocator, compiler_dir, .existence_verified);
}
}

Expand Down Expand Up @@ -623,7 +623,22 @@ fn printDefaultCompiler(allocator: *Allocator) !void {
}
}

fn setDefaultCompiler(allocator: *Allocator, compiler_dir: []const u8) !void {
const ExistVerify = enum { existence_verified, verify_existence };

fn setDefaultCompiler(allocator: *Allocator, compiler_dir: []const u8, exist_verify: ExistVerify) !void {
switch (exist_verify) {
.existence_verified => {},
.verify_existence => {
(std.fs.openDirAbsolute(compiler_dir, .{}) catch |err| switch (err) {
error.FileNotFound => {
std.debug.print("Error: compiler '{s}' is not installed\n", .{std.fs.path.basename(compiler_dir)});
return error.AlreadyReported;
},
else => |e| return e,
}).close();
},
}

const path_link = try makeZigPathLinkString(allocator);
defer allocator.free(path_link);
const link_target = try std.fs.path.join(allocator, &[_][]const u8{ compiler_dir, "files", "zig" ++ builtin.target.exeFileExt() });
Expand Down