get tests compiled/passing

This commit is contained in:
Emil Lerch 2025-04-01 09:16:35 -07:00
parent db23724e32
commit 9592d4da42
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 57 additions and 55 deletions

View file

@ -91,18 +91,32 @@ fn loadConfig(allocator: std.mem.Allocator, path: []const u8) !std.json.Parsed(C
const content = try file.readToEndAlloc(allocator, max_size); const content = try file.readToEndAlloc(allocator, max_size);
defer allocator.free(content); defer allocator.free(content);
const ext = std.fs.path.extension(path); return try parseConfig(allocator, content, try detectFileType(path));
if (std.mem.eql(u8, ext, ".json")) {
return try std.json.parseFromSlice(Config, allocator, content, .{});
} else if (std.mem.eql(u8, ext, ".zon")) {
// TODO: Implement ZON parsing
return error.UnsupportedConfigFormat;
} else if (std.mem.eql(u8, ext, ".yaml") or std.mem.eql(u8, ext, ".yml")) {
// TODO: Implement YAML parsing
return error.UnsupportedConfigFormat;
} else {
return error.UnsupportedConfigFormat;
} }
const FileType = enum {
json,
zon,
yaml,
};
fn detectFileType(path: []const u8) !FileType {
const ext = std.fs.path.extension(path);
if (std.mem.eql(u8, ext, ".json"))
return .json;
if (std.mem.eql(u8, ext, ".zon"))
return .zon;
if (std.mem.eql(u8, ext, ".yaml") or std.mem.eql(u8, ext, ".yml"))
return .yaml;
return error.UnknownFileType;
}
fn parseConfig(allocator: std.mem.Allocator, content: []const u8, file_type: FileType) !std.json.Parsed(Config) {
return switch (file_type) {
.json => try std.json.parseFromSlice(Config, allocator, content, .{}),
.zon => error.UnsupportedConfigFormat, // TODO: Implement ZON parsing
.yaml => error.UnsupportedConfigFormat, // TODO: Implement YAML parsing
};
} }
fn printUsage() void { fn printUsage() void {
@ -148,11 +162,10 @@ test "config loading" {
\\} \\}
; ;
try std.fs.cwd().writeFile("test_config.json", config_json); // Test code:
defer std.fs.cwd().deleteFile("test_config.json") catch {}; const parsed_config = try parseConfig(allocator, config_json, .json);
defer parsed_config.deinit();
const config = try loadConfig(allocator, "test_config.json"); const config = parsed_config.value;
defer config.deinit(allocator);
try testing.expectEqualStrings("http://test:8384", config.syncthing_url); try testing.expectEqualStrings("http://test:8384", config.syncthing_url);
try testing.expectEqual(@as(u32, 3), config.max_retries); try testing.expectEqual(@as(u32, 3), config.max_retries);

View file

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const testing = std.testing;
const mvzr = @import("mvzr"); const mvzr = @import("mvzr");
pub const Config = struct { pub const Config = struct {
@ -29,14 +28,11 @@ pub const Watcher = struct {
.command = try allocator.dupe(u8, command), .command = try allocator.dupe(u8, command),
.compiled_pattern = null, .compiled_pattern = null,
}; };
watcher.compiled_pattern = try mvzr.Pattern.compile(allocator, path_pattern); watcher.compiled_pattern = mvzr.compile(path_pattern);
return watcher; return watcher;
} }
pub fn deinit(self: *Watcher, allocator: std.mem.Allocator) void { pub fn deinit(self: *Watcher, allocator: std.mem.Allocator) void {
if (self.compiled_pattern) |*pattern| {
pattern.deinit();
}
allocator.free(self.folder); allocator.free(self.folder);
allocator.free(self.path_pattern); allocator.free(self.path_pattern);
allocator.free(self.command); allocator.free(self.command);
@ -208,22 +204,18 @@ test "config parsing" {
\\} \\}
; ;
var parser = std.json.Parser.init(testing.allocator, false); const parsed = try std.json.parseFromSlice(Config, std.testing.allocator, config_json, .{});
defer parser.deinit(); defer parsed.deinit();
var tree = try parser.parse(config_json); const config = parsed.value;
defer tree.deinit();
const config = try std.json.parse(Config, &tree, .{ .allocator = testing.allocator }); try std.testing.expectEqualStrings("http://test:8384", config.syncthing_url);
defer config.deinit(testing.allocator); try std.testing.expectEqual(@as(u32, 3), config.max_retries);
try std.testing.expectEqual(@as(u32, 2000), config.retry_delay_ms);
try testing.expectEqualStrings("http://test:8384", config.syncthing_url); try std.testing.expectEqual(@as(usize, 1), config.watchers.len);
try testing.expectEqual(@as(u32, 3), config.max_retries); try std.testing.expectEqualStrings("test", config.watchers[0].folder);
try testing.expectEqual(@as(u32, 2000), config.retry_delay_ms); try std.testing.expectEqualStrings(".*\\.txt$", config.watchers[0].path_pattern);
try testing.expectEqual(@as(usize, 1), config.watchers.len); try std.testing.expectEqualStrings("echo ${path}", config.watchers[0].command);
try testing.expectEqualStrings("test", config.watchers[0].folder);
try testing.expectEqualStrings(".*\\.txt$", config.watchers[0].path_pattern);
try testing.expectEqualStrings("echo ${path}", config.watchers[0].command);
} }
test "event parsing" { test "event parsing" {
@ -236,19 +228,16 @@ test "event parsing" {
\\} \\}
; ;
var parser = std.json.Parser.init(testing.allocator, false); var parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, event_json, .{});
defer parser.deinit(); defer parsed.deinit();
var tree = try parser.parse(event_json); var event = try SyncthingEvent.fromJson(std.testing.allocator, parsed.value);
defer tree.deinit(); defer event.deinit(std.testing.allocator);
var event = try SyncthingEvent.fromJson(testing.allocator, tree.root); try std.testing.expectEqual(@as(i64, 123), event.id);
defer event.deinit(testing.allocator); try std.testing.expectEqualStrings("ItemFinished", event.type);
try std.testing.expectEqualStrings("default", event.folder);
try testing.expectEqual(@as(i64, 123), event.id); try std.testing.expectEqualStrings("test.txt", event.path);
try testing.expectEqualStrings("ItemFinished", event.type);
try testing.expectEqualStrings("default", event.folder);
try testing.expectEqualStrings("test.txt", event.path);
} }
test "command variable expansion" { test "command variable expansion" {
@ -260,10 +249,10 @@ test "command variable expansion" {
}; };
const command = "convert ${path} -resize 800x600 thumb_${folder}_${type}.jpg"; const command = "convert ${path} -resize 800x600 thumb_${folder}_${type}.jpg";
const expanded = try expandCommandVariables(testing.allocator, command, event); const expanded = try expandCommandVariables(std.testing.allocator, command, event);
defer testing.allocator.free(expanded); defer std.testing.allocator.free(expanded);
try testing.expectEqualStrings( try std.testing.expectEqualStrings(
"convert vacation.jpg -resize 800x600 thumb_photos_ItemFinished.jpg", "convert vacation.jpg -resize 800x600 thumb_photos_ItemFinished.jpg",
expanded, expanded,
); );
@ -271,15 +260,15 @@ test "command variable expansion" {
test "watcher pattern matching" { test "watcher pattern matching" {
var watcher = try Watcher.init( var watcher = try Watcher.init(
testing.allocator, std.testing.allocator,
"photos", "photos",
".*\\.jpe?g$", ".*\\.jpe?g$",
"echo ${path}", "echo ${path}",
); );
defer watcher.deinit(testing.allocator); defer watcher.deinit(std.testing.allocator);
try testing.expect(watcher.matches("photos", "test.jpg")); try std.testing.expect(watcher.matches("photos", "test.jpg"));
try testing.expect(watcher.matches("photos", "test.jpeg")); try std.testing.expect(watcher.matches("photos", "test.jpeg"));
try testing.expect(!watcher.matches("photos", "test.png")); try std.testing.expect(!watcher.matches("photos", "test.png"));
try testing.expect(!watcher.matches("documents", "test.jpg")); try std.testing.expect(!watcher.matches("documents", "test.jpg"));
} }