Skip to content

Commit 997f76e

Browse files
committed
Add check for prohibited characters in sketch file names
1 parent b8d8b61 commit 997f76e

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed

check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,21 @@ var configurations = []Type{
866866
ErrorModes: []checkmode.Type{checkmode.Default},
867867
CheckFunction: checkfunctions.ProhibitedCharactersInLibraryFolderName,
868868
},
869+
{
870+
ProjectType: projecttype.Sketch,
871+
Category: "structure",
872+
Subcategory: "",
873+
ID: "",
874+
Brief: "disallowed characters in file name",
875+
Description: "",
876+
MessageTemplate: "Prohibited characters in file name(s): {{.}}. See: https://arduino.github.io/arduino-cli/latest/sketch-specification/#sketch-root-folder",
877+
DisableModes: nil,
878+
EnableModes: []checkmode.Type{checkmode.All},
879+
InfoModes: nil,
880+
WarningModes: nil,
881+
ErrorModes: []checkmode.Type{checkmode.Default},
882+
CheckFunction: checkfunctions.ProhibitedCharactersInSketchFileName,
883+
},
869884
{
870885
ProjectType: projecttype.Sketch,
871886
Category: "structure",

check/checkfunctions/sketch.go

+22
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@ import (
2222

2323
"github.com/arduino/arduino-check/check/checkdata"
2424
"github.com/arduino/arduino-check/check/checkresult"
25+
"github.com/arduino/arduino-check/project/sketch"
2526
)
2627

28+
// ProhibitedCharactersInSketchFileName checks for prohibited characters in the sketch file names.
29+
func ProhibitedCharactersInSketchFileName() (result checkresult.Type, output string) {
30+
directoryListing, _ := checkdata.ProjectPath().ReadDir()
31+
directoryListing.FilterOutDirs()
32+
33+
foundInvalidSketchFileNames := []string{}
34+
for _, potentialSketchFile := range directoryListing {
35+
if sketch.HasSupportedExtension(potentialSketchFile) {
36+
if !validProjectPathBaseName(potentialSketchFile.Base()) {
37+
foundInvalidSketchFileNames = append(foundInvalidSketchFileNames, potentialSketchFile.Base())
38+
}
39+
}
40+
}
41+
42+
if len(foundInvalidSketchFileNames) > 0 {
43+
return checkresult.Fail, strings.Join(foundInvalidSketchFileNames, ", ")
44+
}
45+
46+
return checkresult.Pass, ""
47+
}
48+
2749
// PdeSketchExtension checks for use of deprecated .pde sketch file extensions.
2850
func PdeSketchExtension() (result checkresult.Type, output string) {
2951
directoryListing, _ := checkdata.ProjectPath().ReadDir()

check/checkfunctions/sketch_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package checkfunctions
17+
18+
import (
19+
"os"
20+
"regexp"
21+
"testing"
22+
23+
"github.com/arduino/arduino-check/check/checkdata"
24+
"github.com/arduino/arduino-check/check/checkresult"
25+
"github.com/arduino/arduino-check/project"
26+
"github.com/arduino/arduino-check/project/projecttype"
27+
"github.com/arduino/go-paths-helper"
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
var sketchesTestDataPath *paths.Path
32+
33+
func init() {
34+
workingDirectory, _ := os.Getwd()
35+
sketchesTestDataPath = paths.New(workingDirectory, "testdata", "sketches")
36+
}
37+
38+
type sketchCheckFunctionTestTable struct {
39+
testName string
40+
sketchFolderName string
41+
expectedCheckResult checkresult.Type
42+
expectedOutputQuery string
43+
}
44+
45+
func checkSketchCheckFunction(checkFunction Type, testTables []sketchCheckFunctionTestTable, t *testing.T) {
46+
for _, testTable := range testTables {
47+
expectedOutputRegexp := regexp.MustCompile(testTable.expectedOutputQuery)
48+
49+
testProject := project.Type{
50+
Path: sketchesTestDataPath.Join(testTable.sketchFolderName),
51+
ProjectType: projecttype.Sketch,
52+
SuperprojectType: projecttype.Sketch,
53+
}
54+
55+
checkdata.Initialize(testProject, schemasPath)
56+
57+
result, output := checkFunction()
58+
assert.Equal(t, testTable.expectedCheckResult, result, testTable.testName)
59+
assert.True(t, expectedOutputRegexp.MatchString(output), testTable.testName)
60+
}
61+
}
62+
63+
func TestProhibitedCharactersInSketchFileName(t *testing.T) {
64+
testTables := []sketchCheckFunctionTestTable{
65+
{"Has prohibited characters", "ProhibitedCharactersInFileName", checkresult.Fail, "^Prohibited CharactersInFileName.h$"},
66+
{"No prohibited characters", "Valid", checkresult.Pass, ""},
67+
}
68+
69+
checkSketchCheckFunction(ProhibitedCharactersInSketchFileName, testTables, t)
70+
}

check/checkfunctions/testdata/sketches/ProhibitedCharactersInFileName/Prohibited CharactersInFileName.h

Whitespace-only changes.
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,2 @@
1+
void setup() {}
2+
void loop() {}

project/sketch/sketch.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ func HasMainFileValidExtension(filePath *paths.Path) bool {
3030
_, hasMainFileValidExtension := globals.MainFileValidExtensions[filePath.Ext()]
3131
return hasMainFileValidExtension
3232
}
33+
34+
// HasSupportedExtension returns whether the file at the given path has any of the file extensions supported for source/header files of a sketch.
35+
func HasSupportedExtension(filePath *paths.Path) bool {
36+
_, hasAdditionalFileValidExtensions := globals.AdditionalFileValidExtensions[filePath.Ext()]
37+
return hasAdditionalFileValidExtensions || HasMainFileValidExtension(filePath)
38+
}

project/sketch/sketch_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package sketch
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestHasMainFileValidExtension(t *testing.T) {
26+
assert.True(t, HasMainFileValidExtension(paths.New("/foo/bar.ino")))
27+
assert.False(t, HasMainFileValidExtension(paths.New("/foo/bar.h")))
28+
}
29+
30+
func TestHasSupportedExtension(t *testing.T) {
31+
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.ino")))
32+
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.h")))
33+
assert.False(t, HasSupportedExtension(paths.New("/foo/bar.baz")))
34+
}

0 commit comments

Comments
 (0)