From e3638053d0b0fad4d33cefc1d7ea7acb803ecba7 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 16 Apr 2025 19:34:36 +0800 Subject: [PATCH] better error messages in WPT report (in line with what main branch is doing) --- src/browser/dom/node.zig | 19 +++++++++++-------- src/testing.zig | 7 ++++--- src/wpt/run.zig | 16 ++++++++-------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/browser/dom/node.zig b/src/browser/dom/node.zig index 1d7339e5..4359e07f 100644 --- a/src/browser/dom/node.zig +++ b/src/browser/dom/node.zig @@ -411,14 +411,17 @@ test "Browser.DOM.node" { var runner = try testing.jsRunner(testing.tracking_allocator, .{}); defer runner.deinit(); - try runner.exec( - \\ function trimAndReplace(str) { - \\ str = str.replace(/(\r\n|\n|\r)/gm,''); - \\ str = str.replace(/\s+/g, ' '); - \\ str = str.trim(); - \\ return str; - \\ } - ); + { + var err_out: ?[]const u8 = null; + try runner.exec( + \\ function trimAndReplace(str) { + \\ str = str.replace(/(\r\n|\n|\r)/gm,''); + \\ str = str.replace(/\s+/g, ' '); + \\ str = str.trim(); + \\ return str; + \\ } + , &err_out); + } try runner.testCases(&.{ .{ "document.body.compareDocumentPosition(document.firstChild); ", "10" }, diff --git a/src/testing.zig b/src/testing.zig index 304df25f..b4c121a1 100644 --- a/src/testing.zig +++ b/src/testing.zig @@ -468,17 +468,18 @@ pub const JsRunner = struct { } } - pub fn exec(self: *JsRunner, src: []const u8) !void { - _ = try self.eval(src); + pub fn exec(self: *JsRunner, src: []const u8, err_msg: *?[]const u8) !void { + _ = try self.eval(src, err_msg); } - pub fn eval(self: *JsRunner, src: []const u8) !Env.Value { + pub fn eval(self: *JsRunner, src: []const u8, err_msg: *?[]const u8) !Env.Value { var try_catch: Env.TryCatch = undefined; try_catch.init(self.executor); defer try_catch.deinit(); return self.executor.exec(src, null) catch |err| { if (try try_catch.err(self.arena)) |msg| { + err_msg.* = msg; std.debug.print("Error runnign script: {s}\n", .{msg}); } return err; diff --git a/src/wpt/run.zig b/src/wpt/run.zig index 27dc28c8..ab8de337 100644 --- a/src/wpt/run.zig +++ b/src/wpt/run.zig @@ -30,7 +30,7 @@ const polyfill = @import("../browser/polyfill/polyfill.zig"); // runWPT parses the given HTML file, starts a js env and run the first script // tags containing javascript sources. // It loads first the js libs files. -pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader, msg_out: *?[]const u8) ![]const u8 { +pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader, err_msg: *?[]const u8) ![]const u8 { // document const html = blk: { const file = try std.fs.cwd().openFile(f, .{}); @@ -48,7 +48,7 @@ pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *F // display console logs defer { - const res = runner.eval("console.join('\\n');") catch unreachable; + const res = runner.eval("console.join('\\n');", err_msg) catch unreachable; const log = res.toString(arena) catch unreachable; if (log.len > 0) { std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{log}); @@ -63,7 +63,7 @@ pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *F \\ console.debug = function () { \\ console.push("debug", ...arguments); \\ }; - ); + , err_msg); // loop over the scripts. const doc = parser.documentHTMLToDocument(runner.state.document.?); @@ -79,12 +79,12 @@ pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *F // no need to free path, thanks to the arena. path = try fspath.join(arena, &.{ "/", dirname, path }); } - try runner.exec(try loader.get(path)); + try runner.exec(try loader.get(path), err_msg); } // If the script as a source text, execute it. const src = try parser.nodeTextContent(s) orelse continue; - try runner.exec(src); + try runner.exec(src, err_msg); } // Mark tests as ready to run. @@ -104,17 +104,17 @@ pub fn run(arena: Allocator, comptime dir: []const u8, f: []const u8, loader: *F defer try_catch.deinit(); runner.loop.run() catch |err| { if (try try_catch.err(arena)) |msg| { - msg_out.* = msg; + err_msg.* = msg; } return err; }; } // Check the final test status. - try runner.exec("report.status;"); + try runner.exec("report.status;", err_msg); // return the detailed result. - const res = try runner.eval("report.log"); + const res = try runner.eval("report.log", err_msg); return res.toString(arena); }