From 32b2da2b5af71a66629816f33034247c319899c9 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Wed, 19 Aug 2020 20:01:54 -0400 Subject: Go 1.15 bump and other updates Signed-off-by: Dave Henderson --- cmd/gomplate/config_test.go | 132 +++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 69 deletions(-) (limited to 'cmd') diff --git a/cmd/gomplate/config_test.go b/cmd/gomplate/config_test.go index dc05dfbb..24178423 100644 --- a/cmd/gomplate/config_test.go +++ b/cmd/gomplate/config_test.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "os" "testing" "time" @@ -183,76 +184,69 @@ func TestPickConfigFile(t *testing.T) { assert.Equal(t, "config.file", cf) } -func TestApplyEnvVars_PluginTimeout(t *testing.T) { - os.Setenv("GOMPLATE_PLUGIN_TIMEOUT", "bogus") - - ctx := context.TODO() - cfg := &config.Config{} - _, err := applyEnvVars(ctx, cfg) - assert.Error(t, err) - - cfg = &config.Config{ - PluginTimeout: 2 * time.Second, - } - expected := &config.Config{ - PluginTimeout: 2 * time.Second, - } - actual, err := applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) - - os.Setenv("GOMPLATE_PLUGIN_TIMEOUT", "2s") - defer os.Unsetenv("GOMPLATE_PLUGIN_TIMEOUT") - - cfg = &config.Config{} - actual, err = applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) - - cfg = &config.Config{ - PluginTimeout: 100 * time.Millisecond, - } - expected = &config.Config{ - PluginTimeout: 100 * time.Millisecond, - } - actual, err = applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) - -} - -func TestApplyEnvVars_SuppressEmpty(t *testing.T) { - os.Setenv("GOMPLATE_SUPPRESS_EMPTY", "bogus") - defer os.Unsetenv("GOMPLATE_SUPPRESS_EMPTY") - - ctx := context.TODO() - cfg := &config.Config{} - expected := &config.Config{ - SuppressEmpty: false, - } - actual, err := applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) - - os.Setenv("GOMPLATE_SUPPRESS_EMPTY", "true") - - cfg = &config.Config{} - expected = &config.Config{ - SuppressEmpty: true, +func TestApplyEnvVars(t *testing.T) { + data := []struct { + env string + value string + shouldErr bool + input, expected *config.Config + }{ + { + "GOMPLATE_PLUGIN_TIMEOUT", "bogus", + true, + &config.Config{}, nil, + }, + { + "GOMPLATE_PLUGIN_TIMEOUT", "bogus", + false, + &config.Config{PluginTimeout: 2 * time.Second}, + &config.Config{PluginTimeout: 2 * time.Second}, + }, + { + "GOMPLATE_PLUGIN_TIMEOUT", "2s", + false, + &config.Config{}, + &config.Config{PluginTimeout: 2 * time.Second}, + }, + { + "GOMPLATE_PLUGIN_TIMEOUT", "2s", + false, + &config.Config{PluginTimeout: 100 * time.Millisecond}, + &config.Config{PluginTimeout: 100 * time.Millisecond}, + }, + + { + "GOMPLATE_SUPPRESS_EMPTY", "bogus", + false, + &config.Config{}, + &config.Config{SuppressEmpty: false}, + }, + { + "GOMPLATE_SUPPRESS_EMPTY", "true", + false, + &config.Config{}, + &config.Config{SuppressEmpty: true}, + }, + { + "GOMPLATE_SUPPRESS_EMPTY", "false", + false, + &config.Config{SuppressEmpty: true}, + &config.Config{SuppressEmpty: true}, + }, } - actual, err = applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) - os.Setenv("GOMPLATE_SUPPRESS_EMPTY", "false") - - cfg = &config.Config{ - SuppressEmpty: true, - } - expected = &config.Config{ - SuppressEmpty: true, + for i, d := range data { + t.Run(fmt.Sprintf("applyEnvVars_%s_%s/%d", d.env, d.value, i), func(t *testing.T) { + os.Setenv(d.env, d.value) + + actual, err := applyEnvVars(context.Background(), d.input) + os.Unsetenv(d.env) + if d.shouldErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.EqualValues(t, d.expected, actual) + } + }) } - actual, err = applyEnvVars(ctx, cfg) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) } -- cgit v1.2.3