Skip to content

Commit

Permalink
add files for presentation at next stage2 meeting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Philipp Hafer committed Feb 3, 2023
1 parent cae796a commit dc46f32
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
55 changes: 55 additions & 0 deletions example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");

// PROTOTYPE intended for presentation

const panic_runner = @import("lib/panic_runner.zig");
test "testme1" {
std.debug.assert(true);
panic_runner.writeExpectedPanicMsg("test123");
@panic("test123");
}
test "testme2" {
std.debug.assert(true);
}
// test "testme4" {
// panic_runner.writeExpectedPanicMsg("bruh");
// @panic("wrongmsg");
// }

// test "testme4" {
// @panic("no_expected_panic_msg");
// }

// tradeoffs:
// + simple and works with current routines
// - only 1 panic inside a test block and must be last item
// - requires process spawning and IPC (separates embedded and hosted environments)
//
// my opinion:
// - feels like a hacky workaround (like Kernel spawn APIs I needed to modify)
// - more elegant solution would be copying complete program state with a
// bespoke debugging api + let user define store + load points for programs:
// ```
// test "testme" {
// var i: u32 = 0;
// @storeProgramState
// panic_runner.writeExpectedPanicMsg("test123");
// while (i < 100): (i+=1) {
// if (i == 77) {
// panic_runner.writeExpectedPanicMsg("test77");
// @panic("test123");
// }
// }
// @loadProgramState
// i = 100;
// @storeProgramState
// panic_runner.writeExpectedPanicMsg("test123");
// while (i < 200): (i+=1) {
// if (i == 177) {
// panic_runner.writeExpectedPanicMsg("test177");
// @panic("test123");
// }
// }
// @loadProgramState
// ```

18 changes: 9 additions & 9 deletions lib/panic_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi
msg_wr.writeIntNative(usize, global.failed) catch unreachable;

if (std.mem.eql(u8, message, global.buf_panic_msg[0..global.buf_panic_msg_fill])) {
std.debug.print("execpted panic\n", .{});
// std.debug.print("execpted panic\n", .{});
msg_wr.writeByte(@intCast(u8, @enumToInt(PanicT.expected_panic))) catch unreachable;
msg_pipe_file.close(); // workaround process.exit
std.process.exit(0);
} else {
std.debug.print("unexecpted panic\n", .{});
// std.debug.print("unexecpted panic\n", .{});
msg_wr.writeByte(@intCast(u8, @enumToInt(PanicT.unexpected_panic))) catch unreachable;
msg_pipe_file.close(); // workaround process.exit
std.debug.panic("{s}", .{message});
Expand Down Expand Up @@ -130,12 +130,12 @@ fn processArgs(static_alloc: std.mem.Allocator) Cli {

cli.state = .Worker;
cli.test_nr = std.fmt.parseUnsigned(u64, args[1], 10) catch unreachable;
std.debug.print("test worker (exe_path test_nr handle: ", .{});
std.debug.print("{s} {s} {s}\n", .{ args[0], args[1], args[2] });
// std.debug.print("test worker (exe_path test_nr handle: ", .{});
// std.debug.print("{s} {s} {s}\n", .{ args[0], args[1], args[2] });
} else {
cli.state = .Control;
cli.test_nr = 0;
std.debug.print("test control: {s}\n", .{args[0]});
// std.debug.print("test control: {s}\n", .{args[0]});
}
cli.test_runner_exe_path = args[0];
return cli;
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn main() !void {
try child_proc.spawn();
}
const ret_term = try child_proc.wait();
std.debug.print("ret_term: {any}\n", .{ret_term.Exited});
// std.debug.print("ret_term: {any}\n", .{ret_term.Exited});
if (ret_term.Exited != @enumToInt(ChildProcess.Term.Exited)) {
@panic("TODO: handle printing message for exit reason.");
}
Expand All @@ -187,11 +187,11 @@ pub fn main() !void {
};
const file_rd = file.reader();
const ret_passed = try file_rd.readIntNative(usize);
std.debug.print("ctrl passed: {d}\n", .{ret_passed});
// std.debug.print("ctrl passed: {d}\n", .{ret_passed});
const ret_skipped = try file_rd.readIntNative(usize);
std.debug.print("ctrl skipped: {d}\n", .{ret_skipped});
// std.debug.print("ctrl skipped: {d}\n", .{ret_skipped});
const ret_failed = try file_rd.readIntNative(usize);
std.debug.print("ctrl fail: {d}\n", .{ret_failed});
// std.debug.print("ctrl fail: {d}\n", .{ret_failed});
global.passed += ret_passed;
global.skipped += ret_skipped;
global.failed += ret_failed;
Expand Down
4 changes: 4 additions & 0 deletions t.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
#set -e
./rel/bin/zig test --test-runner lib/panic_runner.zig example.zig
echo "exit status: $?"

0 comments on commit dc46f32

Please sign in to comment.