Skip to content

Commit

Permalink
Add CLI internals tests
Browse files Browse the repository at this point in the history
There are several test decls inside `/src` that are not currently being
tested and have bitrotted as a result. This commit revives those tests
and adds the `test-cli-internals` set of tests which tests everything
reachable from `/src/main.zig`.
  • Loading branch information
castholm committed Nov 6, 2024
1 parent 78bf2c0 commit 59c3678
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
41 changes: 15 additions & 26 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,6 @@ pub fn build(b: *std.Build) !void {
const test_target_filters = b.option([]const []const u8, "test-target-filter", "Skip tests whose target triple do not match any filter") orelse &[0][]const u8{};
const test_slow_targets = b.option(bool, "test-slow-targets", "Enable running module tests for targets that have a slow compiler backend") orelse false;

const test_cases_options = b.addOptions();

test_cases_options.addOption(bool, "enable_tracy", false);
test_cases_options.addOption(bool, "enable_debug_extensions", enable_debug_extensions);
test_cases_options.addOption(bool, "enable_logging", enable_logging);
test_cases_options.addOption(bool, "enable_link_snapshots", enable_link_snapshots);
test_cases_options.addOption(bool, "skip_non_native", skip_non_native);
test_cases_options.addOption(bool, "have_llvm", enable_llvm);
test_cases_options.addOption(bool, "llvm_has_m68k", llvm_has_m68k);
test_cases_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
test_cases_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
test_cases_options.addOption(bool, "llvm_has_xtensa", llvm_has_xtensa);
test_cases_options.addOption(bool, "force_gpa", force_gpa);
test_cases_options.addOption(bool, "enable_qemu", b.enable_qemu);
test_cases_options.addOption(bool, "enable_wine", b.enable_wine);
test_cases_options.addOption(bool, "enable_wasmtime", b.enable_wasmtime);
test_cases_options.addOption(bool, "enable_rosetta", b.enable_rosetta);
test_cases_options.addOption(bool, "enable_darling", b.enable_darling);
test_cases_options.addOption(u32, "mem_leak_frames", mem_leak_frames * 2);
test_cases_options.addOption(bool, "value_tracing", value_tracing);
test_cases_options.addOption(?[]const u8, "glibc_runtimes_dir", b.glibc_runtimes_dir);
test_cases_options.addOption([:0]const u8, "version", version);
test_cases_options.addOption(std.SemanticVersion, "semver", semver);
test_cases_options.addOption([]const []const u8, "test_filters", test_filters);
test_cases_options.addOption(DevEnv, "dev", if (only_c) .bootstrap else .core);

var chosen_opt_modes_buf: [4]builtin.OptimizeMode = undefined;
var chosen_mode_index: usize = 0;
if (!skip_debug) {
Expand Down Expand Up @@ -531,6 +505,21 @@ pub fn build(b: *std.Build) !void {
.max_rss = 5029889638,
}));

test_modules_step.dependOn(tests.addModuleTests(b, .{
.test_filters = test_filters,
.test_target_filters = test_target_filters,
.test_slow_targets = test_slow_targets,
.root_src = "src/main.zig",
.name = "cli-internals",
.desc = "Run the CLI internals tests",
.optimize_modes = optimization_modes,
.include_paths = &.{},
.skip_single_threaded = skip_single_threaded,
.skip_non_native = skip_non_native,
.skip_libc = skip_libc,
.build_options = exe_options,
}));

test_step.dependOn(test_modules_step);

