ai_xiaopei
2026-07-25 b50bb792b77d7743f2c3861ac2c85b215b77fdc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package index
 
import (
    "os"
    "os/exec"
    "path/filepath"
    "testing"
)
 
func TestGetGitCommit(t *testing.T) {
    // 创建临时 git 仓库
    tmpDir := t.TempDir()
    exec.Command("git", "-C", tmpDir, "init").Run()
    exec.Command("git", "-C", tmpDir, "config", "user.email", "test@test.com").Run()
    exec.Command("git", "-C", tmpDir, "config", "user.name", "test").Run()
    os.WriteFile(filepath.Join(tmpDir, "test.md"), []byte("test"), 0644)
    exec.Command("git", "-C", tmpDir, "add", ".").Run()
    exec.Command("git", "-C", tmpDir, "commit", "-m", "init").Run()
 
    commit, err := GetGitCommit(tmpDir)
    if err != nil {
        t.Fatalf("GetGitCommit failed: %v", err)
    }
    if len(commit) != 40 {
        t.Errorf("commit length = %d, want 40", len(commit))
    }
}
 
func TestGetGitCommitNotGit(t *testing.T) {
    tmpDir := t.TempDir()
    _, err := GetGitCommit(tmpDir)
    if err == nil {
        t.Error("expected error for non-git directory")
    }
}