Skip to content

Commit 33b9322

Browse files
author
Massimiliano Pippi
authored
Fix core search not showing board name for 3rd party cores (#556)
* use a real archive as a fake core * fix core search for 3rd party cores with tests
1 parent 43d863d commit 33b9322

File tree

7 files changed

+97
-19
lines changed

7 files changed

+97
-19
lines changed

Diff for: cli/core/search.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ func initSearchCommand() *cobra.Command {
5151
func runSearchCommand(cmd *cobra.Command, args []string) {
5252
inst, err := instance.CreateInstance()
5353
if err != nil {
54-
feedback.Errorf("Error saerching for platforms: %v", err)
54+
feedback.Errorf("Error searching for platforms: %v", err)
5555
os.Exit(errorcodes.ErrGeneric)
5656
}
5757

58-
logrus.Info("Executing `arduino core search`")
59-
6058
arguments := strings.ToLower(strings.Join(args, " "))
59+
logrus.Infof("Executing `arduino core search` with args: '%s'", arguments)
60+
6161
resp, err := core.PlatformSearch(inst.GetId(), arguments, allVersions)
6262
if err != nil {
6363
feedback.Errorf("Error saerching for platforms: %v", err)

Diff for: commands/core/search.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func match(line, searchArgs string) bool {
29-
return strings.Contains(strings.ToLower(line), searchArgs)
29+
return strings.Contains(strings.ToLower(line), strings.ToLower(searchArgs))
3030
}
3131

3232
// PlatformSearch FIXMEDOC
@@ -43,12 +43,18 @@ func PlatformSearch(instanceID int32, searchArgs string, allVersions bool) (*rpc
4343
} else {
4444
for _, targetPackage := range pm.Packages {
4545
for _, platform := range targetPackage.Platforms {
46+
// discard invalid platforms
47+
if platform == nil || platform.Name == "" {
48+
continue
49+
}
50+
51+
// discard invalid releases
4652
platformRelease := platform.GetLatestRelease()
4753
if platformRelease == nil {
4854
continue
4955
}
5056

51-
// platform has a release, check if it matches the search arguments
57+
// platform has a valid release, check if it matches the search arguments
5258
if match(platform.Name, searchArgs) || match(platform.Architecture, searchArgs) {
5359
if allVersions {
5460
res = append(res, platform.GetAllReleases()...)

Diff for: commands/core/search_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package core
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestMatch(t *testing.T) {
25+
assert.True(t, match("this is platform Foo", "foo"))
26+
assert.True(t, match("this is platform Foo", "FOO"))
27+
assert.True(t, match("this is platform Foo", ""))
28+
assert.False(t, match("this is platform Foo", "Bar"))
29+
}

Diff for: test/test_core.py

+52-8
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121
def test_core_search(run_command):
2222
url = "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/test_index.json"
2323
assert run_command("core update-index --additional-urls={}".format(url))
24-
# list all
25-
result = run_command("core search")
26-
assert result.ok
27-
# filter out empty lines and subtract 1 for the header line
28-
platforms_count = len([l for l in result.stdout.splitlines() if l]) - 1
29-
result = run_command("core search --format json")
30-
assert result.ok
31-
assert len(json.loads(result.stdout)) == platforms_count
3224
# search a specific core
3325
result = run_command("core search avr")
3426
assert result.ok
@@ -51,3 +43,55 @@ def test_core_search(run_command):
5143
assert result.ok
5244
data = json.loads(result.stdout)
5345
assert 2 == len(data)
46+
47+
48+
def test_core_search_no_args(run_command):
49+
"""
50+
This tests `core search` with and without additional URLs in case no args
51+
are passed (i.e. all results are shown).
52+
"""
53+
# update custom index and install test core (installed cores affect `core search`)
54+
url = "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/test_index.json"
55+
assert run_command("core update-index --additional-urls={}".format(url))
56+
assert run_command("core install test:x86 --additional-urls={}".format(url))
57+
58+
# list all with no additional urls, ensure the test core won't show up
59+
result = run_command("core search")
60+
assert result.ok
61+
num_platforms = 0
62+
for l in result.stdout.splitlines()[1:]: # ignore the header on first line
63+
if l: # ignore empty lines
64+
assert not l.startswith("test:x86")
65+
num_platforms += 1
66+
67+
# same thing in JSON format, also check the number of platforms found is the same
68+
result = run_command("core search --format json")
69+
assert result.ok
70+
platforms = json.loads(result.stdout)
71+
for elem in platforms:
72+
assert elem.get("Name") != "test_core"
73+
assert len(platforms) == num_platforms
74+
75+
# list all with additional urls, check the test core is there
76+
result = run_command("core search --additional-urls={}".format(url))
77+
assert result.ok
78+
num_platforms = 0
79+
found = False
80+
for l in result.stdout.splitlines()[1:]: # ignore the header on first line
81+
if l: # ignore empty lines
82+
if l.startswith("test:x86"):
83+
found = True
84+
num_platforms += 1
85+
assert found
86+
87+
# same thing in JSON format, also check the number of platforms found is the same
88+
result = run_command("core search --format json --additional-urls={}".format(url))
89+
assert result.ok
90+
found = False
91+
platforms = json.loads(result.stdout)
92+
for elem in platforms:
93+
if elem.get("Name") == "test_core":
94+
found = True
95+
break
96+
assert found
97+
assert len(platforms) == num_platforms

Diff for: test/testdata/core.txt

-1
This file was deleted.

Diff for: test/testdata/core.zip

486 Bytes
Binary file not shown.

Diff for: test/testdata/test_index.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"help": {
1313
"online": "https://github.com/Arduino/arduino-cli"
1414
},
15-
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
15+
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/core.zip",
1616
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
1717
"name": "test_core",
1818
"version": "1.0.0",
1919
"architecture": "x86",
20-
"archiveFileName": "core.txt",
20+
"archiveFileName": "core.zip",
2121
"size": "2799",
2222
"toolsDependencies": [],
2323
"boards": [
@@ -31,12 +31,12 @@
3131
"help": {
3232
"online": "https://github.com/Arduino/arduino-cli"
3333
},
34-
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/additional-urls/test/testdata/core.txt",
34+
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/massi/506/test/testdata/core.zip",
3535
"checksum": "SHA-256:1ba93f6aea56842dfef065c0f5eb0a34c1f78b72b3f2426c94e47ba3a359c9ff",
3636
"name": "test_core",
3737
"version": "2.0.0",
3838
"architecture": "x86",
39-
"archiveFileName": "core.txt",
39+
"archiveFileName": "core.zip",
4040
"size": "2799",
4141
"toolsDependencies": [],
4242
"boards": [
@@ -51,4 +51,4 @@
5151
"name": "test"
5252
}
5353
]
54-
}
54+
}

0 commit comments

Comments
 (0)