/
tmpl_test.go
226 lines (213 loc) · 5.24 KB
/
tmpl_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
package tmpl
import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert"
)
func TestWithArtifact(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
})
ctx.Env = map[string]string{
"FOO": "bar",
}
ctx.Version = "1.2.3"
ctx.Git.CurrentTag = "v1.2.3"
ctx.Semver = context.Semver{
Major: 1,
Minor: 2,
Patch: 3,
}
ctx.Git.Commit = "commit"
ctx.Git.FullCommit = "fullcommit"
ctx.Git.ShortCommit = "shortcommit"
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"softfloat": "{{.Mips}}",
"1.2.3": "{{.Version}}",
"v1.2.3": "{{.Tag}}",
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"": "{{.ArtifactUploadHash}}",
} {
tmpl := tmpl
expect := expect
t.Run(expect, func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).WithArtifact(
&artifact.Artifact{
Name: "not-this-binary",
Goarch: "amd64",
Goos: "linux",
Goarm: "6",
Gomips: "softfloat",
Extra: map[string]interface{}{
"Binary": "binary",
},
},
map[string]string{"linux": "Linux"},
).Apply(tmpl)
assert.NoError(tt, err)
assert.Equal(tt, expect, result)
})
}
t.Run("artifact with gitlab ArtifactUploadHash", func(tt *testing.T) {
tt.Parallel()
uploadHash := "820ead5d9d2266c728dce6d4d55b6460"
result, err := New(ctx).WithArtifact(
&artifact.Artifact{
Name: "another-binary",
Goarch: "amd64",
Goos: "linux",
Goarm: "6",
Extra: map[string]interface{}{
"ArtifactUploadHash": uploadHash,
},
}, map[string]string{},
).Apply("{{ .ArtifactUploadHash }}")
assert.NoError(tt, err)
assert.Equal(tt, uploadHash, result)
})
t.Run("artifact without binary name", func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).WithArtifact(
&artifact.Artifact{
Name: "another-binary",
Goarch: "amd64",
Goos: "linux",
Goarm: "6",
}, map[string]string{},
).Apply("{{ .Binary }}")
assert.NoError(tt, err)
assert.Equal(tt, ctx.Config.ProjectName, result)
})
t.Run("template using artifact Fields with no artifact", func(tt *testing.T) {
tt.Parallel()
result, err := New(ctx).Apply("{{ .Os }}")
assert.EqualError(tt, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`)
assert.Empty(tt, result)
})
}
func TestEnv(t *testing.T) {
testCases := []struct {
desc string
in string
out string
}{
{
desc: "with env",
in: "{{ .Env.FOO }}",
out: "BAR",
},
{
desc: "with env",
in: "{{ .Env.BAR }}",
out: "",
},
}
var ctx = context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
out, _ := New(ctx).Apply(tC.in)
assert.Equal(t, tC.out, out)
})
}
}
func TestWithEnv(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
ctx.Git.CurrentTag = "v1.2.3"
out, err := New(ctx).WithEnvS([]string{
"FOO=foo",
"BAR=bar",
}).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
assert.NoError(t, err)
assert.Equal(t, "foo-bar", out)
}
func TestFuncMap(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
})
ctx.Git.CurrentTag = "v1.2.4"
for _, tc := range []struct {
Template string
Name string
Expected string
}{
{
Template: `{{ replace "v1.24" "v" "" }}`,
Name: "replace",
Expected: "1.24",
},
{
Template: `{{ time "2006-01-02" }}`,
Name: "time YYYY-MM-DD",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "time MM/DD/YYYY",
},
{
Template: `{{ time "01/02/2006" }}`,
Name: "time MM/DD/YYYY",
},
{
Template: `{{ tolower "TEST" }}`,
Name: "tolower",
Expected: "test",
},
{
Template: `{{ toupper "test" }}`,
Name: "toupper",
Expected: "TEST",
},
{
Template: `{{ trim " test " }}`,
Name: "trim",
Expected: "test",
},
} {
out, err := New(ctx).Apply(tc.Template)
assert.NoError(t, err)
if tc.Expected != "" {
assert.Equal(t, tc.Expected, out)
} else {
assert.NotEmpty(t, out)
}
}
}
func TestInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.1.1"
_, err := New(ctx).Apply("{{{.Foo}")
assert.EqualError(t, err, "template: tmpl:1: unexpected \"{\" in command")
}
func TestEnvNotFound(t *testing.T) {
var ctx = context.New(config.Project{})
ctx.Git.CurrentTag = "v1.2.4"
result, err := New(ctx).Apply("{{.Env.FOO}}")
assert.Empty(t, result)
assert.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`)
}
func TestWithExtraFields(t *testing.T) {
var ctx = context.New(config.Project{})
out, _ := New(ctx).WithExtraFields(Fields{
"MyCustomField": "foo",
}).Apply("{{ .MyCustomField }}")
assert.Equal(t, "foo", out)
}