Skip to content

Commit 887cfc2

Browse files
authored
Merge pull request #36 from arduino/per1234/name-schema-checks
Add schema checks for library.properties name field
2 parents 2ea2986 + 1f8c3a8 commit 887cfc2

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

Diff for: check/checkconfigurations/checkconfigurations.go

+105
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,51 @@ var configurations = []Type{
8686
ErrorModes: []checkmode.Type{checkmode.All},
8787
CheckFunction: checkfunctions.LibraryPropertiesNameFieldMissing,
8888
},
89+
{
90+
ProjectType: projecttype.Library,
91+
Category: "library.properties",
92+
Subcategory: "name field",
93+
ID: "",
94+
Brief: "name < min length",
95+
Description: "",
96+
MessageTemplate: "library.properties name value is less than minimum length",
97+
DisableModes: nil,
98+
EnableModes: []checkmode.Type{checkmode.All},
99+
InfoModes: nil,
100+
WarningModes: nil,
101+
ErrorModes: []checkmode.Type{checkmode.All},
102+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldLTMinLength,
103+
},
104+
{
105+
ProjectType: projecttype.Library,
106+
Category: "library.properties",
107+
Subcategory: "name field",
108+
ID: "",
109+
Brief: "name > max length",
110+
Description: "",
111+
MessageTemplate: "library.properties name value {{.}} is longer than maximum length",
112+
DisableModes: nil,
113+
EnableModes: []checkmode.Type{checkmode.All},
114+
InfoModes: nil,
115+
WarningModes: nil,
116+
ErrorModes: []checkmode.Type{checkmode.All},
117+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldGTMaxLength,
118+
},
119+
{
120+
ProjectType: projecttype.Library,
121+
Category: "library.properties",
122+
Subcategory: "name field",
123+
ID: "",
124+
Brief: "name > recommended length",
125+
Description: "",
126+
MessageTemplate: "library.properties name value {{.}} is longer than recommended max length",
127+
DisableModes: nil,
128+
EnableModes: []checkmode.Type{checkmode.All},
129+
InfoModes: nil,
130+
WarningModes: []checkmode.Type{checkmode.All},
131+
ErrorModes: nil,
132+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldGTRecommendedLength,
133+
},
89134
{
90135
ProjectType: projecttype.Library,
91136
Category: "library.properties",
@@ -101,6 +146,66 @@ var configurations = []Type{
101146
ErrorModes: []checkmode.Type{checkmode.All},
102147
CheckFunction: checkfunctions.LibraryPropertiesNameFieldDisallowedCharacters,
103148
},
149+
{
150+
ProjectType: projecttype.Library,
151+
Category: "library.properties",
152+
Subcategory: "name field",
153+
ID: "",
154+
Brief: "contains spaces",
155+
Description: "Best practices is for the name value, installation folder, and primary header filename to all match, but this is not possible with names containing spaces.",
156+
MessageTemplate: "library.properties name {{.}} contains spaces. Although supported, best practices is to not use spaces.",
157+
DisableModes: nil,
158+
EnableModes: []checkmode.Type{checkmode.All},
159+
InfoModes: nil,
160+
WarningModes: []checkmode.Type{checkmode.All},
161+
ErrorModes: nil,
162+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldHasSpaces,
163+
},
164+
{
165+
ProjectType: projecttype.Library,
166+
Category: "library.properties",
167+
Subcategory: "name field",
168+
ID: "",
169+
Brief: `starts with "Arduino"`,
170+
Description: `Case insensitive. 3rd party libraries added to Library Manager index prior to the enactment of this rule are "grandfathered"`,
171+
MessageTemplate: `Library name {{.}} starts with "Arduino". These names are reserved for official libraries.`,
172+
DisableModes: []checkmode.Type{checkmode.Official},
173+
EnableModes: []checkmode.Type{checkmode.Default},
174+
InfoModes: nil,
175+
WarningModes: []checkmode.Type{checkmode.Permissive},
176+
ErrorModes: []checkmode.Type{checkmode.Default},
177+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldStartsWithArduino,
178+
},
179+
{
180+
ProjectType: projecttype.Library,
181+
Category: "library.properties",
182+
Subcategory: "name field",
183+
ID: "",
184+
Brief: `contains "Arduino"`,
185+
Description: "Case insensitive",
186+
MessageTemplate: `Library name {{.}} contains "Arduino". This is superfluous.`,
187+
DisableModes: []checkmode.Type{checkmode.Official},
188+
EnableModes: []checkmode.Type{checkmode.All},
189+
InfoModes: nil,
190+
WarningModes: []checkmode.Type{checkmode.All},
191+
ErrorModes: nil,
192+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldContainsArduino,
193+
},
194+
{
195+
ProjectType: projecttype.Library,
196+
Category: "library.properties",
197+
Subcategory: "name field",
198+
ID: "",
199+
Brief: `contains "library"`,
200+
Description: "Case insensitive",
201+
MessageTemplate: `Library name {{.}} contains "library". This is superfluous.`,
202+
DisableModes: nil,
203+
EnableModes: []checkmode.Type{checkmode.All},
204+
InfoModes: nil,
205+
WarningModes: []checkmode.Type{checkmode.All},
206+
ErrorModes: nil,
207+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldContainsLibrary,
208+
},
104209
{
105210
ProjectType: projecttype.Library,
106211
Category: "library.properties",

Diff for: check/checkfunctions/library.go

+125
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,59 @@ func LibraryPropertiesNameFieldMissing() (result checkresult.Type, output string
4949
return checkresult.Pass, ""
5050
}
5151

52+
// LibraryPropertiesNameFieldLTMinLength checks if the library.properties "name" value is less than the minimum length.
53+
func LibraryPropertiesNameFieldLTMinLength() (result checkresult.Type, output string) {
54+
if checkdata.LibraryPropertiesLoadError() != nil {
55+
return checkresult.NotRun, ""
56+
}
57+
58+
if !checkdata.LibraryProperties().ContainsKey("name") {
59+
return checkresult.NotRun, ""
60+
}
61+
62+
if schema.PropertyLessThanMinLength("name", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
63+
return checkresult.Fail, ""
64+
}
65+
66+
return checkresult.Pass, ""
67+
}
68+
69+
// LibraryPropertiesNameFieldGTMaxLength checks if the library.properties "name" value is greater than the maximum length.
70+
func LibraryPropertiesNameFieldGTMaxLength() (result checkresult.Type, output string) {
71+
if checkdata.LibraryPropertiesLoadError() != nil {
72+
return checkresult.NotRun, ""
73+
}
74+
75+
name, ok := checkdata.LibraryProperties().GetOk("name")
76+
if !ok {
77+
return checkresult.NotRun, ""
78+
}
79+
80+
if schema.PropertyGreaterThanMaxLength("name", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
81+
return checkresult.Fail, name
82+
}
83+
84+
return checkresult.Pass, ""
85+
}
86+
87+
// LibraryPropertiesNameFieldGTRecommendedLength checks if the library.properties "name" value is greater than the recommended length.
88+
func LibraryPropertiesNameFieldGTRecommendedLength() (result checkresult.Type, output string) {
89+
if checkdata.LibraryPropertiesLoadError() != nil {
90+
return checkresult.NotRun, checkdata.LibraryProperties().Get("name")
91+
}
92+
93+
name, ok := checkdata.LibraryProperties().GetOk("name")
94+
if !ok {
95+
return checkresult.NotRun, ""
96+
}
97+
98+
if schema.PropertyGreaterThanMaxLength("name", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Strict], configuration.SchemasPath()) {
99+
return checkresult.Fail, name
100+
}
101+
102+
return checkresult.Pass, ""
103+
}
104+
52105
// LibraryPropertiesNameFieldDisallowedCharacters checks for disallowed characters in the library.properties "name" field.
53106
func LibraryPropertiesNameFieldDisallowedCharacters() (result checkresult.Type, output string) {
54107
if checkdata.LibraryPropertiesLoadError() != nil {
@@ -62,6 +115,78 @@ func LibraryPropertiesNameFieldDisallowedCharacters() (result checkresult.Type,
62115
return checkresult.Pass, ""
63116
}
64117

118+
// LibraryPropertiesNameFieldHasSpaces checks if the library.properties "name" value contains spaces.
119+
func LibraryPropertiesNameFieldHasSpaces() (result checkresult.Type, output string) {
120+
if checkdata.LibraryPropertiesLoadError() != nil {
121+
return checkresult.NotRun, ""
122+
}
123+
124+
name, ok := checkdata.LibraryProperties().GetOk("name")
125+
if !ok {
126+
return checkresult.NotRun, ""
127+
}
128+
129+
if schema.ValidationErrorMatch("^#/name$", "/patternObjects/notContainsSpaces", "", "", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Strict], configuration.SchemasPath()) {
130+
return checkresult.Fail, name
131+
}
132+
133+
return checkresult.Pass, ""
134+
}
135+
136+
// LibraryPropertiesNameFieldStartsWithArduino checks if the library.properties "name" value starts with "Arduino".
137+
func LibraryPropertiesNameFieldStartsWithArduino() (result checkresult.Type, output string) {
138+
if checkdata.LibraryPropertiesLoadError() != nil {
139+
return checkresult.NotRun, ""
140+
}
141+
142+
name, ok := checkdata.LibraryProperties().GetOk("name")
143+
if !ok {
144+
return checkresult.NotRun, ""
145+
}
146+
147+
if schema.ValidationErrorMatch("^#/name$", "/patternObjects/notStartsWithArduino", "", "", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
148+
return checkresult.Fail, name
149+
}
150+
151+
return checkresult.Pass, ""
152+
}
153+
154+
// LibraryPropertiesNameFieldContainsArduino checks if the library.properties "name" value contains "Arduino".
155+
func LibraryPropertiesNameFieldContainsArduino() (result checkresult.Type, output string) {
156+
if checkdata.LibraryPropertiesLoadError() != nil {
157+
return checkresult.NotRun, ""
158+
}
159+
160+
name, ok := checkdata.LibraryProperties().GetOk("name")
161+
if !ok {
162+
return checkresult.NotRun, ""
163+
}
164+
165+
if schema.ValidationErrorMatch("^#/name$", "/patternObjects/notContainsArduino", "", "", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Strict], configuration.SchemasPath()) {
166+
return checkresult.Fail, name
167+
}
168+
169+
return checkresult.Pass, ""
170+
}
171+
172+
// LibraryPropertiesNameFieldContainsLibrary checks if the library.properties "name" value contains "library".
173+
func LibraryPropertiesNameFieldContainsLibrary() (result checkresult.Type, output string) {
174+
if checkdata.LibraryPropertiesLoadError() != nil {
175+
return checkresult.NotRun, ""
176+
}
177+
178+
name, ok := checkdata.LibraryProperties().GetOk("name")
179+
if !ok {
180+
return checkresult.NotRun, ""
181+
}
182+
183+
if schema.ValidationErrorMatch("^#/name$", "/patternObjects/notContainsSuperfluousTerms", "", "", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Strict], configuration.SchemasPath()) {
184+
return checkresult.Fail, name
185+
}
186+
187+
return checkresult.Pass, ""
188+
}
189+
65190
// LibraryPropertiesNameFieldDuplicate checks whether there is an existing entry in the Library Manager index using the the library.properties `name` value.
66191
func LibraryPropertiesNameFieldDuplicate() (result checkresult.Type, output string) {
67192
if checkdata.LibraryPropertiesLoadError() != nil {

0 commit comments

Comments
 (0)