testing: add LogFilter utility for scoped log suppression

This commit is contained in:
Adrià Arrufat
2026-03-12 13:56:53 +09:00
parent 1522c90294
commit 3d6d669a50
4 changed files with 29 additions and 3 deletions

View File

@@ -3457,6 +3457,9 @@ fn asUint(comptime string: anytype) std.meta.Int(
const testing = @import("../testing.zig");
test "WebApi: Page" {
const filter: testing.LogFilter = .init(.http);
defer filter.deinit();
try testing.htmlRunner("page", .{});
}

View File

@@ -662,6 +662,10 @@ test "cdp.page: getFrameTree" {
}
test "cdp.page: captureScreenshot" {
const LogFilter = @import("../../testing.zig").LogFilter;
const filter: LogFilter = .init(.not_implemented);
defer filter.deinit();
var ctx = testing.context();
defer ctx.deinit();
{

View File

@@ -133,9 +133,8 @@ test "MCP.router - handleMessage - synchronous unit tests" {
// 4. Parse error
{
const old_filter = log.opts.filter_scopes;
log.opts.filter_scopes = &.{.mcp};
defer log.opts.filter_scopes = old_filter;
const filter: testing.LogFilter = .init(.mcp);
defer filter.deinit();
try handleMessage(server, aa, "invalid json");
try testing.expectJson("{\"id\": null, \"error\": {\"code\": -32700}}", out_alloc.writer.buffered());

View File

@@ -610,3 +610,23 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
unreachable;
}
/// LogFilter provides a scoped way to suppress specific log categories during tests.
/// This is useful for tests that trigger expected errors or warnings.
pub const LogFilter = struct {
old_filter: []const log.Scope,
/// Sets the log filter to only include the specified scope.
/// Returns a LogFilter that should be deinitialized to restore previous filters.
pub fn init(comptime scope: log.Scope) LogFilter {
const old_filter = log.opts.filter_scopes;
const new_filter = comptime &[_]log.Scope{scope};
log.opts.filter_scopes = new_filter;
return .{ .old_filter = old_filter };
}
/// Restores the log filters to their previous state.
pub fn deinit(self: LogFilter) void {
log.opts.filter_scopes = self.old_filter;
}
};