summaryrefslogtreecommitdiff
path: root/internal/tests/integration/config_test.go
blob: f30d263a7ce380fe6f732b481430083dcaffc95d (plain)
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
package integration

import (
	"io/ioutil"
	"os"
	"testing"

	"gotest.tools/v3/assert"
	"gotest.tools/v3/fs"
)

func setupConfigTest(t *testing.T) *fs.Dir {
	tmpDir := fs.NewDir(t, "gomplate-inttests",
		fs.WithDir("indir"),
		fs.WithDir("outdir"),
		fs.WithFile(".gomplate.yaml", "in: hello world\n"),
		fs.WithFile("sleep.sh", "#!/bin/sh\n\nexec sleep $1\n", fs.WithMode(0755)),
	)
	t.Cleanup(tmpDir.Remove)
	return tmpDir
}

func writeFile(t *testing.T, dir *fs.Dir, f, content string) {
	f = dir.Join(f)
	err := ioutil.WriteFile(f, []byte(content), 0600)
	if err != nil {
		t.Fatal(err)
	}
}

func writeConfig(t *testing.T, dir *fs.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 := ioutil.ReadFile(tmpDir.Join("outdir", "file"))
	assert.NilError(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 := ioutil.ReadFile(tmpDir.Join("out"))
	assert.NilError(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_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()
	assert.NilError(t, err)
	assert.Equal(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()
	assert.NilError(t, err)
	assert.Equal(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()
	assert.NilError(t, err)

	_, err = os.Stat(tmpDir.Join("missing"))
	assert.Equal(t, true, os.IsNotExist(err))
}