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);
defer allocator.free(content);
return try parseConfig(allocator, content, try detectFileType(path));
}
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 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;
}
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 {
@ -148,11 +162,10 @@ test "config loading" {
\\}
;
try std.fs.cwd().writeFile("test_config.json", config_json);
defer std.fs.cwd().deleteFile("test_config.json") catch {};
const config = try loadConfig(allocator, "test_config.json");
defer config.deinit(allocator);
// Test code:
const parsed_config = try parseConfig(allocator, config_json, .json);
defer parsed_config.deinit();
const config = parsed_config.value;
try testing.expectEqualStrings("http://test:8384", config.syncthing_url);
try testing.expectEqual(@as(u32, 3), config.max_retries);

View file

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