yaml_test_suite.zig (2930B)
1 const std = @import("std"); 2 3 const yaml = @import("yaml"); 4 5 pub fn main() !void { 6 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 7 defer _ = gpa.deinit(); 8 const allocator = gpa.allocator(); 9 10 const args = try std.process.argsAlloc(allocator); 11 defer std.process.argsFree(allocator, args); 12 13 if (args.len < 2) { 14 std.debug.print( 15 "Usage: {s} <yaml-test-suite-directory>\n", 16 .{ args[0] }, 17 ); 18 std.process.exit(1); 19 } 20 const test_suite_dir = args[1]; 21 22 try runTestSuite(allocator, test_suite_dir); 23 } 24 25 fn runTestSuite(allocator: std.mem.Allocator, suite_dir: []const u8) !void { 26 var dir = try std.fs.cwd().openDir(suite_dir, .{ .iterate = true }); 27 defer dir.close(); 28 29 var it = dir.iterate(); 30 while (try it.next()) |entry| { 31 switch (entry.kind) { 32 .directory => { 33 // it seems like these directories are primarily symlinks to 34 // tests in the root directory. there seems to be a few tests 35 // that are just in these folders, but we'll ignore them for 36 // now 37 if (std.mem.eql(u8, entry.name, "tags")) { 38 continue; 39 } 40 if (std.mem.eql(u8, entry.name, "name")) { 41 continue; 42 } 43 try runTestSet(allocator, dir, entry.name); 44 }, 45 else => continue, 46 } 47 } 48 } 49 50 fn runTestSet( 51 allocator: std.mem.Allocator, 52 suite_dir: std.fs.Dir, 53 test_set_id: []const u8, 54 ) !void { 55 var dir = try suite_dir.openDir(test_set_id, .{ .iterate = true }); 56 defer dir.close(); 57 58 // a set may contain multiple tests 59 dir.access("in.yaml", .{}) catch { 60 var it = dir.iterate(); 61 while (try it.next()) |entry| { 62 switch (entry.kind) { 63 .directory => try runTest(allocator, dir, entry.name), 64 else => continue, 65 } 66 } 67 return; 68 }; 69 // or a single test case 70 try runTestCase(allocator, dir); 71 } 72 73 fn runTest( 74 allocator: std.mem.Allocator, 75 test_set_dir: std.fs.Dir, 76 test_id: []const u8, 77 ) !void { 78 var dir = try test_set_dir.openDir(test_id, .{ .iterate = true }); 79 defer dir.close(); 80 81 try runTestCase(allocator, dir); 82 } 83 84 fn runTestCase( 85 allocator: std.mem.Allocator, 86 test_dir: std.fs.Dir, 87 ) !void { 88 const input_path = "in.yaml"; 89 const error_path = "error"; 90 91 const expect_parse_error = blk: { 92 test_dir.access(error_path, .{}) catch |e| { 93 switch (e) { 94 error.FileNotFound => break :blk false, 95 else => return e, 96 } 97 }; 98 break :blk true; 99 }; 100 _ = expect_parse_error; 101 102 const yaml_data = try test_dir.readFileAlloc( 103 allocator, 104 input_path, 105 1024*1024 106 ); 107 defer allocator.free(yaml_data); 108 }