diff --git a/licenses.txt b/licenses.txt index 7a8b0cc141d38fe7c419649ef69666de9d08ebac..45c688c3d1ca2fe4f3bee649417ec3235ac4295c 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,6 +1,7 @@ MIT: = https://spdx.org/licenses/MIT - This +- git https://github.com/nektro/zig-expect - git https://github.com/nektro/zig-extras - git https://github.com/nektro/zig-time - git https://github.com/nektro/zig-tracer diff --git a/test.zig b/test.zig index e400ec1180afb904df01b152628790f9217554b6..4cf614ede294ddcc5a3f3513ea879ba7af02975c 100644 --- a/test.zig +++ b/test.zig @@ -1,18 +1,185 @@ const std = @import("std"); const git = @import("git"); +const extras = @import("extras"); +const expect = @import("expect").expect; test { - _ = &git.getBranches; + _ = &git.version; _ = &git.getHEAD; - _ = &git.getObject; - _ = &git.getObjectSize; _ = &git.getTags; - _ = &git.getTreeDiff; - _ = &git.getType; - _ = &git.isType; - _ = &git.parseCommit; - _ = &git.parseTreeDiff; - _ = &git.parseTreeDiffMeta; _ = &git.revList; - _ = &git.version; +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + const branch_refs = try git.getBranches(alloc, git_dir); + const branch_names = try extras.mapBy(alloc, branch_refs, .label); + try expect(branch_names).toEqualStringSlice(&.{"master"}); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.getObject(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqualString( + \\tree 5403fecad0fde9120535321f222a061abc2849d9 + \\parent c39f57f6bb01664a7146ddbfc3debe76ec135f44 + \\author Meghan Denny 1692246864 -0700 + \\committer Meghan Denny 1692246864 -0700 + \\ + \\update to Zig 0.11 + \\ + ); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.getObjectSize(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(229); + try expect(try git.getObjectSize(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(45 + 47 + 55 + 58 + 0 + 18 + 6); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .blob)).toEqual(false); + try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .tree)).toEqual(false); + try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .commit)).toEqual(true); + try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .tag)).toEqual(false); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.getType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(.commit); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + const c = try git.parseCommit(alloc, try git.getObject(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")); + try expect(c.tree.id).toEqualString("5403fecad0fde9120535321f222a061abc2849d9"); + try expect(c.parents.len).toEqual(1); + try expect(c.parents[0].id).toEqualString("c39f57f6bb01664a7146ddbfc3debe76ec135f44"); + try expect(c.author.name).toEqualString("Meghan Denny"); + try expect(c.author.email).toEqualString("hello@nektro.net"); + // TODO: test .at when we upgrade to 0.14 and have decl literals + try expect(c.committer.name).toEqualString("Meghan Denny"); + try expect(c.committer.email).toEqualString("hello@nektro.net"); + // TODO: test .at when we upgrade to 0.14 and have decl literals + try expect(c.message).toEqualString( + \\update to Zig 0.11 + \\ + ); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.getObject(alloc, git_dir, "5403fecad0fde9120535321f222a061abc2849d9")).toEqualString( + \\100644 blob 8e8d2ceba4b327ce9db93f988492d0e21a461012 .gitattributes + \\100644 blob bb2a57bd81d13975f2a74ae5dd0e652de07bb8a7 .gitignore + \\100644 blob 37be0c1cfa4f097edbb1fb5c0585cd18cb08df13 LICENSE + \\100644 blob b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454 git.zig + \\100644 blob bb3f1c135632cfca760bd84fb18acdab8dae8ec3 zig.mod + \\ + ); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + const t = try git.parseTree(alloc, try git.getObject(alloc, git_dir, "5403fecad0fde9120535321f222a061abc2849d9")); + // TODO: test fields when we upgrade to 0.14 and have decl literals + // try expect(try extras.mapBy(alloc, t.children, .id)).toEqualSlice(&.{ + // .{ .blob = .{ .id = "8e8d2ceba4b327ce9db93f988492d0e21a461012" } }, + // .{ .blob = .{ .id = "bb2a57bd81d13975f2a74ae5dd0e652de07bb8a7" } }, + // .{ .blob = .{ .id = "37be0c1cfa4f097edbb1fb5c0585cd18cb08df13" } }, + // .{ .blob = .{ .id = "b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454" } }, + // .{ .blob = .{ .id = "bb3f1c135632cfca760bd84fb18acdab8dae8ec3" } }, + // }); + try expect(try extras.mapBy(alloc, t.children, .name)).toEqualStringSlice(&.{ + ".gitattributes", + ".gitignore", + "LICENSE", + "git.zig", + "zig.mod", + }); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + try expect(try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })).toEqualString( + \\:100644 100644 73c7032166db0eb23c4be11a4ff8ff26ec47c582 b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454 M git.zig + \\ + \\diff --git a/git.zig b/git.zig + \\index 73c7032..b229ead 100644 + \\--- a/git.zig + \\+++ b/git.zig + \\@@ -251,10 +251,10 @@ pub fn parseTree(alloc: std.mem.Allocator, treefile: string) !Tree { + \\ fn parseTreeMode(input: string) !Tree.Object.Mode { + \\ std.debug.assert(input.len == 6); + \\ return .{ + \\- .type = @intToEnum(Tree.Object.Type, try std.fmt.parseInt(u16, input[0..3], 10)), + \\- .perm_user = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[3..][0..1], 8)), + \\- .perm_group = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[4..][0..1], 8)), + \\- .perm_other = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[5..][0..1], 8)), + \\+ .type = @enumFromInt(try std.fmt.parseInt(u16, input[0..3], 10)), + \\+ .perm_user = @bitCast(try std.fmt.parseInt(u3, input[3..][0..1], 8)), + \\+ .perm_group = @bitCast(try std.fmt.parseInt(u3, input[4..][0..1], 8)), + \\+ .perm_other = @bitCast(try std.fmt.parseInt(u3, input[5..][0..1], 8)), + \\ }; + \\ } + \\ + ++ " "); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + const t = try git.parseTreeDiffMeta(try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })); + try expect(t.files_changed).toEqual(1); + try expect(t.lines_added).toEqual(4); + try expect(t.lines_removed).toEqual(4); +} + +test { + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + var git_dir = try std.fs.cwd().openDir(".git", .{}); + defer git_dir.close(); + const t = try git.parseTreeDiff(alloc, try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })); + _ = t; // TODO: test fields when we upgrade to 0.14 and have decl literals } diff --git a/zig.mod b/zig.mod index 65187cb5ae8340b460479bd3b8d7b2ce11c40bbe..ed46b4389cf7e44ad207d600587e2bd154469f1d 100644 --- a/zig.mod +++ b/zig.mod @@ -7,3 +7,6 @@ dependencies: - src: git https://github.com/nektro/zig-time - src: git https://github.com/nektro/zig-extras - src: git https://github.com/nektro/zig-tracer +root_dependencies: + - src: git https://github.com/nektro/zig-extras + - src: git https://github.com/nektro/zig-expect