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
|
package integration
import (
"io/fs"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tfs "gotest.tools/v3/fs"
)
func setupConfigTest(t *testing.T) *tfs.Dir {
t.Helper()
tmpDir := tfs.NewDir(t, "gomplate-inttests",
tfs.WithDir("indir"),
tfs.WithDir("outdir"),
tfs.WithFile(".gomplate.yaml", "in: hello world\n"),
tfs.WithFile("sleep.sh", "#!/bin/sh\n\nexec sleep $1\n", tfs.WithMode(0o755)),
)
t.Cleanup(tmpDir.Remove)
return tmpDir
}
func writeFile(t *testing.T, dir *tfs.Dir, f, content string) {
t.Helper()
f = dir.Join(f)
err := os.WriteFile(f, []byte(content), 0o600)
if err != nil {
t.Fatal(err)
}
}
func writeConfig(t *testing.T, dir *tfs.Dir, content string) {
t.Helper()
writeFile(t, dir, ".gomplate.yaml", content)
t.Logf("writing config: %s", content)
}
func TestConfig_ReadsFromSimpleConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "hello world")
}
func TestConfig_ReadsStdin(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, "inputFiles: [-]")
o, e, err := cmd(t).withDir(tmpDir.Path()).withStdin("foo bar").run()
assertSuccess(t, o, e, err, "foo bar")
}
func TestConfig_FlagOverridesConfig(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, "inputFiles: [in]")
o, e, err := cmd(t, "-i", "hello from the cli").
withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "hello from the cli")
}
func TestConfig_ReadsFromInputFile(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, "inputFiles: [in]")
writeFile(t, tmpDir, "in", "blah blah")
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "blah blah")
}
func TestConfig_Datasource(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputFiles: [in]
datasources:
data:
url: in.yaml
`)
writeFile(t, tmpDir, "in", `{{ (ds "data").value }}`)
writeFile(t, tmpDir, "in.yaml", `value: hello world`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "hello world")
}
func TestConfig_OutputDir(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputDir: indir/
outputDir: outdir/
datasources:
data:
url: in.yaml
`)
writeFile(t, tmpDir, "indir/file", `{{ (ds "data").value }}`)
writeFile(t, tmpDir, "in.yaml", `value: hello world`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "")
b, err := os.ReadFile(tmpDir.Join("outdir", "file"))
require.NoError(t, err)
assert.Equal(t, "hello world", string(b))
}
func TestConfig_ExecPipeOverridesConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
// make sure exec-pipe works, and outFiles is replaced
writeConfig(t, tmpDir, `in: hello world
outputFiles: ['-']
`)
o, e, err := cmd(t, "-i", "hi", "--exec-pipe", "--", "tr", "[a-z]", "[A-Z]").
withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "HI")
}
func TestConfig_OutFile(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `in: hello world
outputFiles: [out]
`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "")
b, err := os.ReadFile(tmpDir.Join("out"))
require.NoError(t, err)
assert.Equal(t, "hello world", string(b))
}
func TestConfig_AlternateConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
writeFile(t, tmpDir, "config.yaml", `in: this is from an alternate config
`)
o, e, err := cmd(t, "--config=config.yaml").withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "this is from an alternate config")
}
func TestConfig_EnvConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
writeFile(t, tmpDir, "envconfig.yaml", `in: yet another alternate config
`)
o, e, err := cmd(t).withDir(tmpDir.Path()).
withEnv("GOMPLATE_CONFIG", "./envconfig.yaml").run()
assertSuccess(t, o, e, err, "yet another alternate config")
}
func TestConfig_SkipConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
// first set a poisoned default config to prove that it's not being read
writeFile(t, tmpDir, ".gomplate.yaml", `badyaml`)
o, e, err := cmd(t, "--config", "", "--in", "foo").withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "foo")
o, e, err = cmd(t, "--in", "foo").withDir(tmpDir.Path()).
withEnv("GOMPLATE_CONFIG", "").run()
assertSuccess(t, o, e, err, "foo")
}
func TestConfig_ConfigOverridesEnvDelim(t *testing.T) {
if isWindows {
t.Skip()
}
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputFiles: [in]
leftDelim: (╯°□°)╯︵ ┻━┻
datasources:
data:
url: in.yaml
`)
writeFile(t, tmpDir, "in", `(╯°□°)╯︵ ┻━┻ (ds "data").value }}`)
writeFile(t, tmpDir, "in.yaml", `value: hello world`)
o, e, err := cmd(t).withDir(tmpDir.Path()).
withEnv("GOMPLATE_LEFT_DELIM", "<<").run()
require.NoError(t, err)
assert.Empty(t, e)
assert.Equal(t, "hello world", o)
}
func TestConfig_FlagOverridesAllDelim(t *testing.T) {
if isWindows {
t.Skip()
}
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputFiles: [in]
leftDelim: (╯°□°)╯︵ ┻━┻
datasources:
data:
url: in.yaml
`)
writeFile(t, tmpDir, "in", `{{ (ds "data").value }}`)
writeFile(t, tmpDir, "in.yaml", `value: hello world`)
o, e, err := cmd(t, "--left-delim={{").
withDir(tmpDir.Path()).
withEnv("GOMPLATE_LEFT_DELIM", "<<").run()
require.NoError(t, err)
assert.Empty(t, e)
assert.Equal(t, "hello world", o)
}
func TestConfig_ConfigOverridesEnvPluginTimeout(t *testing.T) {
if isWindows {
t.Skip()
}
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `in: hi there {{ sleep 2 }}
plugins:
sleep: echo
pluginTimeout: 500ms
`)
_, _, err := cmd(t, "--plugin", "sleep="+tmpDir.Join("sleep.sh")).
withDir(tmpDir.Path()).
withEnv("GOMPLATE_PLUGIN_TIMEOUT", "5s").run()
assert.ErrorContains(t, err, "plugin timed out")
}
func TestConfig_ConfigOverridesEnvSuppressEmpty(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `in: |
{{- print "\t \n\n\r\n\t\t \v\n" -}}
{{ print " " -}}
out: ./missing
suppressEmpty: true
`)
_, _, err := cmd(t).withDir(tmpDir.Path()).
withEnv("GOMPLATE_SUPPRESS_EMPTY", "false").run()
require.NoError(t, err)
_, err = os.Stat(tmpDir.Join("missing"))
require.ErrorIs(t, err, fs.ErrNotExist)
}
func TestConfig_ConfigParseErrorSpecifiesFilename(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `templates:
dir: /foo/bar
`)
_, _, err := cmd(t).withDir(tmpDir.Path()).run()
assert.ErrorContains(t, err, `parsing config file ".gomplate.yaml": YAML decoding failed`)
}
func TestConfig_ConfigTemplatesSupportsMap(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `in: '{{ template "t1" (dict "testValue" "12345") }}'
templates:
t1:
url: t1.tmpl
`)
writeFile(t, tmpDir, "t1.tmpl", `{{ .testValue }}`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "12345")
}
func TestConfig_ConfigTemplatesSupportsArray(t *testing.T) {
tmpDir := setupConfigTest(t)
// TODO: remove this test once the array format is no longer supported
writeConfig(t, tmpDir, `in: '{{ template "t1" (dict "testValue" "12345") }}'
templates:
- t1=t1.tmpl
`)
writeFile(t, tmpDir, "t1.tmpl", `{{ .testValue }}`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assert.Contains(t, e, "Deprecated: config: the YAML array form for 'templates' is deprecated")
assert.Equal(t, "12345", o)
require.NoError(t, err)
}
func TestConfig_MissingKeyDefault(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputFiles: [in]
missingKey: default
`)
writeFile(t, tmpDir, "in", `{{ .name }}`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, `<no value>`)
}
func TestConfig_MissingKeyNotDefined(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, `inputFiles: [in]`)
writeFile(t, tmpDir, "in", `{{ .name }}`)
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertFailed(t, o, e, err, `map has no entry for key \"name\"`)
}
|