Skip to content

Commit

Permalink
zig fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Sep 6, 2023
1 parent ca4e390 commit 962c153
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 92 deletions.
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ fn buildOrFail(b: *Builder) anyerror {
}

// TODO: remove the following if https://github.com/ziglang/zig/pull/9987 is integrated
fn getBuildArgs(self: *Builder) ! []const [:0]const u8 {
fn getBuildArgs(self: *Builder) ![]const [:0]const u8 {
const args = try std.process.argsAlloc(self.allocator);
return args[5..];
}
pub fn addBuild(self: *Builder, build_file: std.build.FileSource, _: struct { }) *std.build.RunStep {
pub fn addBuild(self: *Builder, build_file: std.build.FileSource, _: struct {}) *std.build.RunStep {
const run_step = std.build.RunStep.create(
self,
self.fmt("zig build {s}", .{build_file.getDisplayName()}),
Expand Down
9 changes: 4 additions & 5 deletions build2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn addZigupExe(
target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
win32exelink_mod: ?*std.build.Module,
ssl_backend: ?SslBackend
ssl_backend: ?SslBackend,
) !*std.build.LibExeObjStep {
const require_ssl_backend = b.allocator.create(RequireSslBackendStep) catch unreachable;
require_ssl_backend.* = RequireSslBackendStep.init(b, "the zigup exe", ssl_backend);
Expand Down Expand Up @@ -157,7 +157,7 @@ const SslBackendFailedStep = struct {
}
fn make(step: *std.build.Step) !void {
const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{self.context, self.backend});
std.debug.print("error: the {s} failed to add the {s} SSL backend\n", .{ self.context, self.backend });
std.os.exit(1);
}
};
Expand All @@ -181,7 +181,7 @@ const RequireSslBackendStep = struct {
fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const self = @fieldParentPtr(RequireSslBackendStep, "step", step);
if (self.backend) |_| { } else {
if (self.backend) |_| {} else {
std.debug.print("error: {s} requires an SSL backend:\n", .{self.context});
inline for (zigetbuild.ssl_backends) |field| {
std.debug.print(" -D{s}\n", .{field.name});
Expand All @@ -192,14 +192,13 @@ const RequireSslBackendStep = struct {
};

fn addGithubReleaseExe(b: *Builder, github_release_step: *std.build.Step, ziget_repo: []const u8, comptime target_triple: []const u8, comptime ssl_backend: SslBackend) !void {

const small_release = true;

const target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = target_triple });
const mode = if (small_release) .ReleaseSafe else .Debug;
const exe = try addZigupExe(b, ziget_repo, target, mode, ssl_backend);
if (small_release) {
exe.strip = true;
exe.strip = true;
}
exe.setOutputDir("github-release" ++ std.fs.path.sep_str ++ target_triple ++ std.fs.path.sep_str ++ @tagName(ssl_backend));
github_release_step.dependOn(&exe.step);
Expand Down
4 changes: 2 additions & 2 deletions fixdeletetree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn deleteTree(dir: std.fs.Dir, sub_path: []const u8) !void {
if (builtin.os.tag != .windows) {
return dir.deleteTree(sub_path);
}

// workaround issue on windows where it just doesn't delete things
const MAX_ATTEMPTS = 10;
var attempt: u8 = 0;
Expand All @@ -19,7 +19,7 @@ pub fn deleteTree(dir: std.fs.Dir, sub_path: []const u8) !void {
if (attempt == MAX_ATTEMPTS) return err;
switch (err) {
error.FileBusy => {
std.log.warn("path '{s}' is busy (attempt {}), will retry", .{sub_path, attempt});
std.log.warn("path '{s}' is busy (attempt {}), will retry", .{ sub_path, attempt });
std.time.sleep(std.time.ns_per_ms * 100); // sleep for 100 ms
},
else => |e| return e,
Expand Down
171 changes: 113 additions & 58 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ pub fn main() !u8 {
.{},
);

const zigup_args = &[_][]const u8 { zigup } ++ (
if (builtin.os.tag == .windows) &[_][]const u8 { } else &[_][]const u8 { "--install-dir", install_dir }
);
const install_args = if (builtin.os.tag == .windows) [_][]const u8{} else [_][]const u8{ "--install-dir", install_dir };
const zigup_args = &[_][]const u8{zigup} ++ install_args;

var allocator_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer allocator_store.deinit();
Expand All @@ -58,65 +57,92 @@ pub fn main() !u8 {
{
const scratch_bin_path = try std.fs.path.join(allocator, &.{ cwd, bin_dir });
defer allocator.free(scratch_bin_path);
setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin_path, path_env_sep, original_path_env}));
setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin_path, path_env_sep, original_path_env }));
}

{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default", "master"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "master" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "master has not been fetched"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"-h"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"-h"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"--help"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"--help"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "Usage"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.eql(u8, result.stdout, "<no-default>\n"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"fetch-index"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"fetch-index"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
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.7.0"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.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.7.0' is not installed\n"));
}
try runNoCapture(zigup_args ++ &[_][]const u8 {"0.7.0"});
try runNoCapture(zigup_args ++ &[_][]const u8{"0.7.0"});
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
dumpExecResult(result);
try testing.expect(std.mem.eql(u8, result.stdout, "0.7.0\n"));
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"fetch", "0.7.0"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "fetch", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "already installed"));
}
try runNoCapture(zigup_args ++ &[_][]const u8 {"master"});
try runNoCapture(zigup_args ++ &[_][]const u8 {"0.8.0"});
try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
try runNoCapture(zigup_args ++ &[_][]const u8{"0.8.0"});
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"default"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
dumpExecResult(result);
try testing.expect(std.mem.eql(u8, result.stdout, "0.8.0\n"));
Expand All @@ -125,64 +151,85 @@ pub fn main() !u8 {
const save_path_env = path_env_ptr.*;
defer setPathEnv(save_path_env);
setPathEnv("");
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default", "master"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "master" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, " is not in PATH"));
}
try runNoCapture(zigup_args ++ &[_][]const u8 {"default", "master"});
try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "master" });
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"list"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"list"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.7.0"));
try testing.expect(std.mem.containsAtLeast(u8, result.stdout, 1, "0.8.0"));
}
try runNoCapture(zigup_args ++ &[_][]const u8 {"default", "0.7.0"});
try runNoCapture(zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"run", "0.8.0", "version"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "0.8.0", "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expectEqualSlices(u8, "0.8.0\n", result.stdout);
}
{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"run", "doesnotexist", "version"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "run", "doesnotexist", "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expectEqualSlices(u8, "error: compiler 'doesnotexist' does not exist, fetch it first with: zigup fetch doesnotexist\n", result.stderr);
}
try runNoCapture(zigup_args ++ &[_][]const u8 {"keep", "0.8.0"});
try runNoCapture(zigup_args ++ &[_][]const u8{ "keep", "0.8.0" });
// doesn't delete anything because we have keepfile and master doens't get deleted
try runNoCapture(zigup_args ++ &[_][]const u8 {"clean"});
try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));

