-
-
Notifications
You must be signed in to change notification settings - Fork 408
Add library validation step when installing from zip or git #1234
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a49e375
Add library validation when installing from zip or git
silvanocerza 0a22a8a
[skip changelog] Fix tests temp directory deletion on Windows
silvanocerza 8fc7554
[skip changelog] Fix test_install_git_invalid_library test
silvanocerza db26701
[skip changelog] Add more check in lib install git repo tests
silvanocerza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,34 @@ | |
import pytest | ||
from git import Repo | ||
from pathlib import Path | ||
import tempfile | ||
import requests | ||
import zipfile | ||
import io | ||
import re | ||
|
||
|
||
# Util function to download library from URL | ||
def download_lib(url, download_dir): | ||
tmp = Path(tempfile.TemporaryDirectory().name) | ||
tmp.mkdir(parents=True, exist_ok=True) | ||
regex = re.compile(r"^(.*)-[0-9]+.[0-9]+.[0-9]") | ||
response = requests.get(url) | ||
# Download and unzips library removing version suffix | ||
with zipfile.ZipFile(io.BytesIO(response.content)) as thezip: | ||
for zipinfo in thezip.infolist(): | ||
with thezip.open(zipinfo) as f: | ||
dest_dir = tmp / regex.sub("\\g<1>", zipinfo.filename) | ||
if zipinfo.is_dir(): | ||
dest_dir.mkdir(parents=True, exist_ok=True) | ||
else: | ||
dest_dir.write_bytes(f.read()) | ||
|
||
# Recreates zip with folder without version suffix | ||
z = zipfile.ZipFile(download_dir, "w") | ||
for f in tmp.glob("**/*"): | ||
z.write(f, arcname=f.relative_to(tmp)) | ||
z.close() | ||
|
||
|
||
def test_list(run_command): | ||
|
@@ -252,8 +280,12 @@ def test_install_git_url_and_zip_path_flags_visibility(run_command, data_dir, do | |
assert res.failed | ||
assert "--git-url and --zip-path are disabled by default, for more information see:" in res.stderr | ||
|
||
assert run_command("lib download [email protected]") | ||
zip_path = Path(downloads_dir, "libraries", "AudioZero-1.0.0.zip") | ||
# Download library | ||
url = "https://github.com/arduino-libraries/AudioZero/archive/refs/tags/1.1.1.zip" | ||
zip_path = Path(downloads_dir, "libraries", "AudioZero.zip") | ||
zip_path.parent.mkdir(parents=True, exist_ok=True) | ||
download_lib(url, zip_path) | ||
|
||
res = run_command(f"lib install --zip-path {zip_path}") | ||
assert res.failed | ||
assert "--git-url and --zip-path are disabled by default, for more information see:" in res.stderr | ||
|
@@ -330,13 +362,16 @@ def test_install_with_zip_path(run_command, data_dir, downloads_dir): | |
assert run_command("config init --dest-dir .", custom_env=env) | ||
|
||
# Download a specific lib version | ||
assert run_command("lib download [email protected]") | ||
# Download library | ||
url = "https://github.com/arduino-libraries/AudioZero/archive/refs/tags/1.1.1.zip" | ||
zip_path = Path(downloads_dir, "libraries", "AudioZero.zip") | ||
zip_path.parent.mkdir(parents=True, exist_ok=True) | ||
download_lib(url, zip_path) | ||
|
||
lib_install_dir = Path(data_dir, "libraries", "AudioZero-1.0.0") | ||
lib_install_dir = Path(data_dir, "libraries", "AudioZero") | ||
# Verifies library is not already installed | ||
assert not lib_install_dir.exists() | ||
|
||
zip_path = Path(downloads_dir, "libraries", "AudioZero-1.0.0.zip") | ||
# Test zip-path install | ||
res = run_command(f"lib install --zip-path {zip_path}") | ||
assert res.ok | ||
|
@@ -688,13 +723,14 @@ def test_install_with_zip_path_multiple_libraries(run_command, downloads_dir, da | |
} | ||
|
||
# Downloads zip to be installed later | ||
assert run_command("lib download [email protected]") | ||
assert run_command("lib download [email protected]") | ||
wifi_zip_path = Path(downloads_dir, "libraries", "WiFi101-0.16.1.zip") | ||
ble_zip_path = Path(downloads_dir, "libraries", "ArduinoBLE-1.1.3.zip") | ||
download_lib("https://github.com/arduino-libraries/WiFi101/archive/refs/tags/0.16.1.zip", wifi_zip_path) | ||
download_lib("https://github.com/arduino-libraries/ArduinoBLE/archive/refs/tags/1.1.3.zip", ble_zip_path) | ||
|
||
wifi_install_dir = Path(data_dir, "libraries", "WiFi101") | ||
ble_install_dir = Path(data_dir, "libraries", "ArduinoBLE") | ||
|
||
wifi_install_dir = Path(data_dir, "libraries", "WiFi101-0.16.1") | ||
ble_install_dir = Path(data_dir, "libraries", "ArduinoBLE-1.1.3") | ||
# Verifies libraries are not installed | ||
assert not wifi_install_dir.exists() | ||
assert not ble_install_dir.exists() | ||
|
@@ -860,6 +896,7 @@ def test_install_zip_lib_with_macos_metadata(run_command, data_dir, downloads_di | |
assert lib_install_dir.exists() | ||
files = list(lib_install_dir.glob("**/*")) | ||
assert lib_install_dir / "library.properties" in files | ||
assert lib_install_dir / "src" / "fake-lib.h" in files | ||
|
||
# Reinstall library | ||
assert run_command(f"lib install --zip-path {zip_path}") | ||
|
@@ -868,3 +905,81 @@ def test_install_zip_lib_with_macos_metadata(run_command, data_dir, downloads_di | |
assert lib_install_dir.exists() | ||
files = list(lib_install_dir.glob("**/*")) | ||
assert lib_install_dir / "library.properties" in files | ||
assert lib_install_dir / "src" / "fake-lib.h" in files | ||
|
||
|
||
def test_install_zip_invalid_library(run_command, data_dir, downloads_dir): | ||
# Initialize configs to enable --zip-path flag | ||
env = { | ||
"ARDUINO_DATA_DIR": data_dir, | ||
"ARDUINO_DOWNLOADS_DIR": downloads_dir, | ||
"ARDUINO_SKETCHBOOK_DIR": data_dir, | ||
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true", | ||
} | ||
assert run_command("config init --dest-dir .", custom_env=env) | ||
|
||
lib_install_dir = Path(data_dir, "libraries", "lib-without-header") | ||
# Verifies library is not already installed | ||
assert not lib_install_dir.exists() | ||
|
||
zip_path = Path(__file__).parent / "testdata" / "lib-without-header.zip" | ||
# Test zip-path install | ||
res = run_command(f"lib install --zip-path {zip_path}") | ||
assert res.failed | ||
assert 'library is not valid: missing header file "lib-without-header.h"' in res.stderr | ||
|
||
lib_install_dir = Path(data_dir, "libraries", "lib-without-properties") | ||
# Verifies library is not already installed | ||
assert not lib_install_dir.exists() | ||
|
||
zip_path = Path(__file__).parent / "testdata" / "lib-without-properties.zip" | ||
# Test zip-path install | ||
res = run_command(f"lib install --zip-path {zip_path}") | ||
assert res.failed | ||
assert 'library is not valid: missing file "library.properties"' in res.stderr | ||
|
||
|
||
def test_install_git_invalid_library(run_command, data_dir, downloads_dir): | ||
# Initialize configs to enable --zip-path flag | ||
env = { | ||
"ARDUINO_DATA_DIR": data_dir, | ||
"ARDUINO_DOWNLOADS_DIR": downloads_dir, | ||
"ARDUINO_SKETCHBOOK_DIR": data_dir, | ||
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true", | ||
} | ||
assert run_command("config init --dest-dir .", custom_env=env) | ||
|
||
# Create fake library repository | ||
repo_dir = Path(data_dir, "lib-without-header") | ||
with Repo.init(repo_dir) as repo: | ||
lib_properties = Path(repo_dir, "library.properties") | ||
lib_properties.touch() | ||
repo.index.add([str(lib_properties)]) | ||
repo.index.commit("First commit") | ||
|
||
lib_install_dir = Path(data_dir, "libraries", "lib-without-header") | ||
# Verifies library is not already installed | ||
assert not lib_install_dir.exists() | ||
|
||
res = run_command(f"lib install --git-url {repo_dir}", custom_env=env) | ||
assert res.failed | ||
assert 'library is not valid: missing header file "lib-without-header.h"' in res.stderr | ||
silvanocerza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert not lib_install_dir.exists() | ||
|
||
# Create another fake library repository | ||
repo_dir = Path(data_dir, "lib-without-properties") | ||
with Repo.init(repo_dir) as repo: | ||
lib_header = Path(repo_dir, "src", "lib-without-properties.h") | ||
lib_header.parent.mkdir(parents=True, exist_ok=True) | ||
lib_header.touch() | ||
repo.index.add([str(lib_header)]) | ||
repo.index.commit("First commit") | ||
|
||
lib_install_dir = Path(data_dir, "libraries", "lib-without-properties") | ||
# Verifies library is not already installed | ||
assert not lib_install_dir.exists() | ||
|
||
res = run_command(f"lib install --git-url {repo_dir}", custom_env=env) | ||
assert res.failed | ||
assert 'library is not valid: missing file "library.properties"' in res.stderr | ||
assert not lib_install_dir.exists() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.