test(cli): fix and update hash tests for current architecture

Fix broken hash tests to work with current CLI architecture:
- Update import to use @import(src) module system
- Add hash module export to utils.zig
- Make validatePath() public for testing
- Fix Zig 0.15 API: writeFile options struct, var tmp_dir for cleanup
- Fix file paths: use tmp_dir realpath for hashFile
- Replace std.fs.MAX_PATH_BYTES with 4096 buffer

All hash tests now passing.
This commit is contained in:
Jeremie Fraeys 2026-03-04 20:35:19 -05:00
parent 69951ce5a1
commit bb584b3410
No known key found for this signature in database
3 changed files with 29 additions and 10 deletions

View file

@ -3,6 +3,7 @@ pub const auth = @import("utils/auth.zig");
pub const colors = @import("utils/colors.zig");
pub const crypto = @import("utils/crypto.zig");
pub const flags = @import("utils/flags.zig");
pub const hash = @import("utils/hash.zig");
pub const history = @import("utils/history.zig");
pub const io = @import("utils/io.zig");
pub const json = @import("utils/json.zig");

View file

@ -66,8 +66,7 @@ pub const DatasetHash = struct {
: [eax] "+r" (eax),
[ebx] "=r" (ebx),
: [ecx] "r" (ecx),
: .{ .edx = true, .memory = true }
);
: .{ .edx = true, .memory = true });
// Bit 29 of EBX indicates SHA-NI support
return (ebx & (1 << 29)) != 0;
@ -279,7 +278,7 @@ pub const DatasetHash = struct {
};
/// Security: validate path for traversal attacks
fn validatePath(path: []const u8) !void {
pub fn validatePath(path: []const u8) !void {
// Check for path traversal attempts
if (std.mem.indexOf(u8, path, "..") != null) {
// Only allow ".." at start or after "/"

View file

@ -1,21 +1,31 @@
const std = @import("std");
const hash = @import("../src/utils/hash.zig");
const src = @import("src");
const hash = src.utils.hash;
test "hash single file" {
const allocator = std.testing.allocator;
// Create a temporary file
const tmp_dir = std.testing.tmpDir(.{});
var tmp_dir = std.testing.tmpDir(.{});
defer tmp_dir.cleanup();
const test_content = "hello world";
try tmp_dir.dir.writeFile("test.txt", test_content);
try tmp_dir.dir.writeFile(.{
.sub_path = "test.txt",
.data = test_content,
});
// Get the temp directory path
var path_buf: [4096]u8 = undefined;
const tmp_path = try tmp_dir.dir.realpath(".", &path_buf);
const file_path = try std.fs.path.join(allocator, &[_][]const u8{ tmp_path, "test.txt" });
defer allocator.free(file_path);
// Hash the file
var hasher = try hash.DatasetHash.init(allocator, 0);
defer hasher.deinit();
const file_hash = try hasher.hashFile("test.txt");
const file_hash = try hasher.hashFile(file_path);
try std.testing.expectEqual(@as(usize, 64), file_hash.len);
}
@ -23,15 +33,24 @@ test "hash empty file" {
const allocator = std.testing.allocator;
// Create a temporary empty file
const tmp_dir = std.testing.tmpDir(.{});
var tmp_dir = std.testing.tmpDir(.{});
defer tmp_dir.cleanup();
try tmp_dir.dir.writeFile("empty.txt", "");
try tmp_dir.dir.writeFile(.{
.sub_path = "empty.txt",
.data = "",
});
// Get the temp directory path
var path_buf: [4096]u8 = undefined;
const tmp_path = try tmp_dir.dir.realpath(".", &path_buf);
const file_path = try std.fs.path.join(allocator, &[_][]const u8{ tmp_path, "empty.txt" });
defer allocator.free(file_path);
var hasher = try hash.DatasetHash.init(allocator, 0);
defer hasher.deinit();
const file_hash = try hasher.hashFile("empty.txt");
const file_hash = try hasher.hashFile(file_path);
try std.testing.expectEqual(@as(usize, 64), file_hash.len);
}