Skip to content

Add min length rule for library.properties url field #176

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 1 commit into from
May 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@
"allOf": [
{
"type": "string"
},
{
"minLength": 1
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions internal/rule/ruleconfiguration/ruleconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,22 @@ var configurations = []Type{
ErrorModes: []rulemode.Type{rulemode.Default},
RuleFunction: rulefunction.LibraryPropertiesUrlFieldMissing,
},
{
ProjectType: projecttype.Library,
SuperprojectType: projecttype.All,
Category: "library.properties",
Subcategory: "url field",
ID: "LP056",
Brief: "url < min length",
Description: "",
MessageTemplate: "library.properties url value is less than minimum length.",
DisableModes: nil,
EnableModes: []rulemode.Type{rulemode.Default},
InfoModes: nil,
WarningModes: nil,
ErrorModes: []rulemode.Type{rulemode.Default},
RuleFunction: rulefunction.LibraryPropertiesUrlFieldLTMinLength,
},
{
ProjectType: projecttype.Library,
SuperprojectType: projecttype.All,
Expand Down
17 changes: 17 additions & 0 deletions internal/rule/rulefunction/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,23 @@ func LibraryPropertiesUrlFieldMissing() (result ruleresult.Type, output string)
return ruleresult.Pass, ""
}

// LibraryPropertiesUrlFieldLTMinLength checks if the library.properties "url" value is less than the minimum length.
func LibraryPropertiesUrlFieldLTMinLength() (result ruleresult.Type, output string) {
if projectdata.LibraryPropertiesLoadError() != nil {
return ruleresult.NotRun, "Couldn't load library.properties"
}

if !projectdata.LibraryProperties().ContainsKey("url") {
return ruleresult.NotRun, "Field not present"
}

if schema.PropertyLessThanMinLength("url", projectdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Permissive]) {
return ruleresult.Fail, ""
}

return ruleresult.Pass, ""
}

// LibraryPropertiesUrlFieldInvalid checks whether the library.properties "url" value has a valid URL format.
func LibraryPropertiesUrlFieldInvalid() (result ruleresult.Type, output string) {
if projectdata.LibraryPropertiesLoadError() != nil {
Expand Down
11 changes: 11 additions & 0 deletions internal/rule/rulefunction/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ func TestLibraryPropertiesUrlFieldMissing(t *testing.T) {
checkLibraryRuleFunction(LibraryPropertiesUrlFieldMissing, testTables, t)
}

func TestLibraryPropertiesUrlFieldLTMinLength(t *testing.T) {
testTables := []libraryRuleFunctionTestTable{
{"Invalid", "InvalidLibraryProperties", ruleresult.NotRun, ""},
{"Legacy", "Legacy", ruleresult.NotRun, ""},
{"url field too short", "UrlLTMinLength", ruleresult.Fail, ""},
{"Valid", "Recursive", ruleresult.Pass, ""},
}

checkLibraryRuleFunction(LibraryPropertiesUrlFieldLTMinLength, testTables, t)
}

func TestLibraryPropertiesUrlFieldInvalid(t *testing.T) {
testTables := []libraryRuleFunctionTestTable{
{"Invalid", "InvalidLibraryProperties", ruleresult.NotRun, ""},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=UrlLTMinLength
version=1.0.0
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
maintainer=Cristian Maglie <[email protected]>
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=
architectures=avr
includes=UrlLTMinLength.h
3 changes: 3 additions & 0 deletions internal/rule/schema/schemadata/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,9 @@ var _arduinoLibraryPropertiesDefinitionsSchemaJson = []byte(`{
"allOf": [
{
"type": "string"
},
{
"minLength": 1
}
]
}
Expand Down