Skip to content

Commit 32b0230

Browse files
committed
Add check for dead URL in library.properties url field
1 parent 336fc8f commit 32b0230

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ var configurations = []Type{
146146
ErrorModes: []checkmode.Type{checkmode.All},
147147
CheckFunction: checkfunctions.LibraryPropertiesVersionFieldMissing,
148148
},
149+
{
150+
ProjectType: projecttype.Library,
151+
Category: "library.properties",
152+
Subcategory: "url field",
153+
ID: "",
154+
Brief: "Dead URL",
155+
Description: "",
156+
MessageTemplate: "Unable to load the library.properties url field: {{.}}",
157+
DisableModes: nil,
158+
EnableModes: []checkmode.Type{checkmode.All},
159+
InfoModes: nil,
160+
WarningModes: []checkmode.Type{checkmode.All},
161+
ErrorModes: nil,
162+
CheckFunction: checkfunctions.LibraryPropertiesUrlFieldDeadLink,
163+
},
149164
{
150165
ProjectType: projecttype.Library,
151166
Category: "library.properties",

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"
@@ -110,6 +111,30 @@ func LibraryPropertiesVersionFieldMissing() (result checkresult.Type, output str
110111
return checkresult.Pass, ""
111112
}
112113

114+
// LibraryPropertiesUrlFieldDeadLink checks whether the URL in the library.properties `url` field can be loaded.
115+
func LibraryPropertiesUrlFieldDeadLink() (result checkresult.Type, output string) {
116+
if checkdata.LibraryPropertiesLoadError() != nil {
117+
return checkresult.NotRun, ""
118+
}
119+
120+
url, ok := checkdata.LibraryProperties().GetOk("url")
121+
if !ok {
122+
return checkresult.NotRun, ""
123+
}
124+
125+
logrus.Tracef("Checking URL: %s", url)
126+
httpResponse, err := http.Get(url)
127+
if err != nil {
128+
return checkresult.Fail, err.Error()
129+
}
130+
131+
if httpResponse.StatusCode == http.StatusOK {
132+
return checkresult.Pass, ""
133+
}
134+
135+
return checkresult.Fail, httpResponse.Status
136+
}
137+
113138
// LibraryPropertiesDependsFieldNotInIndex checks whether the libraries listed in the library.properties `depends` field are in the Library Manager index.
114139
func LibraryPropertiesDependsFieldNotInIndex() (result checkresult.Type, output string) {
115140
if checkdata.LibraryPropertiesLoadError() != nil {

check/checkfunctions/library_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) {
8282
checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t)
8383
}
8484

85+
func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) {
86+
testTables := []checkFunctionTestTable{
87+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
88+
{"Bad URL", "BadURL", checkresult.Fail, "^Get \"http://invalid/\": dial tcp: lookup invalid:"},
89+
{"HTTP error 404", "URL404", checkresult.Fail, "^404 Not Found$"},
90+
{"Good URL", "Recursive", checkresult.Pass, ""},
91+
}
92+
93+
checkCheckFunction(LibraryPropertiesUrlFieldDeadLink, testTables, t)
94+
}
95+
8596
func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {
8697
testTables := []checkFunctionTestTable{
8798
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=BadURL
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
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

check/checkfunctions/testdata/libraries/BadURL/src/BadURL.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Recursive
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
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://example.com/
9+
architectures=avr
10+
includes=Recursive.h

check/checkfunctions/testdata/libraries/Recursive/src/Recursive.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 <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
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

check/checkfunctions/testdata/libraries/URL404/src/URL404.h

Whitespace-only changes.

0 commit comments

Comments
 (0)