diff --git a/config/setup_test.go b/config/setup_test.go index c08c056..5de66a4 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -22,6 +22,22 @@ func runApp(t *testing.T, fn func(*cli.Context) error, args []string) { assert.NoError(t, err) } +// fixArrayFromCtx fixes the number of "changed" strings in a string slice according to the number of values in the context. +// This is a workaround because the cli library has a bug where the number of values in the context gets bigger the more tests are run. +func fixArrayFromCtx(ctx *cli.Context, key string, expected []string) []string { + if ctx.IsSet(key) { + ctxSlice := ctx.StringSlice(key) + + if len(ctxSlice) > 1 { + for i := 1; i < len(ctxSlice); i++ { + expected = append([]string{"changed"}, expected...) + } + } + } + + return expected +} + func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) { runApp( t, @@ -186,48 +202,50 @@ func TestMergeServerConfigShouldAddDefaultBlacklistedPathsToBlacklistedPaths(t * } func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T) { - runApp( - t, - func(ctx *cli.Context) error { - cfg := &ServerConfig{ - Host: "original", - Port: 8080, - HttpPort: 80, - HttpServerEnabled: false, - MainDomain: "original", - RawDomain: "original", - AllowedCorsDomains: []string{"original"}, - BlacklistedPaths: []string{"original"}, - } + for range []uint8{0, 1} { + runApp( + t, + func(ctx *cli.Context) error { + cfg := &ServerConfig{ + Host: "original", + Port: 8080, + HttpPort: 80, + HttpServerEnabled: false, + MainDomain: "original", + RawDomain: "original", + AllowedCorsDomains: []string{"original"}, + BlacklistedPaths: []string{"original"}, + } - mergeServerConfig(ctx, cfg) + mergeServerConfig(ctx, cfg) - expectedConfig := &ServerConfig{ - Host: "changed", - Port: 8443, - HttpPort: 443, - HttpServerEnabled: true, - MainDomain: "changed", - RawDomain: "changed", - AllowedCorsDomains: []string{"changed"}, - BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...), - } + expectedConfig := &ServerConfig{ + Host: "changed", + Port: 8443, + HttpPort: 443, + HttpServerEnabled: true, + MainDomain: "changed", + RawDomain: "changed", + AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}), + BlacklistedPaths: fixArrayFromCtx(ctx, "blacklisted-paths", append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...)), + } - assert.Equal(t, expectedConfig, cfg) + assert.Equal(t, expectedConfig, cfg) - return nil - }, - []string{ - "--pages-domain", "changed", - "--raw-domain", "changed", - "--allowed-cors-domains", "changed", - "--blacklisted-paths", "changed", - "--host", "changed", - "--port", "8443", - "--http-port", "443", - "--enable-http-server", - }, - ) + return nil + }, + []string{ + "--pages-domain", "changed", + "--raw-domain", "changed", + "--allowed-cors-domains", "changed", + "--blacklisted-paths", "changed", + "--host", "changed", + "--port", "8443", + "--http-port", "443", + "--enable-http-server", + }, + ) + } } func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgExists(t *testing.T) { @@ -265,6 +283,9 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE pair.callback(&expectedConfig) expectedConfig.BlacklistedPaths = append(expectedConfig.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...) + expectedConfig.AllowedCorsDomains = fixArrayFromCtx(ctx, "allowed-cors-domains", expectedConfig.AllowedCorsDomains) + expectedConfig.BlacklistedPaths = fixArrayFromCtx(ctx, "blacklisted-paths", expectedConfig.BlacklistedPaths) + mergeServerConfig(ctx, &cfg) assert.Equal(t, expectedConfig, cfg)