Unverified Commit e404a3ba authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

Improved library search (#1896)

parent baa322f9
......@@ -17,11 +17,11 @@ package lib
import (
"context"
"strings"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
semver "go.bug.st/relaxed-semver"
......@@ -40,12 +40,37 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
res := []*rpc.SearchedLibrary{}
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
// Split on anything but 0-9, a-z or :
queryTerms := strings.FieldsFunc(strings.ToLower(req.GetQuery()), func(r rune) bool {
return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':')
})
for _, lib := range lm.Index.Libraries {
toTest := []string{lib.Name, lib.Latest.Paragraph, lib.Latest.Sentence}
if !utils.MatchAny(req.GetQuery(), toTest) {
continue
matchTerm := func(x string) bool {
if strings.Contains(strings.ToLower(lib.Name), x) ||
strings.Contains(strings.ToLower(lib.Latest.Paragraph), x) ||
strings.Contains(strings.ToLower(lib.Latest.Sentence), x) ||
strings.Contains(strings.ToLower(lib.Latest.Author), x) {
return true
}
for _, include := range lib.Latest.ProvidesIncludes {
if strings.Contains(strings.ToLower(include), x) {
return true
}
}
return false
}
match := func() bool {
for _, term := range queryTerms {
if !matchTerm(term) {
return false
}
}
return true
}
if match() {
res = append(res, indexLibraryToRPCSearchLibrary(lib))
}
res = append(res, indexLibraryToRPCSearchLibrary(lib))
}
return &rpc.LibrarySearchResponse{Libraries: res, Status: status}
......
package lib
import (
"sort"
"strings"
"testing"
......@@ -8,20 +9,17 @@ import (
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var customIndexPath = paths.New("testdata")
var customIndexPath = paths.New("testdata", "test1")
var fullIndexPath = paths.New("testdata", "full")
func TestSearchLibrary(t *testing.T) {
lm := librariesmanager.NewLibraryManager(customIndexPath, nil)
lm.LoadIndex()
req := &rpc.LibrarySearchRequest{
Instance: &rpc.Instance{Id: 1},
Query: "test",
}
resp := searchLibrary(req, lm)
resp := searchLibrary(&rpc.LibrarySearchRequest{Query: "test"}, lm)
assert := assert.New(t)
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS)
assert.Equal(len(resp.GetLibraries()), 2)
......@@ -33,12 +31,7 @@ func TestSearchLibrarySimilar(t *testing.T) {
lm := librariesmanager.NewLibraryManager(customIndexPath, nil)
lm.LoadIndex()
req := &rpc.LibrarySearchRequest{
Instance: &rpc.Instance{Id: 1},
Query: "arduino",
}
resp := searchLibrary(req, lm)
resp := searchLibrary(&rpc.LibrarySearchRequest{Query: "arduino"}, lm)
assert := assert.New(t)
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS)
assert.Equal(len(resp.GetLibraries()), 2)
......@@ -49,3 +42,38 @@ func TestSearchLibrarySimilar(t *testing.T) {
assert.Contains(libs, "ArduinoTestPackage")
assert.Contains(libs, "Arduino")
}
func TestSearchLibraryFields(t *testing.T) {
lm := librariesmanager.NewLibraryManager(fullIndexPath, nil)
lm.LoadIndex()
query := func(q string) []string {
libs := []string{}
for _, lib := range searchLibrary(&rpc.LibrarySearchRequest{Query: q}, lm).Libraries {
libs = append(libs, lib.Name)
}
sort.Strings(libs)
return libs
}
res := query("SparkFun_u-blox_GNSS")
require.Len(t, res, 3)
require.Equal(t, "SparkFun u-blox Arduino Library", res[0])
require.Equal(t, "SparkFun u-blox GNSS Arduino Library", res[1])
require.Equal(t, "SparkFun u-blox SARA-R5 Arduino Library", res[2])
res = query("SparkFun u-blox GNSS")
require.Len(t, res, 3)
require.Equal(t, "SparkFun u-blox Arduino Library", res[0])
require.Equal(t, "SparkFun u-blox GNSS Arduino Library", res[1])
require.Equal(t, "SparkFun u-blox SARA-R5 Arduino Library", res[2])
res = query("painlessMesh")
require.Len(t, res, 1)
require.Equal(t, "Painless Mesh", res[0])
res = query("cristian maglie")
require.Len(t, res, 2)
require.Equal(t, "Arduino_ConnectionHandler", res[0])
require.Equal(t, "FlashStorage_SAMD", res[1])
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment