Skip to content

Commit 824d963

Browse files
committed
Add checks for incorrect Arduino.h filename case in #include directives
1 parent 027afc4 commit 824d963

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

check/checkconfigurations/checkconfigurations.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,21 @@ var configurations = []Type{
11211121
ErrorModes: []checkmode.Type{checkmode.Default},
11221122
CheckFunction: checkfunctions.RecursiveLibraryWithUtilityFolder,
11231123
},
1124+
{
1125+
ProjectType: projecttype.Library,
1126+
Category: "code",
1127+
Subcategory: "",
1128+
ID: "",
1129+
Brief: "incorrect Arduino.h case",
1130+
Description: "This causes compilation failure on filename case-sensitive OS (e.g., Linux).",
1131+
MessageTemplate: "Incorrect of Arduino.h filename case detected in #include directive: {{.}}",
1132+
DisableModes: nil,
1133+
EnableModes: []checkmode.Type{checkmode.Default},
1134+
InfoModes: nil,
1135+
WarningModes: []checkmode.Type{checkmode.Default},
1136+
ErrorModes: []checkmode.Type{checkmode.Strict},
1137+
CheckFunction: checkfunctions.IncorrectArduinoDotHFileNameCase,
1138+
},
11241139
{
11251140
ProjectType: projecttype.Sketch,
11261141
Category: "structure",
@@ -1196,6 +1211,21 @@ var configurations = []Type{
11961211
ErrorModes: []checkmode.Type{checkmode.Strict},
11971212
CheckFunction: checkfunctions.MissingLicenseFile,
11981213
},
1214+
{
1215+
ProjectType: projecttype.Sketch,
1216+
Category: "code",
1217+
Subcategory: "",
1218+
ID: "",
1219+
Brief: "incorrect Arduino.h case",
1220+
Description: "This causes compilation failure on filename case-sensitive OS (e.g., Linux).",
1221+
MessageTemplate: "Incorrect of Arduino.h filename case detected in #include directive: {{.}}",
1222+
DisableModes: nil,
1223+
EnableModes: []checkmode.Type{checkmode.Default},
1224+
InfoModes: nil,
1225+
WarningModes: []checkmode.Type{checkmode.Default},
1226+
ErrorModes: []checkmode.Type{checkmode.Strict},
1227+
CheckFunction: checkfunctions.IncorrectArduinoDotHFileNameCase,
1228+
},
11991229
{
12001230
ProjectType: projecttype.Platform,
12011231
Category: "configuration files",
@@ -1256,6 +1286,21 @@ var configurations = []Type{
12561286
ErrorModes: []checkmode.Type{checkmode.Strict},
12571287
CheckFunction: checkfunctions.MissingLicenseFile,
12581288
},
1289+
{
1290+
ProjectType: projecttype.Platform,
1291+
Category: "code",
1292+
Subcategory: "",
1293+
ID: "",
1294+
Brief: "incorrect Arduino.h case",
1295+
Description: "This causes compilation failure on filename case-sensitive OS (e.g., Linux).",
1296+
MessageTemplate: "Incorrect of Arduino.h filename case detected in #include directive: {{.}}",
1297+
DisableModes: nil,
1298+
EnableModes: []checkmode.Type{checkmode.Default},
1299+
InfoModes: nil,
1300+
WarningModes: []checkmode.Type{checkmode.Default},
1301+
ErrorModes: []checkmode.Type{checkmode.Strict},
1302+
CheckFunction: checkfunctions.IncorrectArduinoDotHFileNameCase,
1303+
},
12591304
{
12601305
ProjectType: projecttype.PackageIndex,
12611306
Category: "data",

check/checkfunctions/checkfunctions.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package checkfunctions
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"regexp"
2223
"strings"
2324

2425
"github.com/arduino/arduino-check/check/checkdata"
2526
"github.com/arduino/arduino-check/check/checkresult"
27+
"github.com/arduino/arduino-check/project/sketch"
2628
"github.com/arduino/go-paths-helper"
2729
)
2830

@@ -106,6 +108,36 @@ func MissingLicenseFile() (result checkresult.Type, output string) {
106108
return checkresult.Fail, ""
107109
}
108110

111+
// IncorrectArduinoDotHFileNameCase checks for incorrect file name case of Arduino.h in #include directives.
112+
func IncorrectArduinoDotHFileNameCase() (result checkresult.Type, output string) {
113+
incorrectCaseRegexp := regexp.MustCompile(`^\s*#\s*include\s*["<](a((?i)rduino)|(ARDUINO))\.[hH][">]`)
114+
115+
directoryListing, err := checkdata.ProjectPath().ReadDirRecursive()
116+
if err != nil {
117+
panic(err)
118+
}
119+
directoryListing.FilterOutDirs()
120+
121+
for _, file := range directoryListing {
122+
if !sketch.HasSupportedExtension(file) { // Won't catch all possible files, but good enough.
123+
continue
124+
}
125+
126+
lines, err := file.ReadFileAsLines()
127+
if err != nil {
128+
panic(err)
129+
}
130+
131+
for lineNumber, line := range lines {
132+
if incorrectCaseRegexp.MatchString(line) {
133+
return checkresult.Fail, fmt.Sprintf("%s:%v: %s", file, lineNumber+1, line)
134+
}
135+
}
136+
}
137+
138+
return checkresult.Pass, ""
139+
}
140+
109141
// pathContainsRegexpMatch checks if the provided path contains a file name matching the given regular expression.
110142
func pathContainsRegexpMatch(path *paths.Path, pathRegexp *regexp.Regexp) bool {
111143
listing, err := path.ReadDir()

check/checkfunctions/checkfunctions_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,13 @@ func TestMissingLicenseFile(t *testing.T) {
8282

8383
checkCheckFunction(MissingLicenseFile, testTables, t)
8484
}
85+
86+
func TestIncorrectArduinoDotHFileNameCase(t *testing.T) {
87+
testTables := []checkFunctionTestTable{
88+
{"Incorrect, angle brackets", "arduino.h-angle", projecttype.Sketch, projecttype.Sketch, checkresult.Fail, ""},
89+
{"Incorrect, quotes", "arduino.h-quote", projecttype.Sketch, projecttype.Sketch, checkresult.Fail, ""},
90+
{"Correct case", "Arduino.h", projecttype.Sketch, projecttype.Sketch, checkresult.Pass, ""},
91+
}
92+
93+
checkCheckFunction(IncorrectArduinoDotHFileNameCase, testTables, t)
94+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <Arduino.h>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <arduino.h>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <arduino.h>

0 commit comments

Comments
 (0)