-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprev_test.go
More file actions
122 lines (116 loc) · 2.59 KB
/
prev_test.go
File metadata and controls
122 lines (116 loc) · 2.59 KB
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
package main
import (
"cmp"
"testing"
"github.com/stretchr/testify/require"
)
func Test_getPrevTag(t *testing.T) {
t.Parallel()
const stdSetup = `
git init
git commit --allow-empty -m "first"
git tag v0.1.0
git tag foo0.1.0
git tag v0.1.1
git tag foo0.1.1
git tag v0.2.0
git tag foo0.2.0
git tag v1.0.0
git commit --allow-empty -m "second"
git commit --allow-empty -m "third"
git tag v2.0.0
git tag v4
git tag foo
git tag foo3.0.0
git commit --allow-empty -m "forth"
git tag bar
`
stdSetupEnv := map[string]string{
"GIT_AUTHOR_NAME": "foo",
"GIT_COMMITTER_NAME": "foo",
"EMAIL": "foo@example.com",
}
for _, test := range []struct {
name string
setupCmd string
setupEnv map[string]string
opts getPrevTagOpts
wantTag string
wantErr bool
}{
{
name: "",
opts: getPrevTagOpts{TagPrefix: "v"},
wantTag: "v2.0.0",
},
{
name: "no tags",
setupCmd: `git init && git commit --allow-empty -m "first"`,
wantTag: "",
},
{
name: "no matching prefix",
opts: getPrevTagOpts{TagPrefix: "z"},
wantTag: "",
},
{
name: "StableOnly",
setupCmd: stdSetup + "\ngit tag v2.1.0-beta.1\n",
opts: getPrevTagOpts{TagPrefix: "v", StableOnly: true},
wantTag: "v2.0.0",
},
{
name: "prerelease tag",
setupCmd: stdSetup + "\ngit tag v2.1.0-beta.1\n",
opts: getPrevTagOpts{TagPrefix: "v"},
wantTag: "v2.1.0-beta.1",
},
{
name: "specific head",
setupCmd: stdSetup + "\ngit tag v3.0.0\n",
opts: getPrevTagOpts{TagPrefix: "v", Head: "HEAD~1"},
wantTag: "v2.0.0",
},
{
name: "no prefix no match",
opts: getPrevTagOpts{TagPrefix: ""},
},
{
name: "no prefix match",
setupCmd: stdSetup + "\ngit tag 1.2.3-alpha.1\n",
opts: getPrevTagOpts{TagPrefix: ""},
wantTag: "1.2.3-alpha.1",
},
{
name: "git error",
setupCmd: "echo 'do nothing'",
opts: getPrevTagOpts{TagPrefix: "v"},
wantErr: true,
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
dir := t.TempDir()
setupOpts := &runCmdOpts{
dir: dir,
env: test.setupEnv,
}
setupCmd := cmp.Or(test.setupCmd, stdSetup)
if setupOpts.env == nil {
setupOpts.env = stdSetupEnv
}
_, err := runCmd(ctx, setupOpts, "sh", "-c", setupCmd)
require.NoError(t, err)
opts := test.opts
opts.RepoDir = cmp.Or(opts.RepoDir, dir)
got, err := getPrevTag(ctx, &opts)
if test.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.wantTag, got)
})
}
}