Commit 3b60ab4872

Andrew Kelley <andrew@ziglang.org>
2022-05-06 07:17:21
stage2: fix std lib tests always filtering out all tests
1 parent f034cef
src/Compilation.zig
@@ -1418,6 +1418,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
                 .gpa = gpa,
                 .comp = comp,
                 .main_pkg = main_pkg,
+                .main_pkg_is_std = try main_pkg.isInsideOf(std_pkg.*),
                 .root_pkg = root_pkg,
                 .zig_cache_artifact_directory = zig_cache_artifact_directory,
                 .global_zir_cache = global_zir_cache,
src/Module.zig
@@ -130,6 +130,10 @@ stage1_flags: packed struct {
 } = .{},
 
 job_queued_update_builtin_zig: bool = true,
+/// This makes it so that we can run `zig test` on the standard library.
+/// Otherwise, the logic for scanning test decls skips all of them because
+/// `main_pkg != std_pkg`.
+main_pkg_is_std: bool,
 
 compile_log_text: ArrayListUnmanaged(u8) = .{},
 
@@ -4528,14 +4532,22 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
                 // test decl with no name. Skip the part where we check against
                 // the test name filter.
                 if (!mod.comp.bin_file.options.is_test) break :blk false;
-                if (decl_pkg != mod.main_pkg) break :blk false;
+                if (decl_pkg != mod.main_pkg) {
+                    if (!mod.main_pkg_is_std) break :blk false;
+                    const std_pkg = mod.main_pkg.table.get("std").?;
+                    if (std_pkg != decl_pkg) break :blk false;
+                }
                 try mod.test_functions.put(gpa, new_decl_index, {});
                 break :blk true;
             },
             else => blk: {
                 if (!is_named_test) break :blk false;
                 if (!mod.comp.bin_file.options.is_test) break :blk false;
-                if (decl_pkg != mod.main_pkg) break :blk false;
+                if (decl_pkg != mod.main_pkg) {
+                    if (!mod.main_pkg_is_std) break :blk false;
+                    const std_pkg = mod.main_pkg.table.get("std").?;
+                    if (std_pkg != decl_pkg) break :blk false;
+                }
                 // TODO check the name against --test-filter
                 try mod.test_functions.put(gpa, new_decl_index, {});
                 break :blk true;
src/Package.zig
@@ -124,3 +124,11 @@ pub fn addAndAdopt(parent: *Package, gpa: Allocator, name: []const u8, child: *P
     child.parent = parent;
     return parent.add(gpa, name, child);
 }
+
+pub fn isInsideOf(pkg: Package, another: Package) !bool {
+    var pkg_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
+    var another_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
+    const pkg_path = try pkg.root_src_directory.handle.realpath(pkg.root_src_path, &pkg_buffer);
+    const another_path = try another.root_src_directory.handle.realpath(".", &another_buffer);
+    return mem.startsWith(u8, pkg_path, another_path);
+}