Skip to content

Increase test coverage #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
7 changes: 0 additions & 7 deletions check/checkdata/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions check/checkdata/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions check/checkdata/schema/testdata/referenced-schema-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
"enumObject": {
"enum": ["baz"]
},
"notPatternObject": {
"not": {
"allOf": [
{
"pattern": "[A-Z]"
}
]
}
},
"misspelledOptionalProperties": {
"propertyNames": {
"not": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"allOf": [
{
"$ref": "referenced-schema-2.json#/definitions/enumObject"
},
{
"$ref": "referenced-schema-2.json#/definitions/notPatternObject"
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""},
Expand Down
9 changes: 9 additions & 0 deletions check/checkfunctions/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions check/checkfunctions/testdata/sketches/Pde/Pde.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void setup() {}
void loop() {}
54 changes: 54 additions & 0 deletions configuration/checkmode/checkmode_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 14 additions & 1 deletion project/library/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")))
}
48 changes: 48 additions & 0 deletions project/packageindex/packageindex_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
46 changes: 46 additions & 0 deletions project/platform/platform_test.go
Original file line number Diff line number Diff line change
@@ -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")))
}
40 changes: 40 additions & 0 deletions project/projecttype/projecttype_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
43 changes: 43 additions & 0 deletions result/outputformat/outputformat_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}