Skip to content

Commit 2ba9047

Browse files
committed
Add check for missing library examples
1 parent d442d8b commit 2ba9047

File tree

6 files changed

+58
-0
lines changed

6 files changed

+58
-0
lines changed

check/checkconfigurations/checkconfigurations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,21 @@ var configurations = []Type{
10161016
ErrorModes: []checkmode.Type{checkmode.Default},
10171017
CheckFunction: checkfunctions.IncorrectLibrarySrcFolderNameCase,
10181018
},
1019+
{
1020+
ProjectType: projecttype.Library,
1021+
Category: "structure",
1022+
Subcategory: "",
1023+
ID: "",
1024+
Brief: "no examples",
1025+
Description: "",
1026+
MessageTemplate: "No example sketches found. Please provide examples. See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-examples",
1027+
DisableModes: nil,
1028+
EnableModes: []checkmode.Type{checkmode.Default},
1029+
InfoModes: nil,
1030+
WarningModes: []checkmode.Type{checkmode.Default},
1031+
ErrorModes: nil,
1032+
CheckFunction: checkfunctions.MissingExamples,
1033+
},
10191034
{
10201035
ProjectType: projecttype.Library,
10211036
Category: "structure",

check/checkfunctions/library.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,28 @@ func IncorrectLibrarySrcFolderNameCase() (result checkresult.Type, output string
10841084
return checkresult.Pass, ""
10851085
}
10861086

1087+
// MissingExamples checks whether the library is missing examples.
1088+
func MissingExamples() (result checkresult.Type, output string) {
1089+
for _, examplesFolderName := range library.ExamplesFolderSupportedNames() {
1090+
examplesPath := checkdata.ProjectPath().Join(examplesFolderName)
1091+
exists, err := examplesPath.IsDirCheck()
1092+
if err != nil {
1093+
panic(err)
1094+
}
1095+
if exists {
1096+
directoryListing, _ := examplesPath.ReadDirRecursive()
1097+
directoryListing.FilterDirs()
1098+
for _, potentialExamplePath := range directoryListing {
1099+
if sketch.ContainsMainSketchFile(potentialExamplePath) {
1100+
return checkresult.Pass, ""
1101+
}
1102+
}
1103+
}
1104+
}
1105+
1106+
return checkresult.Fail, ""
1107+
}
1108+
10871109
// MisspelledExamplesFolderName checks for incorrectly spelled `examples` folder name.
10881110
func MisspelledExamplesFolderName() (result checkresult.Type, output string) {
10891111
directoryListing, err := checkdata.ProjectPath().ReadDir()

check/checkfunctions/library_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ func TestIncorrectLibrarySrcFolderNameCase(t *testing.T) {
342342
checkLibraryCheckFunction(IncorrectLibrarySrcFolderNameCase, testTables, t)
343343
}
344344

345+
func TestMissingExamples(t *testing.T) {
346+
testTables := []libraryCheckFunctionTestTable{
347+
{"Has examples", "ExamplesFolder", checkresult.Pass, ""},
348+
{`Has examples (in "example" folder)`, "ExampleFolder", checkresult.Pass, ""},
349+
{"No examples", "NoExamples", checkresult.Fail, ""},
350+
}
351+
352+
checkLibraryCheckFunction(MissingExamples, testTables, t)
353+
}
354+
345355
func TestMisspelledExamplesFolderName(t *testing.T) {
346356
testTables := []libraryCheckFunctionTestTable{
347357
{"Correctly spelled", "ExamplesFolder", checkresult.Pass, ""},
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ExampleFolder
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr

check/checkfunctions/testdata/libraries/ExampleFolder/src/ExampleFolder.h

Whitespace-only changes.

0 commit comments

Comments
 (0)