From e43541b05b8dfd2f7232c433b7a759411170820a Mon Sep 17 00:00:00 2001 From: per1234 Date: Mon, 23 Nov 2020 20:02:34 -0800 Subject: [PATCH] Add schema provided checks for library.properties category field --- .../checkconfigurations.go | 45 +++++++++++++++++ check/checkfunctions/library.go | 48 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/check/checkconfigurations/checkconfigurations.go b/check/checkconfigurations/checkconfigurations.go index 94c40f34..a319512d 100644 --- a/check/checkconfigurations/checkconfigurations.go +++ b/check/checkconfigurations/checkconfigurations.go @@ -506,6 +506,51 @@ var configurations = []Type{ ErrorModes: nil, CheckFunction: checkfunctions.LibraryPropertiesParagraphFieldRepeatsSentence, }, + { + ProjectType: projecttype.Library, + Category: "library.properties", + Subcategory: "category field", + ID: "", + Brief: "missing category field", + Description: `This can cause a warning and results in the default "Uncategorized" category being used.`, + MessageTemplate: "missing category field in library.properties. See https://arduino.github.io/arduino-cli/latest/library-specification/#libraryproperties-file-format", + DisableModes: nil, + EnableModes: []checkmode.Type{checkmode.All}, + InfoModes: nil, + WarningModes: []checkmode.Type{checkmode.All}, + ErrorModes: nil, + CheckFunction: checkfunctions.LibraryPropertiesCategoryFieldMissing, + }, + { + ProjectType: projecttype.Library, + Category: "library.properties", + Subcategory: "category field", + ID: "", + Brief: "invalid category value", + Description: `This can cause a warning and results in the default "Uncategorized" category being used.`, + MessageTemplate: "invalid category field value {{.}} in library.properties. See https://arduino.github.io/arduino-cli/latest/library-specification/#libraryproperties-file-format", + DisableModes: nil, + EnableModes: []checkmode.Type{checkmode.All}, + InfoModes: nil, + WarningModes: []checkmode.Type{checkmode.Permissive}, + ErrorModes: []checkmode.Type{checkmode.Default}, + CheckFunction: checkfunctions.LibraryPropertiesCategoryFieldInvalid, + }, + { + ProjectType: projecttype.Library, + Category: "library.properties", + Subcategory: "category field", + ID: "", + Brief: `"Uncategorized" category value`, + Description: "There is no good reason for using this non-specification compliant category value.", + MessageTemplate: `Use of "Uncategorized" category value in library.properties. Please use one of the supported categories listed at https://arduino.github.io/arduino-cli/latest/library-specification/#libraryproperties-file-format`, + DisableModes: nil, + EnableModes: []checkmode.Type{checkmode.All}, + InfoModes: nil, + WarningModes: []checkmode.Type{checkmode.All}, + ErrorModes: nil, + CheckFunction: checkfunctions.LibraryPropertiesCategoryFieldUncategorized, + }, { ProjectType: projecttype.Library, Category: "library.properties", diff --git a/check/checkfunctions/library.go b/check/checkfunctions/library.go index 3fd01046..15b26dcc 100644 --- a/check/checkfunctions/library.go +++ b/check/checkfunctions/library.go @@ -490,6 +490,54 @@ func LibraryPropertiesParagraphFieldRepeatsSentence() (result checkresult.Type, return checkresult.Pass, "" } +// LibraryPropertiesCategoryFieldMissing checks for missing library.properties "category" field. +func LibraryPropertiesCategoryFieldMissing() (result checkresult.Type, output string) { + if checkdata.LibraryPropertiesLoadError() != nil { + return checkresult.NotRun, "" + } + + if schema.RequiredPropertyMissing("category", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) { + return checkresult.Fail, "" + } + return checkresult.Pass, "" +} + +// LibraryPropertiesCategoryFieldInvalid checks for invalid category in the library.properties "category" field. +func LibraryPropertiesCategoryFieldInvalid() (result checkresult.Type, output string) { + if checkdata.LibraryPropertiesLoadError() != nil { + return checkresult.NotRun, "" + } + + category, ok := checkdata.LibraryProperties().GetOk("category") + if !ok { + return checkresult.NotRun, "" + } + + if schema.PropertyEnumMismatch("category", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) { + return checkresult.Fail, category + } + + return checkresult.Pass, "" +} + +// LibraryPropertiesCategoryFieldUncategorized checks whether the library.properties "category" value is "Uncategorized". +func LibraryPropertiesCategoryFieldUncategorized() (result checkresult.Type, output string) { + if checkdata.LibraryPropertiesLoadError() != nil { + return checkresult.NotRun, "" + } + + category, ok := checkdata.LibraryProperties().GetOk("category") + if !ok { + return checkresult.NotRun, "" + } + + if category == "Uncategorized" { + return checkresult.Fail, "" + } + + return checkresult.Pass, "" +} + // LibraryPropertiesDependsFieldNotInIndex checks whether the libraries listed in the library.properties `depends` field are in the Library Manager index. func LibraryPropertiesDependsFieldNotInIndex() (result checkresult.Type, output string) { if checkdata.LibraryPropertiesLoadError() != nil {