// Just make a directory to trick zigup into thinking there is another compiler so we don't have to wait for it to download/install
try std.fs.cwd().makeDir(install_dir ++ sep ++ "0.9.0");
try testing.expectEqual(@as(u32, 4), try getCompilerCount(install_dir));
try runNoCapture(zigup_args ++ &[_][]const u8 {"clean"});
try runNoCapture(zigup_args ++ &[_][]const u8{"clean"});
try testing.expectEqual(@as(u32, 3), try getCompilerCount(install_dir));

{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"clean", "0.8.0"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "clean", "0.8.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "deleting "));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "0.8.0"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));

{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"clean"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"clean"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "it is master"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));

try runNoCapture(zigup_args ++ &[_][]const u8 {"master"});
try runNoCapture(zigup_args ++ &[_][]const u8{"master"});
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));

{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"DOESNOTEXST"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{"DOESNOTEXST"});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "HTTP request failed"));
}
try testing.expectEqual(@as(u32, 2), try getCompilerCount(install_dir));
Expand All @@ -202,15 +249,18 @@ pub fn main() !u8 {
try file.writer().writeAll("a fake executable");
}

setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin2_path, path_env_sep, previous_path}));
setPathEnv(try std.mem.concat(allocator, u8, &.{ scratch_bin2_path, path_env_sep, previous_path }));
defer setPathEnv(previous_path);

