forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex_test.go
141 lines (120 loc) · 5.13 KB
/
index_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package librariesindex
import (
json "encoding/json"
"fmt"
"testing"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/go-paths-helper"
easyjson "github.com/mailru/easyjson"
"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
)
func TestIndexer(t *testing.T) {
fail1, err := LoadIndex(paths.New("testdata/inexistent"))
require.Error(t, err)
require.Nil(t, fail1)
fail2, err := LoadIndex(paths.New("testdata/invalid.json"))
require.Error(t, err)
require.Nil(t, fail2)
index, err := LoadIndex(paths.New("testdata/library_index.json"))
require.NoError(t, err)
require.Equal(t, 4124, len(index.Libraries), "parsed libraries count")
alp := index.Libraries["Arduino Low Power"]
require.NotNil(t, alp)
require.Equal(t, 5, len(alp.Releases))
require.Equal(t, "Arduino Low [email protected]", alp.Latest.String())
require.Len(t, alp.Latest.Dependencies, 1)
require.Equal(t, "RTCZero", alp.Latest.Dependencies[0].GetName())
require.Equal(t, "", alp.Latest.Dependencies[0].GetConstraint().String())
require.Equal(t, "[1.0.0 1.1.0 1.2.0 1.2.1 1.2.2]", fmt.Sprintf("%v", alp.Versions()))
rtc100ref := &Reference{Name: "RTCZero", Version: semver.MustParse("1.0.0")}
require.Equal(t, "[email protected]", rtc100ref.String())
rtc100 := index.FindRelease(rtc100ref)
require.NotNil(t, rtc100)
require.Equal(t, "[email protected]", rtc100.String())
rtcLatestRef := &Reference{Name: "RTCZero"}
require.Equal(t, "RTCZero", rtcLatestRef.String())
rtcLatest := index.FindRelease(rtcLatestRef)
require.NotNil(t, rtcLatest)
require.Equal(t, "[email protected]", rtcLatest.String())
rtcInexistent := index.FindRelease(&Reference{
Name: "RTCZero",
Version: semver.MustParse("0.0.0-blah"),
})
require.Nil(t, rtcInexistent)
rtcInexistent = index.FindRelease(&Reference{
Name: "RTCZero-blah",
})
require.Nil(t, rtcInexistent)
rtc := index.FindIndexedLibrary(&libraries.Library{Name: "RTCZero"})
require.NotNil(t, rtc)
require.Equal(t, "RTCZero", rtc.Name)
rtcUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("1.0.0")})
require.NotNil(t, rtcUpdate)
require.Equal(t, "[email protected]", rtcUpdate.String())
rtcUpdateNoVersion := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: nil})
require.NotNil(t, rtcUpdateNoVersion)
require.Equal(t, "[email protected]", rtcUpdateNoVersion.String())
rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")})
require.Nil(t, rtcNoUpdate)
rtcInexistent2 := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero-blah", Version: semver.MustParse("1.0.0")})
require.Nil(t, rtcInexistent2)
resolve1 := index.ResolveDependencies(alp.Releases["1.2.1"], nil)
require.Len(t, resolve1, 2)
require.Contains(t, resolve1, alp.Releases["1.2.1"])
require.Contains(t, resolve1, rtc.Releases["1.6.0"])
oauth010 := index.FindRelease(&Reference{Name: "Arduino_OAuth", Version: semver.MustParse("0.1.0")})
require.NotNil(t, oauth010)
require.Equal(t, "[email protected]", oauth010.String())
eccx135 := index.FindRelease(&Reference{Name: "ArduinoECCX08", Version: semver.MustParse("1.3.5")})
require.NotNil(t, eccx135)
require.Equal(t, "[email protected]", eccx135.String())
bear172 := index.FindRelease(&Reference{Name: "ArduinoBearSSL", Version: semver.MustParse("1.7.2")})
require.NotNil(t, bear172)
require.Equal(t, "[email protected]", bear172.String())
http040 := index.FindRelease(&Reference{Name: "ArduinoHttpClient", Version: semver.MustParse("0.4.0")})
require.NotNil(t, http040)
require.Equal(t, "[email protected]", http040.String())
resolve2 := index.ResolveDependencies(oauth010, nil)
require.Len(t, resolve2, 4)
require.Contains(t, resolve2, oauth010)
require.Contains(t, resolve2, eccx135)
require.Contains(t, resolve2, bear172)
require.Contains(t, resolve2, http040)
}
func BenchmarkIndexParsingStdJSON(b *testing.B) {
indexFile := paths.New("testdata/library_index.json")
buff, err := indexFile.ReadFile()
require.NoError(b, err)
b.SetBytes(int64(len(buff)))
for i := 0; i < b.N; i++ {
var i indexJSON
err = json.Unmarshal(buff, &i)
require.NoError(b, err)
}
}
func BenchmarkIndexParsingEasyJSON(b *testing.B) {
indexFile := paths.New("testdata/library_index.json")
buff, err := indexFile.ReadFile()
require.NoError(b, err)
b.SetBytes(int64(len(buff)))
for i := 0; i < b.N; i++ {
var i indexJSON
err = easyjson.Unmarshal(buff, &i)
require.NoError(b, err)
}
}