|
| 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 [email protected]. |
| 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 [email protected]", 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, "[email protected]", rtc100ref. String()) |
| 54 | + rtc100 := index.FindRelease(rtc100ref) |
| 55 | + require.NotNil(t, rtc100) |
| 56 | + require. Equal( t, "[email protected]", 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, "[email protected]", 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, "[email protected]", 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 | +} |
0 commit comments