|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2022 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 license@arduino.cc. |
| 15 | + |
| 16 | +package core_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 25 | + "github.com/arduino/go-paths-helper" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + "github.com/tidwall/gjson" |
| 28 | +) |
| 29 | + |
| 30 | +func TestCoreSearch(t *testing.T) { |
| 31 | + env := integrationtest.NewEnvironment(t) |
| 32 | + defer env.CleanUp() |
| 33 | + |
| 34 | + cli := integrationtest.NewArduinoCliWithinEnvironment(t, &integrationtest.ArduinoCLIConfig{ |
| 35 | + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), |
| 36 | + UseSharedStagingFolder: true, |
| 37 | + }, env) |
| 38 | + defer cli.CleanUp() |
| 39 | + |
| 40 | + // Set up an http server to serve our custom index file |
| 41 | + test_index := paths.New("..", "testdata", "test_index.json") |
| 42 | + url, httpClose := integrationtest.HTTPServeFile(t, 8000, test_index) |
| 43 | + defer httpClose() |
| 44 | + |
| 45 | + // Run update-index with our test index |
| 46 | + _, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String()) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + // Search a specific core |
| 50 | + out, _, err := cli.Run("core", "search", "avr") |
| 51 | + require.NoError(t, err) |
| 52 | + require.Greater(t, len(strings.Split(string(out), "\n")), 2) |
| 53 | + |
| 54 | + out, _, err = cli.Run("core", "search", "avr", "--format", "json") |
| 55 | + require.NoError(t, err) |
| 56 | + data := make([]interface{}, 0) |
| 57 | + require.NoError(t, json.Unmarshal(out, &data)) |
| 58 | + require.NotEmpty(t, data) |
| 59 | + // same check using gjson lib |
| 60 | + require.NotEmpty(t, gjson.ParseBytes(out).Array()) |
| 61 | + |
| 62 | + // additional URL |
| 63 | + out, _, err = cli.Run("core", "search", "test_core", "--format", "json", "--additional-urls="+url.String()) |
| 64 | + require.NoError(t, err) |
| 65 | + require.NoError(t, json.Unmarshal(out, &data)) |
| 66 | + require.Len(t, data, 1) |
| 67 | + |
| 68 | + // show all versions |
| 69 | + out, _, err = cli.Run("core", "search", "test_core", "--all", "--format", "json", "--additional-urls="+url.String()) |
| 70 | + require.NoError(t, err) |
| 71 | + require.NoError(t, json.Unmarshal(out, &data)) |
| 72 | + require.Len(t, data, 2) |
| 73 | + // alternative check using gjson: |
| 74 | + require.Len(t, gjson.ParseBytes(out).Array(), 2) |
| 75 | + // alternative using gojq: |
| 76 | + integrationtest.JQQuery(t, out, "length", 2) |
| 77 | + |
| 78 | + checkPlatformIsInJSONOutput := func(stdout []byte, id, version string) { |
| 79 | + // Alternative solution with gojq |
| 80 | + jqquery := fmt.Sprintf(`contains( [{id:"%s", latest:"%s"}] )`, id, version) |
| 81 | + integrationtest.JQQuery(t, out, jqquery, true, "platform %s@%s is missing from the output", id, version) |
| 82 | + |
| 83 | + // Alternative solution with gjson |
| 84 | + // query := fmt.Sprintf("#(id=%s)#|#(latest=%s)", id, version) |
| 85 | + // if gjson.ParseBytes(out).Get(query).Exists() { |
| 86 | + // return |
| 87 | + // } |
| 88 | + // require.FailNowf(t, "Wrong output", "platform %s@%s is missing from the output", id, version) |
| 89 | + |
| 90 | + // Alternative solution: |
| 91 | + // for _, platform := range gjson.ParseBytes(out).Array() { |
| 92 | + // if platform.Get("id").Str == id && platform.Get("latest").Str == version { |
| 93 | + // return |
| 94 | + // } |
| 95 | + // } |
| 96 | + // require.FailNowf(t, "Wrong output", "platform %s@%s is missing from the output", id, version) |
| 97 | + } |
| 98 | + |
| 99 | + // Search all Retrokit platforms |
| 100 | + out, _, err = cli.Run("core", "search", "retrokit", "--all", "--additional-urls="+url.String(), "--format", "json") |
| 101 | + require.NoError(t, err) |
| 102 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5") |
| 103 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6") |
| 104 | + //checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.9") // Test failure |
| 105 | + |
| 106 | + // Search using Retrokit Package Maintainer |
| 107 | + out, _, err = cli.Run("core", "search", "Retrokits-RK002", "--all", "--additional-urls="+url.String(), "--format", "json") |
| 108 | + require.NoError(t, err) |
| 109 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5") |
| 110 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6") |
| 111 | + |
| 112 | + // Search using the Retrokit Platform name |
| 113 | + out, _, err = cli.Run("core", "search", "rk002", "--all", "--additional-urls="+url.String(), "--format", "json") |
| 114 | + require.NoError(t, err) |
| 115 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5") |
| 116 | + checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6") |
| 117 | + |
| 118 | + // Search using board names |
| 119 | + out, _, err = cli.Run("core", "search", "myboard", "--all", "--additional-urls="+url.String(), "--format", "json") |
| 120 | + require.NoError(t, err) |
| 121 | + checkPlatformIsInJSONOutput(out, "Package:x86", "1.2.3") |
| 122 | + |
| 123 | + // Check search with case, accents and spaces |
| 124 | + runSearch := func(searchArgs string, expectedIDs ...string) { |
| 125 | + args := []string{"core", "search", "--format", "json"} |
| 126 | + args = append(args, strings.Split(searchArgs, " ")...) |
| 127 | + out, _, err := cli.Run(args...) |
| 128 | + require.NoError(t, err) |
| 129 | + |
| 130 | + // Alternative solution with gojq |
| 131 | + for _, id := range expectedIDs { |
| 132 | + jqquery := fmt.Sprintf(`contains( [{id:"%s"}] )`, id) |
| 133 | + integrationtest.JQQuery(t, out, jqquery, true, "platform %s is missing from the output", id) |
| 134 | + } |
| 135 | + |
| 136 | + // Alternative solution with gjson |
| 137 | + // data := gjson.ParseBytes(out) |
| 138 | + // for _, expectedID := range expectedIDs { |
| 139 | + // query := fmt.Sprintf("#(id=%s)", expectedID) |
| 140 | + // if !data.Get(query).Exists() { |
| 141 | + // require.FailNowf(t, "Wrong output", "platform %s is missing from the output", expectedID) |
| 142 | + // } |
| 143 | + // } |
| 144 | + } |
| 145 | + |
| 146 | + runSearch("mkr1000", "arduino:samd") |
| 147 | + runSearch("mkr 1000", "arduino:samd") |
| 148 | + |
| 149 | + runSearch("yún", "arduino:avr") |
| 150 | + runSearch("yùn", "arduino:avr") |
| 151 | + runSearch("yun", "arduino:avr") |
| 152 | + |
| 153 | + runSearch("nano 33", "arduino:samd", "arduino:mbed_nano") |
| 154 | + runSearch("nano ble", "arduino:mbed_nano") |
| 155 | + runSearch("ble", "arduino:mbed_nano") |
| 156 | + runSearch("ble nano", "arduino:mbed_nano") |
| 157 | + runSearch("nano", "arduino:avr", "arduino:megaavr", "arduino:samd", "arduino:mbed_nano") |
| 158 | +} |
0 commit comments