test_step.dependOn(tests.addCompareOutputTests(b, test_filters, optimization_modes));
Expand Down
1 change: 1 addition & 0 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9804,6 +9804,7 @@ test "basic usage" {
const gpa = std.testing.allocator;

var ip: InternPool = .empty;
try ip.init(gpa, 1);
defer ip.deinit(gpa);

const i32_type = try ip.get(gpa, .main, .{ .int_type = .{
Expand Down
16 changes: 11 additions & 5 deletions src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,7 @@ test "zip" {
defer fb.deinit();

try fetch.run();
try std.testing.expectEqual(.path, fetch.actual_path_or_url_kind);

var out = try fb.packageDir();
defer out.close();
Expand Down Expand Up @@ -2087,6 +2088,7 @@ test "zip with one root folder" {
defer fb.deinit();

try fetch.run();
try std.testing.expectEqual(.path, fetch.actual_path_or_url_kind);

var out = try fb.packageDir();
defer out.close();
Expand Down Expand Up @@ -2126,7 +2128,7 @@ test "tarball with duplicate paths" {

try fb.expectFetchErrors(1,
\\error: unable to unpack tarball
\\ note: unable to create file 'dir1/file1': PathAlreadyExists
\\ note: unable to create file 'duplicate_paths/dir1/file1': PathAlreadyExists
\\
);
}
Expand Down Expand Up @@ -2161,6 +2163,7 @@ test "tarball with excluded duplicate paths" {
"12200bafe035cbb453dd717741b66e9f9d1e6c674069d06121dafa1b2e62eb6b22da",
&hex_digest,
);
try std.testing.expectEqual(.path, fetch.actual_path_or_url_kind);

const expected_files: []const []const u8 = &.{
"build.zig",
Expand Down Expand Up @@ -2205,6 +2208,7 @@ test "tarball without root folder" {
"12209f939bfdcb8b501a61bb4a43124dfa1b2848adc60eec1e4624c560357562b793",
&hex_digest,
);
try std.testing.expectEqual(.path, fetch.actual_path_or_url_kind);

const expected_files: []const []const u8 = &.{
"build.zig",
Expand Down Expand Up @@ -2243,6 +2247,7 @@ test "set executable bit based on file content" {
"1220fecb4c06a9da8673c87fe8810e15785f1699212f01728eadce094d21effeeef3",
&Manifest.hexDigest(fetch.actual_hash),
);
try std.testing.expectEqual(.path, fetch.actual_path_or_url_kind);

var out = try fb.packageDir();
defer out.close();
Expand Down Expand Up @@ -2279,7 +2284,6 @@ const TestFetchBuilder = struct {
thread_pool: ThreadPool,
http_client: std.http.Client,
global_cache_directory: Cache.Directory,
progress: std.Progress,
job_queue: Fetch.JobQueue,
fetch: Fetch,

Expand All @@ -2295,8 +2299,6 @@ const TestFetchBuilder = struct {
self.http_client = .{ .allocator = allocator };
self.global_cache_directory = .{ .handle = cache_dir, .path = null };

self.progress = .{ .dont_print_on_dumb = true };

self.job_queue = .{
.http_client = &self.http_client,
.thread_pool = &self.thread_pool,
Expand All @@ -2316,10 +2318,11 @@ const TestFetchBuilder = struct {
.lazy_status = .eager,
.parent_package_root = Cache.Path{ .root_dir = Cache.Directory{ .handle = cache_dir, .path = null } },
.parent_manifest_ast = null,
.prog_node = self.progress.start("Fetch", 0),
.prog_node = std.Progress.Node.none,
.job_queue = &self.job_queue,
.omit_missing_hash_error = true,
.allow_missing_paths_field = false,
.use_latest_commit = true,

.package_root = undefined,
.error_bundle = undefined,
Expand All @@ -2328,6 +2331,9 @@ const TestFetchBuilder = struct {
.actual_hash = undefined,
.has_build_zig = false,
.oom_flag = false,
.latest_commit = null,
.actual_path_or_url_kind = undefined,

.module = null,
};
return &self.fetch;
Expand Down
2 changes: 1 addition & 1 deletion src/link/MachO/dyld_info/Rebase.zig
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ test "rebase - no entries" {
defer rebase.deinit(gpa);

try rebase.finalize(gpa);
try testing.expectEqual(@as(u64, 0), rebase.size());
try testing.expectEqual(0, rebase.buffer.items.len);
}

test "rebase - single entry" {
Expand Down
4 changes: 4 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const Zcu = @import("Zcu.zig");
const mingw = @import("mingw.zig");
const dev = @import("dev.zig");

test {
_ = Package;
}

pub const std_options: std.Options = .{
.wasiCwd = wasi_cwd,
.logFn = log,
Expand Down
4 changes: 4 additions & 0 deletions test/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ const ModuleTestOptions = struct {
skip_libc: bool,
max_rss: usize = 0,
no_builtin: bool = false,
build_options: ?*std.Build.Step.Options = null,
};

pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
Expand Down Expand Up @@ -1374,6 +1375,9 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
.strip = test_target.strip,
});
if (options.no_builtin) these_tests.no_builtin = true;
if (options.build_options) |build_options| {
these_tests.root_module.addOptions("build_options", build_options);
}
const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else "";
const backend_suffix = if (test_target.use_llvm == true)
"-llvm"
Expand Down

0 comments on commit 59c3678

Please sign in to comment.