Skip to content

Commit b592a1f

Browse files
committed
Add check for invalid package index JSON format
1 parent e7d7808 commit b592a1f

File tree

8 files changed

+213
-0
lines changed

8 files changed

+213
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
# Test files
88
/check/checkdata/schema/testdata/invalid-schema.json
99
/check/checkdata/testdata/packageindexes/invalid-JSON/package_foo_index.json
10+
/check/checkfunctions/testdata/packageindexes/invalid-JSON/package_foo_index.json

check/checkconfigurations/checkconfigurations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,4 +1151,19 @@ var configurations = []Type{
11511151
ErrorModes: []checkmode.Type{checkmode.Strict},
11521152
CheckFunction: checkfunctions.MissingReadme,
11531153
},
1154+
{
1155+
ProjectType: projecttype.PackageIndex,
1156+
Category: "data",
1157+
Subcategory: "",
1158+
ID: "",
1159+
Brief: "Invalid JSON format",
1160+
Description: "",
1161+
MessageTemplate: "Invalid JSON format.",
1162+
DisableModes: nil,
1163+
EnableModes: []checkmode.Type{checkmode.Default},
1164+
InfoModes: nil,
1165+
WarningModes: nil,
1166+
ErrorModes: []checkmode.Type{checkmode.Default},
1167+
CheckFunction: checkfunctions.PackageIndexJSONFormat,
1168+
},
11541169
}

check/checkfunctions/checkfunctions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package checkfunctions
1818

1919
import (
20+
"encoding/json"
2021
"regexp"
2122
"strings"
2223

@@ -97,3 +98,12 @@ func pathContainsReadme(path *paths.Path, readmeRegexp *regexp.Regexp) bool {
9798

9899
return false
99100
}
101+
102+
// isValidJSON checks whether the specified file is a valid JSON document.
103+
func isValidJSON(path *paths.Path) bool {
104+
data, err := path.ReadFile()
105+
if err != nil {
106+
panic(err)
107+
}
108+
return json.Valid(data)
109+
}

check/checkfunctions/packageindex.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 checkfunctions
17+
18+
import (
19+
"github.com/arduino/arduino-check/check/checkdata"
20+
"github.com/arduino/arduino-check/check/checkresult"
21+
)
22+
23+
// The check functions for package indexes.
24+
25+
// PackageIndexJSONFormat checks whether the package index file is a valid JSON document.
26+
func PackageIndexJSONFormat() (result checkresult.Type, output string) {
27+
if isValidJSON(checkdata.ProjectPath()) {
28+
return checkresult.Pass, ""
29+
}
30+
31+
return checkresult.Fail, ""
32+
}
33+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 checkfunctions
17+
18+
import (
19+
"regexp"
20+
"testing"
21+
22+
"github.com/arduino/arduino-check/check/checkdata"
23+
"github.com/arduino/arduino-check/check/checkresult"
24+
"github.com/arduino/arduino-check/project"
25+
"github.com/arduino/arduino-check/project/projecttype"
26+
"github.com/arduino/go-paths-helper"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
var packageIndexesTestDataPath *paths.Path
31+
32+
func init() {
33+
workingDirectory, _ := paths.Getwd()
34+
packageIndexesTestDataPath = workingDirectory.Join("testdata", "packageindexes")
35+
}
36+
37+
type packageIndexCheckFunctionTestTable struct {
38+
testName string
39+
packageIndexFolderName string
40+
expectedCheckResult checkresult.Type
41+
expectedOutputQuery string
42+
}
43+
44+
func checkPackageIndexCheckFunction(checkFunction Type, testTables []packageIndexCheckFunctionTestTable, t *testing.T) {
45+
for _, testTable := range testTables {
46+
expectedOutputRegexp := regexp.MustCompile(testTable.expectedOutputQuery)
47+
48+
testProject := project.Type{
49+
Path: packageIndexesTestDataPath.Join(testTable.packageIndexFolderName),
50+
ProjectType: projecttype.PackageIndex,
51+
SuperprojectType: projecttype.PackageIndex,
52+
}
53+
54+
checkdata.Initialize(testProject, nil)
55+
56+
result, output := checkFunction()
57+
assert.Equal(t, testTable.expectedCheckResult, result, testTable.testName)
58+
assert.True(t, expectedOutputRegexp.MatchString(output), testTable.testName)
59+
}
60+
}
61+
62+
func TestPackageIndexJSONFormat(t *testing.T) {
63+
testTables := []packageIndexCheckFunctionTestTable{
64+
{"Invalid JSON", "invalid-JSON", checkresult.Fail, ""},
65+
{"Not valid package index", "invalid-package-index", checkresult.Pass, ""},
66+
{"Valid package index", "valid-package-index", checkresult.Pass, ""},
67+
}
68+
69+
checkPackageIndexCheckFunction(PackageIndexJSONFormat, testTables, t)
70+
}
71+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
8+
]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "myboard",
5+
"maintainer": "Jane Developer",
6+
"websiteURL": "https://github.com/janedeveloper/myboard",
7+
"email": "jane@example.com",
8+
"help": {
9+
"online": "http://example.com/forum/myboard"
10+
},
11+
"platforms": [
12+
{
13+
"name": "My Board",
14+
"architecture": "avr",
15+
"version": "1.0.0",
16+
"category": "Contributed",
17+
"help": {
18+
"online": "http://example.com/forum/myboard"
19+
},
20+
"url": "https://janedeveloper.github.io/myboard/myboard-1.0.0.zip",
21+
"archiveFileName": "myboard-1.0.0.zip",
22+
"checksum": "SHA-256:ec3ff8a1dc96d3ba6f432b9b837a35fd4174a34b3d2927de1d51010e8b94f9f1",
23+
"size": "15005",
24+
"boards": [{ "name": "My Board" }, { "name": "My Board Pro" }],
25+
"toolsDependencies": [
26+
{
27+
"packager": "arduino",
28+
"name": "avr-gcc",
29+
"version": "4.8.1-arduino5"
30+
},
31+
{
32+
"packager": "arduino",
33+
"name": "avrdude",
34+
"version": "6.0.1-arduino5"
35+
}
36+
]
37+
},
38+
{
39+
"name": "My Board",
40+
"architecture": "avr",
41+
"version": "1.0.1",
42+
"category": "Contributed",
43+
"help": {
44+
"online": "http://example.com/forum/myboard"
45+
},
46+
"url": "https://janedeveloper.github.io/myboard/myboard-1.0.1.zip",
47+
"archiveFileName": "myboard-1.0.1.zip",
48+
"checksum": "SHA-256:9c86ee28a7ce9fe33e8b07ec643316131e0031b0d22e63bb398902a5fdadbca9",
49+
"size": "15125",
50+
"boards": [{ "name": "My Board" }, { "name": "My Board Pro" }],
51+
"toolsDependencies": [
52+
{
53+
"packager": "arduino",
54+
"name": "avr-gcc",
55+
"version": "4.8.1-arduino5"
56+
},
57+
{
58+
"packager": "arduino",
59+
"name": "avrdude",
60+
"version": "6.0.1-arduino5"
61+
}
62+
]
63+
}
64+
],
65+
"tools": []
66+
}
67+
]
68+
}

0 commit comments

Comments
 (0)