49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package git
|
|
|
|
import (
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatCommitLine_nil(t *testing.T) {
|
|
if got := FormatCommitLine(nil); got != "" {
|
|
t.Errorf("FormatCommitLine(nil) = %q; want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestFormatCommitLine_valid(t *testing.T) {
|
|
commit := &CommitInfo{
|
|
ShortSHA: "abc1234",
|
|
Message: "Initial commit",
|
|
}
|
|
expected := "> Commit: abc1234 (Initial commit)\n"
|
|
if got := FormatCommitLine(commit); got != expected {
|
|
t.Errorf("FormatCommitLine() = %q; want %q", got, expected)
|
|
}
|
|
}
|
|
|
|
func TestGetCommitInfo_no_git(t *testing.T) {
|
|
cmd := exec.Command("git", "-C", "/nonexistent", "log", "-1", "--format=%h %s")
|
|
output, err := cmd.Output()
|
|
if err == nil {
|
|
t.Fatalf("expected error from nonexistent path, got %s", string(output))
|
|
}
|
|
|
|
result := GetCommitInfo("/nonexistent")
|
|
if result != nil {
|
|
t.Errorf("GetCommitInfo(/nonexistent) = %v; want nil", result)
|
|
}
|
|
}
|
|
|
|
func TestGetCommitInfo_no_commits(t *testing.T) {
|
|
out, err := exec.Command("sh", "-c", "echo 'abc123'").Output()
|
|
if err != nil {
|
|
t.Skip("shell command failed")
|
|
}
|
|
parts := strings.SplitN(strings.TrimSpace(string(out)), " ", 2)
|
|
if len(parts) < 2 {
|
|
t.Skip("cannot verify single-part output")
|
|
}
|
|
}
|