Skip to content

Commit 52e129d

Browse files
authored
Merge pull request #45 from arduino/per1234/dead-url-check
Add check for dead URL in library.properties url field
2 parents cb91a80 + cb8557b commit 52e129d

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

Diff for: check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,21 @@ var configurations = []Type{
581581
ErrorModes: []checkmode.Type{checkmode.Default},
582582
CheckFunction: checkfunctions.LibraryPropertiesUrlFieldInvalid,
583583
},
584+
{
585+
ProjectType: projecttype.Library,
586+
Category: "library.properties",
587+
Subcategory: "url field",
588+
ID: "",
589+
Brief: "Dead URL",
590+
Description: "",
591+
MessageTemplate: "Unable to load the library.properties url field: {{.}}",
592+
DisableModes: nil,
593+
EnableModes: []checkmode.Type{checkmode.All},
594+
InfoModes: nil,
595+
WarningModes: []checkmode.Type{checkmode.All},
596+
ErrorModes: nil,
597+
CheckFunction: checkfunctions.LibraryPropertiesUrlFieldDeadLink,
598+
},
584599
{
585600
ProjectType: projecttype.Library,
586601
Category: "library.properties",

Diff for: check/checkfunctions/library.go

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package checkfunctions
1818
// The check functions for libraries.
1919

2020
import (
21+
"net/http"
2122
"strings"
2223

2324
"github.com/arduino/arduino-check/check/checkdata"
@@ -568,6 +569,30 @@ func LibraryPropertiesUrlFieldInvalid() (result checkresult.Type, output string)
568569
return checkresult.Pass, ""
569570
}
570571

572+
// LibraryPropertiesUrlFieldDeadLink checks whether the URL in the library.properties `url` field can be loaded.
573+
func LibraryPropertiesUrlFieldDeadLink() (result checkresult.Type, output string) {
574+
if checkdata.LibraryPropertiesLoadError() != nil {
575+
return checkresult.NotRun, ""
576+
}
577+
578+
url, ok := checkdata.LibraryProperties().GetOk("url")
579+
if !ok {
580+
return checkresult.NotRun, ""
581+
}
582+
583+
logrus.Tracef("Checking URL: %s", url)
584+
httpResponse, err := http.Get(url)
585+
if err != nil {
586+
return checkresult.Fail, err.Error()
587+
}
588+
589+
if httpResponse.StatusCode == http.StatusOK {
590+
return checkresult.Pass, ""
591+
}
592+
593+
return checkresult.Fail, httpResponse.Status
594+
}
595+
571596
// LibraryPropertiesDependsFieldNotInIndex checks whether the libraries listed in the library.properties `depends` field are in the Library Manager index.
572597
func LibraryPropertiesDependsFieldNotInIndex() (result checkresult.Type, output string) {
573598
if checkdata.LibraryPropertiesLoadError() != nil {

Diff for: check/checkfunctions/library_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ func TestLibraryPropertiesParagraphFieldRepeatsSentence(t *testing.T) {
124124

125125
checkCheckFunction(LibraryPropertiesParagraphFieldRepeatsSentence, testTables, t)
126126
}
127+
func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) {
128+
testTables := []checkFunctionTestTable{
129+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
130+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
131+
{"Bad URL", "BadURL", checkresult.Fail, "^Get \"http://invalid/\": dial tcp: lookup invalid:"},
132+
{"HTTP error 404", "URL404", checkresult.Fail, "^404 Not Found$"},
133+
{"Good URL", "Recursive", checkresult.Pass, ""},
134+
}
135+
136+
checkCheckFunction(LibraryPropertiesUrlFieldDeadLink, testTables, t)
137+
}
127138

128139
func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {
129140
testTables := []checkFunctionTestTable{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=BadURL
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://invalid/
9+
architectures=avr

Diff for: check/checkfunctions/testdata/libraries/BadURL/src/BadURL.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=URL404
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://httpstat.us/404
9+
architectures=avr

Diff for: check/checkfunctions/testdata/libraries/URL404/src/URL404.h

Whitespace-only changes.

0 commit comments

Comments
 (0)