8000 Enhanced search logic of board listall, core search and lib search by silvanocerza · Pull Request #1222 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

Enhanced search logic of board listall, core search and lib search #1222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove fuzzy logic from board listall command
  • Loading branch information
silvanocerza committed Mar 15, 2021
commit a1ca032ffcc3bfa4784315d8769b45e26715cb97
31 changes: 20 additions & 11 deletions commands/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"errors"
"strings"

"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/lithammer/fuzzysearch/fuzzy"
)

// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
Expand All @@ -36,18 +36,26 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllRe
return nil, errors.New("invalid instance")
}

searchArgs := strings.Join(req.SearchArgs, " ")
searchArgs := []string{}
for _, s := range req.SearchArgs {
searchArgs = append(searchArgs, strings.Trim(s, " "))
}

match := func(toTest []string) bool {
match := func(toTest []string) (bool, error) {
if len(searchArgs) == 0 {
return true
return true, nil
}
for _, rank := range fuzzy.RankFindNormalizedFold(searchArgs, toTest) {
if rank.Distance < maximumSearchDistance {
return true

for _, t := range toTest {
matches, err := utils.Match(t, searchArgs)
if err != nil {
return false, err
}
if matches {
return matches, nil
}
}
return false
return false, nil
}

list := &rpc.BoardListAllResp{Boards: []*rpc.BoardListItem{}}
Expand Down Expand Up @@ -90,10 +98,11 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllReq) (*rpc.BoardListAllRe
continue
}

toTest := toTest
toTest = append(toTest, strings.Split(board.Name(), " ")...)
toTest := append(toTest, board.Name())
toTest = append(toTest, board.FQBN())
if !match(toTest) {
if ok, err := match(toTest); err != nil {
return nil, err
} else if !ok {
continue
}

Expand Down
28 changes: 0 additions & 28 deletions test/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,34 +455,6 @@ def test_board_listall_with_manually_installed_platform(run_command, data_dir):
assert "Arduino SAMD (32-bits ARM Cortex-M0+) Boards" == platform["Name"]


def test_board_listall_fuzzy_search(run_command, data_dir):
assert run_command("update")

# Install from platform manager
assert run_command("core install arduino:avr@1.8.3")

# Manually installs a core in sketchbooks hardware folder
git_url = "https://github.com/arduino/ArduinoCore-samd.git"
repo_dir = Path(data_dir, "hardware", "arduino-beta-development", "samd")
assert Repo.clone_from(git_url, repo_dir, multi_options=["-b 1.8.11"])

res = run_command("board listall --format json samd")
assert res.ok
data = json.loads(res.stdout)
boards = {b["FQBN"]: b for b in data["boards"]}
assert len(boards) == 17
assert "arduino-beta-development:samd:mkr1000" in boards
assert "arduino:avr:uno" not in boards

res = run_command("board listall --format json avr")
assert res.ok
data = json.loads(res.stdout)
boards = {b["FQBN"]: b for b in data["boards"]}
assert len(boards) == 26
assert "arduino:avr:uno" in boards
assert "arduino-beta-development:samd:mkr1000" not in boards


def test_board_details(run_command):
run_command("core update-index")
# Download samd core pinned to 1.8.6
Expand Down
0