@@ -18,16 +18,20 @@ package rulefunction
18
18
19
19
import (
20
20
"fmt"
21
+ "net/http"
21
22
"os"
22
23
"regexp"
24
+ "strings"
23
25
"testing"
24
26
"time"
25
27
26
28
"github.com/arduino/arduino-lint/internal/project"
27
29
"github.com/arduino/arduino-lint/internal/project/projectdata"
28
30
"github.com/arduino/arduino-lint/internal/project/projecttype"
29
31
"github.com/arduino/arduino-lint/internal/rule/ruleresult"
32
+ "github.com/arduino/arduino-lint/internal/util/test"
30
33
"github.com/arduino/go-paths-helper"
34
+ "github.com/arduino/go-properties-orderedmap"
31
35
"github.com/go-git/go-git/v5"
32
36
"github.com/go-git/go-git/v5/plumbing/object"
33
37
"github.com/stretchr/testify/assert"
@@ -780,11 +784,75 @@ func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) {
780
784
{"Unable to load" , "InvalidLibraryProperties" , ruleresult .NotRun , "" },
781
785
{"Not defined" , "MissingFields" , ruleresult .NotRun , "" },
782
786
{"Bad URL" , "BadURL" , ruleresult .Fail , "^Head \" http://invalid/\" : dial tcp: lookup invalid" },
783
- {"HTTP error 404" , "URL404" , ruleresult .Fail , "^404 Not Found$" },
784
- {"Good URL" , "Recursive" , ruleresult .Pass , "" },
785
787
}
786
788
787
789
checkLibraryRuleFunction (LibraryPropertiesURLFieldDeadLink , testTables , t )
790
+
791
+ /*
792
+ In order to avoid a dependency on an external site, a test HTTP server is used for the tests covering handling of
793
+ various HTTP response status codes. For this reason, the following tests can't be performed via the
794
+ checkLibraryRuleFunction function.
795
+ */
796
+ statusTestTables := []struct {
797
+ testName string
798
+ serverStatus int
799
+ expectedRuleResult ruleresult.Type
800
+ expectedOutputQuery string
801
+ }{
802
+ {"HTTP error 404" , http .StatusNotFound , ruleresult .Fail , "^404 Not Found$" },
803
+ {"Good URL" , http .StatusOK , ruleresult .Pass , "" },
804
+ }
805
+
806
+ var propertiesMap = map [string ]string {
807
+ "name" : "WebServer" ,
808
+ "version" : "1.0.0" ,
809
+ "author" :
"Cristian Maglie <[email protected] >, Pippo Pluto <[email protected] >" ,
810
+ "maintainer" :
"Cristian Maglie <[email protected] >" ,
811
+ "sentence" : "A library that makes coding a Webserver a breeze." ,
812
+ "paragraph" : "Supports HTTP1.1 and you can do GET and POST." ,
813
+ "category" : "Communication" ,
814
+ "architectures" : "avr" ,
815
+ }
816
+
817
+ libraryProperties := properties .NewFromHashmap (propertiesMap )
818
+
819
+ for _ , testTable := range statusTestTables {
820
+ // Create an HTTP server that will return the desired status.
821
+ server := test .StatusServer (testTable .serverStatus )
822
+ defer server .Close ()
823
+
824
+ libraryProperties .Set ("url" , server .URL )
825
+ // AsSlice is the closest thing to a []byte output function provided by
826
+ // `github.com/arduino/go-properties-orderedmap`.
827
+ propertiesBytes := []byte (strings .Join (libraryProperties .AsSlice (), "\n " ))
828
+
829
+ // Generate the test data library.
830
+ sourcePath := librariesTestDataPath .Join ("TestLibraryPropertiesUrlFieldDeadLink" )
831
+ tempPath , err := paths .MkTempDir ("" , "TestLibraryPropertiesUrlFieldDeadLink" )
832
+ defer tempPath .RemoveAll () // Clean up after the test.
833
+ require .NoError (t , err )
834
+ libraryPath := tempPath .Join ("TestLibraryPropertiesUrlFieldDeadLink" )
835
+ err = sourcePath .CopyDirTo (libraryPath )
836
+ require .NoError (t , err )
837
+ err = libraryPath .Join ("library.properties" ).WriteFile (propertiesBytes )
838
+ require .NoError (t , err )
839
+
840
+ testProject := project.Type {
841
+ Path : libraryPath ,
842
+ ProjectType : projecttype .Library ,
843
+ SuperprojectType : projecttype .Library ,
844
+ }
845
+ projectdata .Initialize (testProject )
846
+
847
+ result , output := LibraryPropertiesURLFieldDeadLink ()
848
+ assert .Equal (t , testTable .expectedRuleResult , result , testTable .testName )
849
+ expectedOutputRegexp := regexp .MustCompile (testTable .expectedOutputQuery )
850
+ assert .True (
851
+ t ,
852
+ expectedOutputRegexp .MatchString (output ),
853
+ fmt .Sprintf ("%s (output: %s, assertion regex: %s)" , testTable .testName , output , testTable .expectedOutputQuery ),
854
+ )
855
+ }
788
856
}
789
857
790
858
func TestLibraryPropertiesArchitecturesFieldMissing (t * testing.T ) {
0 commit comments