From 6243adbf103004a4b6b38d51b24d078c6de61d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Wed, 6 Nov 2024 22:30:46 +0100 Subject: [PATCH] Add CLI internals tests 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`. --- build.zig | 41 +++++++++++------------------ src/InternPool.zig | 1 + src/Package/Fetch.zig | 16 +++++++---- src/link/MachO/dyld_info/Rebase.zig | 2 +- src/main.zig | 4 +++ test/tests.zig | 4 +++ 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/build.zig b/build.zig index 633d11419f94..49d75872ede3 100644 --- a/build.zig +++ b/build.zig @@ -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) { @@ -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 = true, + .skip_libc = skip_libc, + .build_options = exe_options, + })); + test_step.dependOn(test_modules_step); test_step.dependOn(tests.addCompareOutputTests(b, test_filters, optimization_modes)); diff --git a/src/InternPool.zig b/src/InternPool.zig index 59db85681a9c..f76d90354c3a 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -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 = .{ diff --git a/src/Package/Fetch.zig b/src/Package/Fetch.zig index 3fdb2eb6ce8c..913a040c6460 100644 --- a/src/Package/Fetch.zig +++ b/src/Package/Fetch.zig @@ -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(); @@ -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(); @@ -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 \\ ); } @@ -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", @@ -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", @@ -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(); @@ -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, @@ -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, @@ -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, @@ -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; diff --git a/src/link/MachO/dyld_info/Rebase.zig b/src/link/MachO/dyld_info/Rebase.zig index cbd0461431b6..dc4fcbcc1d70 100644 --- a/src/link/MachO/dyld_info/Rebase.zig +++ b/src/link/MachO/dyld_info/Rebase.zig @@ -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" { diff --git a/src/main.zig b/src/main.zig index 09d9af5d14fb..c83c7506186e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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, diff --git a/test/tests.zig b/test/tests.zig index d7b36f522b10..27beb2b4cd72 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -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 { @@ -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"