Skip to content

Fix core search not showing board name for 3rd party cores #556

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 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func initSearchCommand() *cobra.Command {
func runSearchCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error saerching for platforms: %v", err)
feedback.Errorf("Error searching for platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

logrus.Info("Executing `arduino core search`")

arguments := strings.ToLower(strings.Join(args, " "))
logrus.Infof("Executing `arduino core search` with args: '%s'", arguments)

resp, err := core.PlatformSearch(inst.GetId(), arguments, allVersions)
if err != nil {
feedback.Errorf("Error saerching for platforms: %v", err)
Expand Down
10 changes: 8 additions & 2 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func match(line, searchArgs string) bool {
return strings.Contains(strings.ToLower(line), searchArgs)
return strings.Contains(strings.ToLower(line), strings.ToLower(searchArgs))
}

// PlatformSearch FIXMEDOC
Expand All @@ -43,12 +43,18 @@ func PlatformSearch(instanceID int32, searchArgs string, allVersions bool) (*rpc
} else {
for _, targetPackage := range pm.Packages {
for _, platform := range targetPackage.Platforms {
// discard invalid platforms
if platform == nil || platform.Name == "" {
continue
}

// discard invalid releases
platformRelease := platform.GetLatestRelease()
if platformRelease == nil {
continue
}

// platform has a release, check if it matches the search arguments
// platform has a valid release, check if it matches the search arguments
if match(platform.Name, searchArgs) || match(platform.Architecture, searchArgs) {
if allVersions {
res = append(res, platform.GetAllReleases()...)
Expand Down
29 changes: 29 additions & 0 deletions commands/core/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 core

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMatch(t *testing.T) {
assert.True(t, match("this is platform Foo", "foo"))
assert.True(t, match("this is platform Foo", "FOO"))
assert.True(t, match("this is platform Foo", ""))
assert.False(t, match("this is platform Foo", "Bar"))
}
60 changes: 52 additions & 8 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
def test_core_search(run_command):
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
assert run_command("core update-index --additional-urls={}".format(url))
# list all
result = run_command("core search")
assert result.ok
# filter out empty lines and subtract 1 for the header line
platforms_count = len([l for l in result.stdout.splitlines() if l]) - 1
result = run_command("core search --format json")
assert result.ok
assert len(json.loads(result.stdout)) == platforms_count
# search a specific core
result = run_command("core search avr")
assert result.ok
Expand All @@ -51,3 +43,55 @@ def test_core_search(run_command):
assert result.ok
data = json.loads(result.stdout)
assert 2 == len(data)


def test_core_search_no_args(run_command):
"""
This tests `core search` with and without additional URLs in case no args
are passed (i.e. all results are shown).
"""
# update custom index and install test core (installed cores affect `core search`)
url = "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/test_index.json"
assert run_command("core update-index --additional-urls={}".format(url))
assert run_command("core install test:x86 --additional-urls={}".format(url))

# list all with no additional urls, ensure the test core won't show up
result = run_command("core search")
assert result.ok
num_platforms = 0
for l in result.stdout.splitlines()[1:]: # ignore the header on first line
if l: # ignore empty lines
assert not l.startswith("test:x86")
num_platforms += 1

# same thing in JSON format, also check the number of platforms found is the same
result = run_command("core search --format json")
assert result.ok
platforms = json.loads(result.stdout)
for elem in platforms:
assert elem.get("Name") != "test_core"
assert len(platforms) == num_platforms

# list all with additional urls, check the test core is there
result = run_command("core search --additional-urls={}".format(url))
assert result.ok
num_platforms = 0
found = False
for l in result.stdout.splitlines()[1:]: # ignore the header on first line
if l: # ignore empty lines
if l.startswith("test:x86"):
found = True
num_platforms += 1
assert found

# same thing in JSON format, also check the number of platforms found is the same
result = run_command("core search --format json --additional-urls={}".format(url))
assert result.ok
found = False
platforms = json.loads(result.stdout)
for elem in platforms:
if elem.get("Name") == "test_core":
found = True
break
assert found
assert len(platforms) == num_platforms
1 change: 0 additions & 1 deletion test/testdata/core.txt

This file was deleted.

Binary file added test/testdata/core.zip
Binary file not shown.
10 changes: 5 additions & 5 deletions test/testdata/test_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"help": {
"online": "https://github.com/Arduino/arduino-cli"
},
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/core.zip",
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
"name": "test_core",
"version": "1.0.0",
"architecture": "x86",
"archiveFileName": "core.txt",
"archiveFileName": "core.zip",
"size": "2799",
"toolsDependencies": [],
"boards": [
Expand All @@ -31,12 +31,12 @@
"help": {
"online": "https://github.com/Arduino/arduino-cli"
},
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/core.zip",
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
"name": "test_core",
"version": "2.0.0",
"architecture": "x86",
"archiveFileName": "core.txt",
"archiveFileName": "core.zip",
"size": "2799",
"toolsDependencies": [],
"boards": [
Expand All @@ -51,4 +51,4 @@
"name": "test"
}
]
}
}