Skip to content

Fix some commands crashing when an installed library has invalid version #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion arduino/libraries/librariesindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release {
if indexLib == 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 nil
Expand Down
4 changes: 4 additions & 0 deletions arduino/libraries/librariesindex/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func TestIndexer(t *testing.T) {
require.NotNil(t, rtcUpdate)
require.Equal(t, "[email protected]", rtcUpdate.String())

rtcUpdateNoVersion := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: nil})
require.NotNil(t, rtcUpdateNoVersion)
require.Equal(t, "[email protected]", rtcUpdateNoVersion.String())

rtcNoUpdate := index.FindLibraryUpdate(&libraries.Library{Name: "RTCZero", Version: semver.MustParse("3.0.0")})
require.Nil(t, rtcNoUpdate)

Expand Down
2 changes: 1 addition & 1 deletion arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
if installedLib.Location != libraries.User {
continue
}
if installedLib.Version.Equal(indexLibrary.Version) {
if installedLib.Version != nil && installedLib.Version.Equal(indexLibrary.Version) {
return installedLib.InstallDir, nil, ErrAlreadyInstalled
}
replaced = installedLib
Expand Down
62 changes: 62 additions & 0 deletions test/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,65 @@ def test_lib_examples_with_case_mismatch(run_command, data_dir):
# Verifies sketches with wrong casing are not returned
assert str(examples_path / "NonBlocking" / "OnDemandNonBlocking") 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 [email protected]")

# 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 [email protected]")

# 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"]
25 changes: 25 additions & 0 deletions test/test_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to [email protected].

from pathlib import Path


def test_outdated(run_command):
# Updates index for cores and libraries
Expand All @@ -33,3 +35,26 @@ def test_outdated(run_command):
lines = [l.strip() for l in result.stdout.splitlines()]
assert lines[1].startswith("Arduino AVR Boards")
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]
24 changes: 24 additions & 0 deletions test/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to [email protected].

from pathlib import Path


def test_update(run_command):
res = run_command("update")
Expand Down Expand Up @@ -73,3 +75,25 @@ def test_update_with_url_internal_server_error(run_command, httpserver):
assert res.failed
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


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
24 changes: 24 additions & 0 deletions test/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# software without disclosing the source code of your own applications. To purchase
# a commercial license, send an email to [email protected].

from pathlib import Path


def test_upgrade(run_command):
# Updates index for cores and libraries
Expand Down Expand Up @@ -41,3 +43,25 @@ def test_upgrade(run_command):
result = run_command("outdated")
assert result.ok
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