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 6c5b62b
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 42 deletions.
150 changes: 150 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,150 @@ 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 = "default-0.7.0",
.env = _7,
.argv = &.{"default"},
.check = .{ .expect_stdout_exact = "0.7.0\n" },
});
}

const master = tests.add(.{
.name = "test-master",
.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_and_8,
.argv = &.{ "fetch", "master" },
.check = .{ .expect_stderr_match = "already installed" },
});

_ = tests.add(.{
.name = "default-8",
.env = master_and_8,
.argv = &.{"default"},
.check = .{ .expect_stdout_exact = "0.8.1\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,
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);
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;
}
};
42 changes: 0 additions & 42 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,48 +92,6 @@ 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 {
Expand Down

0 comments on commit 6c5b62b

Please sign in to comment.