Skip to content

Commit afd8240

Browse files
committed
Add check for sketch.json invalid JSON document
1 parent fe35fa2 commit afd8240

File tree

9 files changed

+59
-0
lines changed

9 files changed

+59
-0
lines changed

Diff for: .prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/check/checkdata/schema/testdata/invalid-schema.json
99
/check/checkdata/testdata/packageindexes/invalid-JSON/package_foo_index.json
1010
/check/checkfunctions/testdata/packageindexes/invalid-JSON/package_foo_index.json
11+
/check/checkfunctions/testdata/sketches/InvalidJSONMetadataFile/sketch.json

Diff for: check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,21 @@ var configurations = []Type{
12111211
ErrorModes: []checkmode.Type{checkmode.Strict},
12121212
CheckFunction: checkfunctions.MissingLicenseFile,
12131213
},
1214+
{
1215+
ProjectType: projecttype.Sketch,
1216+
Category: "metadata",
1217+
Subcategory: "sketch.json",
1218+
ID: "",
1219+
Brief: "invalid sketch.json JSON format",
1220+
Description: "",
1221+
MessageTemplate: "sketch.json is not a valid JSON document. See: https://arduino.github.io/arduino-cli/latest/sketch-specification/#metadata",
1222+
DisableModes: nil,
1223+
EnableModes: []checkmode.Type{checkmode.Default},
1224+
InfoModes: nil,
1225+
WarningModes: []checkmode.Type{checkmode.Permissive},
1226+
ErrorModes: []checkmode.Type{checkmode.Default},
1227+
CheckFunction: checkfunctions.SketchDotJSONJSONFormat,
1228+
},
12141229
{
12151230
ProjectType: projecttype.Sketch,
12161231
Category: "code",

Diff for: check/checkfunctions/sketch.go

+14
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,17 @@ func PdeSketchExtension() (result checkresult.Type, output string) {
100100

101101
return checkresult.Pass, ""
102102
}
103+
104+
// SketchDotJSONJSONFormat checks whether the sketch.json metadata file is a valid JSON document.
105+
func SketchDotJSONJSONFormat() (result checkresult.Type, output string) {
106+
metadataPath := sketch.MetadataPath(checkdata.ProjectPath())
107+
if metadataPath == nil {
108+
return checkresult.NotRun, "No metadata file"
109+
}
110+
111+
if isValidJSON(metadataPath) {
112+
return checkresult.Pass, ""
113+
}
114+
115+
return checkresult.Fail, ""
116+
}

Diff for: check/checkfunctions/sketch_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,13 @@ func TestPdeSketchExtension(t *testing.T) {
9595

9696
checkSketchCheckFunction(PdeSketchExtension, testTables, t)
9797
}
98+
99+
func TestSketchDotJSONJSONFormat(t *testing.T) {
100+
testTables := []sketchCheckFunctionTestTable{
101+
{"No metadata file", "NoMetadataFile", checkresult.NotRun, ""},
102+
{"Valid", "ValidMetadataFile", checkresult.Pass, ""},
103+
{"Invalid", "InvalidJSONMetadataFile", checkresult.Fail, ""},
104+
}
105+
106+
checkSketchCheckFunction(SketchDotJSONJSONFormat, testTables, t)
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cpu": {
3+
"fqbn": "arduino:avr:uno",
4+
"port": ""
5+
}
6+
}

0 commit comments

Comments
 (0)