-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
91 lines (85 loc) · 1.95 KB
/
helpers_test.go
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
package lazycommit
import (
"os"
"os/exec"
"path"
"testing"
)
// Helper function to create a new repository in a temporary directory.
// Returns the path to the repository.
func tempRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
cmd := exec.Command("git", "init")
cmd.Dir = dir
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
cmd = exec.Command("git", "config", "--local", "user.name", "Test User")
cmd.Dir = dir
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
cmd = exec.Command("git", "config", "--local", "user.email", "[email protected]")
cmd.Dir = dir
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
cmd = exec.Command("git", "config", "--local", "commit.gpgsign", "false")
cmd.Dir = dir
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
return dir
}
// Helper function that writes a file, but does not stage or commit it.
func writeFile(t *testing.T, dir, filename, contents string) *os.File {
t.Helper()
f, err := os.Create(path.Join(dir, filename))
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(contents)
if err != nil {
t.Fatal(err)
}
return f
}
// Helper function that writes a file and stages it (but doesn't commit it).
func addFile(t *testing.T, dir, filename, contents string) *os.File {
t.Helper()
f := writeFile(t, dir, filename, contents)
cmd := exec.Command("git", "add", filename)
cmd.Dir = dir
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
return f
}
// Helper function that commits a file to the repository.
func commitFile(t *testing.T, dir, filename, contents string) *os.File {
t.Helper()
f := addFile(t, dir, filename, contents)
cmd := exec.Command("git", "commit", "-m", "test")
cmd.Dir = dir
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
return f
}
// Helper function that moves a file.
func moveFile(t *testing.T, dir, oldName, newName string) {
t.Helper()
cmd := exec.Command("git", "mv", oldName, newName)
cmd.Dir = dir
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
}