-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathindexes_test.go
88 lines (75 loc) · 2.04 KB
/
indexes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pkgs_test
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/arduino/arduino-create-agent/gen/indexes"
"github.com/arduino/arduino-create-agent/v2/pkgs"
)
// TestIndexes performs a series of operations about indexes, ensuring it behaves as expected.
func TestIndexes(t *testing.T) {
// Use local file as index
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/package_index.json")
}))
defer ts.Close()
// Initialize indexes with a temp folder
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
// Create extraneous folder in temp folder
os.MkdirAll(filepath.Join(tmp, "arduino"), 0755)
service := pkgs.Indexes{
Folder: tmp,
}
ctx := context.Background()
// List indexes, they should be 0
list, err := service.List(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatalf("expected %d == %d (%s)", len(list), 0, "len(list)")
}
// Add a faulty index
_, err = service.Add(ctx, &indexes.IndexPayload{URL: ":"})
if err == nil || !strings.Contains(err.Error(), "parse :: missing protocol scheme") {
t.Fatalf("expected '%v' == '%v' (%s)", err, "parse :: missing protocol scheme", "err")
}
// Add a new index
_, err = service.Add(ctx, &indexes.IndexPayload{URL: ts.URL})
if err != nil {
t.Fatal(err)
}
// List indexes, they should be 1
list, err = service.List(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Fatalf("expected %d == %d (%s)", len(list), 1, "len(list)")
}
if list[0] != ts.URL {
t.Fatalf("expected %s == %s (%s)", list[0], "downloads.arduino.cc/packages/package_index.json", "list[0]")
}
// Remove the index
_, err = service.Remove(ctx, &indexes.IndexPayload{URL: ts.URL})
if err != nil {
t.Fatal(err)
}
// List indexes, they should be 0
list, err = service.List(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatalf("expected %d == %d (%s)", len(list), 0, "len(list)")
}
}