github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/config_test.go (about) 1 package integration 2 3 import ( 4 "os" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 "gotest.tools/v3/fs" 9 ) 10 11 func setupConfigTest(t *testing.T) *fs.Dir { 12 tmpDir := fs.NewDir(t, "gomplate-inttests", 13 fs.WithDir("indir"), 14 fs.WithDir("outdir"), 15 fs.WithFile(".gomplate.yaml", "in: hello world\n"), 16 fs.WithFile("sleep.sh", "#!/bin/sh\n\nexec sleep $1\n", fs.WithMode(0o755)), 17 ) 18 t.Cleanup(tmpDir.Remove) 19 return tmpDir 20 } 21 22 func writeFile(t *testing.T, dir *fs.Dir, f, content string) { 23 f = dir.Join(f) 24 err := os.WriteFile(f, []byte(content), 0o600) 25 if err != nil { 26 t.Fatal(err) 27 } 28 } 29 30 func writeConfig(t *testing.T, dir *fs.Dir, content string) { 31 t.Helper() 32 33 writeFile(t, dir, ".gomplate.yaml", content) 34 t.Logf("writing config: %s", content) 35 } 36 37 func TestConfig_ReadsFromSimpleConfigFile(t *testing.T) { 38 tmpDir := setupConfigTest(t) 39 40 o, e, err := cmd(t).withDir(tmpDir.Path()).run() 41 assertSuccess(t, o, e, err, "hello world") 42 } 43 44 func TestConfig_ReadsStdin(t *testing.T) { 45 tmpDir := setupConfigTest(t) 46 writeConfig(t, tmpDir, "inputFiles: [-]") 47 48 o, e, err := cmd(t).withDir(tmpDir.Path()).withStdin("foo bar").run() 49 assertSuccess(t, o, e, err, "foo bar") 50 } 51 52 func TestConfig_FlagOverridesConfig(t *testing.T) { 53 tmpDir := setupConfigTest(t) 54 writeConfig(t, tmpDir, "inputFiles: [in]") 55 56 o, e, err := cmd(t, "-i", "hello from the cli"). 57 withDir(tmpDir.Path()).run() 58 assertSuccess(t, o, e, err, "hello from the cli") 59 } 60 61 func TestConfig_ReadsFromInputFile(t *testing.T) { 62 tmpDir := setupConfigTest(t) 63 writeConfig(t, tmpDir, "inputFiles: [in]") 64 writeFile(t, tmpDir, "in", "blah blah") 65 66 o, e, err := cmd(t).withDir(tmpDir.Path()).run() 67 assertSuccess(t, o, e, err, "blah blah") 68 } 69 70 func TestConfig_Datasource(t *testing.T) { 71 tmpDir := setupConfigTest(t) 72 writeConfig(t, tmpDir, `inputFiles: [in] 73 datasources: 74 data: 75 url: in.yaml 76 `) 77 writeFile(t, tmpDir, "in", `{{ (ds "data").value }}`) 78 writeFile(t, tmpDir, "in.yaml", `value: hello world`) 79 80 o, e, err := cmd(t).withDir(tmpDir.Path()).run() 81 assertSuccess(t, o, e, err, "hello world") 82 } 83 84 func TestConfig_OutputDir(t *testing.T) { 85 tmpDir := setupConfigTest(t) 86 87 writeConfig(t, tmpDir, `inputDir: indir/ 88 outputDir: outdir/ 89 datasources: 90 data: 91 url: in.yaml 92 `) 93 writeFile(t, tmpDir, "indir/file", `{{ (ds "data").value }}`) 94 writeFile(t, tmpDir, "in.yaml", `value: hello world`) 95 96 o, e, err := cmd(t).withDir(tmpDir.Path()).run() 97 assertSuccess(t, o, e, err, "") 98 99 b, err := os.ReadFile(tmpDir.Join("outdir", "file")) 100 assert.NilError(t, err) 101 assert.Equal(t, "hello world", string(b)) 102 } 103 104 func TestConfig_ExecPipeOverridesConfigFile(t *testing.T) { 105 tmpDir := setupConfigTest(t) 106 107 // make sure exec-pipe works, and outFiles is replaced 108 writeConfig(t, tmpDir, `in: hello world 109 outputFiles: ['-'] 110 `) 111 o, e, err := cmd(t, "-i", "hi", "--exec-pipe", "--", "tr", "[a-z]", "[A-Z]"). 112 withDir(tmpDir.Path()).run() 113 assertSuccess(t, o, e, err, "HI") 114 } 115 116 func TestConfig_OutFile(t *testing.T) { 117 tmpDir := setupConfigTest(t) 118 119 writeConfig(t, tmpDir, `in: hello world 120 outputFiles: [out] 121 `) 122 o, e, err := cmd(t).withDir(tmpDir.Path()).run() 123 assertSuccess(t, o, e, err, "") 124 125 b, err := os.ReadFile(tmpDir.Join("out")) 126 assert.NilError(t, err) 127 assert.Equal(t, "hello world", string(b)) 128 } 129 130 func TestConfig_AlternateConfigFile(t *testing.T) { 131 tmpDir := setupConfigTest(t) 132 writeFile(t, tmpDir, "config.yaml", `in: this is from an alternate config 133 `) 134 135 o, e, err := cmd(t, "--config=config.yaml").withDir(tmpDir.Path()).run() 136 assertSuccess(t, o, e, err, "this is from an alternate config") 137 } 138 139 func TestConfig_EnvConfigFile(t *testing.T) { 140 tmpDir := setupConfigTest(t) 141 142 writeFile(t, tmpDir, "envconfig.yaml", `in: yet another alternate config 143 `) 144 145 o, e, err := cmd(t).withDir(tmpDir.Path()). 146 withEnv("GOMPLATE_CONFIG", "./envconfig.yaml").run() 147 assertSuccess(t, o, e, err, "yet another alternate config") 148 } 149 150 func TestConfig_ConfigOverridesEnvDelim(t *testing.T) { 151 if isWindows { 152 t.Skip() 153 } 154 155 tmpDir := setupConfigTest(t) 156 157 writeConfig(t, tmpDir, `inputFiles: [in] 158 leftDelim: (╯°□°)╯︵ ┻━┻ 159 datasources: 160 data: 161 url: in.yaml 162 `) 163 writeFile(t, tmpDir, "in", `(╯°□°)╯︵ ┻━┻ (ds "data").value }}`) 164 writeFile(t, tmpDir, "in.yaml", `value: hello world`) 165 166 o, e, err := cmd(t).withDir(tmpDir.Path()). 167 withEnv("GOMPLATE_LEFT_DELIM", "<<").run() 168 assert.NilError(t, err) 169 assert.Equal(t, "", e) 170 assert.Equal(t, "hello world", o) 171 } 172 173 func TestConfig_FlagOverridesAllDelim(t *testing.T) { 174 if isWindows { 175 t.Skip() 176 } 177 178 tmpDir := setupConfigTest(t) 179 180 writeConfig(t, tmpDir, `inputFiles: [in] 181 leftDelim: (╯°□°)╯︵ ┻━┻ 182 datasources: 183 data: 184 url: in.yaml 185 `) 186 writeFile(t, tmpDir, "in", `{{ (ds "data").value }}`) 187 writeFile(t, tmpDir, "in.yaml", `value: hello world`) 188 189 o, e, err := cmd(t, "--left-delim={{"). 190 withDir(tmpDir.Path()). 191 withEnv("GOMPLATE_LEFT_DELIM", "<<").run() 192 assert.NilError(t, err) 193 assert.Equal(t, "", e) 194 assert.Equal(t, "hello world", o) 195 } 196 197 func TestConfig_ConfigOverridesEnvPluginTimeout(t *testing.T) { 198 if isWindows { 199 t.Skip() 200 } 201 202 tmpDir := setupConfigTest(t) 203 204 writeConfig(t, tmpDir, `in: hi there {{ sleep 2 }} 205 plugins: 206 sleep: echo 207 208 pluginTimeout: 500ms 209 `) 210 211 _, _, err := cmd(t, "--plugin", "sleep="+tmpDir.Join("sleep.sh")). 212 withDir(tmpDir.Path()). 213 withEnv("GOMPLATE_PLUGIN_TIMEOUT", "5s").run() 214 assert.ErrorContains(t, err, "plugin timed out") 215 } 216 217 func TestConfig_ConfigOverridesEnvSuppressEmpty(t *testing.T) { 218 tmpDir := setupConfigTest(t) 219 220 writeConfig(t, tmpDir, `in: | 221 {{- print "\t \n\n\r\n\t\t \v\n" -}} 222 223 {{ print " " -}} 224 out: ./missing 225 suppressEmpty: true 226 `) 227 228 _, _, err := cmd(t).withDir(tmpDir.Path()). 229 withEnv("GOMPLATE_SUPPRESS_EMPTY", "false").run() 230 assert.NilError(t, err) 231 232 _, err = os.Stat(tmpDir.Join("missing")) 233 assert.Equal(t, true, os.IsNotExist(err)) 234 }