Skip to content

Commit 9948ac1

Browse files
committed
Add test for incorrect check configurations
The enable/disable configuration of each check must be configured for every check mode. The level configuration of each check must be configured for every check mode the check is enabled for.
1 parent 3cfc97b commit 9948ac1

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

check/check.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func shouldRun(checkConfiguration checkconfigurations.Type, currentProject proje
7171
return false, nil
7272
}
7373

74+
return IsEnabled(checkConfiguration, configurationCheckModes)
75+
}
76+
77+
func IsEnabled(checkConfiguration checkconfigurations.Type, configurationCheckModes map[checkmode.Type]bool) (bool, error) {
7478
for _, disableMode := range checkConfiguration.DisableModes {
7579
if configurationCheckModes[disableMode] {
7680
return false, nil
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 license@arduino.cc.
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 TestConfigurations(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+
}

check/checklevel/checklevel.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const (
3939
// CheckLevel determines the check level assigned to failure of the given check under the current tool configuration.
4040
func CheckLevel(checkConfiguration checkconfigurations.Type) (Type, error) {
4141
configurationCheckModes := configuration.CheckModes(checkConfiguration.ProjectType)
42+
return CheckLevelForCheckModes(checkConfiguration, configurationCheckModes)
43+
}
44+
45+
func CheckLevelForCheckModes(checkConfiguration checkconfigurations.Type, configurationCheckModes map[checkmode.Type]bool) (Type, error) {
4246
for _, errorMode := range checkConfiguration.ErrorModes {
4347
if configurationCheckModes[errorMode] {
4448
return Error, nil

configuration/checkmode/checkmode.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ const (
3838
Default // default
3939
)
4040

41+
var empty struct{}
42+
43+
// Types provides an iterator and validator for Type.
44+
var Types = map[Type]struct{}{
45+
Strict: empty,
46+
Specification: empty,
47+
Permissive: empty,
48+
LibraryManagerSubmission: empty,
49+
LibraryManagerIndexed: empty,
50+
Official: empty,
51+
Default: empty,
52+
}
53+
4154
// ComplianceModeFromString parses the --compliance flag value and returns the corresponding check mode settings.
4255
func ComplianceModeFromString(complianceModeString string) (bool, bool, bool, error) {
4356
switch strings.ToLower(complianceModeString) {

configuration/checkmode/checkmode_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ import (
2323
"github.com/stretchr/testify/assert"
2424
)
2525

26+
func TestTypes(t *testing.T) {
27+
for key := range Types {
28+
_, valid := Types[key]
29+
assert.True(t, valid)
30+
}
31+
32+
_, valid := Types[Strict]
33+
assert.True(t, valid)
34+
_, valid = Types[42]
35+
assert.False(t, valid)
36+
}
37+
2638
func TestMode(t *testing.T) {
2739
defaultCheckModes := map[projecttype.Type]map[Type]bool{
2840
projecttype.Sketch: {

0 commit comments

Comments
 (0)