|
| 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 [email protected]. |
| 15 | + |
| 16 | +package daemon_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestDaemonBundleLibInstall(t *testing.T) { |
| 30 | + env, cli := createEnvForDaemon(t) |
| 31 | + defer env.CleanUp() |
| 32 | + |
| 33 | + grpcInst := cli.Create() |
| 34 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 35 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 36 | + })) |
| 37 | + |
| 38 | + // Install libraries in bundled dir (should fail) |
| 39 | + { |
| 40 | + instCl, err := grpcInst.LibraryInstall(context.Background(), "Arduino_BuiltIn", "", false, false, true) |
| 41 | + require.NoError(t, err) |
| 42 | + for { |
| 43 | + msg, err := instCl.Recv() |
| 44 | + if err == io.EOF { |
| 45 | + require.FailNow(t, "LibraryInstall is supposed to fail because builtin libraries directory is not set") |
| 46 | + } |
| 47 | + if err != nil { |
| 48 | + fmt.Println("LIB INSTALL ERROR:", err) |
| 49 | + break |
| 50 | + } |
| 51 | + fmt.Printf("LIB INSTALL> %+v\n", msg) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Set builtin libraries dir |
| 56 | + builtinLibsDir := cli.DataDir().Join("libraries") |
| 57 | + jsonBuiltinLibsDir, err := json.Marshal(builtinLibsDir) |
| 58 | + require.NoError(t, err) |
| 59 | + err = cli.SetValue("directories.builtin.libraries", string(jsonBuiltinLibsDir)) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // Re-init |
| 63 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 64 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 65 | + })) |
| 66 | + |
| 67 | + // Install libraries in bundled dir |
| 68 | + { |
| 69 | + instCl, err := grpcInst.LibraryInstall(context.Background(), "Arduino_BuiltIn", "", false, false, true) |
| 70 | + require.NoError(t, err) |
| 71 | + for { |
| 72 | + msg, err := instCl.Recv() |
| 73 | + if err == io.EOF { |
| 74 | + break |
| 75 | + } |
| 76 | + require.NoError(t, err) |
| 77 | + fmt.Printf("LIB INSTALL> %+v\n", msg) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Check if libraries are installed as expected |
| 82 | + { |
| 83 | + resp, err := grpcInst.LibraryList(context.Background(), "", "", true, false) |
| 84 | + require.NoError(t, err) |
| 85 | + libsAndLocation := map[string]commands.LibraryLocation{} |
| 86 | + for _, lib := range resp.GetInstalledLibraries() { |
| 87 | + libsAndLocation[lib.Library.Name] = lib.Library.Location |
| 88 | + } |
| 89 | + require.Contains(t, libsAndLocation, "Ethernet") |
| 90 | + require.Contains(t, libsAndLocation, "SD") |
| 91 | + require.Contains(t, libsAndLocation, "Firmata") |
| 92 | + require.Equal(t, libsAndLocation["Ethernet"], commands.LibraryLocation_LIBRARY_LOCATION_BUILTIN) |
| 93 | + require.Equal(t, libsAndLocation["SD"], commands.LibraryLocation_LIBRARY_LOCATION_BUILTIN) |
| 94 | + require.Equal(t, libsAndLocation["Firmata"], commands.LibraryLocation_LIBRARY_LOCATION_BUILTIN) |
| 95 | + } |
| 96 | + |
| 97 | + // Install a library in sketchbook to override bundled |
| 98 | + { |
| 99 | + instCl, err := grpcInst.LibraryInstall(context.Background(), "Ethernet", "", false, false, false) |
| 100 | + require.NoError(t, err) |
| 101 | + for { |
| 102 | + msg, err := instCl.Recv() |
| 103 | + if err == io.EOF { |
| 104 | + break |
| 105 | + } |
| 106 | + require.NoError(t, err) |
| 107 | + fmt.Printf("LIB INSTALL> %+v\n", msg) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Check if libraries are installed as expected |
| 112 | + { |
| 113 | + resp, err := grpcInst.LibraryList(context.Background(), "", "", true, false) |
| 114 | + require.NoError(t, err) |
| 115 | + libsAndLocation := map[string]commands.LibraryLocation{} |
| 116 | + for _, lib := range resp.GetInstalledLibraries() { |
| 117 | + libsAndLocation[lib.Library.Name] = lib.Library.Location |
| 118 | + } |
| 119 | + require.Contains(t, libsAndLocation, "Ethernet") |
| 120 | + require.Contains(t, libsAndLocation, "SD") |
| 121 | + require.Contains(t, libsAndLocation, "Firmata") |
| 122 | + require.Equal(t, libsAndLocation["Ethernet"], commands.LibraryLocation_LIBRARY_LOCATION_USER) |
| 123 | + require.Equal(t, libsAndLocation["SD"], commands.LibraryLocation_LIBRARY_LOCATION_BUILTIN) |
| 124 | + require.Equal(t, libsAndLocation["Firmata"], commands.LibraryLocation_LIBRARY_LOCATION_BUILTIN) |
| 125 | + } |
| 126 | +} |
0 commit comments