Compare commits
2 Commits
7ae1907189
...
31c74fc960
| Author | SHA1 | Date | |
|---|---|---|---|
| 31c74fc960 | |||
| d56010b064 |
18
build.zig
18
build.zig
@@ -34,9 +34,18 @@ pub fn build(b: *std.Build) void
|
||||
.target = target,
|
||||
});
|
||||
|
||||
const test_mod = b.createModule(.{
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.link_libc = true,
|
||||
});
|
||||
|
||||
mod.addImport("toml", toml_dep.module("toml"));
|
||||
mod.addImport("args", args_dep.module("args"));
|
||||
|
||||
test_mod.addImport("toml", toml_dep.module("toml"));
|
||||
test_mod.addImport("args", args_dep.module("args"));
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "zocket",
|
||||
.root_module = b.createModule(.{
|
||||
@@ -66,20 +75,13 @@ pub fn build(b: *std.Build) void
|
||||
}
|
||||
|
||||
const mod_tests = b.addTest(.{
|
||||
.root_module = mod,
|
||||
.root_module = test_mod,
|
||||
});
|
||||
|
||||
const run_mod_tests = b.addRunArtifact(mod_tests);
|
||||
|
||||
const exe_tests = b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
|
||||
const run_exe_tests = b.addRunArtifact(exe_tests);
|
||||
|
||||
const test_step = b.step("test", "Run tests");
|
||||
test_step.dependOn(&run_mod_tests.step);
|
||||
test_step.dependOn(&run_exe_tests.step);
|
||||
}
|
||||
|
||||
fn getGitVersion(b: *std.Build) []const u8
|
||||
|
||||
@@ -147,3 +147,90 @@ pub const Logger = struct {
|
||||
try logger.writer.interface.flush();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
extern fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
|
||||
|
||||
fn mktmpdir(buffer: *[64:0]u8, pattern: []const u8) ![]u8
|
||||
{
|
||||
if (pattern.len >= buffer.len) return error.PatternTooLong;
|
||||
|
||||
@memcpy(buffer[0..pattern.len], pattern);
|
||||
buffer[pattern.len] = 0;
|
||||
|
||||
const result = mkdtemp(buffer) orelse return error.MkDtempFailed;
|
||||
|
||||
return std.mem.span(result);
|
||||
}
|
||||
|
||||
test "filtering, buffering and automatic flushing"
|
||||
{
|
||||
const io = std.testing.io;
|
||||
const allocator = std.testing.allocator;
|
||||
const fname = "logger.test";
|
||||
|
||||
var path_buf: [64:0]u8 = undefined;
|
||||
var read_buf: [512]u8 = undefined;
|
||||
|
||||
const path = try mktmpdir(&path_buf,"/tmp/zocket-test-XXXXXX");
|
||||
|
||||
const dir = try std.Io.Dir.openDirAbsolute(io, path, .{});
|
||||
defer
|
||||
{
|
||||
dir.deleteFile(io, fname) catch unreachable;
|
||||
dir.close(io);
|
||||
std.Io.Dir.deleteDirAbsolute(io, path) catch unreachable;
|
||||
}
|
||||
|
||||
const file = try dir.createFile(io, fname, .{ .lock = .exclusive });
|
||||
defer file.close(io);
|
||||
|
||||
var log = try Logger.init(allocator, .{
|
||||
.io = io, .buffer = 256, .file = file
|
||||
});
|
||||
defer log.deinit();
|
||||
|
||||
//
|
||||
|
||||
// The file is expected to be empty, as this does not force the internal buffer to auto-flush.
|
||||
log.info("info={d}", .{1});
|
||||
|
||||
{
|
||||
const contents = try dir.readFile(io, fname, &read_buf);
|
||||
try std.testing.expect(contents.len == 0);
|
||||
}
|
||||
|
||||
// The file should include 'info=1' after flushing to disk.
|
||||
try log.flush();
|
||||
|
||||
{
|
||||
const contents = try dir.readFile(io, fname, &read_buf);
|
||||
try std.testing.expectEqualStrings("info=1", contents);
|
||||
}
|
||||
try file.setLength(io, 0);
|
||||
try log.writer.seekTo(0);
|
||||
@memset(read_buf[0..], 0);
|
||||
|
||||
// The file should include 'warn=1' as warnings and errors automatically trigger a flush to disk.
|
||||
log.warn("warn={d}", .{1});
|
||||
|
||||
{
|
||||
const contents = try dir.readFile(io, fname, &read_buf);
|
||||
try std.testing.expectEqualStrings("warn=1", contents);
|
||||
}
|
||||
try file.setLength(io, 0);
|
||||
try log.writer.seekTo(0);
|
||||
@memset(read_buf[0..], 0);
|
||||
|
||||
// The file should include 'info=x...' as it's bigger than the allocated 256 buffer, forcing it to automatically drain on overflow.
|
||||
log.info("info={s}", .{[_]u8{'x'} ** 300});
|
||||
|
||||
{
|
||||
const contents = try dir.readFile(io, fname, &read_buf);
|
||||
try std.testing.expectEqualStrings("info=" ++ ([_]u8{'x'} ** 300), contents);
|
||||
}
|
||||
try file.setLength(io, 0);
|
||||
try log.writer.seekTo(0);
|
||||
@memset(read_buf[0..], 0);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,10 @@ pub const memory = @import("memory/util.zig");
|
||||
pub const signals = @import("signals/models.zig");
|
||||
pub const logging = @import("logging/log.zig");
|
||||
|
||||
//
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
test {
|
||||
_ = config;
|
||||
_ = memory;
|
||||
_ = signals;
|
||||
std.testing.refAllDecls(@This());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user