Skip to content

Add checks for incorrect src subfolder name case #77

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 3 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
30 changes: 30 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,21 @@ var configurations = []Type{
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.LibraryFolderNameGTMaxLength,
},
{
ProjectType: projecttype.Library,
Category: "structure",
Subcategory: "",
ID: "",
Brief: "incorrect src folder case",
Description: "",
MessageTemplate: "Incorrect src folder case. This will cause the library to not be recognized on case-sensitive operating systems. See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-root-folder",
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.Default},
InfoModes: nil,
WarningModes: nil,
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.IncorrectLibrarySrcFolderNameCase,
},
{
ProjectType: projecttype.Library,
Category: "structure",
Expand Down Expand Up @@ -1046,6 +1061,21 @@ var configurations = []Type{
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.RecursiveLibraryWithUtilityFolder,
},
{
ProjectType: projecttype.Sketch,
Category: "structure",
Subcategory: "",
ID: "",
Brief: "incorrect src folder case",
Description: "",
MessageTemplate: "Incorrect src folder case. This will cause the source files under it to not be compiled on case-sensitive operating systems. See: https://arduino.github.io/arduino-cli/latest/sketch-specification/#src-subfolder",
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.Default},
InfoModes: nil,
WarningModes: []checkmode.Type{checkmode.Default},
ErrorModes: []checkmode.Type{checkmode.Strict},
CheckFunction: checkfunctions.IncorrectSketchSrcFolderNameCase,
},
{
ProjectType: projecttype.Sketch,
Category: "structure",
Expand Down
34 changes: 24 additions & 10 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,8 @@ func LibraryPropertiesMisspelledOptionalField() (result checkresult.Type, output

// LibraryInvalid checks whether the provided path is a valid library.
func LibraryInvalid() (result checkresult.Type, output string) {
directoryListing, err := checkdata.LoadedLibrary().SourceDir.ReadDir()
if err != nil {
panic(err)
}

directoryListing.FilterOutDirs()
for _, potentialHeaderFile := range directoryListing {
if library.HasHeaderFileValidExtension(potentialHeaderFile) {
return checkresult.Pass, ""
}
if library.ContainsHeaderFile(checkdata.LoadedLibrary().SourceDir) {
return checkresult.Pass, ""
}

return checkresult.Fail, ""
Expand Down Expand Up @@ -1016,6 +1008,28 @@ func LibraryFolderNameGTMaxLength() (result checkresult.Type, output string) {
return checkresult.Pass, ""
}

// IncorrectLibrarySrcFolderNameCase checks for incorrect case of src subfolder name in recursive format libraries.
func IncorrectLibrarySrcFolderNameCase() (result checkresult.Type, output string) {
if library.ContainsMetadataFile(checkdata.ProjectPath()) && library.ContainsHeaderFile(checkdata.ProjectPath()) {
// Flat layout, so no special treatment of src subfolder.
return checkresult.NotRun, ""
}

// The library is intended to have the recursive layout.
directoryListing, err := checkdata.ProjectPath().ReadDir()
if err != nil {
panic(err)
}
directoryListing.FilterDirs()

path, found := containsIncorrectPathBaseCase(directoryListing, "src")
if found {
return checkresult.Fail, path.String()
}

return checkresult.Pass, ""
}

// MisspelledExamplesFolderName checks for incorrectly spelled `examples` folder name.
func MisspelledExamplesFolderName() (result checkresult.Type, output string) {
directoryListing, err := checkdata.ProjectPath().ReadDir()
Expand Down
10 changes: 10 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ func TestLibraryFolderNameGTMaxLength(t *testing.T) {
checkLibraryCheckFunction(LibraryFolderNameGTMaxLength, testTables, t)
}

func TestIncorrectLibrarySrcFolderNameCase(t *testing.T) {
testTables := []libraryCheckFunctionTestTable{
{"Flat, not precompiled", "Flat", checkresult.NotRun, ""},
{"Incorrect case", "IncorrectSrcFolderNameCase", checkresult.Fail, ""},
{"Correct case", "Recursive", checkresult.Pass, ""},
}

checkLibraryCheckFunction(IncorrectLibrarySrcFolderNameCase, testTables, t)
}

func TestMisspelledExamplesFolderName(t *testing.T) {
testTables := []libraryCheckFunctionTestTable{
{"Correctly spelled", "ExamplesFolder", checkresult.Pass, ""},
Expand Down
16 changes: 16 additions & 0 deletions check/checkfunctions/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ import (
"github.com/arduino/arduino-check/project/sketch"
)

// IncorrectSketchSrcFolderNameCase checks for incorrect case of src subfolder name in recursive format libraries.
func IncorrectSketchSrcFolderNameCase() (result checkresult.Type, output string) {
directoryListing, err := checkdata.ProjectPath().ReadDir()
if err != nil {
panic(err)
}
directoryListing.FilterDirs()

path, found := containsIncorrectPathBaseCase(directoryListing, "src")
if found {
return checkresult.Fail, path.String()
}

return checkresult.Pass, ""
}

// ProhibitedCharactersInSketchFileName checks for prohibited characters in the sketch file names.
func ProhibitedCharactersInSketchFileName() (result checkresult.Type, output string) {
directoryListing, _ := checkdata.ProjectPath().ReadDir()
Expand Down
9 changes: 9 additions & 0 deletions check/checkfunctions/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func checkSketchCheckFunction(checkFunction Type, testTables []sketchCheckFuncti
}
}

func TestIncorrectSketchSrcFolderNameCase(t *testing.T) {
testTables := []sketchCheckFunctionTestTable{
{"Incorrect case", "IncorrectSrcFolderNameCase", checkresult.Fail, ""},
{"Correct case", "Valid", checkresult.Pass, ""},
}

checkSketchCheckFunction(IncorrectSketchSrcFolderNameCase, testTables, t)
}

func TestProhibitedCharactersInSketchFileName(t *testing.T) {
testTables := []sketchCheckFunctionTestTable{
{"Has prohibited characters", "ProhibitedCharactersInFileName", checkresult.Fail, "^Prohibited CharactersInFileName.h$"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=IncorrectSrcFolderNameCase
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
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,2 @@
void setup() {}
void loop() {}
Empty file.
50 changes: 50 additions & 0 deletions project/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package library

import (
"fmt"

"github.com/arduino/go-paths-helper"
)

Expand All @@ -35,6 +37,30 @@ func HasHeaderFileValidExtension(filePath *paths.Path) bool {
return hasHeaderFileValidExtension
}

// ContainsHeaderFile checks whether the provided path contains a file with valid header extension.
func ContainsHeaderFile(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 HasHeaderFileValidExtension(potentialHeaderFile) {
return true
}
}

return false
}

// See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-metadata
var metadataFilenames = map[string]struct{}{
"library.properties": empty,
Expand All @@ -49,6 +75,30 @@ func IsMetadataFile(filePath *paths.Path) bool {
return false
}

// ContainsMetadataFile checks whether the provided path contains an Arduino library metadata file.
func ContainsMetadataFile(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 _, potentialMetadataFile := range directoryListing {
if IsMetadataFile(potentialMetadataFile) {
return true
}
}

return false
}

// See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-examples
var examplesFolderValidNames = map[string]struct{}{
"examples": empty,
Expand Down
41 changes: 41 additions & 0 deletions project/library/library_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of arduino-check.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-check.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package library

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 TestContainsHeaderFile(t *testing.T) {
assert.True(t, ContainsHeaderFile(testDataPath.Join("ContainsHeaderFile")))
assert.False(t, ContainsHeaderFile(testDataPath.Join("ContainsNoHeaderFile")))
}

func TestContainsMetadataFile(t *testing.T) {
assert.True(t, ContainsMetadataFile(testDataPath.Join("ContainsMetadataFile")))
assert.False(t, ContainsMetadataFile(testDataPath.Join("ContainsNoMetadataFile")))
}
Empty file.
Empty file.
Empty file.
Empty file.