-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathclone_test.go
More file actions
78 lines (71 loc) · 1.84 KB
/
clone_test.go
File metadata and controls
78 lines (71 loc) · 1.84 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
package git
import (
"os"
"path/filepath"
"testing"
)
func TestCloneWithRef(t *testing.T) {
// Create a temporary directory for the test
tempDir, err := os.MkdirTemp("", "git-clone-test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() {
_ = os.RemoveAll(tempDir)
}()
// Define test cases
tests := []struct {
name string
repoURL string
ref string
wantErr bool
}{
{
name: "clone with main branch",
repoURL: "https://github.com/octocat/Hello-World.git",
ref: "master",
wantErr: false,
},
{
name: "clone with specific commit",
repoURL: "https://github.com/octocat/Hello-World.git",
ref: "7fd1a60", // First 7 chars of a commit hash
wantErr: false,
},
{
name: "clone with empty ref",
repoURL: "https://github.com/octocat/Hello-World.git",
ref: "",
wantErr: false,
},
{
name: "clone with non-existent ref",
repoURL: "https://github.com/octocat/Hello-World.git",
ref: "non-existent-branch",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a unique directory for each test
cloneDir := filepath.Join(tempDir, tt.name)
err := CloneWithRef(tt.repoURL, cloneDir, tt.ref)
if (err != nil) != tt.wantErr {
t.Errorf("CloneWithRef() error = %v, wantErr %v", err, tt.wantErr)
return
}
// If we expected success and got it, verify the clone
if !tt.wantErr && err == nil {
// Check if the directory exists
if _, err := os.Stat(cloneDir); os.IsNotExist(err) {
t.Errorf("Clone directory does not exist: %s", cloneDir)
}
// Check if .git directory exists
gitDir := filepath.Join(cloneDir, ".git")
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
t.Errorf(".git directory does not exist in clone: %s", gitDir)
}
}
})
}
}