@@ -3,6 +3,8 @@ package pkgs_test
33import (
44 "context"
55 "io/ioutil"
6+ "net/http"
7+ "net/http/httptest"
68 "os"
79 "strings"
810 "testing"
@@ -14,12 +16,17 @@ import (
1416// TestIndexes performs a series of operations about indexes, ensuring it behaves as expected.
1517// This test depends on the internet so it could fail unexpectedly
1618func TestIndexes (t * testing.T ) {
19+ // Use local file as index
20+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
21+ http .ServeFile (w , r , "testdata/package_index.json" )
22+ }))
23+ defer ts .Close ()
24+
1725 // Initialize indexes with a temp folder
1826 tmp , err := ioutil .TempDir ("" , "" )
1927 if err != nil {
2028 t .Fatal (err )
2129 }
22-
2330 defer os .RemoveAll (tmp )
2431
2532 service := pkgs.Indexes {
@@ -44,7 +51,7 @@ func TestIndexes(t *testing.T) {
4451 }
4552
4653 // Add a new index
47- err = service .Add (ctx , & indexes.IndexPayload {URL : "https://downloads.arduino.cc/packages/package_index.json" })
54+ err = service .Add (ctx , & indexes.IndexPayload {URL : ts . URL })
4855 if err != nil {
4956 t .Fatal (err )
5057 }
@@ -57,12 +64,12 @@ func TestIndexes(t *testing.T) {
5764 if len (list ) != 1 {
5865 t .Fatalf ("expected %d == %d (%s)" , len (list ), 1 , "len(list)" )
5966 }
60- if list [0 ] != "https://downloads.arduino.cc/packages/package_index.json" {
67+ if list [0 ] != ts . URL {
6168 t .Fatalf ("expected %s == %s (%s)" , list [0 ], "downloads.arduino.cc/packages/package_index.json" , "list[0]" )
6269 }
6370
6471 // Remove the index
65- err = service .Remove (ctx , & indexes.IndexPayload {URL : "https://downloads.arduino.cc/packages/package_index.json" })
72+ err = service .Remove (ctx , & indexes.IndexPayload {URL : ts . URL })
6673 if err != nil {
6774 t .Fatal (err )
6875 }
@@ -75,4 +82,7 @@ func TestIndexes(t *testing.T) {
7582 if len (list ) != 0 {
7683 t .Fatalf ("expected %d == %d (%s)" , len (list ), 0 , "len(list)" )
7784 }
85+
86+ t .Fail ()
87+
7888}
0 commit comments