wpt: restore the test results

This commit is contained in:
Pierre Tachoire
2024-07-18 16:40:00 +02:00
parent b8ec53f708
commit 4434e11bdd
2 changed files with 58 additions and 18 deletions

View File

@@ -141,7 +141,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(alloc); var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit(); defer arena.deinit();
const result = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| { const res = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| {
const suite = try Suite.init(alloc, tc, false, @errorName(err), null); const suite = try Suite.init(alloc, tc, false, @errorName(err), null);
try results.append(suite); try results.append(suite);
@@ -151,8 +151,9 @@ pub fn main() !void {
failures += 1; failures += 1;
continue; continue;
}; };
defer res.deinit(arena.allocator());
const suite = try Suite.init(alloc, tc, true, result, null); const suite = try Suite.init(alloc, tc, res.ok, res.msg orelse "", null);
try results.append(suite); try results.append(suite);
if (out == .json) { if (out == .json) {

View File

@@ -36,7 +36,7 @@ const Client = @import("../async/Client.zig");
// runWPT parses the given HTML file, starts a js env and run the first script // runWPT parses the given HTML file, starts a js env and run the first script
// tags containing javascript sources. // tags containing javascript sources.
// It loads first the js libs files. // It loads first the js libs files.
pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) ![]const u8 { pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !Res {
const alloc = arena.allocator(); const alloc = arena.allocator();
try parser.init(); try parser.init();
defer parser.deinit(); defer parser.deinit();
@@ -75,10 +75,11 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
// display console logs // display console logs
defer { defer {
const res = evalJS(js_env, "console.join('\\n');", "console") catch unreachable; const res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
const res_str = res.toString(alloc, js_env) catch unreachable; defer res.deinit(alloc);
if (res_str.len > 0) {
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res_str}); if (res.msg != null and res.msg.?.len > 0) {
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.msg.?});
} }
} }
@@ -88,8 +89,6 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
window.setStorageShelf(&storageShelf); window.setStorageShelf(&storageShelf);
try js_env.bindGlobal(&window); try js_env.bindGlobal(&window);
// thanks to the arena, we don't need to deinit res.
const init = const init =
\\console = []; \\console = [];
\\console.log = function () { \\console.log = function () {
@@ -99,7 +98,9 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
\\ console.push("debug", ...arguments); \\ console.push("debug", ...arguments);
\\}; \\};
; ;
_ = try evalJS(js_env, init, "init"); var res = try evalJS(js_env, alloc, init, "init");
if (!res.ok) return res;
res.deinit(alloc);
// loop hover the scripts. // loop hover the scripts.
const doc = parser.documentHTMLToDocument(html_doc); const doc = parser.documentHTMLToDocument(html_doc);
@@ -116,12 +117,16 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
path = try fspath.join(alloc, &.{ "/", dirname, path }); path = try fspath.join(alloc, &.{ "/", dirname, path });
} }
_ = try evalJS(js_env, try loader.get(path), src); res = try evalJS(js_env, alloc, try loader.get(path), src);
if (!res.ok) return res;
res.deinit(alloc);
} }
// If the script as a source text, execute it. // If the script as a source text, execute it.
const src = try parser.nodeTextContent(s) orelse continue; const src = try parser.nodeTextContent(s) orelse continue;
_ = try evalJS(js_env, src, ""); res = try evalJS(js_env, alloc, src, "");
if (!res.ok) return res;
res.deinit(alloc);
} }
// Mark tests as ready to run. // Mark tests as ready to run.
@@ -135,18 +140,52 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
); );
// wait for all async executions // wait for all async executions
_ = try js_env.wait(); var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(js_env);
defer try_catch.deinit();
js_env.wait() catch {
return .{
.ok = false,
.msg = try try_catch.err(alloc, js_env),
};
};
// Check the final test status. // Check the final test status.
_ = try evalJS(js_env, "report.status;", "teststatus"); res = try evalJS(js_env, alloc, "report.status;", "teststatus");
if (!res.ok) return res;
res.deinit(alloc);
// return the detailed result. // return the detailed result.
const res = try evalJS(js_env, "report.log", "teststatus"); return try evalJS(js_env, alloc, "report.log", "teststatus");
return try res.toString(alloc, js_env);
} }
fn evalJS(env: jsruntime.Env, script: []const u8, name: ?[]const u8) !jsruntime.JSValue { pub const Res = struct {
return try env.exec(script, name); ok: bool,
msg: ?[]const u8,
pub fn deinit(res: Res, alloc: std.mem.Allocator) void {
if (res.msg) |msg| {
alloc.free(msg);
}
}
};
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !Res {
var try_catch: jsruntime.TryCatch = undefined;
try_catch.init(env);
defer try_catch.deinit();
const v = env.exec(script, name) catch {
return .{
.ok = false,
.msg = try try_catch.err(alloc, env),
};
};
return .{
.ok = true,
.msg = try v.toString(alloc, env),
};
} }
// browse the path to find the tests list. // browse the path to find the tests list.