Skip to content

Commit b60803f

Browse files
authored
Merge pull request #27 from arduino/per1234/checkconfiguration-parsing-code-tests
Add tests for check configuration parsing functions
2 parents 47f9cd1 + 1fee890 commit b60803f

File tree

4 files changed

+180
-10
lines changed

4 files changed

+180
-10
lines changed

check/check_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 check
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-check/check/checkconfigurations"
22+
"github.com/arduino/arduino-check/configuration"
23+
"github.com/arduino/arduino-check/configuration/checkmode"
24+
"github.com/arduino/arduino-check/project"
25+
"github.com/arduino/arduino-check/project/projecttype"
26+
"github.com/arduino/arduino-check/util/test"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func Test_shouldRun(t *testing.T) {
31+
testTables := []struct {
32+
testName string
33+
checkProjectType projecttype.Type
34+
projectType projecttype.Type
35+
disableModes []checkmode.Type
36+
enableModes []checkmode.Type
37+
libraryManagerSetting string
38+
permissiveSetting string
39+
shouldRunAssertion assert.BoolAssertionFunc
40+
errorAssertion assert.ErrorAssertionFunc
41+
}{
42+
{"Project type mismatch", projecttype.Library, projecttype.Sketch, []checkmode.Type{}, []checkmode.Type{}, "false", "false", assert.False, assert.NoError},
43+
{"Disable mode match", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{}, "submit", "false", assert.False, assert.NoError},
44+
{"Enable mode match", projecttype.Library, projecttype.Library, []checkmode.Type{}, []checkmode.Type{checkmode.LibraryManagerSubmission}, "submit", "false", assert.True, assert.NoError},
45+
{"Disable mode default", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.Default}, []checkmode.Type{checkmode.LibraryManagerSubmission}, "update", "false", assert.False, assert.NoError},
46+
{"Disable mode default override", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.Default}, []checkmode.Type{checkmode.LibraryManagerSubmission}, "submit", "false", assert.True, assert.NoError},
47+
{"Enable mode default", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{checkmode.Default}, "update", "false", assert.True, assert.NoError},
48+
{"Disable mode default override", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{checkmode.Default}, "submit", "false", assert.False, assert.NoError},
49+
{"Unable to resolve", projecttype.Library, projecttype.Library, []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{checkmode.LibraryManagerIndexed}, "false", "false", assert.False, assert.Error},
50+
}
51+
52+
flags := test.ConfigurationFlags()
53+
54+
for _, testTable := range testTables {
55+
flags.Set("library-manager", testTable.libraryManagerSetting)
56+
flags.Set("permissive", testTable.permissiveSetting)
57+
58+
configuration.Initialize(flags, []string{"/foo"})
59+
60+
checkConfiguration := checkconfigurations.Type{
61+
ProjectType: testTable.checkProjectType,
62+
DisableModes: testTable.disableModes,
63+
EnableModes: testTable.enableModes,
64+
}
65+
66+
project := project.Type{
67+
ProjectType: testTable.projectType,
68+
}
69+
run, err := shouldRun(checkConfiguration, project)
70+
testTable.errorAssertion(t, err)
71+
if err == nil {
72+
testTable.shouldRunAssertion(t, run)
73+
}
74+
}
75+
}