// verify zig isn't currently on 0.7.0 before we set it as the default
try checkZigVersion(allocator, path_link, expected_zig_version_0_7_0, .not_equal);

{
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8 {"default", "0.7.0"});
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
std.log.info("output: {s}", .{result.stderr});
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "error: zig compiler '"));
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' is higher priority in PATH than the path-link '"));
Expand All @@ -225,23 +275,29 @@ pub fn main() !u8 {
// NOTE: this test will eventually break when these builds are cleaned up,
// we should support downloading from bazel and use that instead since
// it should be more permanent
try runNoCapture(zigup_args ++ &[_][]const u8 { "0.11.0-dev.4263+f821543e4" });
try runNoCapture(zigup_args ++ &[_][]const u8{"0.11.0-dev.4263+f821543e4"});

std.log.info("Success", .{});
return 0;
}

fn checkZigVersion(allocator: std.mem.Allocator, zig: []const u8, compare: []const u8, want_equal: enum { not_equal, equal }) !void {
const result = try runCaptureOuts(allocator, &[_][]const u8 {zig, "version" });
defer { allocator.free(result.stdout); allocator.free(result.stderr); }
const result = try runCaptureOuts(allocator, &[_][]const u8{ zig, "version" });
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
try passOrDumpAndThrow(result);

const actual_version = std.mem.trimRight(u8, result.stdout, "\r\n");
const actual_equal = std.mem.eql(u8, compare, actual_version);
const expected_equal = switch (want_equal) { .not_equal => false, .equal => true };
const expected_equal = switch (want_equal) {
.not_equal => false,
.equal => true,
};
if (expected_equal != actual_equal) {
const prefix: []const u8 = if (expected_equal) "" else " NOT";
std.log.info("expected zig version to{s} be '{s}', but is '{s}'", .{prefix, compare, actual_version});
std.log.info("expected zig version to{s} be '{s}', but is '{s}'", .{ prefix, compare, actual_version });
return error.TestUnexpectedResult;
}
}
Expand All @@ -265,17 +321,16 @@ fn getCompilerCount(install_dir: []const u8) !u32 {
return count;
}


fn trailNl(s: []const u8) []const u8 {
return if (s.len == 0 or s[s.len-1] != '\n') "\n" else "";
return if (s.len == 0 or s[s.len - 1] != '\n') "\n" else "";
}

fn dumpExecResult(result: std.ChildProcess.ExecResult) void {
if (result.stdout.len > 0) {
std.debug.print("--- STDOUT ---\n{s}{s}--------------\n", .{result.stdout, trailNl(result.stdout)});
std.debug.print("--- STDOUT ---\n{s}{s}--------------\n", .{ result.stdout, trailNl(result.stdout) });
}
if (result.stderr.len > 0) {
std.debug.print("--- STDERR ---\n{s}{s}--------------\n", .{result.stderr, trailNl(result.stderr)});
std.debug.print("--- STDERR ---\n{s}{s}--------------\n", .{ result.stderr, trailNl(result.stderr) });
}
}

Expand All @@ -292,7 +347,7 @@ fn runCaptureOuts(allocator: std.mem.Allocator, argv: []const []const u8) !std.C
defer allocator.free(cmd);
std.log.info("RUN: {s}", .{cmd});
}
return try std.ChildProcess.exec(.{.allocator = allocator, .argv = argv, .env_map = &child_env_map});
return try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv, .env_map = &child_env_map });
}
fn passOrThrow(term: std.ChildProcess.Term) error{ChildProcessFailed}!void {
if (!execResultPassed(term)) {
Expand Down
Loading

0 comments on commit 962c153

Please sign in to comment.