Skip to content

Commit 26b7c9d

Browse files
committed
Fix handling of missing lib examples folder
The `rulefunction.MissingExamples` function was previously relying on the github.com/arduino/go-paths-helper IsDirCheck() method for two things - Does examples path exist? - Is examples path a folder? The code made the unreasonable assumption that IsDirCheck() would return `false, nil` if the path did not exist, and an error only if some unexpected failure occurred. That is not how IsDirCheck() is intended to work, as is clearly described in its documentation. A bug fix to align the method's behavior with that stated in its docs caused it to correctly return an error when the path did not exist, which caused an inappropriate panic from the `rulefunction.MissingExamples` function when checking an examples folder that did not exist. There is a dedicated method for checking path existence. By calling this method before `IsDirCheck()`, the code can achieve its intended behavior of handling missing examples folders gracefully, only panicing in the event of some unexpected failure that can't be handled without risking spurious linting results.
1 parent 0d75f45 commit 26b7c9d

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

internal/rule/rulefunction/library.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -1456,17 +1456,28 @@ func LibraryHasStraySketches() (result ruleresult.Type, output string) {
14561456
func MissingExamples() (result ruleresult.Type, output string) {
14571457
for _, examplesFolderName := range library.ExamplesFolderSupportedNames() {
14581458
examplesPath := projectdata.ProjectPath().Join(examplesFolderName)
1459-
exists, err := examplesPath.IsDirCheck()
1459+
1460+
exists, err := examplesPath.ExistCheck()
14601461
if err != nil {
14611462
panic(err)
14621463
}
1463-
if exists {
1464-
directoryListing, _ := examplesPath.ReadDirRecursive()
1465-
directoryListing.FilterDirs()
1466-
for _, potentialExamplePath := range directoryListing {
1467-
if sketch.ContainsMainSketchFile(potentialExamplePath) {
1468-
return ruleresult.Pass, ""
1469-
}
1464+
if !exists {
1465+
continue
1466+
}
1467+
1468+
isDir, err := examplesPath.IsDirCheck()
1469+
if err != nil {
1470+
panic(err)
1471+
}
1472+
if !isDir {
1473+
continue
1474+
}
1475+
1476+
directoryListing, _ := examplesPath.ReadDirRecursive()
1477+
directoryListing.FilterDirs()
1478+
for _, potentialExamplePath := range directoryListing {
1479+
if sketch.ContainsMainSketchFile(potentialExamplePath) {
1480+
return ruleresult.Pass, ""
14701481
}
14711482
}
14721483
}

internal/rule/rulefunction/library_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ func TestLibraryHasStraySketches(t *testing.T) {
952952

953953
func TestMissingExamples(t *testing.T) {
954954
testTables := []libraryRuleFunctionTestTable{
955+
{"File name collision", "ExamplesFile", ruleresult.Fail, ""},
955956
{"Has examples", "ExamplesFolder", ruleresult.Pass, ""},
956957
{`Has examples (in "example" folder)`, "ExampleFolder", ruleresult.Pass, ""},
957958
{"No examples", "NoExamples", ruleresult.Fail, ""},

internal/rule/rulefunction/testdata/libraries/ExamplesFile/examples

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ExamplesFolder
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
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

internal/rule/rulefunction/testdata/libraries/ExamplesFile/src/ExamplesFolder.h

Whitespace-only changes.

0 commit comments

Comments
 (0)