Skip to content

Commit a58d5ad

Browse files
authored
fix: scan libraries on install (#2037)
1 parent 1897368 commit a58d5ad

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(name string, version *semve
6464
return nil, err
6565
}
6666

67+
lm.RescanLibraries()
6768
libs := lm.FindByReference(&librariesindex.Reference{Name: name}, installLocation)
6869

6970
if len(libs) > 1 {

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

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (lm *LibrariesManager) AddPlatformReleaseLibrariesDir(plaftormRelease *core
132132

133133
// RescanLibraries reload all installed libraries in the system.
134134
func (lm *LibrariesManager) RescanLibraries() []*status.Status {
135+
lm.clearLibraries()
135136
statuses := []*status.Status{}
136137
for _, dir := range lm.LibrariesDir {
137138
if errs := lm.LoadLibrariesFromDir(dir); len(errs) > 0 {
@@ -217,3 +218,9 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, in
217218
}
218219
return alternatives.FilterByVersionAndInstallLocation(libRef.Version, installLocation)
219220
}
221+
222+
func (lm *LibrariesManager) clearLibraries() {
223+
for k := range lm.Libraries {
224+
delete(lm.Libraries, k)
225+
}
226+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
package librariesmanager
16+
17+
import (
18+
"testing"
19+
20+
"github.com/arduino/arduino-cli/arduino/libraries"
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func Test_RescanLibrariesCallClear(t *testing.T) {
26+
baseDir := paths.New(t.TempDir())
27+
lm := NewLibraryManager(baseDir.Join("index_dir"), baseDir.Join("downloads_dir"))
28+
lm.Libraries["testLibA"] = libraries.List{}
29+
lm.Libraries["testLibB"] = libraries.List{}
30+
lm.RescanLibraries()
31+
require.Len(t, lm.Libraries, 0)
32+
}

Diff for: internal/integrationtest/daemon/daemon_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,36 @@ func TestDaemonBundleLibInstall(t *testing.T) {
382382
}
383383
}
384384
}
385+
386+
func TestDaemonLibrariesRescanOnInstall(t *testing.T) {
387+
/*
388+
Ensures that the libraries are rescanned prior to installing a new one,
389+
to avoid clashes with libraries installed after the daemon initialization.
390+
To perform the check:
391+
- the daemon is run and a gprc instance initialized
392+
- a library is installed through the cli
393+
- an attempt to install a new version of the library is done
394+
with the gprc instance
395+
The last attempt is expected to not raise an error
396+
*/
397+
env, cli := createEnvForDaemon(t)
398+
defer env.CleanUp()
399+
400+
grpcInst := cli.Create()
401+
require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) {
402+
fmt.Printf("INIT> %v\n", ir.GetMessage())
403+
}))
404+
cli.Run("lib", "install", "[email protected]")
405+
406+
instCl, err := grpcInst.LibraryInstall(context.Background(), "SD", "1.2.4", false, false, true)
407+
408+
require.NoError(t, err)
409+
for {
410+
_, err := instCl.Recv()
411+
if err == io.EOF {
412+
break
413+
}
414+
require.NoError(t, err)
415+
}
416+
417+
}

0 commit comments

Comments
 (0)