Skip to content

Commit

Permalink
Prevent unused flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa committed Jan 28, 2024
1 parent a0fc4c3 commit f114b0b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -148,6 +149,13 @@ var testFlags = map[string]interface{}{
EnableDiffMarkdownFormat: false,
}

// Settings used by userConfig that do not have flags
var nonFlagSettings = []string{
"require-undiverged",
"silence-vcs-status-no-projects",
"webhooks",
}

func TestExecute_Defaults(t *testing.T) {
t.Log("Should set the defaults for all unspecified flags.")

Expand Down Expand Up @@ -211,6 +219,31 @@ func TestExecute_Flags(t *testing.T) {
}
}

func TestUserConfigNoExtra(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) {
_, ok := testFlags[userConfigKey]
if ok {
return
}
if slices.Contains(nonFlagSettings, userConfigKey) {
return
}
if !ok {
t.Errorf("server.UserConfig has field with mapstructure %s that is either not tested, or not in use in server.go", 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

0 comments on commit f114b0b

Please sign in to comment.