lawrence-laz / neotest-zig

Test runner for Zig in Neovim using Neotest backend.
MIT License
27 stars 7 forks source link

Detect leaks in tests #20

Closed ziontee113 closed 5 months ago

ziontee113 commented 5 months ago

Is your feature request related to a problem? Please describe. The following example from zig init:

test "simple test" {
    var list = std.ArrayList(i32).init(std.testing.allocator);
    // defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
    try list.append(42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

Currently, neotest-zig doesn't tell us if there's a leak detected while running tests (no output, tests still green). Screenshot from 2024-06-08 07-07-28

Using zig build test, while it says the tests passes, it also gives us info about leaks.

  /mnt/136gb_SSD/learn-zig/hello-zig   master !1 ❯ zig build test                                                                                               07:06:27
test
└─ run test 2/2 passed, 1 leaked
error: 'main.test.simple test' leaked: [gpa] (err): memory address 0x7b4178427000 leaked:
/usr/lib/zig/std/array_list.zig:457:67: 0x104be4e in ensureTotalCapacityPrecise (test)
                const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
                                                                  ^
/usr/lib/zig/std/array_list.zig:434:51: 0x1041e00 in ensureTotalCapacity (test)
            return self.ensureTotalCapacityPrecise(better_capacity);
                                                  ^
/usr/lib/zig/std/array_list.zig:483:41: 0x103e5f0 in addOne (test)
            try self.ensureTotalCapacity(newlen);
                                        ^
/usr/lib/zig/std/array_list.zig:262:49: 0x103b3bc in append (test)
            const new_item_ptr = try self.addOne();
                                                ^
/mnt/136gb_SSD/learn-zig/hello-zig/src/main.zig:22:20: 0x1039592 in test.simple test (test)
    try list.append(42);
                   ^
/usr/lib/zig/compiler/test_runner.zig:93:29: 0x10484ab in mainServer (test)
                test_fn.func() catch |err| switch (err) {
                            ^
/usr/lib/zig/compiler/test_runner.zig:33:26: 0x103f6ae in main (test)
        return mainServer() catch @panic("internal test runner failure");
                         ^
/usr/lib/zig/std/start.zig:501:22: 0x103ba59 in posixCallMainAndExit (test)
            root.main();
                     ^
/usr/lib/zig/std/start.zig:253:5: 0x103b5c1 in _start (test)
    asm volatile (switch (native_arch) {
    ^
error: while executing test 'main.test.simple test 2', the following test command failed:

Describe the solution you'd like Maybe give users a warning or fails straight up.

lawrence-laz commented 5 months ago

Makes sense. Could you check out if https://github.com/lawrence-laz/neotest-zig/pull/22 acts as you would expect?

image image