Skip to content

Commit

Permalink
rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Dec 15, 2024
1 parent 3885585 commit 9b1a9d7
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 98 deletions.
210 changes: 210 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub fn build(b: *std.Build) !void {
};

const test_step = b.step("test", "test the executable");

addTests(b, target, zigup_exe_native, test_step);

{
const exe = b.addExecutable(.{
.name = "test",
Expand Down Expand Up @@ -219,3 +222,210 @@ fn makeCiArchiveStep(
tar.step.dependOn(&exe_install.step);
return &tar.step;
}

fn addTests(
b: *std.Build,
target: std.Build.ResolvedTarget,
zigup_exe: *std.Build.Step.Compile,
test_step: *std.Build.Step,
) void {
const zigupwithenv_exe = b.addExecutable(.{
.name = "zigupwithenv",
.root_source_file = b.path("zigupwithenv.zig"),
.target = target,
});
const tests: Tests = .{
.b = b,
.test_step = test_step,
.zigup_exe = zigup_exe,
.zigupwithenv_exe = zigupwithenv_exe,
};

_ = tests.add(.{
.name = "test-usage-h",
.argv = &.{"-h"},
.check = .{ .expect_stderr_match = "Usage" },
});
_ = tests.add(.{
.name = "test-usage-help",
.argv = &.{"--help"},
.check = .{ .expect_stderr_match = "Usage" },
});

_ = tests.add(.{
.name = "test-fetch-index",
.argv = &.{"fetch-index"},
.checks = &.{
.{ .expect_stdout_match = "master" },
.{ .expect_stdout_match = "version" },
.{ .expect_stdout_match = "0.13.0" },
},
});

_ = tests.add(.{
.name = "test-no-default",
.argv = &.{"default"},
.check = .{ .expect_stdout_exact = "<no-default>\n" },
});
_ = tests.add(.{
.name = "test-default-master-not-fetched",
.argv = &.{ "default", "master" },
.check = .{ .expect_stderr_match = "master has not been fetched" },
});
_ = tests.add(.{
.name = "test-default-0.7.0-not-fetched",
.argv = &.{ "default", "0.7.0" },
.check = .{ .expect_stderr_match = "error: compiler '0.7.0' is not installed\n" },
});

const _7 = tests.add(.{
.name = "test-0.7.0",
.argv = &.{"0.7.0"},
//.check = .{ .expect_stderr_match = "error: compiler '0.7.0' is not installed\n" },
});
_ = tests.add(.{
.name = "test-already-fetched-0.7.0",
.env = _7,
.argv = &.{ "fetch", "0.7.0" },
.check = .{ .expect_stderr_match = "already installed" },
});
_ = tests.add(.{
.name = "test-get-default-7",
.env = _7,
.argv = &.{"default"},
.check = .{ .expect_stdout_exact = "0.7.0\n" },
});
_ = tests.add(.{
.name = "test-get-default-7-no-path",
.env = _7,
.add_path = false,
.argv = &.{ "default", "0.7.0" },
.check = .{ .expect_stderr_exact = " is not in PATH" },
});

const _7_and_8 = tests.add(.{
.name = "test-fetch-8",
.env = _7,
.argv = &.{ "fetch", "0.8.0" },
});
_ = tests.add(.{
.name = "test-get-default-7-after-fetch-8",
.env = _7_and_8,
.argv = &.{"default"},
.check = .{ .expect_stdout_exact = "0.7.0\n" },
});
_ = tests.add(.{
.name = "test-already-fetched-8",
.env = _7_and_8,
.argv = &.{ "fetch", "0.8.0" },
.check = .{ .expect_stderr_match = "already installed" },
});
const _7_and_default_8 = tests.add(.{
.name = "test-set-default-8",
.env = _7_and_8,
.argv = &.{ "default", "0.8.0" },
.check = .{ .expect_stdout_exact = "" },
});
_ = tests.add(.{
.name = "test-7-after-default-8",
.env = _7_and_default_8,
.argv = &.{"0.7.0"},
.check = .{ .expect_stdout_exact = "" },
});

const master_7_and_8 = tests.add(.{
.name = "test-master",
.env = _7_and_8,
.argv = &.{"master"},
//.check = .{ .expect_stderr_match = "error: compiler '0.7.0' is not installed\n" },
});

// const master_and_8 = tests.add(.{
// .name = "test-master-and-8",
// .env = master,
// .argv = &.{"0.8.0"},
// });

_ = tests.add(.{
.name = "test-already-fetched-master",
.env = master_7_and_8,
.argv = &.{ "fetch", "master" },
.check = .{ .expect_stderr_match = "already installed" },
});

_ = tests.add(.{
.name = "test-default-after-master",
.env = master_7_and_8,
.argv = &.{"default"},
// master version could be anything so we won't check
});
_ = tests.add(.{
.name = "test-default-not-in-path",
.add_path = false,
.env = master_7_and_8,
.argv = &.{ "default", "master" },
.check = .{ .expect_stderr_match = " is not in PATH" },
});
_ = tests.add(.{
.name = "test-default-master",
.env = master_7_and_8,
.argv = &.{ "default", "master" },
});

_ = tests.add(.{
.name = "test-list",
.env = master_7_and_8,
.argv = &.{"list"},
.checks = &.{
.{ .expect_stdout_match = "0.7.0\n" },
.{ .expect_stdout_match = "0.8.0\n" },
},
});

//_ = master_and_8;
//test_step.dependOn(&.step);
}

const native_exe_ext = builtin.os.tag.exeFileExt(builtin.cpu.arch);

const Tests = struct {
b: *std.Build,
test_step: *std.Build.Step,
zigup_exe: *std.Build.Step.Compile,
zigupwithenv_exe: *std.Build.Step.Compile,

fn add(
tests: Tests,
opt: struct {
name: []const u8,
env: ?std.Build.LazyPath = null,
add_path: bool = true,
argv: []const []const u8,
check: ?std.Build.Step.Run.StdIo.Check = null,
checks: []const std.Build.Step.Run.StdIo.Check = &.{},
},
) std.Build.LazyPath {
const b = tests.b;
const run = std.Build.Step.Run.create(b, b.fmt("run {s}", .{opt.name}));
run.addArtifactArg(tests.zigupwithenv_exe);
run.addArg(if (opt.add_path) "--with-path" else "--no-path");
if (opt.env) |env| {
run.addDirectoryArg(env);
} else {
run.addArg("--no-input-environment");
}
const out_env = run.addOutputDirectoryArg("env");
run.addFileArg(tests.zigup_exe.getEmittedBin());
run.addArgs(opt.argv);
if (opt.check) |check| {
run.addCheck(check);
}
for (opt.checks) |check| {
run.addCheck(check);
}

b.step(opt.name, "").dependOn(&run.step);
tests.test_step.dependOn(&run.step);
return out_env;
}
};
98 changes: 2 additions & 96 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,72 +92,7 @@ pub fn main() !u8 {
const original_path_env = path_env_ptr.*;
setPathEnv(try std.mem.concat(allocator, u8, &.{ bin_dir, 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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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"});
{
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"));
}

// verify we print a nice error message if we can't update the symlink
// because it's a directory
Expand Down Expand Up @@ -194,39 +129,10 @@ pub fn main() !u8 {
try std.fs.cwd().deleteDir(zig_exe_link);
}

{
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"});
{
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"));
}
{
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);
}
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 {
Expand Down
2 changes: 1 addition & 1 deletion win32exelink.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn main() !u8 {
global.child = std.process.Child.init(args, global.arena);

if (0 == win32.SetConsoleCtrlHandler(consoleCtrlHandler, 1)) {
log.err("SetConsoleCtrlHandler failed, error={}", .{win32.GetLastError()});
log.err("SetConsoleCtrlHandler failed, error={}", .{@intFromEnum(win32.GetLastError())});
return 0xff; // fail
}

Expand Down
5 changes: 4 additions & 1 deletion zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,10 @@ fn setDefaultCompiler(allocator: Allocator, compiler_dir: []const u8, exist_veri
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", comptime "zig" ++ builtin.target.exeFileExt() });
const link_target = try std.fs.path.join(
allocator,
&[_][]const u8{ compiler_dir, "files", comptime "zig" ++ builtin.target.exeFileExt() },
);
defer allocator.free(link_target);
if (builtin.os.tag == .windows) {
try createExeLink(link_target, path_link);
Expand Down
Loading

0 comments on commit 9b1a9d7

Please sign in to comment.