|
| 1 | +// This file is part of arduino-check. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-check. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package checkconfigurations_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-check/check" |
| 23 | + "github.com/arduino/arduino-check/check/checkconfigurations" |
| 24 | + "github.com/arduino/arduino-check/check/checklevel" |
| 25 | + "github.com/arduino/arduino-check/configuration/checkmode" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestConfigurationResolution(t *testing.T) { |
| 30 | + for _, checkConfiguration := range checkconfigurations.Configurations() { |
| 31 | + for checkMode := range checkmode.Types { |
| 32 | + enabled, err := check.IsEnabled(checkConfiguration, map[checkmode.Type]bool{checkMode: true}) |
| 33 | + assert.Nil(t, err, fmt.Sprintf("Enable configuration of check %s doesn't resolve for check mode %s", checkConfiguration.ID, checkMode)) |
| 34 | + if err == nil && enabled { |
| 35 | + _, err := checklevel.CheckLevelForCheckModes(checkConfiguration, map[checkmode.Type]bool{checkMode: true}) |
| 36 | + assert.Nil(t, err, fmt.Sprintf("Level configuration of check %s doesn't resolve for check mode %s", checkConfiguration.ID, checkMode)) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 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