-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test.go
More file actions
144 lines (127 loc) · 3.83 KB
/
Copy pathgit_test.go
File metadata and controls
144 lines (127 loc) · 3.83 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
package changes
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestGitClient_ChangedFiles(t *testing.T) {
dir := initTestRepo(t)
writeFile(t, dir, "hello.go", "package main\n")
gitCommit(t, dir, "initial")
writeFile(t, dir, "hello.go", "package main\n\nfunc Hello() {}\n")
git := NewExecGitClient()
files, err := git.ChangedFiles(context.Background(), dir, "HEAD")
if err != nil {
t.Fatal(err)
}
if len(files) != 1 {
t.Fatalf("expected 1 changed file, got %d", len(files))
}
if files[0] != "hello.go" {
t.Errorf("expected hello.go, got %s", files[0])
}
}
func TestGitClient_RejectsOptionLikeBaseRef(t *testing.T) {
dir := initTestRepo(t)
writeFile(t, dir, "hello.go", "package main\n")
gitCommit(t, dir, "initial")
git := NewExecGitClient()
for _, base := range []string{"--output=/tmp/pwned", "-U9999", "--no-index"} {
if _, err := git.ChangedFiles(context.Background(), dir, base); err == nil {
t.Errorf("ChangedFiles accepted option-like base %q", base)
}
if _, err := git.DiffHunks(context.Background(), dir, base, []string{"hello.go"}); err == nil {
t.Errorf("DiffHunks accepted option-like base %q", base)
}
}
}
func TestGitClient_ChangedFiles_NonASCIIPath(t *testing.T) {
dir := initTestRepo(t)
writeFile(t, dir, "한글.go", "package main\n")
gitCommit(t, dir, "initial")
writeFile(t, dir, "한글.go", "package main\n\nfunc Hello() {}\n")
git := NewExecGitClient()
files, err := git.ChangedFiles(context.Background(), dir, "HEAD")
if err != nil {
t.Fatal(err)
}
if len(files) != 1 || files[0] != "한글.go" {
t.Fatalf("expected unquoted 한글.go, got %v", files)
}
hunks, err := git.DiffHunks(context.Background(), dir, "HEAD", []string{"한글.go"})
if err != nil {
t.Fatal(err)
}
if len(hunks) == 0 || hunks[0].FilePath != "한글.go" {
t.Fatalf("expected hunk for unquoted 한글.go, got %+v", hunks)
}
}
func TestGitClient_DiffHunks(t *testing.T) {
dir := initTestRepo(t)
writeFile(t, dir, "hello.go", "package main\n\nfunc Old() {}\n")
gitCommit(t, dir, "initial")
writeFile(t, dir, "hello.go", "package main\n\nfunc New() {}\n")
git := NewExecGitClient()
hunks, err := git.DiffHunks(context.Background(), dir, "HEAD", []string{"hello.go"})
if err != nil {
t.Fatal(err)
}
if len(hunks) == 0 {
t.Fatal("expected at least 1 hunk")
}
if hunks[0].FilePath != "hello.go" {
t.Errorf("expected hello.go, got %s", hunks[0].FilePath)
}
}
func TestRunGitLimitedWithMaxRejectsLargeOutput(t *testing.T) {
dir := initTestRepo(t)
_, err := runGitLimitedWithMax(context.Background(), dir, []string{"--version"}, 1)
if err == nil {
t.Fatal("expected output cap error")
}
if !strings.Contains(err.Error(), "git output exceeds") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunGitLimitedHonorsContextTimeout(t *testing.T) {
dir := initTestRepo(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
time.Sleep(time.Millisecond)
_, err := runGitLimited(ctx, dir, []string{"status"})
if err == nil {
t.Fatal("expected context timeout error")
}
}
func initTestRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
run(t, dir, "git", "init")
run(t, dir, "git", "config", "user.email", "test@test.com")
run(t, dir, "git", "config", "user.name", "Test")
return dir
}
func writeFile(t *testing.T, dir, name, content string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0644); err != nil {
t.Fatal(err)
}
}
func gitCommit(t *testing.T, dir, msg string) {
t.Helper()
run(t, dir, "git", "add", ".")
run(t, dir, "git", "commit", "-m", msg)
}
func run(t *testing.T, dir string, name string, args ...string) {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s %v: %v\n%s", name, args, err, out)
}
}