1
Fork 0

better char debugging experience

main
Motiejus Jakštys 2022-07-02 16:17:10 +03:00
parent 186796fffc
commit 83fa0c8733
4 changed files with 25 additions and 12 deletions

View File

@ -196,8 +196,8 @@ pub fn testCorpus(allocator: Allocator) !Corpus {
.gid = math.maxInt(u32),
.name = "Name" ** 8,
.gecos = "Gecos" ** 51,
.home = "Home" ** 16,
.shell = "She.LllL" ** 8,
.home = "/Hom" ** 16,
.shell = "/She.Lll" ** 8,
}, User{
.uid = 100000,
.gid = 1002,

View File

@ -184,7 +184,7 @@ test "User.fromLine" {
},
.{
.line = "žemas:x:0:0:root:/root:/bin/bash",
.wantErr = "invalid name 'žemas': invalid character at position 0",
.wantErr = "invalid name 'žemas': invalid character 0xC5 at position 0",
},
.{
.line = "root:x:-1:0:root:/root:/bin/bash",

View File

@ -220,7 +220,7 @@ test "smoke test" {
};
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args);
if (true) return error.SkipZigTest;
//if (true) return error.SkipZigTest;
try testing.expectEqualStrings("", stderr.items);
try testing.expectEqual(@as(u8, 0), exit_code);
}

View File

@ -1,5 +1,6 @@
const std = @import("std");
const ascii = std.ascii;
const BoundedArray = std.BoundedArray;
const ErrCtx = @import("ErrCtx.zig");
@ -13,6 +14,14 @@ pub fn utf8(s: []const u8) error{InvalidRecord}!void {
}
}
fn debugChar(c: u8) BoundedArray(u8, 4) {
var res = BoundedArray(u8, 4).init(0) catch unreachable;
if (ascii.isPrint(c)) {
res.writer().print("'{c}'", .{c}) catch unreachable;
} else res.writer().print("0x{X}", .{c}) catch unreachable;
return res;
}
// # adduser žmogus
// adduser: To avoid problems, the username should consist only of letters,
// digits, underscores, periods, at signs and dashes, and not start with a dash
@ -24,13 +33,17 @@ pub fn name(s: []const u8, err: *ErrCtx) error{InvalidRecord}!void {
const c0 = s[0];
if (!(ascii.isAlNum(c0) or c0 == '_' or c0 == '.' or c0 == '@'))
return err.returnf("invalid character at position 0", .{}, error.InvalidRecord);
return err.returnf(
"invalid character {s} at position 0",
.{debugChar(c0).constSlice()},
error.InvalidRecord,
);
for (s[1..]) |c, i| {
if (!(ascii.isAlNum(c) or c == '_' or c == '.' or c == '@' or c == '-'))
return err.returnf(
"invalid character at position {d}",
.{i + 2},
"invalid character {s} at position {d}",
.{ debugChar(c).constSlice(), i + 2 },
error.InvalidRecord,
);
}
@ -59,7 +72,7 @@ pub fn path(s: []const u8, err: *ErrCtx) error{InvalidRecord}!void {
for (s[1..]) |c, i| {
if (!(ascii.isAlNum(c) or c == '/' or c == '_' or c == '.' or c == '@' or c == '-'))
return err.returnf(
"invalid character at position {d}",
"invalid character 0xD at position {d}",
.{i + 2},
error.InvalidRecord,
);
@ -76,8 +89,8 @@ test "validate name" {
.{ .name = "all-good" },
.{ .name = "..." },
.{ .name = "", .wantErr = "cannot be empty" },
.{ .name = "-no-start-dash", .wantErr = "invalid character at position 0" },
.{ .name = "Herbasž", .wantErr = "invalid character at position 7" },
.{ .name = "-no-start-dash", .wantErr = "invalid character '-' at position 0" },
.{ .name = "Herbasž", .wantErr = "invalid character 0xC5 at position 7" },
};
for (examples) |tt| {
var err = ErrCtx{};
@ -130,8 +143,8 @@ test "validate path" {
.{ .path = "/" },
.{ .path = "foo", .wantErr = "must start with /" },
.{ .path = "", .wantErr = "cannot be empty" },
.{ .path = "/path:motiejus", .wantErr = "invalid character at position 6" },
.{ .path = "/Herbasž", .wantErr = "invalid character at position 8" },
.{ .path = "/path:motiejus", .wantErr = "invalid character 0xD at position 6" },
.{ .path = "/Herbasž", .wantErr = "invalid character 0xD at position 8" },
};
for (examples) |tt| {
var err = ErrCtx{};