Skip to content

Add check for stray sketches in library #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,21 @@ var configurations = []Type{
ErrorModes: nil,
CheckFunction: checkfunctions.LibraryHasExe,
},
{
ProjectType: projecttype.Library,
Category: "structure",
Subcategory: "",
ID: "",
Brief: "stray sketch",
Description: "",
MessageTemplate: "Sketch(es) found outside examples and extras folders: {{.}}. See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-examples",
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.All},
InfoModes: nil,
WarningModes: []checkmode.Type{checkmode.Permissive},
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.LibraryHasStraySketches,
},
{
ProjectType: projecttype.Library,
Category: "structure",
Expand Down
40 changes: 40 additions & 0 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/arduino/arduino-check/check/checkresult"
"github.com/arduino/arduino-check/configuration"
"github.com/arduino/arduino-check/project/library"
"github.com/arduino/arduino-check/project/sketch"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/go-properties-orderedmap"
Expand Down Expand Up @@ -990,6 +991,45 @@ func LibraryHasExe() (result checkresult.Type, output string) {
return checkresult.Pass, ""
}

// LibraryHasStraySketches checks for sketches outside the `examples` and `extras` folders.
func LibraryHasStraySketches() (result checkresult.Type, output string) {
straySketchPaths := []string{}
if sketch.ContainsMainSketchFile(checkdata.ProjectPath()) { // Check library root.
straySketchPaths = append(straySketchPaths, checkdata.ProjectPath().String())
}

// Check subfolders.
projectPathListing, err := checkdata.ProjectPath().ReadDir()
if err != nil {
panic(err)
}
projectPathListing.FilterDirs()

for _, topLevelSubfolder := range projectPathListing {
if topLevelSubfolder.Base() == "examples" || topLevelSubfolder.Base() == "extras" {
continue // Skip valid sketch locations.
}

topLevelSubfolderRecursiveListing, err := topLevelSubfolder.ReadDirRecursive()
if err != nil {
panic(err)
}
topLevelSubfolderRecursiveListing.FilterDirs()

for _, subfolder := range topLevelSubfolderRecursiveListing {
if sketch.ContainsMainSketchFile(subfolder) {
straySketchPaths = append(straySketchPaths, subfolder.String())
}
}
}

if len(straySketchPaths) > 0 {
return checkresult.Fail, strings.Join(straySketchPaths, ", ")
}

return checkresult.Pass, ""
}

// ProhibitedCharactersInLibraryFolderName checks for prohibited characters in the library folder name.
func ProhibitedCharactersInLibraryFolderName() (result checkresult.Type, output string) {
if !validProjectPathBaseName(checkdata.ProjectPath().Base()) {
Expand Down
12 changes: 12 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ func TestLibraryHasExe(t *testing.T) {

checkLibraryCheckFunction(LibraryHasExe, testTables, t)
}

func TestLibraryHasStraySketches(t *testing.T) {
testTables := []libraryCheckFunctionTestTable{
{"Sketch in root", "SketchInRoot", checkresult.Fail, ""},
{"Sketch in subfolder", "MisspelledExamplesFolder", checkresult.Fail, ""},
{"Sketch in legit location", "ExamplesFolder", checkresult.Pass, ""},
{"No sketches", "Recursive", checkresult.Pass, ""},
}

checkLibraryCheckFunction(LibraryHasStraySketches, testTables, t)
}

func TestProhibitedCharactersInLibraryFolderName(t *testing.T) {
testTables := []libraryCheckFunctionTestTable{
{"Has prohibited characters", "Prohibited CharactersInFolderName", checkresult.Fail, ""},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void setup() {}
void loop() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=SketchInRoot
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
sentence=A library that makes coding a web server a breeze.
paragraph=Supports HTTP1.1 and you can do GET and POST.
category=Communication
url=http://example.com/
architectures=avr
Empty file.
26 changes: 26 additions & 0 deletions project/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ See: https://arduino.github.io/arduino-cli/latest/sketch-specification/
package sketch

import (
"fmt"

"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/go-paths-helper"
)
Expand All @@ -31,6 +33,30 @@ func HasMainFileValidExtension(filePath *paths.Path) bool {
return hasMainFileValidExtension
}

// ContainsMainSketchFile checks whether the provided path contains a file with valid main sketch file extension.
func ContainsMainSketchFile(searchPath *paths.Path) bool {
if searchPath.NotExist() {
panic(fmt.Sprintf("Error: provided path %s does not exist.", searchPath))
}
if searchPath.IsNotDir() {
panic(fmt.Sprintf("Error: provided path %s is not a directory.", searchPath))
}

directoryListing, err := searchPath.ReadDir()
if err != nil {
panic(err)
}

directoryListing.FilterOutDirs()
for _, potentialHeaderFile := range directoryListing {
if HasMainFileValidExtension(potentialHeaderFile) {
return true
}
}

return false
}

// HasSupportedExtension returns whether the file at the given path has any of the file extensions supported for source/header files of a sketch.
func HasSupportedExtension(filePath *paths.Path) bool {
_, hasAdditionalFileValidExtensions := globals.AdditionalFileValidExtensions[filePath.Ext()]
Expand Down
13 changes: 13 additions & 0 deletions project/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@
package sketch

import (
"os"
"testing"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
)

var testDataPath *paths.Path

func init() {
workingDirectory, _ := os.Getwd()
testDataPath = paths.New(workingDirectory, "testdata")
}

func TestHasMainFileValidExtension(t *testing.T) {
assert.True(t, HasMainFileValidExtension(paths.New("/foo/bar.ino")))
assert.False(t, HasMainFileValidExtension(paths.New("/foo/bar.h")))
}

func TestContainsMainSketchFile(t *testing.T) {
assert.True(t, ContainsMainSketchFile(testDataPath.Join("Valid")))
assert.False(t, ContainsMainSketchFile(testDataPath.Join("ContainsNoMainSketchFile")))
}

func TestHasSupportedExtension(t *testing.T) {
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.ino")))
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.h")))
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions project/sketch/testdata/Valid/Valid.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void setup() {}
void loop() {}