This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
project_git_test.go
201 lines (154 loc) · 6.1 KB
/
project_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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package godm
import (
"io/ioutil"
"testing"
"os"
"path"
"github.com/hectorj/godm/git"
"github.com/stretchr/testify/assert"
)
func newLocalGitProject(t *testing.T, gitStub *git.GitStub) (project *localGitProject, gitRepoStub *git.GitRepoStub) {
tmpDirPath, err := ioutil.TempDir("", "godm-local_git_project_test")
if !assert.Nil(t, err, "Failed creating a temp dir for localGitProject tests.") {
t.FailNow()
}
if gitStub == nil {
gitStub = git.NewGitStub()
}
gitRepoStub = git.NewGitRepoStub()
gitStub.Repos[tmpDirPath] = gitRepoStub
git.Service = gitStub
project, err = NewGitProjectFromPath(tmpDirPath, tmpDirPath)
if !assert.Nil(t, err, "Failed creating a localGitProject for tests.") {
t.FailNow()
}
if !assert.Equal(t, tmpDirPath, project.GetBaseDir(), "Fresh localGitProject from temp dir has a wrong base dir.") {
t.FailNow()
}
return
}
var gitTestCases = map[string]struct {
prepare func(t *testing.T, project *localGitProject, gitRepoStub *git.GitRepoStub, failMessageAndArgs ...interface{})
expectedImportPaths map[string]LocalProject
}{
"none": {
prepare: func(_ *testing.T, _ *localGitProject, _ *git.GitRepoStub, _ ...interface{}) {},
expectedImportPaths: nil,
},
"1 NoVCL vendor": {
prepare: func(t *testing.T, project *localGitProject, _ *git.GitRepoStub, failMessageAndArgs ...interface{}) {
targetDirPath := path.Join(project.GetBaseDir(), "vendor", "test1") + "/"
err := os.MkdirAll(targetDirPath, os.ModeDir|os.ModePerm)
assert.Nil(t, err, failMessageAndArgs...)
targetFilePath := path.Join(targetDirPath, "whatever.go")
file, err := os.Create(targetFilePath)
assert.Nil(t, err, failMessageAndArgs...)
_, err = file.WriteString("package test1\n")
assert.Nil(t, err, failMessageAndArgs...)
},
expectedImportPaths: map[string]LocalProject{
"test1": &ProjectNoVCL{},
},
},
"1 Git vendor": {
prepare: func(t *testing.T, project *localGitProject, gitRepoStub *git.GitRepoStub, failMessageAndArgs ...interface{}) {
targetDirPath := path.Join(project.GetBaseDir(), "vendor", "test1") + "/"
err := os.MkdirAll(targetDirPath, os.ModeDir|os.ModePerm)
assert.Nil(t, err, failMessageAndArgs...)
targetFilePath := path.Join(targetDirPath, "whatever.go")
file, err := os.Create(targetFilePath)
assert.Nil(t, err, failMessageAndArgs...)
_, err = file.WriteString("package test1\n")
assert.Nil(t, err, failMessageAndArgs...)
gitStub := git.Service.(*git.GitStub)
gitStub.Repos[targetDirPath] = git.NewGitRepoStub()
gitRepoStub.Submodules[path.Join("vendor", "test1")] = gitStub.Repos[targetDirPath]
},
expectedImportPaths: map[string]LocalProject{
"test1": &localGitProject{},
},
},
}
func TestLocalGitProject_GetVendors(t *testing.T) {
testCasesLoop:
for caseName, testCase := range gitTestCases {
failMessageAndArgs := []interface{}{"Test case %q failed.", caseName}
project, gitRepoStub := newLocalGitProject(t, nil)
defer os.RemoveAll(project.GetBaseDir())
testCase.prepare(t, project, gitRepoStub, failMessageAndArgs...)
vendors, err := project.GetVendors()
if !assert.Nil(t, err, failMessageAndArgs...) {
continue testCasesLoop
}
if !assert.Len(t, vendors, len(testCase.expectedImportPaths), failMessageAndArgs...) {
continue testCasesLoop
}
vendorsLoop:
for importPath, projectType := range testCase.expectedImportPaths {
vendor, exists := vendors[importPath]
specificFailMessageAndArgs := make([]interface{}, len(failMessageAndArgs)+1)
copy(specificFailMessageAndArgs, failMessageAndArgs)
specificFailMessageAndArgs[0] = specificFailMessageAndArgs[0].(string) + " (vendor %q)"
specificFailMessageAndArgs[len(failMessageAndArgs)] = importPath
if !assert.True(t, exists, specificFailMessageAndArgs...) {
continue vendorsLoop
}
if !assert.NotNil(t, vendor, specificFailMessageAndArgs...) {
continue vendorsLoop
}
if !assert.IsType(t, projectType, vendor.GetProject(), specificFailMessageAndArgs...) {
continue vendorsLoop
}
}
}
}
func TestLocalGitProject_RemoveVendor(t *testing.T) {
//testCasesLoop:
for caseName, testCase := range gitTestCases {
failMessageAndArgs := []interface{}{"Test case %q failed.", caseName}
project, gitRepoStub := newLocalGitProject(t, nil)
defer os.RemoveAll(project.GetBaseDir())
testCase.prepare(t, project, gitRepoStub, failMessageAndArgs...)
vendorsLoop:
for importPath, _ := range testCase.expectedImportPaths {
specificFailMessageAndArgs := make([]interface{}, len(failMessageAndArgs)+1)
copy(specificFailMessageAndArgs, failMessageAndArgs)
specificFailMessageAndArgs[0] = specificFailMessageAndArgs[0].(string) + " (vendor %q)"
specificFailMessageAndArgs[len(failMessageAndArgs)] = importPath
vendorPath := path.Join(project.GetBaseDir(), "vendor", importPath)
// Check that vendor's dir exists
_, err := os.Stat(vendorPath)
if !assert.Nil(t, err, specificFailMessageAndArgs...) {
continue vendorsLoop
}
// Remove vendor
err = project.RemoveVendor(importPath)
if !assert.Nil(t, err, specificFailMessageAndArgs...) {
continue vendorsLoop
}
// Check that vendor's dir doesn't exist anymore
_, err = os.Stat(vendorPath)
if !assert.True(t, os.IsNotExist(err), specificFailMessageAndArgs...) {
continue vendorsLoop
}
}
// Check that PROJECTPATH/vendor is empty or doesn't exists
fileInfos, err := ioutil.ReadDir(path.Join(project.GetBaseDir(), "vendor"))
if os.IsNotExist(err) || assert.Nil(t, err, failMessageAndArgs...) {
assert.Len(t, fileInfos, 0)
}
}
}
func TestLocalGitProject_AddVendor(t *testing.T) {
project, _ := newLocalGitProject(t, nil)
defer os.RemoveAll(project.GetBaseDir())
futureVendor, _ := newLocalGitProject(t, git.Service.(*git.GitStub))
defer os.RemoveAll(futureVendor.GetBaseDir())
importPath := "completely/madeup/importpath"
actualVendor, err := project.AddVendor(importPath, futureVendor)
assert.Nil(t, err)
assert.Equal(t, path.Join(project.GetBaseDir(), "vendor", importPath), actualVendor.GetBaseDir())
fileInfo, err := os.Stat(actualVendor.GetBaseDir())
assert.Nil(t, err)
assert.True(t, fileInfo.IsDir())
}