-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test.go
More file actions
193 lines (169 loc) · 4.94 KB
/
git_test.go
File metadata and controls
193 lines (169 loc) · 4.94 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"errors"
"os/exec"
"testing"
)
func TestGitRoot(t *testing.T) {
// Save original function and restore after test
origGitRoot := gitRootFn
defer func() {
gitRootFn = origGitRoot
}()
t.Run("delegates to gitRootFn", func(t *testing.T) {
called := false
gitRootFn = func() (string, error) {
called = true
return "/test/path", nil
}
result, err := gitRoot()
if !called {
t.Error("gitRoot() did not call gitRootFn")
}
if err != nil {
t.Errorf("gitRoot() unexpected error: %v", err)
}
if result != "/test/path" {
t.Errorf("gitRoot() = %q, want %q", result, "/test/path")
}
})
}
func TestGitCmd(t *testing.T) {
// Save original function and restore after test
origGitCmd := gitCmdFn
defer func() {
gitCmdFn = origGitCmd
}()
t.Run("delegates to gitCmdFn", func(t *testing.T) {
var capturedDir string
var capturedArgs []string
gitCmdFn = func(dir string, args ...string) error {
capturedDir = dir
capturedArgs = args
return nil
}
err := gitCmd("/test/dir", "status", "-s")
if err != nil {
t.Errorf("gitCmd() unexpected error: %v", err)
}
if capturedDir != "/test/dir" {
t.Errorf("gitCmd() dir = %q, want %q", capturedDir, "/test/dir")
}
if len(capturedArgs) != 2 || capturedArgs[0] != "status" || capturedArgs[1] != "-s" {
t.Errorf("gitCmd() args = %v, want [status -s]", capturedArgs)
}
})
}
func TestGitMainRoot(t *testing.T) {
// Save original function and restore after test
origGitMainRoot := gitMainRootFn
defer func() {
gitMainRootFn = origGitMainRoot
}()
t.Run("delegates to gitMainRootFn", func(t *testing.T) {
called := false
gitMainRootFn = func() (string, error) {
called = true
return "/test/main/path", nil
}
result, err := gitMainRoot()
if !called {
t.Error("gitMainRoot() did not call gitMainRootFn")
}
if err != nil {
t.Errorf("gitMainRoot() unexpected error: %v", err)
}
if result != "/test/main/path" {
t.Errorf("gitMainRoot() = %q, want %q", result, "/test/main/path")
}
})
}
func TestDefaultGitRoot(t *testing.T) {
t.Run("in git repo", func(t *testing.T) {
// Test that defaultGitRoot returns a valid path when run from a git repo
// (which the test itself runs from)
root, err := defaultGitRoot()
if err != nil {
t.Errorf("defaultGitRoot() unexpected error: %v", err)
}
if root == "" {
t.Error("defaultGitRoot() returned empty string")
}
})
t.Run("not in git repo", func(t *testing.T) {
// Set GIT_DIR to an invalid path to simulate not being in a git repo
t.Setenv("GIT_DIR", "/nonexistent/path")
_, err := defaultGitRoot()
if err == nil {
t.Error("defaultGitRoot() expected error when not in git repo")
}
if err.Error() != "not in a git repository" {
t.Errorf("defaultGitRoot() error = %q, want %q", err.Error(), "not in a git repository")
}
})
}
func TestDefaultGitMainRoot(t *testing.T) {
t.Run("in git repo", func(t *testing.T) {
// Test that defaultGitMainRoot returns a valid path when run from a git repo
// (which the test itself runs from)
root, err := defaultGitMainRoot()
if err != nil {
t.Errorf("defaultGitMainRoot() unexpected error: %v", err)
}
if root == "" {
t.Error("defaultGitMainRoot() returned empty string")
}
})
t.Run("not in git repo", func(t *testing.T) {
// Set GIT_DIR to an invalid path to simulate not being in a git repo
t.Setenv("GIT_DIR", "/nonexistent/path")
_, err := defaultGitMainRoot()
if err == nil {
t.Error("defaultGitMainRoot() expected error when not in git repo")
}
if err.Error() != "not in a git repository" {
t.Errorf("defaultGitMainRoot() error = %q, want %q", err.Error(), "not in a git repository")
}
})
t.Run("filepath.Abs error", func(t *testing.T) {
// Save and restore original function
origFilepathAbs := filepathAbsFn
defer func() {
filepathAbsFn = origFilepathAbs
}()
filepathAbsFn = func(path string) (string, error) {
return "", errors.New("mock abs error")
}
_, err := defaultGitMainRoot()
if err == nil {
t.Error("defaultGitMainRoot() expected error when filepath.Abs fails")
}
if err.Error() != "failed to resolve git directory path" {
t.Errorf("defaultGitMainRoot() error = %q, want %q", err.Error(), "failed to resolve git directory path")
}
})
}
func TestDefaultGitCmd(t *testing.T) {
t.Run("successful command", func(t *testing.T) {
tmpDir := t.TempDir()
// Initialize a git repo
initCmd := exec.Command("git", "init")
initCmd.Dir = tmpDir
if err := initCmd.Run(); err != nil {
t.Skipf("git init failed: %v", err)
}
// Run a simple git command
err := defaultGitCmd(tmpDir, "status")
if err != nil {
t.Errorf("defaultGitCmd() unexpected error: %v", err)
}
})
t.Run("failing command", func(t *testing.T) {
tmpDir := t.TempDir()
// Run a git command that should fail (not a git repo, invalid command)
err := defaultGitCmd(tmpDir, "invalid-command-xyz")
if err == nil {
t.Error("defaultGitCmd() expected error for invalid command")
}
})
}