Skip to content

Commit d977fc2

Browse files
silvanocerzar10r
andcommitted
Fix some commands crashing when an installed library has invalid version (#1189)
* librariesindex: Fix nil pointer. Refs #1176 Let the library index return the latest known version, if a library without a version is found. Signed-off-by: Ruben Jenster <[email protected]> * Remove logging statement from FindLibraryUpdate. Signed-off-by: Ruben Jenster <[email protected]> * Add a small comment to the lib.Version nil check. Signed-off-by: Ruben Jenster <[email protected]> * Fix some commands failing when an installed library has invalid version * [skip changelog] Add integration tests Co-authored-by: Ruben Jenster <[email protected]>
1 parent c977a23 commit d977fc2

File tree

7 files changed

+143
-2
lines changed

7 files changed

+143
-2
lines changed

arduino/libraries/librariesindex/index.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release {
136136
if indexLib == nil {
137137
return nil
138138
}
139-
if indexLib.Latest.Version.GreaterThan(lib.Version) {
139+
// If a library.properties has an invalid version property, usually empty or malformed,
140+
// the latest available version is returned
141+
if lib.Version == nil || indexLib.Latest.Version.GreaterThan(lib.Version) {
140142
return indexLib.Latest
141143
}
142144
return nil

arduino/libraries/librariesindex/index_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func TestIndexer(t *testing.T) {
7878
require.NotNil(t, rtcUpdate)
7979
require.Equal(t, "[email protected]", rtcUpdate.String())
8080

81+
rtcUpdateNoVersion := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: nil})
82+
require.NotNil(t, rtcUpdateNoVersion)
83+
require.Equal(t, "[email protected]", rtcUpdateNoVersion.String())
84+
8185
rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")})
8286
require.Nil(t, rtcNoUpdate)
8387

arduino/libraries/librariesmanager/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
5050
if installedLib.Location != libraries.User {
5151
continue
5252
}
53-
if installedLib.Version.Equal(indexLibrary.Version) {
53+
if installedLib.Version != nil && installedLib.Version.Equal(indexLibrary.Version) {
5454
return installedLib.InstallDir, nil, ErrAlreadyInstalled
5555
}
5656
replaced = installedLib

test/test_lib.py

+62
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,65 @@ def test_lib_examples_with_case_mismatch(run_command, data_dir):
703703
# Verifies sketches with wrong casing are not returned
704704
assert str(examples_path / "NonBlocking" / "OnDemandNonBlocking") not in examples
705705
assert str(examples_path / "OnDemand" / "OnDemandWebPortal") not in examples
706+
707+
708+
def test_lib_list_using_library_with_invalid_version(run_command, data_dir):
709+
assert run_command("update")
710+
711+
# Install a library
712+
assert run_command("lib install [email protected]")
713+
714+
# Verifies library is correctly returned
715+
res = run_command("lib list --format json")
716+
assert res.ok
717+
data = json.loads(res.stdout)
718+
assert len(data) == 1
719+
assert "0.16.1" == data[0]["library"]["version"]
720+
721+
# Changes the version of the currently installed library so that it's
722+
# invalid
723+
lib_path = Path(data_dir, "libraries", "WiFi101")
724+
Path(lib_path, "library.properties").write_text("version=1.0001")
725+
726+
# Verifies version is now empty
727+
res = run_command("lib list --format json")
728+
assert res.ok
729+
data = json.loads(res.stdout)
730+
assert len(data) == 1
731+
assert "version" not in data[0]["library"]
732+
733+
734+
def test_lib_upgrade_using_library_with_invalid_version(run_command, data_dir):
735+
assert run_command("update")
736+
737+
# Install a library
738+
assert run_command("lib install [email protected]")
739+
740+
# Verifies library is correctly returned
741+
res = run_command("lib list --format json")
742+
assert res.ok
743+
data = json.loads(res.stdout)
744+
assert len(data) == 1
745+
assert "0.16.1" == data[0]["library"]["version"]
746+
747+
# Changes the version of the currently installed library so that it's
748+
# invalid
749+
lib_path = Path(data_dir, "libraries", "WiFi101")
750+
Path(lib_path, "library.properties").write_text("version=1.0001")
751+
752+
# Verifies version is now empty
753+
res = run_command("lib list --format json")
754+
assert res.ok
755+
data = json.loads(res.stdout)
756+
assert len(data) == 1
757+
assert "version" not in data[0]["library"]
758+
759+
# Upgrade library
760+
assert run_command("lib upgrade WiFi101")
761+
762+
# Verifies library has been updated
763+
res = run_command("lib list --format json")
764+
assert res.ok
765+
data = json.loads(res.stdout)
766+
assert len(data) == 1
767+
assert "" != data[0]["library"]["version"]

test/test_outdated.py

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
1515

16+
from pathlib import Path
17+
1618

1719
def test_outdated(run_command):
1820
# Updates index for cores and libraries
@@ -33,3 +35,26 @@ def test_outdated(run_command):
3335
lines = [l.strip() for l in result.stdout.splitlines()]
3436
assert lines[1].startswith("Arduino AVR Boards")
3537
assert lines[4].startswith("USBHost")
38+
39+
40+
def test_outdated_using_library_with_invalid_version(run_command, data_dir):
41+
assert run_command("update")
42+
43+
# Install latest version of a library library
44+
assert run_command("lib install WiFi101")
45+
46+
# Verifies library is correctly returned
47+
res = run_command("outdated")
48+
assert res.ok
49+
assert "WiFi101" not in res.stdout
50+
51+
# Changes the version of the currently installed library so that it's
52+
# invalid
53+
lib_path = Path(data_dir, "libraries", "WiFi101")
54+
Path(lib_path, "library.properties").write_text("version=1.0001")
55+
56+
# Verifies library is correctly returned
57+
res = run_command("outdated")
58+
assert res.ok
59+
lines = [l.strip().split() for l in res.stdout.splitlines()]
60+
assert "WiFi101" == lines[1][0]

test/test_update.py

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
1515

16+
from pathlib import Path
17+
1618

1719
def test_update(run_command):
1820
res = run_command("update")
@@ -73,3 +75,25 @@ def test_update_with_url_internal_server_error(run_command, httpserver):
7375
assert res.failed
7476
lines = [l.strip() for l in res.stderr.splitlines()]
7577
assert f"Error updating core and libraries index: downloading index {url}: 500 INTERNAL SERVER ERROR" in lines
78+
79+
80+
def test_update_showing_outdated_using_library_with_invalid_version(run_command, data_dir):
81+
assert run_command("update")
82+
83+
# Install latest version of a library
84+
assert run_command("lib install WiFi101")
85+
86+
# Verifies library doesn't get updated
87+
res = run_command("update --show-outdated")
88+
assert res.ok
89+
assert "WiFi101" not in res.stdout
90+
91+
# Changes the version of the currently installed library so that it's
92+
# invalid
93+
lib_path = Path(data_dir, "libraries", "WiFi101")
94+
Path(lib_path, "library.properties").write_text("version=1.0001")
95+
96+
# Verifies library gets updated
97+
res = run_command("update --show-outdated")
98+
assert res.ok
99+
assert "WiFi101" in res.stdout

test/test_upgrade.py

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
1515

16+
from pathlib import Path
17+
1618

1719
def test_upgrade(run_command):
1820
# Updates index for cores and libraries
@@ -41,3 +43,25 @@ def test_upgrade(run_command):
4143
result = run_command("outdated")
4244
assert result.ok
4345
assert result.stdout == ""
46+
47+
48+
def test_upgrade_using_library_with_invalid_version(run_command, data_dir):
49+
assert run_command("update")
50+
51+
# Install latest version of a library
52+
assert run_command("lib install WiFi101")
53+
54+
# Verifies library is not shown
55+
res = run_command("outdated")
56+
assert res.ok
57+
assert "WiFi101" not in res.stdout
58+
59+
# Changes the version of the currently installed library so that it's
60+
# invalid
61+
lib_path = Path(data_dir, "libraries", "WiFi101")
62+
Path(lib_path, "library.properties").write_text("version=1.0001")
63+
64+
# Verifies library gets upgraded
65+
res = run_command("upgrade")
66+
assert res.ok
67+
assert "WiFi101" in res.stdout

0 commit comments

Comments
 (0)