-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit_test.go
75 lines (63 loc) · 1.79 KB
/
commit_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
package lazycommit
import (
"strings"
"testing"
)
// Tests that a commit message can't be built when there are no staged changes.
func TestBuildCommitMessage(t *testing.T) {
t.Log("Creating a new repo.")
dir := tempRepo(t)
repo := Repo(dir)
_, err := repo.CommitMsg()
if err == nil && err.Error() != "no tracked files" {
t.Errorf(`Expected "no tracked files", got %v`, err)
}
f := commitFile(t, dir, "test.txt", "test")
defer f.Close()
t.Log(`Modifying test.txt`)
commitFile(t, dir, "test.txt", "")
addFile(t, dir, "test.txt", "different text")
msg, err := repo.CommitMsg()
if err != nil {
t.Fatal(err)
}
if msg != "Update test.txt" {
t.Errorf(`Expected "Update test.txt", got %v`, msg)
}
t.Log(`Adding a new file`)
addFile(t, dir, "test2.txt", "test")
msg, err = repo.CommitMsg()
if err != nil {
t.Fatal(err)
}
lines := strings.Split(msg, "\n")
if lines[0] != "Update files" {
t.Errorf(`Expected "Update files" in the header, got %v`, lines[0])
}
if lines[1] != "" {
t.Errorf(`Expected an empty line after the header, got %v`, lines[1])
}
body := strings.Join(lines[2:], "\n")
t.Logf("Body:\n %v", body)
for _, want := range []string{"- Update test.txt", "- Create test2.txt"} {
if !strings.Contains(body, want) {
t.Errorf(`Expected %v in the body`, want)
}
}
}
// TestBuildCommitMessageWithRename tests that a commit message can be built when a file is renamed.
func TestBuildCommitMessageWithRename(t *testing.T) {
dir := tempRepo(t)
repo := Repo(dir)
f := commitFile(t, dir, "foo.txt", "test")
defer f.Close()
t.Log(`Renaming test.txt to test2.txt`)
moveFile(t, dir, "foo.txt", "bar.txt")
msg, err := repo.CommitMsg()
if err != nil {
t.Fatal(err)
}
if msg != "Rename foo.txt to bar.txt" {
t.Errorf(`Expected "Rename foo.txt to bar.txt", got %v`, msg)
}
}