Skip to content

Add check for dead URL in library.properties url field #45

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
Nov 24, 2020
Merged
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
15 changes: 15 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
@@ -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",
25 changes: 25 additions & 0 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
@@ -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 {
11 changes: 11 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
@@ -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{
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=BadURL
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=URL404
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
Empty file.