wip: failure tests

This commit is contained in:
Motiejus Jakštys 2022-06-22 21:16:08 +03:00
parent 7d73667250
commit 0865965762

View File

@ -117,9 +117,7 @@ fn execute(
fn fail(errc: *ErrCtx, stderr: anytype, err: anytype) u8 { fn fail(errc: *ErrCtx, stderr: anytype, err: anytype) u8 {
stderr.print("ERROR {s}", .{@errorName(err)}) catch {}; stderr.print("ERROR {s}", .{@errorName(err)}) catch {};
var it = errc.rev(); var it = errc.rev();
while (it.next()) |msg| while (it.next()) |msg| stderr.print(": {s}", .{msg}) catch {};
stderr.print(": {s}", .{msg}) catch {};
stderr.print("\n", .{}) catch {}; stderr.print("\n", .{}) catch {};
return 1; return 1;
} }
@ -131,12 +129,10 @@ test "invalid argument" {
const args = &[_][*:0]const u8{"--invalid-argument"}; const args = &[_][*:0]const u8{"--invalid-argument"};
var stderr = ArrayList(u8).init(allocator); var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit(); defer stderr.deinit();
var stderrw = stderr.writer();
var stdout = ArrayList(u8).init(allocator); var stdout = ArrayList(u8).init(allocator);
defer stdout.deinit(); defer stdout.deinit();
var stdoutw = stdout.writer();
const exit_code = execute(allocator, stdoutw, stderrw, args[0..]); const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code); try testing.expectEqual(@as(u8, 1), exit_code);
try testing.expect(mem.startsWith( try testing.expect(mem.startsWith(
u8, u8,
@ -145,6 +141,35 @@ test "invalid argument" {
)); ));
} }
test "trivial error: missing passwd file" {
const allocator = testing.allocator;
const args = &[_][*:0]const u8{};
var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit();
var stdout = ArrayList(u8).init(allocator);
defer stdout.deinit();
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code);
std.debug.print("stderr: '{s}'\n", .{stderr.items});
if (true) return error.SkipZigTest;
try testing.expect(mem.startsWith(
u8,
stderr.items,
"ERROR: FileNotFound: fubar",
));
}
test "fail" {
var errc = ErrCtx{};
var stderr = ArrayList(u8).init(testing.allocator);
defer stderr.deinit();
var stderrw = stderr.writer();
const exit_code = fail(errc.wrapf("got 1", .{}), stderrw, error.NotSure);
try testing.expectEqual(exit_code, 1);
std.debug.print("stderr: '{s}'\n", .{stderr.items});
}
test "smoke test" { test "smoke test" {
const allocator = testing.allocator; const allocator = testing.allocator;
var stderr = ArrayList(u8).init(allocator); var stderr = ArrayList(u8).init(allocator);