-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgit_test.go
116 lines (95 loc) · 3.25 KB
/
git_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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"testing"
"github.com/docker/docker/pkg/archive"
)
const REPO_PATH = "/tmp/git_repo"
var (
branches = []branch{
newBranch(0, "4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8125"),
newBranch(1, "4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8126"),
newBranch(2, "4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8127"),
newBranch(3, "4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8128"),
}
)
func asserErrNil(err error, t *testing.T) {
if err != nil {
t.Fatal(err)
}
}
func TestGitFlow(t *testing.T) {
fmt.Printf("Testing git ... ")
r, err := newGitRepo(REPO_PATH)
asserErrNil(err, t)
defer os.RemoveAll(REPO_PATH)
//Create 3 branches
for i := 0; i < 3; i++ {
br := branches[i]
_, err = r.checkoutB(br)
asserErrNil(err, t)
curBr, err := r.currentBranch()
asserErrNil(err, t)
if br != curBr {
t.Fatalf("current branch: %v expected %v", curBr, br)
}
f, err := os.Create(path.Join(r.Path, "br"+strconv.Itoa(i)+".txt"))
asserErrNil(err, t)
f.Close()
_, err = r.addAllAndCommit("commit message")
asserErrNil(err, t)
}
exportChangeSet(r, branches[0], []string{"br0.txt"}, []string{"br1.txt", "br2.txt", ".git"}, t)
exportChangeSet(r, branches[1], []string{"br1.txt"}, []string{"br0.txt", "br2.txt"}, t)
exportChangeSet(r, branches[2], []string{"br2.txt"}, []string{"br0.txt", "br1.txt"}, t)
//Modify files
err = ioutil.WriteFile(path.Join(r.Path, "br0.txt"), []byte("hello world !!"), 0777)
asserErrNil(err, t)
_, err = r.addAllAndCommit("commit message")
asserErrNil(err, t)
exportChangeSet(r, branches[2], []string{"br2.txt", "br0.txt"}, []string{"br1.txt"}, t)
//Delete file
err = os.Remove(path.Join(r.Path, "br1.txt"))
asserErrNil(err, t)
_, err = r.addAllAndCommit("commit message")
exportChangeSet(r, branches[2], []string{"br2.txt", ".wh.br1.txt", "br0.txt"}, []string{"br1.txt"}, t)
//Uncommited changes
_, err = r.checkoutB(branches[3])
asserErrNil(err, t)
f, err := os.Create(path.Join(r.Path, "br3.txt"))
asserErrNil(err, t)
f.Close()
exportUncommitedChangeSet(r, []string{"br3.txt"}, []string{"br1.txt", ".wh.br1.txt", "br0.txt", "br2.txt"}, t)
fmt.Printf("OK\n")
}
func exportUncommitedChangeSet(r *gitRepo, expectedFiles, unexpectedFiles []string, t *testing.T) {
tar, err := r.exportUncommitedChangeSet()
asserErrNil(err, t)
defer tar.Close()
checkTarCorrect(tar, expectedFiles, unexpectedFiles, t)
}
func exportChangeSet(r *gitRepo, br branch, expectedFiles, unexpectedFiles []string, t *testing.T) {
tar, err := r.exportChangeSet(br)
asserErrNil(err, t)
defer tar.Close()
checkTarCorrect(tar, expectedFiles, unexpectedFiles, t)
}
func checkTarCorrect(tar archive.Archive, expectedFiles, unexpectedFiles []string, t *testing.T) {
err := archive.Untar(tar, "/tmp/tar", nil)
asserErrNil(err, t)
defer os.RemoveAll("/tmp/tar")
filesShouldExist(true, expectedFiles, "/tmp/tar", t)
filesShouldExist(false, unexpectedFiles, "/tmp/tar", t)
}
func filesShouldExist(shouldExist bool, files []string, basePath string, t *testing.T) {
for _, f := range files {
exist := fileExists(path.Join(basePath, f))
if exist != shouldExist {
t.Fatalf("file %v should exist ? %v", f, shouldExist)
}
}
}