Unverified Commit 550179ee authored by Silvano Cerza's avatar Silvano Cerza Committed by GitHub

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: default avatarRuben Jenster <r.jenster@drachenfels.de>

* Remove logging statement from FindLibraryUpdate.
Signed-off-by: default avatarRuben Jenster <r.jenster@drachenfels.de>

* Add a small comment to the lib.Version nil check.
Signed-off-by: default avatarRuben Jenster <r.jenster@drachenfels.de>

* Fix some commands failing when an installed library has invalid version

* [skip changelog] Add integration tests
Co-authored-by: default avatarRuben Jenster <r.jenster@drachenfels.de>
parent 2c7b6ba5
...@@ -136,7 +136,9 @@ func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release { ...@@ -136,7 +136,9 @@ func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release {
if indexLib == nil { if indexLib == nil {
return nil return nil
} }
if indexLib.Latest.Version.GreaterThan(lib.Version) { // If a library.properties has an invalid version property, usually empty or malformed,
// the latest available version is returned
if lib.Version == nil || indexLib.Latest.Version.GreaterThan(lib.Version) {
return indexLib.Latest return indexLib.Latest
} }
return nil return nil
......
...@@ -78,6 +78,10 @@ func TestIndexer(t *testing.T) { ...@@ -78,6 +78,10 @@ func TestIndexer(t *testing.T) {
require.NotNil(t, rtcUpdate) require.NotNil(t, rtcUpdate)
require.Equal(t, "RTCZero@1.6.0", rtcUpdate.String()) require.Equal(t, "RTCZero@1.6.0", rtcUpdate.String())
rtcUpdateNoVersion := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: nil})
require.NotNil(t, rtcUpdateNoVersion)
require.Equal(t, "RTCZero@1.6.0", rtcUpdateNoVersion.String())
rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")}) rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")})
require.Nil(t, rtcNoUpdate) require.Nil(t, rtcNoUpdate)
......
...@@ -50,7 +50,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde ...@@ -50,7 +50,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
if installedLib.Location != libraries.User { if installedLib.Location != libraries.User {
continue continue
} }
if installedLib.Version.Equal(indexLibrary.Version) { if installedLib.Version != nil && installedLib.Version.Equal(indexLibrary.Version) {
return installedLib.InstallDir, nil, ErrAlreadyInstalled return installedLib.InstallDir, nil, ErrAlreadyInstalled
} }
replaced = installedLib replaced = installedLib
......
...@@ -703,3 +703,65 @@ def test_lib_examples_with_case_mismatch(run_command, data_dir): ...@@ -703,3 +703,65 @@ def test_lib_examples_with_case_mismatch(run_command, data_dir):
# Verifies sketches with wrong casing are not returned # Verifies sketches with wrong casing are not returned
assert str(examples_path / "NonBlocking" / "OnDemandNonBlocking") not in examples assert str(examples_path / "NonBlocking" / "OnDemandNonBlocking") not in examples
assert str(examples_path / "OnDemand" / "OnDemandWebPortal") not in examples assert str(examples_path / "OnDemand" / "OnDemandWebPortal") not in examples
def test_lib_list_using_library_with_invalid_version(run_command, data_dir):
assert run_command("update")
# Install a library
assert run_command("lib install WiFi101@0.16.1")
# Verifies library is correctly returned
res = run_command("lib list --format json")
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "0.16.1" == data[0]["library"]["version"]
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("version=1.0001")
# Verifies version is now empty
res = run_command("lib list --format json")
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "version" not in data[0]["library"]
def test_lib_upgrade_using_library_with_invalid_version(run_command, data_dir):
assert run_command("update")
# Install a library
assert run_command("lib install WiFi101@0.16.1")
# Verifies library is correctly returned
res = run_command("lib list --format json")
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "0.16.1" == data[0]["library"]["version"]
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("version=1.0001")
# Verifies version is now empty
res = run_command("lib list --format json")
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "version" not in data[0]["library"]
# Upgrade library
assert run_command("lib upgrade WiFi101")
# Verifies library has been updated
res = run_command("lib list --format json")
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "" != data[0]["library"]["version"]
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase # software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc. # a commercial license, send an email to license@arduino.cc.
from pathlib import Path
def test_outdated(run_command): def test_outdated(run_command):
# Updates index for cores and libraries # Updates index for cores and libraries
...@@ -33,3 +35,26 @@ def test_outdated(run_command): ...@@ -33,3 +35,26 @@ def test_outdated(run_command):
lines = [l.strip() for l in result.stdout.splitlines()] lines = [l.strip() for l in result.stdout.splitlines()]
assert lines[1].startswith("Arduino AVR Boards") assert lines[1].startswith("Arduino AVR Boards")
assert lines[4].startswith("USBHost") assert lines[4].startswith("USBHost")
def test_outdated_using_library_with_invalid_version(run_command, data_dir):
assert run_command("update")
# Install latest version of a library library
assert run_command("lib install WiFi101")
# Verifies library is correctly returned
res = run_command("outdated")
assert res.ok
assert "WiFi101" not in res.stdout
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("version=1.0001")
# Verifies library is correctly returned
res = run_command("outdated")
assert res.ok
lines = [l.strip().split() for l in res.stdout.splitlines()]
assert "WiFi101" == lines[1][0]
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase # software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc. # a commercial license, send an email to license@arduino.cc.
from pathlib import Path
def test_update(run_command): def test_update(run_command):
res = run_command("update") res = run_command("update")
...@@ -73,3 +75,25 @@ def test_update_with_url_internal_server_error(run_command, httpserver): ...@@ -73,3 +75,25 @@ def test_update_with_url_internal_server_error(run_command, httpserver):
assert res.failed assert res.failed
lines = [l.strip() for l in res.stderr.splitlines()] lines = [l.strip() for l in res.stderr.splitlines()]
assert f"Error updating core and libraries index: downloading index {url}: 500 INTERNAL SERVER ERROR" in lines assert f"Error updating core and libraries index: downloading index {url}: 500 INTERNAL SERVER ERROR" in lines
def test_update_showing_outdated_using_library_with_invalid_version(run_command, data_dir):
assert run_command("update")
# Install latest version of a library
assert run_command("lib install WiFi101")
# Verifies library doesn't get updated
res = run_command("update --show-outdated")
assert res.ok
assert "WiFi101" not in res.stdout
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("version=1.0001")
# Verifies library gets updated
res = run_command("update --show-outdated")
assert res.ok
assert "WiFi101" in res.stdout
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase # software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to license@arduino.cc. # a commercial license, send an email to license@arduino.cc.
from pathlib import Path
def test_upgrade(run_command): def test_upgrade(run_command):
# Updates index for cores and libraries # Updates index for cores and libraries
...@@ -41,3 +43,25 @@ def test_upgrade(run_command): ...@@ -41,3 +43,25 @@ def test_upgrade(run_command):
result = run_command("outdated") result = run_command("outdated")
assert result.ok assert result.ok
assert result.stdout == "" assert result.stdout == ""
def test_upgrade_using_library_with_invalid_version(run_command, data_dir):
assert run_command("update")
# Install latest version of a library
assert run_command("lib install WiFi101")
# Verifies library is not shown
res = run_command("outdated")
assert res.ok
assert "WiFi101" not in res.stdout
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("version=1.0001")
# Verifies library gets upgraded
res = run_command("upgrade")
assert res.ok
assert "WiFi101" in res.stdout
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment