Skip to content

Commit

Permalink
Add basic cli test infrastructure
Browse files Browse the repository at this point in the history
The test cases should be expanded, just a basic 'including windows.h doesn't explode' test for now
  • Loading branch information
squeek502 committed Mar 5, 2024
1 parent aad1328 commit e74f4a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,21 @@ pub fn build(b: *std.Build) void {
compiler_tests.root_module.addImport("resinator", resinator);
const run_compiler_tests = b.addRunArtifact(compiler_tests);

const cli_test_options = b.addOptions();
cli_test_options.addOptionPath("cli_exe_path", exe.getEmittedBin());
const cli_tests = b.addTest(.{
.name = "cli",
.root_source_file = .{ .path = "test/cli.zig" },
});
cli_tests.root_module.addOptions("build_options", cli_test_options);
const run_cli_tests = b.addRunArtifact(cli_tests);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_exe_tests.step);
test_step.dependOn(&run_reference_tests.step);
test_step.dependOn(&run_parser_tests.step);
test_step.dependOn(&run_compiler_tests.step);
test_step.dependOn(&run_cli_tests.step);

// TODO: coverage across all test steps?
const coverage = b.option(bool, "test-coverage", "Generate test coverage") orelse false;
Expand Down
37 changes: 37 additions & 0 deletions test/cli.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");
const build_options = @import("build_options");
const exe_path = build_options.cli_exe_path;

test "including windows.h" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();

try tmp.dir.writeFile("test.rc",
\\#include "windows.h"
);

try testExpectNoError(tmp.dir, &.{ exe_path, "test.rc" });
try testExpectNoError(tmp.dir, &.{ exe_path, "/:auto-includes", "gnu", "test.rc" });
}

fn testExpectNoError(dir: std.fs.Dir, argv: []const []const u8) !void {
const allocator = std.testing.allocator;

const dir_path = try dir.realpathAlloc(allocator, ".");
defer allocator.free(dir_path);

const result = try std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv,
.max_output_bytes = std.math.maxInt(u32),
.cwd = dir_path,
.cwd_dir = dir,
});
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);

if (result.term != .Exited or result.term.Exited != 0) {
std.debug.print("command failed with unexpected errors:\n---\nstdout:\n---\n{s}\n---\nstderr:\n---\n{s}\n", .{ result.stdout, result.stderr });
return error.UnexpectedErrors;
}
}

0 comments on commit e74f4a6

Please sign in to comment.