|  | 
|  | 1 | +/* | 
|  | 2 | + * This file is part of arduino-cli. | 
|  | 3 | + * | 
|  | 4 | + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) | 
|  | 5 | + * | 
|  | 6 | + * This software is released under the GNU General Public License version 3, | 
|  | 7 | + * which covers the main part of arduino-cli. | 
|  | 8 | + * The terms of this license can be found at: | 
|  | 9 | + * https://www.gnu.org/licenses/gpl-3.0.en.html | 
|  | 10 | + * | 
|  | 11 | + * You can be released from the requirements of the above licenses by purchasing | 
|  | 12 | + * a commercial license. Buying such a license is mandatory if you want to modify or | 
|  | 13 | + * otherwise use the software for commercial activities involving the Arduino | 
|  | 14 | + * software without disclosing the source code of your own applications. To purchase | 
|  | 15 | + * a commercial license, send an email to license@arduino.cc. | 
|  | 16 | + */ | 
|  | 17 | + | 
|  | 18 | +package librariesindex | 
|  | 19 | + | 
|  | 20 | +import ( | 
|  | 21 | +	"fmt" | 
|  | 22 | +	"testing" | 
|  | 23 | + | 
|  | 24 | +	"github.com/arduino/arduino-cli/arduino/libraries" | 
|  | 25 | +	"github.com/arduino/go-paths-helper" | 
|  | 26 | +	"github.com/stretchr/testify/require" | 
|  | 27 | +	semver "go.bug.st/relaxed-semver" | 
|  | 28 | +) | 
|  | 29 | + | 
|  | 30 | +func TestIndexer(t *testing.T) { | 
|  | 31 | +	fail1, err := LoadIndex(paths.New("testdata/inexistent")) | 
|  | 32 | +	require.Error(t, err) | 
|  | 33 | +	require.Nil(t, fail1) | 
|  | 34 | + | 
|  | 35 | +	fail2, err := LoadIndex(paths.New("testdata/invalid.json")) | 
|  | 36 | +	require.Error(t, err) | 
|  | 37 | +	require.Nil(t, fail2) | 
|  | 38 | + | 
|  | 39 | +	index, err := LoadIndex(paths.New("testdata/library_index.json")) | 
|  | 40 | +	require.NoError(t, err) | 
|  | 41 | +	require.Equal(t, 2380, len(index.Libraries), "parsed libraries count") | 
|  | 42 | + | 
|  | 43 | +	alp := index.Libraries["Arduino Low Power"] | 
|  | 44 | +	require.NotNil(t, alp) | 
|  | 45 | +	require.Equal(t, 4, len(alp.Releases)) | 
|  | 46 | +	require.Equal(t, "Arduino Low Power@1.2.1", alp.Latest.String()) | 
|  | 47 | +	require.Len(t, alp.Latest.Dependencies, 1) | 
|  | 48 | +	require.Equal(t, "RTCZero", alp.Latest.Dependencies[0].GetName()) | 
|  | 49 | +	require.Equal(t, "", alp.Latest.Dependencies[0].GetConstraint().String()) | 
|  | 50 | +	require.Equal(t, "[1.0.0 1.1.0 1.2.0 1.2.1]", fmt.Sprintf("%v", alp.Versions())) | 
|  | 51 | + | 
|  | 52 | +	rtc100ref := &Reference{Name: "RTCZero", Version: semver.MustParse("1.0.0")} | 
|  | 53 | +	require.Equal(t, "RTCZero@1.0.0", rtc100ref.String()) | 
|  | 54 | +	rtc100 := index.FindRelease(rtc100ref) | 
|  | 55 | +	require.NotNil(t, rtc100) | 
|  | 56 | +	require.Equal(t, "RTCZero@1.0.0", rtc100.String()) | 
|  | 57 | + | 
|  | 58 | +	rtcLatestRef := &Reference{Name: "RTCZero"} | 
|  | 59 | +	require.Equal(t, "RTCZero", rtcLatestRef.String()) | 
|  | 60 | +	rtcLatest := index.FindRelease(rtcLatestRef) | 
|  | 61 | +	require.NotNil(t, rtcLatest) | 
|  | 62 | +	require.Equal(t, "RTCZero@1.6.0", rtcLatest.String()) | 
|  | 63 | + | 
|  | 64 | +	rtcInexistent := index.FindRelease(&Reference{ | 
|  | 65 | +		Name:    "RTCZero", | 
|  | 66 | +		Version: semver.MustParse("0.0.0-blah"), | 
|  | 67 | +	}) | 
|  | 68 | +	require.Nil(t, rtcInexistent) | 
|  | 69 | + | 
|  | 70 | +	rtcInexistent = index.FindRelease(&Reference{ | 
|  | 71 | +		Name: "RTCZero-blah", | 
|  | 72 | +	}) | 
|  | 73 | +	require.Nil(t, rtcInexistent) | 
|  | 74 | + | 
|  | 75 | +	rtc := index.FindIndexedLibrary(&libraries.Library{Name: "RTCZero"}) | 
|  | 76 | +	require.NotNil(t, rtc) | 
|  | 77 | +	require.Equal(t, "RTCZero", rtc.Name) | 
|  | 78 | + | 
|  | 79 | +	rtcUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("1.0.0")}) | 
|  | 80 | +	require.NotNil(t, rtcUpdate) | 
|  | 81 | +	require.Equal(t, "RTCZero@1.6.0", rtcUpdate.String()) | 
|  | 82 | + | 
|  | 83 | +	rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")}) | 
|  | 84 | +	require.Nil(t, rtcNoUpdate) | 
|  | 85 | + | 
|  | 86 | +	rtcInexistent2 := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero-blah", Version: semver.MustParse("1.0.0")}) | 
|  | 87 | +	require.Nil(t, rtcInexistent2) | 
|  | 88 | + | 
|  | 89 | +	resolve1 := index.ResolveDependencies(alp.Releases["1.2.1"]) | 
|  | 90 | +	require.Len(t, resolve1, 2) | 
|  | 91 | +	require.Contains(t, resolve1, alp.Releases["1.2.1"]) | 
|  | 92 | +	require.Contains(t, resolve1, rtc.Releases["1.6.0"]) | 
|  | 93 | + | 
|  | 94 | +	oauth010 := index.FindRelease(&Reference{Name: "Arduino_OAuth", Version: semver.MustParse("0.1.0")}) | 
|  | 95 | +	require.NotNil(t, oauth010) | 
|  | 96 | +	require.Equal(t, "Arduino_OAuth@0.1.0", oauth010.String()) | 
|  | 97 | +	eccx133 := index.FindRelease(&Reference{Name: "ArduinoECCX08", Version: semver.MustParse("1.3.3")}) | 
|  | 98 | +	require.NotNil(t, eccx133) | 
|  | 99 | +	require.Equal(t, "ArduinoECCX08@1.3.3", eccx133.String()) | 
|  | 100 | +	bear130 := index.FindRelease(&Reference{Name: "ArduinoBearSSL", Version: semver.MustParse("1.3.0")}) | 
|  | 101 | +	require.NotNil(t, bear130) | 
|  | 102 | +	require.Equal(t, "ArduinoBearSSL@1.3.0", bear130.String()) | 
|  | 103 | +	http040 := index.FindRelease(&Reference{Name: "ArduinoHttpClient", Version: semver.MustParse("0.4.0")}) | 
|  | 104 | +	require.NotNil(t, http040) | 
|  | 105 | +	require.Equal(t, "ArduinoHttpClient@0.4.0", http040.String()) | 
|  | 106 | + | 
|  | 107 | +	resolve2 := index.ResolveDependencies(oauth010) | 
|  | 108 | +	require.Len(t, resolve2, 4) | 
|  | 109 | +	require.Contains(t, resolve2, oauth010) | 
|  | 110 | +	require.Contains(t, resolve2, eccx133) | 
|  | 111 | +	require.Contains(t, resolve2, bear130) | 
|  | 112 | +	require.Contains(t, resolve2, http040) | 
|  | 113 | +} | 
0 commit comments