-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helpers.go
More file actions
89 lines (73 loc) · 2.43 KB
/
test_helpers.go
File metadata and controls
89 lines (73 loc) · 2.43 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package git
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
// setupRepo creates a temporary git repository for testing
func setupRepo(t *testing.T) (string, *Git) {
tmpDir, err := os.MkdirTemp("", "git-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
// Initialize git repo
cmd := exec.Command("git", "init", tmpDir)
if err := cmd.Run(); err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("Failed to init git repo: %v", err)
}
// Configure git
exec.Command("git", "-C", tmpDir, "config", "user.name", "Test User").Run()
exec.Command("git", "-C", tmpDir, "config", "user.email", "[email protected]").Run()
git, err := OpenGit(tmpDir)
if err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("Failed to open git repo: %v", err)
}
return tmpDir, git
}
// createCommit creates a commit in the test repository
func createCommit(t *testing.T, git *Git, message string) Hash {
// Create a dummy file to commit
testFile := filepath.Join(git.Path, "test.txt")
err := os.WriteFile(testFile, []byte(message), 0644)
if err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
// Add and commit
exec.Command("git", "-C", git.Path, "add", "test.txt").Run()
cmd := exec.Command("git", "-C", git.Path, "commit", "-m", message)
cmd.Env = append(os.Environ(), "GIT_AUTHOR_DATE=2000-01-01T00:00:00Z", "GIT_COMMITTER_DATE=2000-01-01T00:00:00Z")
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to create commit: %v", err)
}
hashStr, err := git.runGitCommand("rev-parse", "HEAD")
if err != nil {
t.Fatalf("Failed to get commit hash: %v", err)
}
hash, err := NewHash(hashStr)
if err != nil {
t.Fatalf("Failed to parse commit hash: %v", err)
}
return hash
}
// createBranch creates a branch in the test repository
func createBranch(t *testing.T, git *Git, branchName string) {
cmd := exec.Command("git", "-C", git.Path, "checkout", "-b", branchName)
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to create branch: %v", err)
}
}
// createTestHistory creates a test history with commits and branches
func createTestHistory(t *testing.T, git *Git) Hash {
// Create initial commit on master
createCommit(t, git, "test commit on master")
// Create branch
createBranch(t, git, "my-branch")
// Create commits on branch
createCommit(t, git, "commit on new branch")
createCommit(t, git, "second commit on new branch\n\n Long message")
lastHash := createCommit(t, git, "third commit on new branch")
return lastHash
}