diff --git a/check/check_test.go b/check/check_test.go index 9802dc4f7..106f45cd8 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -67,9 +67,9 @@ func Test_shouldRun(t *testing.T) { ProjectType: testTable.projectType, } run, err := shouldRun(checkConfiguration, project) - testTable.errorAssertion(t, err) + testTable.errorAssertion(t, err, testTable.testName) if err == nil { - testTable.shouldRunAssertion(t, run) + testTable.shouldRunAssertion(t, run, testTable.testName) } } } diff --git a/check/checkdata/schema/schema.go b/check/checkdata/schema/schema.go index b61e9a660..e665dff1a 100644 --- a/check/checkdata/schema/schema.go +++ b/check/checkdata/schema/schema.go @@ -122,13 +122,6 @@ func unmarshalJSONFile(filePath *paths.Path) interface{} { return dataInterface } -// compile compiles the parent schema and returns the resulting jsonschema.Schema object. -func compile(compiler *jsonschema.Compiler, schemaFilename string, schemasPath *paths.Path) (*jsonschema.Schema, error) { - schemaPath := schemasPath.Join(schemaFilename) - schemaURI := pathURI(schemaPath) - return compiler.Compile(schemaURI) -} - // pathURI returns the URI representation of the path argument. func pathURI(path *paths.Path) string { absolutePath, err := path.Abs() diff --git a/check/checkdata/schema/schema_test.go b/check/checkdata/schema/schema_test.go index 34cc663b0..7a49b465e 100644 --- a/check/checkdata/schema/schema_test.go +++ b/check/checkdata/schema/schema_test.go @@ -177,6 +177,10 @@ func TestValidationErrorMatch(t *testing.T) { require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult, schemasPath)) require.True(t, ValidationErrorMatch("", "", "", "", validationResult, schemasPath)) + propertiesMap.Set("property3", "bAz") + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) + require.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult, schemasPath), "Match pointer below logic inversion keyword") + propertiesMap = properties.NewFromHashmap(validMap) propertiesMap.Remove("property1") validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) diff --git a/check/checkdata/schema/testdata/referenced-schema-2.json b/check/checkdata/schema/testdata/referenced-schema-2.json index b2331900a..099968dc8 100644 --- a/check/checkdata/schema/testdata/referenced-schema-2.json +++ b/check/checkdata/schema/testdata/referenced-schema-2.json @@ -12,6 +12,15 @@ "enumObject": { "enum": ["baz"] }, + "notPatternObject": { + "not": { + "allOf": [ + { + "pattern": "[A-Z]" + } + ] + } + }, "misspelledOptionalProperties": { "propertyNames": { "not": { diff --git a/check/checkdata/schema/testdata/valid-schema-with-references.json b/check/checkdata/schema/testdata/valid-schema-with-references.json index 8d10ed421..d5628446c 100644 --- a/check/checkdata/schema/testdata/valid-schema-with-references.json +++ b/check/checkdata/schema/testdata/valid-schema-with-references.json @@ -25,6 +25,9 @@ "allOf": [ { "$ref": "referenced-schema-2.json#/definitions/enumObject" + }, + { + "$ref": "referenced-schema-2.json#/definitions/notPatternObject" } ] } diff --git a/check/checkfunctions/library_test.go b/check/checkfunctions/library_test.go index efa89fb5d..3d6c0480f 100644 --- a/check/checkfunctions/library_test.go +++ b/check/checkfunctions/library_test.go @@ -101,6 +101,15 @@ func TestRedundantLibraryProperties(t *testing.T) { checkLibraryCheckFunction(RedundantLibraryProperties, testTables, t) } +func TestLibraryPropertiesFormat(t *testing.T) { + testTables := []libraryCheckFunctionTestTable{ + {"Invalid", "InvalidLibraryProperties", checkresult.Fail, ""}, + {"Valid", "Recursive", checkresult.Pass, ""}, + } + + checkLibraryCheckFunction(LibraryPropertiesFormat, testTables, t) +} + func TestLibraryPropertiesNameFieldMissingOfficialPrefix(t *testing.T) { testTables := []libraryCheckFunctionTestTable{ {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, diff --git a/check/checkfunctions/sketch_test.go b/check/checkfunctions/sketch_test.go index 825751a6d..4ad9a025f 100644 --- a/check/checkfunctions/sketch_test.go +++ b/check/checkfunctions/sketch_test.go @@ -86,3 +86,12 @@ func TestSketchFileNameGTMaxLength(t *testing.T) { checkSketchCheckFunction(SketchFileNameGTMaxLength, testTables, t) } + +func TestPdeSketchExtension(t *testing.T) { + testTables := []sketchCheckFunctionTestTable{ + {"Has .pde", "Pde", checkresult.Fail, ""}, + {"No .pde", "Valid", checkresult.Pass, ""}, + } + + checkSketchCheckFunction(PdeSketchExtension, testTables, t) +} diff --git a/check/checkfunctions/testdata/sketches/Pde/Pde.pde b/check/checkfunctions/testdata/sketches/Pde/Pde.pde new file mode 100644 index 000000000..660bdbccf --- /dev/null +++ b/check/checkfunctions/testdata/sketches/Pde/Pde.pde @@ -0,0 +1,2 @@ +void setup() {} +void loop() {} diff --git a/configuration/checkmode/checkmode_test.go b/configuration/checkmode/checkmode_test.go new file mode 100644 index 000000000..0730c54ea --- /dev/null +++ b/configuration/checkmode/checkmode_test.go @@ -0,0 +1,54 @@ +// This file is part of arduino-check. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-check. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package checkmode + +import ( + "reflect" + "testing" + + "github.com/arduino/arduino-check/project/projecttype" + "github.com/stretchr/testify/assert" +) + +func TestMode(t *testing.T) { + defaultCheckModes := map[projecttype.Type]map[Type]bool{ + projecttype.Sketch: { + LibraryManagerSubmission: false, + LibraryManagerIndexed: false, + Official: false, + All: true, + }, + projecttype.Library: { + LibraryManagerSubmission: true, + LibraryManagerIndexed: false, + Official: false, + All: true, + }, + } + + customCheckModes := make(map[Type]bool) + + testProjectType := projecttype.Library + + mergedCheckModes := Modes(defaultCheckModes, customCheckModes, testProjectType) + + assert.True(t, reflect.DeepEqual(defaultCheckModes[testProjectType], mergedCheckModes), "Default configuration should be used when no custom configuration was set.") + + testCheckMode := Official + customCheckModes[testCheckMode] = !defaultCheckModes[testProjectType][testCheckMode] + mergedCheckModes = Modes(defaultCheckModes, customCheckModes, testProjectType) + assert.Equal(t, customCheckModes[testCheckMode], mergedCheckModes[testCheckMode], "Should be set to custom value") +} diff --git a/project/library/library_test.go b/project/library/library_test.go index e6155c998..2b1384fd4 100644 --- a/project/library/library_test.go +++ b/project/library/library_test.go @@ -26,7 +26,10 @@ import ( var testDataPath *paths.Path func init() { - workingDirectory, _ := os.Getwd() + workingDirectory, err := os.Getwd() + if err != nil { + panic(err) + } testDataPath = paths.New(workingDirectory, "testdata") } @@ -39,3 +42,13 @@ func TestContainsMetadataFile(t *testing.T) { assert.True(t, ContainsMetadataFile(testDataPath.Join("ContainsMetadataFile"))) assert.False(t, ContainsMetadataFile(testDataPath.Join("ContainsNoMetadataFile"))) } + +func TestHasHeaderFileValidExtension(t *testing.T) { + assert.True(t, HasHeaderFileValidExtension(testDataPath.Join("ContainsHeaderFile", "foo.h"))) + assert.False(t, HasHeaderFileValidExtension(testDataPath.Join("ContainsNoHeaderFile", "foo.bar"))) +} + +func TestIsMetadataFile(t *testing.T) { + assert.True(t, IsMetadataFile(testDataPath.Join("ContainsMetadataFile", "library.properties"))) + assert.False(t, IsMetadataFile(testDataPath.Join("ContainsNoMetadataFile", "foo.bar"))) +} diff --git a/project/packageindex/packageindex_test.go b/project/packageindex/packageindex_test.go new file mode 100644 index 000000000..36bf5fe89 --- /dev/null +++ b/project/packageindex/packageindex_test.go @@ -0,0 +1,48 @@ +// This file is part of arduino-check. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-check. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package packageindex + +import ( + "testing" + + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/assert" +) + +func TestHasValidExtension(t *testing.T) { + assert.True(t, HasValidExtension(paths.New("/foo", "bar.json"))) + assert.False(t, HasValidExtension(paths.New("/foo", "bar.baz"))) +} + +func TestHasValidFilename(t *testing.T) { + testTables := []struct { + testName string + filename string + officialCheckMode bool + assertion assert.BoolAssertionFunc + }{ + {"Official, primary", "package_index.json", true, assert.True}, + {"Official, secondary", "package_foo_index.json", true, assert.True}, + {"Official, invalid", "packageindex.json", true, assert.False}, + {"Unofficial, valid", "package_foo_index.json", false, assert.True}, + {"Unofficial, official", "package_index.json", false, assert.False}, + {"Unofficial, invalid", "packageindex.json", false, assert.False}, + } + + for _, testTable := range testTables { + testTable.assertion(t, HasValidFilename(paths.New("/foo", testTable.filename), testTable.officialCheckMode), testTable.testName) + } +} diff --git a/project/platform/platform_test.go b/project/platform/platform_test.go new file mode 100644 index 000000000..a6013349c --- /dev/null +++ b/project/platform/platform_test.go @@ -0,0 +1,46 @@ +// This file is part of arduino-check. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-check. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package platform + +import ( + "testing" + + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/assert" +) + +func TestIsConfigurationFile(t *testing.T) { + testTables := []struct { + filename string + assertion assert.BoolAssertionFunc + }{ + {"boards.txt", assert.True}, + {"boards.local.txt", assert.True}, + {"platform.txt", assert.True}, + {"platform.local.txt", assert.True}, + {"programmers.txt", assert.True}, + {"foo.txt", assert.False}, + } + + for _, testTable := range testTables { + testTable.assertion(t, IsConfigurationFile(paths.New("/foo", testTable.filename)), testTable.filename) + } +} + +func TestIsRequiredConfigurationFile(t *testing.T) { + assert.True(t, IsRequiredConfigurationFile(paths.New("/foo", "boards.txt"))) + assert.False(t, IsRequiredConfigurationFile(paths.New("/foo", "platform.txt"))) +} diff --git a/project/projecttype/projecttype_test.go b/project/projecttype/projecttype_test.go new file mode 100644 index 000000000..512962825 --- /dev/null +++ b/project/projecttype/projecttype_test.go @@ -0,0 +1,40 @@ +// This file is part of arduino-check. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-check. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package projecttype + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMatches(t *testing.T) { + testTables := []struct { + typeA Type + typeB Type + assertion assert.BoolAssertionFunc + }{ + {Not, Not, assert.True}, + {Not, Sketch, assert.False}, + {Not, All, assert.False}, + {Sketch, All, assert.True}, + {Sketch, Sketch, assert.True}, + } + + for _, testTable := range testTables { + testTable.assertion(t, testTable.typeA.Matches(testTable.typeB), testTable.typeA.String()+", "+testTable.typeB.String()) + } +} diff --git a/result/outputformat/outputformat_test.go b/result/outputformat/outputformat_test.go new file mode 100644 index 000000000..2d10c0c53 --- /dev/null +++ b/result/outputformat/outputformat_test.go @@ -0,0 +1,43 @@ +// This file is part of arduino-check. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-check. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package outputformat + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFromString(t *testing.T) { + testTables := []struct { + formatString string + expectedFormat Type + errorAssertion assert.ErrorAssertionFunc + }{ + {"text", Text, assert.NoError}, + {"json", JSON, assert.NoError}, + {"TEXT", Text, assert.NoError}, + {"foo", 0, assert.Error}, + } + + for _, testTable := range testTables { + outputFormat, err := FromString(testTable.formatString) + testTable.errorAssertion(t, err, testTable.formatString) + if err == nil { + assert.Equal(t, testTable.expectedFormat, outputFormat, testTable.formatString) + } + } +}