Skip to content

Commit 97cca95

Browse files
committed
Add check for sketch.json invalid data format
This checks whether sketch.json is compliant with the expected data format.
1 parent afd8240 commit 97cca95

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

Diff for: check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,21 @@ var configurations = []Type{
12261226
ErrorModes: []checkmode.Type{checkmode.Default},
12271227
CheckFunction: checkfunctions.SketchDotJSONJSONFormat,
12281228
},
1229+
{
1230+
ProjectType: projecttype.Sketch,
1231+
Category: "metadata",
1232+
Subcategory: "sketch.json",
1233+
ID: "",
1234+
Brief: "invalid sketch.json data format",
1235+
Description: "",
1236+
MessageTemplate: "sketch.json has an invalid data format: {{.}}. See: https://arduino.github.io/arduino-cli/latest/sketch-specification/#metadata",
1237+
DisableModes: nil,
1238+
EnableModes: []checkmode.Type{checkmode.Default},
1239+
InfoModes: nil,
1240+
WarningModes: []checkmode.Type{checkmode.Permissive},
1241+
ErrorModes: []checkmode.Type{checkmode.Default},
1242+
CheckFunction: checkfunctions.SketchDotJSONFormat,
1243+
},
12291244
{
12301245
ProjectType: projecttype.Sketch,
12311246
Category: "code",

Diff for: check/checkfunctions/sketch.go

+14
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,17 @@ func SketchDotJSONJSONFormat() (result checkresult.Type, output string) {
114114

115115
return checkresult.Fail, ""
116116
}
117+
118+
// SketchDotJSONFormat checks whether the sketch.json metadata file has the required data format.
119+
func SketchDotJSONFormat() (result checkresult.Type, output string) {
120+
metadataPath := sketch.MetadataPath(checkdata.ProjectPath())
121+
if metadataPath == nil {
122+
return checkresult.NotRun, "No metadata file"
123+
}
124+
125+
if checkdata.MetadataLoadError() == nil {
126+
return checkresult.Pass, ""
127+
}
128+
129+
return checkresult.Fail, checkdata.MetadataLoadError().Error()
130+
}

Diff for: check/checkfunctions/sketch_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,14 @@ func TestSketchDotJSONJSONFormat(t *testing.T) {
105105

106106
checkSketchCheckFunction(SketchDotJSONJSONFormat, testTables, t)
107107
}
108+
109+
func TestSketchDotJSONFormat(t *testing.T) {
110+
testTables := []sketchCheckFunctionTestTable{
111+
{"No metadata file", "NoMetadataFile", checkresult.NotRun, ""},
112+
{"Valid", "ValidMetadataFile", checkresult.Pass, ""},
113+
{"Invalid JSON", "InvalidJSONMetadataFile", checkresult.Fail, ""},
114+
{"Invalid data", "InvalidDataMetadataFile", checkresult.Fail, ""},
115+
}
116+
117+
checkSketchCheckFunction(SketchDotJSONFormat, testTables, t)
118+
}
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,8 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
8+
]

0 commit comments

Comments
 (0)