diff --git a/cli/core/search.go b/cli/core/search.go index e4962ee2731..807bc7f216c 100644 --- a/cli/core/search.go +++ b/cli/core/search.go @@ -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) diff --git a/commands/core/search.go b/commands/core/search.go index c84f4093758..914ff5b12e8 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -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 @@ -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()...) diff --git a/commands/core/search_test.go b/commands/core/search_test.go new file mode 100644 index 00000000000..6886440a40a --- /dev/null +++ b/commands/core/search_test.go @@ -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 license@arduino.cc. + +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")) +} diff --git a/test/test_core.py b/test/test_core.py index 868ef653b5f..36121c15b06 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -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 @@ -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 diff --git a/test/testdata/core.txt b/test/testdata/core.txt deleted file mode 100644 index 72c5593cdcb..00000000000 --- a/test/testdata/core.txt +++ /dev/null @@ -1 +0,0 @@ -HELO diff --git a/test/testdata/core.zip b/test/testdata/core.zip new file mode 100644 index 00000000000..eb9bc92d619 Binary files /dev/null and b/test/testdata/core.zip differ diff --git a/test/testdata/test_index.json b/test/testdata/test_index.json index 3965c18c8a1..352e925bb6d 100644 --- a/test/testdata/test_index.json +++ b/test/testdata/test_index.json @@ -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": [ @@ -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": [ @@ -51,4 +51,4 @@ "name": "test" } ] -} +} \ No newline at end of file