/
git_test.go
354 lines (323 loc) · 10.3 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package git
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}
func TestNotAGitFolder(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
require.EqualError(t, Pipe{}.Run(ctx), ErrNotRepository.Error())
}
func TestSingleCommit(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Equal(t, "v0.0.1", ctx.Git.Summary)
require.Equal(t, "commit1", ctx.Git.TagSubject)
require.Equal(t, "commit1", ctx.Git.TagContents)
}
func TestAnnotatedTags(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitAnnotatedTag(t, "v0.0.1", "first version\n\nlalalla\nlalal\nlah")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Equal(t, "first version", ctx.Git.TagSubject)
require.Equal(t, "first version\n\nlalalla\nlalal\nlah", ctx.Git.TagContents)
require.Equal(t, "lalalla\nlalal\nlah", ctx.Git.TagBody)
require.Equal(t, "v0.0.1", ctx.Git.Summary)
}
func TestBranch(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "test-branch-commit")
testlib.GitTag(t, "test-branch-tag")
testlib.GitCheckoutBranch(t, "test-branch")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "test-branch", ctx.Git.Branch)
require.Equal(t, "test-branch-tag", ctx.Git.Summary)
}
func TestNoRemote(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.EqualError(t, Pipe{}.Run(ctx), "couldn't get remote URL: fatal: No remote configured to list refs from.")
}
func TestNewRepository(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
ctx := context.New(config.Project{})
// TODO: improve this error handling
require.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
}
// TestNoTagsNoSnapshot covers the situation where a repository
// only contains simple commits and no tags. In this case you have
// to set the --snapshot flag otherwise an error is returned.
func TestNoTagsNoSnapshot(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "first")
ctx := context.New(config.Project{})
ctx.Snapshot = false
require.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
}
func TestDirty(t *testing.T) {
folder := testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
dummy, err := os.Create(filepath.Join(folder, "dummy"))
require.NoError(t, err)
require.NoError(t, dummy.Close())
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
require.NoError(t, os.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0o644))
t.Run("all checks up", func(t *testing.T) {
err := Pipe{}.Run(context.New(config.Project{}))
require.Error(t, err)
require.Contains(t, err.Error(), "git is in a dirty state")
})
t.Run("skip validate is set", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipValidate = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
t.Run("snapshot", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
func TestRemoteURLContainsWithUsernameAndToken(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAg@gitlab.private.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
}
func TestRemoteURLContainsWithUsernameAndTokenWithInvalidURL(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAggitlab.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.Error(t, Pipe{}.Run(ctx))
}
func TestShallowClone(t *testing.T) {
folder := testlib.Mktmp(t)
require.NoError(
t,
exec.Command(
"git", "clone",
"--depth", "1",
"--branch", "v0.160.0",
"https://github.com/goreleaser/goreleaser",
folder,
).Run(),
)
t.Run("all checks up", func(t *testing.T) {
// its just a warning now
require.NoError(t, Pipe{}.Run(context.New(config.Project{})))
})
t.Run("skip validate is set", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.SkipValidate = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
t.Run("snapshot", func(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
func TestTagSortOrder(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitCommit(t, "commit2")
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1-rc.2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{
Git: config.Git{
TagSort: "-version:creatordate",
},
})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
}
func TestTagIsNotLastCommit(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4")
ctx := context.New(config.Project{})
err := Pipe{}.Run(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
require.Contains(t, ctx.Git.Summary, "v0.0.1-1-g") // commit not represented because it changes every test
}
func TestValidState(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1")
testlib.GitTag(t, "v0.0.2")
testlib.GitCommit(t, "commit4")
testlib.GitTag(t, "v0.0.3")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.2", ctx.Git.PreviousTag)
require.Equal(t, "v0.0.3", ctx.Git.CurrentTag)
require.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL)
}
func TestSnapshotNoTags(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitAdd(t)
testlib.GitCommit(t, "whatever")
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag)
}
func TestSnapshotNoCommits(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo, ctx.Git)
}
func TestSnapshotWithoutRepo(t *testing.T) {
testlib.Mktmp(t)
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo, ctx.Git)
}
func TestSnapshotDirty(t *testing.T) {
folder := testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitAdd(t)
testlib.GitCommit(t, "whatever")
testlib.GitTag(t, "v0.0.1")
require.NoError(t, os.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0o644))
ctx := context.New(config.Project{})
ctx.Snapshot = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.Summary)
}
func TestGitNotInPath(t *testing.T) {
path := os.Getenv("PATH")
defer func() {
require.NoError(t, os.Setenv("PATH", path))
}()
require.NoError(t, os.Setenv("PATH", ""))
require.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error())
}
func TestTagFromCI(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
testlib.GitTag(t, "v0.0.2")
for _, tc := range []struct {
envs map[string]string
expected string
}{
{expected: "v0.0.2"},
{
envs: map[string]string{"GORELEASER_CURRENT_TAG": "v0.0.2"},
expected: "v0.0.2",
},
} {
for name, value := range tc.envs {
require.NoError(t, os.Setenv(name, value))
}
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, tc.expected, ctx.Git.CurrentTag)
for name := range tc.envs {
require.NoError(t, os.Setenv(name, ""))
}
}
}
func TestNoPreviousTag(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag, "should be empty")
}
func TestPreviousTagFromCI(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "commit1")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.2")
for _, tc := range []struct {
envs map[string]string
expected string
}{
{expected: "v0.0.1"},
{
envs: map[string]string{"GORELEASER_PREVIOUS_TAG": "v0.0.2"},
expected: "v0.0.2",
},
} {
t.Run(tc.expected, func(t *testing.T) {
for name, value := range tc.envs {
require.NoError(t, os.Setenv(name, value))
}
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, tc.expected, ctx.Git.PreviousTag)
for name := range tc.envs {
require.NoError(t, os.Setenv(name, ""))
}
})
}
}