summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-04-10 13:38:27 -0400
committerDave Henderson <dhenderson@gmail.com>2022-04-10 14:43:54 -0400
commit6a9e48937e08fd273ec8fc2b7e703511e9016a53 (patch)
tree7d53a4c9db055b9b3a34349b27281c7f2cf0c1fd
parent22d7df6a316322d7cb20cb9badc8ec43e7d02322 (diff)
Removing some panics from tests
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--.github/workflows/build.yml3
-rw-r--r--funcs/math_test.go14
-rw-r--r--gomplate_test.go65
-rw-r--r--internal/tests/integration/config_test.go28
-rw-r--r--internal/tests/integration/datasources_consul_test.go6
-rw-r--r--internal/tests/integration/datasources_git_test.go2
-rw-r--r--internal/tests/integration/datasources_vault_test.go2
-rw-r--r--internal/tests/integration/integration_test.go8
8 files changed, 65 insertions, 63 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 8ca1af1e..c3871a45 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -42,8 +42,9 @@ jobs:
windows-build:
runs-on: windows-latest
env:
- TMP: D:\tmp
+ TMP: D:\a\tmp
steps:
+ - run: pwd
- uses: actions/setup-go@v2
with:
go-version: '1.18'
diff --git a/funcs/math_test.go b/funcs/math_test.go
index ca0d6aff..f413f5a8 100644
--- a/funcs/math_test.go
+++ b/funcs/math_test.go
@@ -83,21 +83,21 @@ func TestPow(t *testing.T) {
assert.Equal(t, 2.25, m.Pow(1.5, 2))
}
-func mustSeq(n ...interface{}) []int64 {
+func mustSeq(t *testing.T, n ...interface{}) []int64 {
m := MathFuncs{}
s, err := m.Seq(n...)
if err != nil {
- panic(err)
+ t.Fatal(err)
}
return s
}
func TestSeq(t *testing.T) {
m := MathFuncs{}
- assert.EqualValues(t, []int64{0, 1, 2, 3}, mustSeq(0, 3))
- assert.EqualValues(t, []int64{1, 0}, mustSeq(0))
- assert.EqualValues(t, []int64{0, 2, 4}, mustSeq(0, 4, 2))
- assert.EqualValues(t, []int64{0, 2, 4}, mustSeq(0, 5, 2))
- assert.EqualValues(t, []int64{0}, mustSeq(0, 5, 8))
+ assert.EqualValues(t, []int64{0, 1, 2, 3}, mustSeq(t, 0, 3))
+ assert.EqualValues(t, []int64{1, 0}, mustSeq(t, 0))
+ assert.EqualValues(t, []int64{0, 2, 4}, mustSeq(t, 0, 4, 2))
+ assert.EqualValues(t, []int64{0, 2, 4}, mustSeq(t, 0, 5, 2))
+ assert.EqualValues(t, []int64{0}, mustSeq(t, 0, 5, 8))
_, err := m.Seq()
assert.Error(t, err)
}
diff --git a/gomplate_test.go b/gomplate_test.go
index 2ea1d241..b6c8e528 100644
--- a/gomplate_test.go
+++ b/gomplate_test.go
@@ -20,12 +20,13 @@ import (
"github.com/stretchr/testify/assert"
)
-func testTemplate(g *gomplate, tmpl string) string {
+func testTemplate(t *testing.T, g *gomplate, tmpl string) string {
+ t.Helper()
+
var out bytes.Buffer
err := g.runTemplate(context.Background(), &tplate{name: "testtemplate", contents: tmpl, target: &out})
- if err != nil {
- panic(err)
- }
+ assert.NoError(t, err)
+
return out.String()
}
@@ -36,9 +37,9 @@ func TestGetenvTemplates(t *testing.T) {
"bool": conv.Bool,
},
}
- assert.Empty(t, testTemplate(g, `{{getenv "BLAHBLAHBLAH"}}`))
- assert.Equal(t, os.Getenv("USER"), testTemplate(g, `{{getenv "USER"}}`))
- assert.Equal(t, "default value", testTemplate(g, `{{getenv "BLAHBLAHBLAH" "default value"}}`))
+ assert.Empty(t, testTemplate(t, g, `{{getenv "BLAHBLAHBLAH"}}`))
+ assert.Equal(t, os.Getenv("USER"), testTemplate(t, g, `{{getenv "USER"}}`))
+ assert.Equal(t, "default value", testTemplate(t, g, `{{getenv "BLAHBLAHBLAH" "default value"}}`))
}
func TestBoolTemplates(t *testing.T) {
@@ -47,10 +48,10 @@ func TestBoolTemplates(t *testing.T) {
"bool": conv.Bool,
},
}
- assert.Equal(t, "true", testTemplate(g, `{{bool "true"}}`))
- assert.Equal(t, "false", testTemplate(g, `{{bool "false"}}`))
- assert.Equal(t, "false", testTemplate(g, `{{bool "foo"}}`))
- assert.Equal(t, "false", testTemplate(g, `{{bool ""}}`))
+ assert.Equal(t, "true", testTemplate(t, g, `{{bool "true"}}`))
+ assert.Equal(t, "false", testTemplate(t, g, `{{bool "false"}}`))
+ assert.Equal(t, "false", testTemplate(t, g, `{{bool "foo"}}`))
+ assert.Equal(t, "false", testTemplate(t, g, `{{bool ""}}`))
}
func TestEc2MetaTemplates(t *testing.T) {
@@ -61,14 +62,14 @@ func TestEc2MetaTemplates(t *testing.T) {
g, s := createGomplate(404, "")
defer s.Close()
- assert.Equal(t, "", testTemplate(g, `{{ec2meta "foo"}}`))
- assert.Equal(t, "default", testTemplate(g, `{{ec2meta "foo" "default"}}`))
-
+ assert.Equal(t, "", testTemplate(t, g, `{{ec2meta "foo"}}`))
+ assert.Equal(t, "default", testTemplate(t, g, `{{ec2meta "foo" "default"}}`))
s.Close()
+
g, s = createGomplate(200, "i-1234")
defer s.Close()
- assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id"}}`))
- assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id" "default"}}`))
+ assert.Equal(t, "i-1234", testTemplate(t, g, `{{ec2meta "instance-id"}}`))
+ assert.Equal(t, "i-1234", testTemplate(t, g, `{{ec2meta "instance-id" "default"}}`))
}
func TestEc2MetaTemplates_WithJSON(t *testing.T) {
@@ -82,8 +83,8 @@ func TestEc2MetaTemplates_WithJSON(t *testing.T) {
},
}
- assert.Equal(t, "bar", testTemplate(g, `{{ (ec2meta "obj" | json).foo }}`))
- assert.Equal(t, "bar", testTemplate(g, `{{ (ec2dynamic "obj" | json).foo }}`))
+ assert.Equal(t, "bar", testTemplate(t, g, `{{ (ec2meta "obj" | json).foo }}`))
+ assert.Equal(t, "bar", testTemplate(t, g, `{{ (ec2dynamic "obj" | json).foo }}`))
}
func TestJSONArrayTemplates(t *testing.T) {
@@ -93,8 +94,8 @@ func TestJSONArrayTemplates(t *testing.T) {
},
}
- assert.Equal(t, "[foo bar]", testTemplate(g, `{{jsonArray "[\"foo\",\"bar\"]"}}`))
- assert.Equal(t, "bar", testTemplate(g, `{{ index (jsonArray "[\"foo\",\"bar\"]") 1 }}`))
+ assert.Equal(t, "[foo bar]", testTemplate(t, g, `{{jsonArray "[\"foo\",\"bar\"]"}}`))
+ assert.Equal(t, "bar", testTemplate(t, g, `{{ index (jsonArray "[\"foo\",\"bar\"]") 1 }}`))
}
func TestYAMLTemplates(t *testing.T) {
@@ -105,9 +106,9 @@ func TestYAMLTemplates(t *testing.T) {
},
}
- assert.Equal(t, "bar", testTemplate(g, `{{(yaml "foo: bar").foo}}`))
- assert.Equal(t, "[foo bar]", testTemplate(g, `{{yamlArray "- foo\n- bar\n"}}`))
- assert.Equal(t, "bar", testTemplate(g, `{{ index (yamlArray "[\"foo\",\"bar\"]") 1 }}`))
+ assert.Equal(t, "bar", testTemplate(t, g, `{{(yaml "foo: bar").foo}}`))
+ assert.Equal(t, "[foo bar]", testTemplate(t, g, `{{yamlArray "- foo\n- bar\n"}}`))
+ assert.Equal(t, "bar", testTemplate(t, g, `{{ index (yamlArray "[\"foo\",\"bar\"]") 1 }}`))
}
func TestSliceTemplates(t *testing.T) {
@@ -116,9 +117,9 @@ func TestSliceTemplates(t *testing.T) {
"slice": conv.Slice,
},
}
- assert.Equal(t, "foo", testTemplate(g, `{{index (slice "foo") 0}}`))
- assert.Equal(t, `[foo bar 42]`, testTemplate(g, `{{slice "foo" "bar" 42}}`))
- assert.Equal(t, `helloworld`, testTemplate(g, `{{range slice "hello" "world"}}{{.}}{{end}}`))
+ assert.Equal(t, "foo", testTemplate(t, g, `{{index (slice "foo") 0}}`))
+ assert.Equal(t, `[foo bar 42]`, testTemplate(t, g, `{{slice "foo" "bar" 42}}`))
+ assert.Equal(t, `helloworld`, testTemplate(t, g, `{{range slice "hello" "world"}}{{.}}{{end}}`))
}
func TestHasTemplate(t *testing.T) {
@@ -128,21 +129,21 @@ func TestHasTemplate(t *testing.T) {
"has": conv.Has,
},
}
- assert.Equal(t, "true", testTemplate(g, `{{has ("foo:\n bar: true" | yaml) "foo"}}`))
- assert.Equal(t, "true", testTemplate(g, `{{has ("foo:\n bar: true" | yaml).foo "bar"}}`))
- assert.Equal(t, "false", testTemplate(g, `{{has ("foo: true" | yaml) "bah"}}`))
+ assert.Equal(t, "true", testTemplate(t, g, `{{has ("foo:\n bar: true" | yaml) "foo"}}`))
+ assert.Equal(t, "true", testTemplate(t, g, `{{has ("foo:\n bar: true" | yaml).foo "bar"}}`))
+ assert.Equal(t, "false", testTemplate(t, g, `{{has ("foo: true" | yaml) "bah"}}`))
tmpl := `{{- $data := yaml "foo: bar\nbaz: qux\n" }}
{{- if (has $data "baz") }}
{{- $data.baz }}
{{- end }}`
- assert.Equal(t, "qux", testTemplate(g, tmpl))
+ assert.Equal(t, "qux", testTemplate(t, g, tmpl))
tmpl = `{{- $data := yaml "foo: bar\nbaz: qux\n" }}
{{- if (has $data "quux") }}
{{- $data.quux }}
{{- else }}
{{- $data.foo }}
{{- end }}`
- assert.Equal(t, "bar", testTemplate(g, tmpl))
+ assert.Equal(t, "bar", testTemplate(t, g, tmpl))
}
func TestCustomDelim(t *testing.T) {
@@ -151,7 +152,7 @@ func TestCustomDelim(t *testing.T) {
rightDelim: "]",
funcMap: template.FuncMap{},
}
- assert.Equal(t, "hi", testTemplate(g, `[print "hi"]`))
+ assert.Equal(t, "hi", testTemplate(t, g, `[print "hi"]`))
}
func TestRunTemplates(t *testing.T) {
diff --git a/internal/tests/integration/config_test.go b/internal/tests/integration/config_test.go
index 7f31bfb6..f30d263a 100644
--- a/internal/tests/integration/config_test.go
+++ b/internal/tests/integration/config_test.go
@@ -20,18 +20,18 @@ func setupConfigTest(t *testing.T) *fs.Dir {
return tmpDir
}
-func writeFile(dir *fs.Dir, f, content string) {
+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 {
- panic(err)
+ t.Fatal(err)
}
}
func writeConfig(t *testing.T, dir *fs.Dir, content string) {
t.Helper()
- writeFile(dir, ".gomplate.yaml", content)
+ writeFile(t, dir, ".gomplate.yaml", content)
t.Logf("writing config: %s", content)
}
@@ -62,7 +62,7 @@ func TestConfig_FlagOverridesConfig(t *testing.T) {
func TestConfig_ReadsFromInputFile(t *testing.T) {
tmpDir := setupConfigTest(t)
writeConfig(t, tmpDir, "inputFiles: [in]")
- writeFile(tmpDir, "in", "blah blah")
+ writeFile(t, tmpDir, "in", "blah blah")
o, e, err := cmd(t).withDir(tmpDir.Path()).run()
assertSuccess(t, o, e, err, "blah blah")
@@ -75,8 +75,8 @@ datasources:
data:
url: in.yaml
`)
- writeFile(tmpDir, "in", `{{ (ds "data").value }}`)
- writeFile(tmpDir, "in.yaml", `value: hello world`)
+ 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")
@@ -91,8 +91,8 @@ datasources:
data:
url: in.yaml
`)
- writeFile(tmpDir, "indir/file", `{{ (ds "data").value }}`)
- writeFile(tmpDir, "in.yaml", `value: hello world`)
+ 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, "")
@@ -130,7 +130,7 @@ outputFiles: [out]
func TestConfig_AlternateConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
- writeFile(tmpDir, "config.yaml", `in: this is from an alternate config
+ writeFile(t, tmpDir, "config.yaml", `in: this is from an alternate config
`)
o, e, err := cmd(t, "--config=config.yaml").withDir(tmpDir.Path()).run()
@@ -140,7 +140,7 @@ func TestConfig_AlternateConfigFile(t *testing.T) {
func TestConfig_EnvConfigFile(t *testing.T) {
tmpDir := setupConfigTest(t)
- writeFile(tmpDir, "envconfig.yaml", `in: yet another alternate config
+ writeFile(t, tmpDir, "envconfig.yaml", `in: yet another alternate config
`)
o, e, err := cmd(t).withDir(tmpDir.Path()).
@@ -161,8 +161,8 @@ datasources:
data:
url: in.yaml
`)
- writeFile(tmpDir, "in", `(╯°□°)╯︵ ┻━┻ (ds "data").value }}`)
- writeFile(tmpDir, "in.yaml", `value: hello world`)
+ 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()
@@ -184,8 +184,8 @@ datasources:
data:
url: in.yaml
`)
- writeFile(tmpDir, "in", `{{ (ds "data").value }}`)
- writeFile(tmpDir, "in.yaml", `value: hello world`)
+ 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()).
diff --git a/internal/tests/integration/datasources_consul_test.go b/internal/tests/integration/datasources_consul_test.go
index 130b0117..23bf1981 100644
--- a/internal/tests/integration/datasources_consul_test.go
+++ b/internal/tests/integration/datasources_consul_test.go
@@ -20,9 +20,9 @@ func setupDatasourcesConsulTest(t *testing.T) (string, *vaultClient) {
pidDir := fs.NewDir(t, "gomplate-inttests-pid")
t.Cleanup(pidDir.Remove)
- httpPort, consulAddr := freeport()
- serverPort, _ := freeport()
- serfLanPort, _ := freeport()
+ httpPort, consulAddr := freeport(t)
+ serverPort, _ := freeport(t)
+ serfLanPort, _ := freeport(t)
tmpDir := fs.NewDir(t, "gomplate-inttests",
fs.WithFile(
diff --git a/internal/tests/integration/datasources_git_test.go b/internal/tests/integration/datasources_git_test.go
index 50b8c02c..5fcce4f6 100644
--- a/internal/tests/integration/datasources_git_test.go
+++ b/internal/tests/integration/datasources_git_test.go
@@ -44,7 +44,7 @@ func startGitDaemon(t *testing.T) string {
pidDir := fs.NewDir(t, "gomplate-inttests-pid")
t.Cleanup(pidDir.Remove)
- port, addr := freeport()
+ port, addr := freeport(t)
gitDaemon := icmd.Command("git", "daemon",
"--verbose",
"--port="+strconv.Itoa(port),
diff --git a/internal/tests/integration/datasources_vault_test.go b/internal/tests/integration/datasources_vault_test.go
index 798f8733..68ecafac 100644
--- a/internal/tests/integration/datasources_vault_test.go
+++ b/internal/tests/integration/datasources_vault_test.go
@@ -58,7 +58,7 @@ func startVault(t *testing.T) (*fs.Dir, *vaultClient) {
os.Rename(tokenFile, path.Join(homeDir, ".vault-token.bak"))
}
- _, vaultAddr := freeport()
+ _, vaultAddr := freeport(t)
vault := icmd.Command("vault", "server",
"-dev",
"-dev-root-token-id="+vaultRootToken,
diff --git a/internal/tests/integration/integration_test.go b/internal/tests/integration/integration_test.go
index db68a334..7b8b7694 100644
--- a/internal/tests/integration/integration_test.go
+++ b/internal/tests/integration/integration_test.go
@@ -73,10 +73,10 @@ func typeHandler(t, body string) func(http.ResponseWriter, *http.Request) {
}
// freeport - find a free TCP port for immediate use. No guarantees!
-func freeport() (port int, addr string) {
+func freeport(t *testing.T) (port int, addr string) {
l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")})
if err != nil {
- panic(err)
+ t.Fatal(err)
}
defer l.Close()
a := l.Addr().(*net.TCPAddr)
@@ -207,13 +207,13 @@ func (c *command) runInProcess() (o, e string, err error) {
//nolint:govet
origWd, err := os.Getwd()
if err != nil {
- panic(err)
+ c.t.Fatal(err)
}
defer os.Chdir(origWd)
err = os.Chdir(c.dir)
if err != nil {
- panic(err)
+ c.t.Fatal(err)
}
}