diff --git a/check/checkconfigurations/checkconfigurations.go b/check/checkconfigurations/checkconfigurations.go index 66c5779f..2f043661 100644 --- a/check/checkconfigurations/checkconfigurations.go +++ b/check/checkconfigurations/checkconfigurations.go @@ -581,6 +581,21 @@ var configurations = []Type{ ErrorModes: []checkmode.Type{checkmode.Default}, CheckFunction: checkfunctions.LibraryPropertiesUrlFieldInvalid, }, + { + ProjectType: projecttype.Library, + Category: "library.properties", + Subcategory: "url field", + ID: "", + Brief: "Dead URL", + Description: "", + MessageTemplate: "Unable to load the library.properties url field: {{.}}", + DisableModes: nil, + EnableModes: []checkmode.Type{checkmode.All}, + InfoModes: nil, + WarningModes: []checkmode.Type{checkmode.All}, + ErrorModes: nil, + CheckFunction: checkfunctions.LibraryPropertiesUrlFieldDeadLink, + }, { ProjectType: projecttype.Library, Category: "library.properties", diff --git a/check/checkfunctions/library.go b/check/checkfunctions/library.go index 1f375fbd..1b0fd5d9 100644 --- a/check/checkfunctions/library.go +++ b/check/checkfunctions/library.go @@ -18,6 +18,7 @@ package checkfunctions // The check functions for libraries. import ( + "net/http" "strings" "github.com/arduino/arduino-check/check/checkdata" @@ -568,6 +569,30 @@ func LibraryPropertiesUrlFieldInvalid() (result checkresult.Type, output string) return checkresult.Pass, "" } +// LibraryPropertiesUrlFieldDeadLink checks whether the URL in the library.properties `url` field can be loaded. +func LibraryPropertiesUrlFieldDeadLink() (result checkresult.Type, output string) { + if checkdata.LibraryPropertiesLoadError() != nil { + return checkresult.NotRun, "" + } + + url, ok := checkdata.LibraryProperties().GetOk("url") + if !ok { + return checkresult.NotRun, "" + } + + logrus.Tracef("Checking URL: %s", url) + httpResponse, err := http.Get(url) + if err != nil { + return checkresult.Fail, err.Error() + } + + if httpResponse.StatusCode == http.StatusOK { + return checkresult.Pass, "" + } + + return checkresult.Fail, httpResponse.Status +} + // 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 { diff --git a/check/checkfunctions/library_test.go b/check/checkfunctions/library_test.go index d9549f98..e8bd053a 100644 --- a/check/checkfunctions/library_test.go +++ b/check/checkfunctions/library_test.go @@ -124,6 +124,17 @@ func TestLibraryPropertiesParagraphFieldRepeatsSentence(t *testing.T) { checkCheckFunction(LibraryPropertiesParagraphFieldRepeatsSentence, testTables, t) } +func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) { + testTables := []checkFunctionTestTable{ + {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, + {"Not defined", "MissingFields", checkresult.NotRun, ""}, + {"Bad URL", "BadURL", checkresult.Fail, "^Get \"http://invalid/\": dial tcp: lookup invalid:"}, + {"HTTP error 404", "URL404", checkresult.Fail, "^404 Not Found$"}, + {"Good URL", "Recursive", checkresult.Pass, ""}, + } + + checkCheckFunction(LibraryPropertiesUrlFieldDeadLink, testTables, t) +} func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) { testTables := []checkFunctionTestTable{ diff --git a/check/checkfunctions/testdata/libraries/BadURL/library.properties b/check/checkfunctions/testdata/libraries/BadURL/library.properties new file mode 100644 index 00000000..5d62d3be --- /dev/null +++ b/check/checkfunctions/testdata/libraries/BadURL/library.properties @@ -0,0 +1,9 @@ +name=BadURL +version=1.0.0 +author=Cristian Maglie , Pippo Pluto +maintainer=Cristian Maglie +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=http://invalid/ +architectures=avr diff --git a/check/checkfunctions/testdata/libraries/BadURL/src/BadURL.h b/check/checkfunctions/testdata/libraries/BadURL/src/BadURL.h new file mode 100644 index 00000000..e69de29b diff --git a/check/checkfunctions/testdata/libraries/URL404/library.properties b/check/checkfunctions/testdata/libraries/URL404/library.properties new file mode 100644 index 00000000..2789ab18 --- /dev/null +++ b/check/checkfunctions/testdata/libraries/URL404/library.properties @@ -0,0 +1,9 @@ +name=URL404 +version=1.0.0 +author=Cristian Maglie , Pippo Pluto +maintainer=Cristian Maglie +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=http://httpstat.us/404 +architectures=avr diff --git a/check/checkfunctions/testdata/libraries/URL404/src/URL404.h b/check/checkfunctions/testdata/libraries/URL404/src/URL404.h new file mode 100644 index 00000000..e69de29b