-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos_test.go
165 lines (130 loc) · 2.59 KB
/
repos_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
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
package zcmd
import (
"context"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/cybozu-go/well"
)
// nolint[gochecknoglobals]
var (
gitURLs = []string{
"https://github.com/mitsutaka/docker-libs",
"https://github.com/mitsutaka/kubernetes-coreos",
}
)
func gitClone(dir string) ([]string, error) {
repos := make([]string, len(gitURLs))
for i, gitURL := range gitURLs {
u, err := url.Parse(gitURL)
if err != nil {
return nil, err
}
gitPath := filepath.Join(dir, filepath.Base(u.Path))
args := []string{"clone", gitURL}
cmd := well.CommandContext(context.Background(), cmdGit, args...)
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return nil, err
}
repos[i] = gitPath
}
return repos, nil
}
func testFind(t *testing.T) {
t.Parallel()
// Prepare test data
dir, err := ioutil.TempDir("", "find")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
repos, err := gitClone(dir)
if err != nil {
t.Fatal(err)
}
upd, err := NewUpdater(dir)
if err != nil {
t.Error(err)
}
err = upd.FindRepositories()
if err != nil {
t.Error(err)
}
updPaths := make([]string, len(gitURLs))
copy(updPaths, upd.repositories)
clonedPaths := make([]string, len(gitURLs))
copy(clonedPaths, repos)
if !reflect.DeepEqual(updPaths, clonedPaths) {
t.Errorf("%#v != %#v", updPaths, clonedPaths)
}
}
func testClean(t *testing.T) {
t.Parallel()
// Prepare test data
dir, err := ioutil.TempDir("", "fetch")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
repos, err := gitClone(dir)
if err != nil {
t.Fatal(err)
}
for _, repo := range repos {
err = clean(context.Background(), repo)
if err != nil {
t.Error(err)
}
}
}
func testCheckout(t *testing.T) {
t.Parallel()
// Prepare test data
dir, err := ioutil.TempDir("", "checkout")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
repos, err := gitClone(dir)
if err != nil {
t.Fatal(err)
}
for _, repo := range repos {
err = checkoutMaster(context.Background(), repo)
if err != nil {
t.Error(err)
}
}
}
func testPull(t *testing.T) {
t.Parallel()
// Prepare test data
dir, err := ioutil.TempDir("", "pull")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
repos, err := gitClone(dir)
if err != nil {
t.Fatal(err)
}
for _, repo := range repos {
err = pull(context.Background(), repo)
if err != nil {
t.Error(err)
}
}
}
func TestReposUpdate(t *testing.T) {
t.Run("find", testFind)
t.Run("clean", testClean)
t.Run("checkout", testCheckout)
t.Run("pull", testPull)
}