check/checklevel/checklevel_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 checklevel
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-check/check/checkconfigurations"
22+
"github.com/arduino/arduino-check/configuration"
23+
"github.com/arduino/arduino-check/configuration/checkmode"
24+
"github.com/arduino/arduino-check/util/test"
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestCheckLevel(t *testing.T) {
29+
testTables := []struct {
30+
testName string
31+
infoModes []checkmode.Type
32+
warningModes []checkmode.Type
33+
errorModes []checkmode.Type
34+
libraryManagerSetting string
35+
permissiveSetting string
36+
expectedLevel Type
37+
errorAssertion assert.ErrorAssertionFunc
38+
}{
39+
{"Error", []checkmode.Type{}, []checkmode.Type{}, []checkmode.Type{checkmode.LibraryManagerSubmission}, "submit", "false", Error, assert.NoError},
40+
{"Warning", []checkmode.Type{}, []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{}, "submit", "false", Warning, assert.NoError},
41+
{"Info", []checkmode.Type{checkmode.LibraryManagerSubmission}, []checkmode.Type{}, []checkmode.Type{}, "submit", "false", Info, assert.NoError},
42+
{"Default to Error", []checkmode.Type{}, []checkmode.Type{}, []checkmode.Type{checkmode.Default}, "submit", "false", Error, assert.NoError},
43+
{"Default to Warning", []checkmode.Type{}, []checkmode.Type{checkmode.Default}, []checkmode.Type{}, "submit", "false", Warning, assert.NoError},
44+
{"Default to Info", []checkmode.Type{checkmode.Default}, []checkmode.Type{}, []checkmode.Type{}, "submit", "false", Info, assert.NoError},
45+
{"Default override", []checkmode.Type{checkmode.Default}, []checkmode.Type{}, []checkmode.Type{checkmode.LibraryManagerSubmission}, "submit", "false", Error, assert.NoError},
46+
{"Unable to resolve", []checkmode.Type{}, []checkmode.Type{}, []checkmode.Type{}, "submit", "false", Info, assert.Error},
47+
}
48+
49+
flags := test.ConfigurationFlags()
50+
51+
for _, testTable := range testTables {
52+
flags.Set("library-manager", testTable.libraryManagerSetting)
53+
flags.Set("permissive", testTable.permissiveSetting)
54+
55+
configuration.Initialize(flags, []string{"/foo"})
56+
57+
checkConfiguration := checkconfigurations.Type{
58+
InfoModes: testTable.infoModes,
59+
WarningModes: testTable.warningModes,
60+
ErrorModes: testTable.errorModes,
61+
}
62+
63+
level, err := CheckLevel(checkConfiguration)
64+
testTable.errorAssertion(t, err)
65+
if err == nil {
66+
assert.Equal(t, testTable.expectedLevel, level)
67+
}
68+
}
69+
}

configuration/configuration_test.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ import (
2121
"github.com/arduino/arduino-check/configuration/checkmode"
2222
"github.com/arduino/arduino-check/project/projecttype"
2323
"github.com/arduino/arduino-check/result/outputformat"
24+
"github.com/arduino/arduino-check/util/test"
2425
"github.com/arduino/go-paths-helper"
25-
"github.com/spf13/pflag"
2626
"github.com/stretchr/testify/assert"
2727
)
2828

2929
func TestInitialize(t *testing.T) {
30-
flags := pflag.NewFlagSet("", pflag.ExitOnError)
31-
flags.String("format", "text", "")
32-
flags.String("library-manager", "", "")
33-
flags.String("log-format", "text", "")
34-
flags.String("log-level", "panic", "")
35-
flags.Bool("permissive", false, "")
36-
flags.String("project-type", "all", "")
37-
flags.Bool("recursive", true, "")
38-
flags.String("report-file", "", "")
30+
flags := test.ConfigurationFlags()
3931

4032
projectPaths := []string{"/foo"}
4133

util/test/test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 test provides resources for testing arduino-check.
17+
package test
18+
19+
import "github.com/spf13/pflag"
20+
21+
// ConfigurationFlags returns a set of the flags used for command line configuration of arduino-check.
22+
func ConfigurationFlags() *pflag.FlagSet {
23+
flags := pflag.NewFlagSet("", pflag.ExitOnError)
24+
flags.String("format", "text", "")
25+
flags.String("library-manager", "", "")
26+
flags.String("log-format", "text", "")
27+
flags.String("log-level", "panic", "")
28+
flags.Bool("permissive", false, "")
29+
flags.String("project-type", "all", "")
30+
flags.Bool("recursive", true, "")
31+
flags.String("report-file", "", "")
32+
33+
return flags
34+
}

0 commit comments

Comments
 (0)