Skip to content

Commit 616432c

Browse files
committed
Added integration test for library install in bundled directory
1 parent 56470a3 commit 616432c

File tree

3 files changed

+189
-12
lines changed

3 files changed

+189
-12
lines changed

Diff for: arduino/libraries/librariesmanager/librariesmanager.go

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
118118
// LoadIndex reads a library_index.json from a file and returns
119119
// the corresponding Index structure.
120120
func (lm *LibrariesManager) LoadIndex() error {
121+
logrus.WithField("index", lm.IndexFile).Info("Loading libraries index file")
121122
index, err := librariesindex.LoadIndex(lm.IndexFile)
122123
if err != nil {
123124
lm.Index = librariesindex.EmptyIndex

Diff for: internal/integrationtest/arduino-cli.go

+62-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package integrationtest
1818
import (
1919
"bytes"
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"io"
2324
"os"
@@ -28,6 +29,7 @@ import (
2829

2930
"github.com/arduino/arduino-cli/executils"
3031
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32+
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
3133
"github.com/arduino/go-paths-helper"
3234
"github.com/fatih/color"
3335
"github.com/stretchr/testify/require"
@@ -61,17 +63,18 @@ func CreateArduinoCLIWithEnvironment(t *testing.T) (*testsuite.Environment, *Ard
6163

6264
// ArduinoCLI is an Arduino CLI client.
6365
type ArduinoCLI struct {
64-
path *paths.Path
65-
t *require.Assertions
66-
proc *executils.Process
67-
cliEnvVars []string
68-
cliConfigPath *paths.Path
69-
stagingDir *paths.Path
70-
dataDir *paths.Path
71-
sketchbookDir *paths.Path
72-
daemonAddr string
73-
daemonConn *grpc.ClientConn
74-
daemonClient commands.ArduinoCoreServiceClient
66+
path *paths.Path
67+
t *require.Assertions
68+
proc *executils.Process
69+
cliEnvVars []string
70+
cliConfigPath *paths.Path
71+
stagingDir *paths.Path
72+
dataDir *paths.Path
73+
sketchbookDir *paths.Path
74+
daemonAddr string
75+
daemonConn *grpc.ClientConn
76+
daemonClient commands.ArduinoCoreServiceClient
77+
daemonSettingsClient settings.SettingsServiceClient
7578
}
7679

7780
// ArduinoCLIConfig is the configuration of the ArduinoCLI client
@@ -196,7 +199,7 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
196199
cli.t.NoError(err)
197200
cli.daemonConn = conn
198201
cli.daemonClient = commands.NewArduinoCoreServiceClient(conn)
199-
202+
cli.daemonSettingsClient = settings.NewSettingsServiceClient(conn)
200203
return cli.daemonAddr
201204
}
202205

@@ -226,6 +229,17 @@ func (cli *ArduinoCLI) Create() *ArduinoCLIInstance {
226229
}
227230
}
228231

232+
// SetValue calls the "SetValue" gRPC method.
233+
func (cli *ArduinoCLI) SetValue(key, jsonData string) error {
234+
req := &settings.SetValueRequest{
235+
Key: key,
236+
JsonData: jsonData,
237+
}
238+
logCallf(">>> SetValue(%+v)\n", req)
239+
_, err := cli.daemonSettingsClient.SetValue(context.Background(), req)
240+
return err
241+
}
242+
229243
// Init calls the "Init" gRPC method.
230244
func (inst *ArduinoCLIInstance) Init(profile string, sketchPath string, respCB func(*commands.InitResponse)) error {
231245
initReq := &commands.InitRequest{
@@ -302,3 +316,39 @@ func (inst *ArduinoCLIInstance) Compile(ctx context.Context, fqbn, sketchPath st
302316
logCallf(">>> Compile(%v %v)\n", fqbn, sketchPath)
303317
return compileCl, err
304318
}
319+
320+
// LibraryList calls the "LibraryList" gRPC method.
321+
func (inst *ArduinoCLIInstance) LibraryList(ctx context.Context, name, fqbn string, all, updatable bool) (*commands.LibraryListResponse, error) {
322+
req := &commands.LibraryListRequest{
323+
Instance: inst.instance,
324+
Name: name,
325+
Fqbn: fqbn,
326+
All: all,
327+
Updatable: updatable,
328+
}
329+
logCallf(">>> LibraryList(%v) -> ", req)
330+
resp, err := inst.cli.daemonClient.LibraryList(ctx, req)
331+
logCallf("err=%v\n", err)
332+
r, _ := json.MarshalIndent(resp, " ", " ")
333+
logCallf("<<< LibraryList resp: %s\n", string(r))
334+
return resp, err
335+
}
336+
337+
// LibraryInstall calls the "LibraryInstall" gRPC method.
338+
func (inst *ArduinoCLIInstance) LibraryInstall(ctx context.Context, name, version string, noDeps, noOverwrite, installAsBundled bool) (commands.ArduinoCoreService_LibraryInstallClient, error) {
339+
installLocation := commands.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_USER
340+
if installAsBundled {
341+
installLocation = commands.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_BUILTIN
342+
}
343+
req := &commands.LibraryInstallRequest{
344+
Instance: inst.instance,
345+
Name: name,
346+
Version: version,
347+
NoDeps: noDeps,
348+
NoOverwrite: noOverwrite,
349+
InstallLocation: installLocation,
350+
}
351+
installCl, err := inst.cli.daemonClient.LibraryInstall(ctx, req)
352+
logCallf(">>> LibraryInstall(%+v)\n", req)
353+
return installCl, err
354+
}
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)