Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,34 @@ pub fn updateCommitMessage(allocator: std.mem.Allocator, file_path: []const u8,

test "updateCommitMessage updates the commit message with the branch name" {
const allocator = testing.allocator;
const cwd = std.fs.cwd();
try std.fs.cwd().makeDir("test_update_commit_message");
var tmp_dir = try std.fs.cwd().openDir(
"test_update_commit_message",
.{ .iterate = true },

const test_dir_rel_path = "test_update_commit_message";
const commit_msg_file_name = "COMMIT_MSG";
const commit_msg_file_path = test_dir_rel_path ++ "/" ++ commit_msg_file_name;

const initial_msg = "Initial commit\n";
const feature_branch = "feature-branch";

try std.fs.cwd().makeDir(test_dir_rel_path);
var test_dir = try std.fs.cwd().openDir(
test_dir_rel_path,
.{},
);
defer {
tmp_dir.close();
cwd.deleteTree("test_update_commit_message") catch unreachable;
test_dir.close();
std.fs.cwd().deleteTree(test_dir_rel_path) catch unreachable;
}

const commit_msg_file = try tmp_dir.createFile("COMMIT_MSG", .{});
const commit_msg_file = try test_dir.createFile(commit_msg_file_name, .{});
defer commit_msg_file.close();

const len = try commit_msg_file.write("Initial commit\n");
try testing.expect(len > 0);
const len = try commit_msg_file.write(initial_msg);
try testing.expectEqual(len, initial_msg.len);

try tmp_dir.setAsCwd();
try updateCommitMessage(allocator, commit_msg_file_path, feature_branch);

try updateCommitMessage(allocator, "COMMIT_MSG", "feature-branch");

const updated_msg = try std.fs.cwd().readFileAlloc(allocator, "COMMIT_MSG", 1024);
const updated_msg = try std.fs.cwd().readFileAlloc(allocator, commit_msg_file_path, 1024 * 1024);
defer allocator.free(updated_msg);

try testing.expectEqualStrings("feature-branch: Initial commit\n", updated_msg);

try cwd.setAsCwd();
try cwd.deleteTree("test_update_commit_message");
try testing.expectEqualStrings(feature_branch ++ ": " ++ initial_msg, updated_msg);
}