Skip to content

Commit

Permalink
Merge pull request #712 from ehaas/progress-updates
Browse files Browse the repository at this point in the history
Zig update: new progress implementation
  • Loading branch information
Vexu authored Jun 15, 2024
2 parents a08792d + 1f81bf1 commit d234cc6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build/GenerateDef.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn create(owner: *std.Build, options: Options) std.Build.Module.Import {
};
}

fn make(step: *Step, prog_node: *std.Progress.Node) !void {
fn make(step: *Step, prog_node: std.Progress.Node) !void {
_ = prog_node;
const b = step.owner;
const self: *GenerateDef = @fieldParentPtr("step", step);
Expand Down
4 changes: 2 additions & 2 deletions src/aro/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ const MsgWriter = struct {
config: std.io.tty.Config,

fn init(config: std.io.tty.Config) MsgWriter {
std.debug.getStderrMutex().lock();
std.debug.lockStdErr();
return .{
.w = std.io.bufferedWriter(std.io.getStdErr().writer()),
.config = config,
Expand All @@ -545,7 +545,7 @@ const MsgWriter = struct {

pub fn deinit(m: *MsgWriter) void {
m.w.flush() catch {};
std.debug.getStderrMutex().unlock();
std.debug.unlockStdErr();
}

pub fn print(m: *MsgWriter, comptime fmt: []const u8, args: anytype) void {
Expand Down
22 changes: 9 additions & 13 deletions test/record_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const Stats = struct {
skip_count: u32 = 0,
invalid_target_count: u32 = 0,
max_alloc: usize = 0,
progress: *std.Progress,
root_node: *std.Progress.Node,
root_node: std.Progress.Node,

const ResultKind = enum {
ok,
Expand Down Expand Up @@ -86,9 +85,6 @@ const ExpectedFailure = struct {
const builtin = @import("builtin");

pub fn main() !void {
if (builtin.os.tag == .windows and (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall)) {
return; // Building the test runner in release modes crashes the Zig compiler.
}
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = general_purpose_allocator.allocator();
defer if (general_purpose_allocator.deinit() == .leak) std.process.exit(1);
Expand Down Expand Up @@ -154,11 +150,13 @@ pub fn main() !void {
try parseTargetsFromCode(&test_cases, path, source);
}

var progress = std.Progress{};
const root_node = progress.start("Layout", test_cases.items.len);
const root_node = std.Progress.start(.{
.disable_printing = false,
.root_name = "Layout",
.estimated_total_items = test_cases.items.len,
});

var stats = Stats{
.progress = &progress,
.root_node = root_node,
};

Expand Down Expand Up @@ -242,13 +240,11 @@ fn singleRun(alloc: std.mem.Allocator, test_dir: []const u8, test_case: TestCase
});

var case_node = stats.root_node.start(case_name.items, 0);
case_node.activate();
defer case_node.end();
stats.progress.refresh();

const file = comp.addSourceFromBuffer(path, test_case.source) catch |err| {
stats.recordResult(.fail);
stats.progress.log("could not add source '{s}': {s}\n", .{ path, @errorName(err) });
std.debug.print("could not add source '{s}': {s}\n", .{ path, @errorName(err) });
return;
};

Expand All @@ -275,7 +271,7 @@ fn singleRun(alloc: std.mem.Allocator, test_dir: []const u8, test_case: TestCase
_ = try pp.preprocess(user_macros);
const eof = pp.preprocess(file) catch |err| {
stats.recordResult(.fail);
stats.progress.log("could not preprocess file '{s}': {s}\n", .{ path, @errorName(err) });
std.debug.print("could not preprocess file '{s}': {s}\n", .{ path, @errorName(err) });
return;
};
try pp.addToken(eof);
Expand All @@ -301,7 +297,7 @@ fn singleRun(alloc: std.mem.Allocator, test_dir: []const u8, test_case: TestCase
const expected = compErr.get(buf[0..buf_strm.pos]) orelse ExpectedFailure{};

if (comp.diagnostics.list.items.len == 0 and expected.any()) {
stats.progress.log("\nTest Passed when failures expected:\n\texpected:{any}\n", .{expected});
std.debug.print("\nTest Passed when failures expected:\n\texpected:{any}\n", .{expected});
} else {
var m = aro.Diagnostics.defaultMsgWriter(std.io.tty.detectConfig(std.io.getStdErr()));
defer m.deinit();
Expand Down
62 changes: 30 additions & 32 deletions test/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ fn testOne(allocator: std.mem.Allocator, path: []const u8, test_dir: []const u8)
}

fn testAllAllocationFailures(cases: [][]const u8, test_dir: []const u8) !void {
var progress = std.Progress{};
const root_node = progress.start("Memory Allocation Test", cases.len);
const root_node = std.Progress.start(.{
.disable_printing = false,
.root_name = "Memory Allocation Test",
.estimated_total_items = cases.len,
});

for (cases) |case| {
const case_name = std.mem.sliceTo(std.fs.path.basename(case), '.');
var case_node = root_node.start(case_name, 0);
case_node.activate();
defer case_node.end();
progress.refresh();

std.testing.checkAllAllocationFailures(std.testing.allocator, testOne, .{ case, test_dir }) catch |er| switch (er) {
error.SwallowedOutOfMemoryError => {},
Expand All @@ -108,10 +109,6 @@ fn testAllAllocationFailures(cases: [][]const u8, test_dir: []const u8) !void {
}

pub fn main() !void {
const mode = @import("builtin").mode;
if (mode == .ReleaseFast or mode == .ReleaseSmall) {
return; // Building the test runner in release modes crashes the Zig compiler.
}
const gpa = general_purpose_allocator.allocator();
defer if (general_purpose_allocator.deinit() == .leak) std.process.exit(1);

Expand Down Expand Up @@ -155,8 +152,11 @@ pub fn main() !void {
return testAllAllocationFailures(cases.items, test_dir);
}

var progress = std.Progress{};
const root_node = progress.start("Test", cases.items.len);
const root_node = std.Progress.start(.{
.disable_printing = false,
.root_name = "Test",
.estimated_total_items = cases.items.len,
});

// prepare compiler
var initial_comp = aro.Compilation.init(gpa);
Expand Down Expand Up @@ -200,13 +200,11 @@ pub fn main() !void {

const case = std.mem.sliceTo(std.fs.path.basename(path), '.');
var case_node = root_node.start(case, 0);
case_node.activate();
defer case_node.end();
progress.refresh();

const file = comp.addSourceFromPath(path) catch |err| {
fail_count += 1;
progress.log("could not add source '{s}': {s}\n", .{ path, @errorName(err) });
std.debug.print("could not add source '{s}': {s}\n", .{ path, @errorName(err) });
continue;
};

Expand Down Expand Up @@ -235,25 +233,25 @@ pub fn main() !void {
_ = try pp.preprocess(user_macros);
const eof = pp.preprocess(file) catch |err| {
fail_count += 1;
progress.log("could not preprocess file '{s}': {s}\n", .{ path, @errorName(err) });
std.debug.print("could not preprocess file '{s}': {s}\n", .{ path, @errorName(err) });
continue;
};
try pp.addToken(eof);

if (pp.defines.get("TESTS_SKIPPED")) |macro| {
if (macro.is_func or macro.tokens.len != 1 or macro.tokens[0].id != .pp_num) {
fail_count += 1;
progress.log("invalid TESTS_SKIPPED, definition should contain exactly one integer literal {}\n", .{macro});
std.debug.print("invalid TESTS_SKIPPED, definition should contain exactly one integer literal {}\n", .{macro});
continue;
}
const tok_slice = pp.tokSlice(macro.tokens[0]);
const tests_skipped = try std.fmt.parseInt(u32, tok_slice, 0);
progress.log("{d} test{s} skipped\n", .{ tests_skipped, if (tests_skipped == 1) @as([]const u8, "") else "s" });
std.debug.print("{s}: {d} test{s} skipped\n", .{ case, tests_skipped, if (tests_skipped == 1) @as([]const u8, "") else "s" });
skip_count += tests_skipped;
}

if (only_preprocess) {
if (try checkExpectedErrors(&pp, &progress, &buf)) |some| {
if (try checkExpectedErrors(&pp, &buf)) |some| {
if (!some) {
fail_count += 1;
continue;
Expand All @@ -272,7 +270,7 @@ pub fn main() !void {

break :blk std.fs.cwd().readFileAlloc(gpa, expanded_path, std.math.maxInt(u32)) catch |err| {
fail_count += 1;
progress.log("could not open expanded file '{s}': {s}\n", .{ path, @errorName(err) });
std.debug.print("could not open expanded file '{s}': {s}\n", .{ path, @errorName(err) });
continue;
};
};
Expand All @@ -294,7 +292,7 @@ pub fn main() !void {

var tree = aro.Parser.parse(&pp) catch |err| switch (err) {
error.FatalError => {
if (try checkExpectedErrors(&pp, &progress, &buf)) |some| {
if (try checkExpectedErrors(&pp, &buf)) |some| {
if (some) ok_count += 1 else fail_count += 1;
}
continue;
Expand Down Expand Up @@ -323,7 +321,7 @@ pub fn main() !void {
if (tree.nodes.items(.tag)[@intFromEnum(decl)] == .fn_def) break tree.nodes.items(.data)[@intFromEnum(decl)];
} else {
fail_count += 1;
progress.log("EXPECTED_TYPES requires a function to be defined\n", .{});
std.debug.print("EXPECTED_TYPES requires a function to be defined\n", .{});
break;
};

Expand All @@ -339,7 +337,7 @@ pub fn main() !void {
if (str.id == .macro_ws) continue;
if (str.id != .string_literal) {
fail_count += 1;
progress.log("EXPECTED_TYPES tokens must be string literals (found {s})\n", .{@tagName(str.id)});
std.debug.print("EXPECTED_TYPES tokens must be string literals (found {s})\n", .{@tagName(str.id)});
continue :next_test;
}
defer i += 1;
Expand All @@ -349,7 +347,7 @@ pub fn main() !void {
const actual_type = actual.types.items[i];
if (!std.mem.eql(u8, expected_type, actual_type)) {
fail_count += 1;
progress.log("expected type '{s}' did not match actual type '{s}'\n", .{
std.debug.print("expected type '{s}' did not match actual type '{s}'\n", .{
expected_type,
actual_type,
});
Expand All @@ -358,15 +356,15 @@ pub fn main() !void {
}
if (i != actual.types.items.len) {
fail_count += 1;
progress.log(
std.debug.print(
"EXPECTED_TYPES count differs: expected {d} found {d}\n",
.{ i, actual.types.items.len },
);
continue;
}
}

if (try checkExpectedErrors(&pp, &progress, &buf)) |some| {
if (try checkExpectedErrors(&pp, &buf)) |some| {
if (some) ok_count += 1 else fail_count += 1;
continue;
}
Expand All @@ -384,13 +382,13 @@ pub fn main() !void {

if (macro.is_func) {
fail_count += 1;
progress.log("invalid EXPECTED_OUTPUT {}\n", .{macro});
std.debug.print("invalid EXPECTED_OUTPUT {}\n", .{macro});
continue;
}

if (macro.tokens.len != 1 or macro.tokens[0].id != .string_literal) {
fail_count += 1;
progress.log("EXPECTED_OUTPUT takes exactly one string", .{});
std.debug.print("EXPECTED_OUTPUT takes exactly one string", .{});
continue;
}

Expand Down Expand Up @@ -432,7 +430,7 @@ pub fn main() !void {

if (!std.mem.eql(u8, expected_output, stdout)) {
fail_count += 1;
progress.log(
std.debug.print(
\\
\\======= expected output =======
\\{s}
Expand Down Expand Up @@ -464,7 +462,7 @@ pub fn main() !void {
}

// returns true if passed
fn checkExpectedErrors(pp: *aro.Preprocessor, progress: *std.Progress, buf: *std.ArrayList(u8)) !?bool {
fn checkExpectedErrors(pp: *aro.Preprocessor, buf: *std.ArrayList(u8)) !?bool {
const macro = pp.defines.get("EXPECTED_ERRORS") orelse return null;

const expected_count = pp.comp.diagnostics.list.items.len;
Expand All @@ -473,15 +471,15 @@ fn checkExpectedErrors(pp: *aro.Preprocessor, progress: *std.Progress, buf: *std
aro.Diagnostics.renderMessages(pp.comp, &m);

if (macro.is_func) {
progress.log("invalid EXPECTED_ERRORS {}\n", .{macro});
std.debug.print("invalid EXPECTED_ERRORS {}\n", .{macro});
return false;
}

var count: usize = 0;
for (macro.tokens) |str| {
if (str.id == .macro_ws) continue;
if (str.id != .string_literal) {
progress.log("EXPECTED_ERRORS tokens must be string literals (found {s})\n", .{@tagName(str.id)});
std.debug.print("EXPECTED_ERRORS tokens must be string literals (found {s})\n", .{@tagName(str.id)});
return false;
}
defer count += 1;
Expand All @@ -495,7 +493,7 @@ fn checkExpectedErrors(pp: *aro.Preprocessor, progress: *std.Progress, buf: *std

const index = std.mem.indexOf(u8, m.buf.items, expected_error);
if (index == null) {
progress.log(
std.debug.print(
\\
\\======= expected to find error =======
\\{s}
Expand All @@ -510,7 +508,7 @@ fn checkExpectedErrors(pp: *aro.Preprocessor, progress: *std.Progress, buf: *std
}

if (count != expected_count) {
progress.log(
std.debug.print(
\\EXPECTED_ERRORS missing errors, expected {d} found {d},
\\=== actual output ===
\\{s}
Expand Down

0 comments on commit d234cc6

Please sign in to comment.