tests are working now

This commit is contained in:
crapStone 2023-11-17 21:14:26 +01:00 committed by crapStone
parent 155d7c4691
commit fdbbc17cca

View File

@ -22,6 +22,22 @@ func runApp(t *testing.T, fn func(*cli.Context) error, args []string) {
assert.NoError(t, err) 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) { func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) {
runApp( runApp(
t, t,
@ -186,6 +202,7 @@ func TestMergeServerConfigShouldAddDefaultBlacklistedPathsToBlacklistedPaths(t *
} }
func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T) { func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T) {
for range []uint8{0, 1} {
runApp( runApp(
t, t,
func(ctx *cli.Context) error { func(ctx *cli.Context) error {
@ -209,8 +226,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
HttpServerEnabled: true, HttpServerEnabled: true,
MainDomain: "changed", MainDomain: "changed",
RawDomain: "changed", RawDomain: "changed",
AllowedCorsDomains: []string{"changed"}, AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...), BlacklistedPaths: fixArrayFromCtx(ctx, "blacklisted-paths", append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...)),
} }
assert.Equal(t, expectedConfig, cfg) assert.Equal(t, expectedConfig, cfg)
@ -228,6 +245,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
"--enable-http-server", "--enable-http-server",
}, },
) )
}
} }
func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgExists(t *testing.T) { func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgExists(t *testing.T) {
@ -265,6 +283,9 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
pair.callback(&expectedConfig) pair.callback(&expectedConfig)
expectedConfig.BlacklistedPaths = append(expectedConfig.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...) 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) mergeServerConfig(ctx, &cfg)
assert.Equal(t, expectedConfig, cfg) assert.Equal(t, expectedConfig, cfg)