Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Prevent unused flags #4182

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ func TestExecute_Flags(t *testing.T) {
}
}

func TestUserConfigAllTested(t *testing.T) {
t.Log("All settings in userConfig should be tested.")

u := reflect.TypeOf(server.UserConfig{})

for i := 0; i < u.NumField(); i++ {

userConfigKey := u.Field(i).Tag.Get("mapstructure")
t.Run(userConfigKey, func(t *testing.T) {
// By default, we expect all fields in UserConfig to have flags defined in server.go and tested here in server_test.go
// Some fields are too complicated to have flags, so are only expressible in the yaml
flagKey := u.Field(i).Tag.Get("flag")
if flagKey == "false" {
return
}
// If a setting is configured in server.UserConfig, it should be tested here. If there is no corresponding const
// for specifying the flag, that probably means one *also* needs to be added to server.go
if _, ok := testFlags[userConfigKey]; !ok {
t.Errorf("server.UserConfig has field with mapstructure %s that is either not tested or not configured as a flag. Either add it to server_test.testFlags, or remove it from server.UserConfig", userConfigKey)
}
})

}

}

func TestExecute_ConfigFile(t *testing.T) {
t.Log("Should use all the values from the config file.")
// Use yaml package to quote values that need quoting
Expand Down
2 changes: 1 addition & 1 deletion server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type UserConfig struct {
VarFileAllowlist string `mapstructure:"var-file-allowlist"`
VCSStatusName string `mapstructure:"vcs-status-name"`
DefaultTFVersion string `mapstructure:"default-tf-version"`
Webhooks []WebhookConfig `mapstructure:"webhooks"`
Webhooks []WebhookConfig `mapstructure:"webhooks" flag:"false"`
WebBasicAuth bool `mapstructure:"web-basic-auth"`
WebUsername string `mapstructure:"web-username"`
WebPassword string `mapstructure:"web-password"`
Expand Down
Loading