Skip to content

Commit 1ca97d6

Browse files
committed
Add test for check mode conflicts in check configurations
In addition to being pointless, having the same check mode in multiple companion configuration fields results in the configuration behavior being dependent on which order the fields are processed in, which may change.
1 parent cde8fb0 commit 1ca97d6

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

check/checkconfigurations/checkconfigurations_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
)
2828

29-
func TestConfigurations(t *testing.T) {
29+
func TestConfigurationResolution(t *testing.T) {
3030
for _, checkConfiguration := range checkconfigurations.Configurations() {
3131
for checkMode := range checkmode.Types {
3232
enabled, err := check.IsEnabled(checkConfiguration, map[checkmode.Type]bool{checkMode: true})
@@ -38,3 +38,30 @@ func TestConfigurations(t *testing.T) {
3838
}
3939
}
4040
}
41+
42+
func TestConfigurationCheckModeConflict(t *testing.T) {
43+
// Having the same check mode in multiple configurations results in the configuration behavior being dependent on which order the fields are processed in, which may change.
44+
for _, checkConfiguration := range checkconfigurations.Configurations() {
45+
conflict, checkMode := checkModeConflict(checkConfiguration.DisableModes, checkConfiguration.EnableModes)
46+
assert.False(t, conflict, fmt.Sprintf("Duplicated check mode %s in enable configuration of check %s", checkMode, checkConfiguration.ID))
47+
48+
conflict, checkMode = checkModeConflict(checkConfiguration.InfoModes, checkConfiguration.WarningModes, checkConfiguration.ErrorModes)
49+
assert.False(t, conflict, fmt.Sprintf("Duplicated check mode %s in level configuration of check %s", checkMode, checkConfiguration.ID))
50+
}
51+
}
52+
53+
// checkModeConflict checks whether the same check mode is present in multiple configuration fields.
54+
func checkModeConflict(configurations ...[]checkmode.Type) (bool, checkmode.Type) {
55+
modeCounter := 0
56+
checkModeMap := make(map[checkmode.Type]bool)
57+
for _, configuration := range configurations {
58+
for _, checkMode := range configuration {
59+
modeCounter += 1
60+
checkModeMap[checkMode] = true
61+
if len(checkModeMap) < modeCounter {
62+
return true, checkMode
63+
}
64+
}
65+
}
66+
return false, checkmode.Default
67+
}

0 commit comments

Comments
 (0)