-
Notifications
You must be signed in to change notification settings - Fork 0
Add function to search for available crates within a directory #4
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
7 commits
Select commit
Hold shift + click to select a range
764776d
Add function to search for available crates within a directory
Blast545 3cd5ca8
Fix linter
Blast545 eaf2389
Fix linter
Blast545 154b422
Update pallet_patcher/search.py
Blast545 8d15a98
Update pallet_patcher/search.py
Blast545 cdf72b9
Track: deal with scenarios where a pkg is not found locally
Blast545 2a03746
Update test/packages/upper_layer/pkg-e-0.0.0/Cargo.toml
Blast545 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| [package] | ||
| name = "pkg-d" | ||
| version = "0.0.0" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Copyright 2025 Open Source Robotics Foundation, Inc. | ||
| # Licensed under the Apache License, Version 2.0 | ||
|
|
||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from pallet_patcher.search import _get_available_crates | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_crates_dir(tmp_path): | ||
| """ | ||
| Create a temporary directory structure mimicking a Rust workspace. | ||
|
|
||
| Structure: | ||
| /tmp_dir/ | ||
| ├── crate_a/Cargo.toml | ||
| ├── crate_b_v1/Cargo.toml | ||
| └── crate_b_v2/Cargo.toml | ||
| """ | ||
| (tmp_path / 'crate_a').mkdir() | ||
| (tmp_path / 'crate_a' / 'Cargo.toml').touch() | ||
|
|
||
| (tmp_path / 'crate_b_v1').mkdir() | ||
| (tmp_path / 'crate_b_v1' / 'Cargo.toml').touch() | ||
|
|
||
| (tmp_path / 'crate_b_v2').mkdir() | ||
| (tmp_path / 'crate_b_v2' / 'Cargo.toml').touch() | ||
|
|
||
| return tmp_path | ||
|
|
||
|
|
||
| def test_get_available_crates_structure(mock_crates_dir): | ||
| """Tests that the function correctly parses a directory of crates.""" | ||
| # We define what load_manifest should return based on the path it receives | ||
| def manifest_side_effect(path): | ||
| str_path = str(path) | ||
| if 'crate_a' in str_path: | ||
| return {'package': {'name': 'alpha-lib', 'version': '0.1.0'}} | ||
| elif 'crate_b_v1' in str_path: | ||
| return {'package': {'name': 'beta-lib', 'version': '1.0.0'}} | ||
| elif 'crate_b_v2' in str_path: | ||
| return {'package': {'name': 'beta-lib', 'version': '2.0.0'}} | ||
| return {} | ||
|
|
||
| # Patch 'load_manifest' in the scope where _get_available_crates is defined | ||
| with patch('pallet_patcher.search.load_manifest', | ||
| side_effect=manifest_side_effect): | ||
| versions, metadata = _get_available_crates(mock_crates_dir) | ||
|
|
||
| assert 'alpha-lib' in versions | ||
| assert 'beta-lib' in versions | ||
|
|
||
| assert versions['alpha-lib'] == {'0.1.0'} | ||
| assert versions['beta-lib'] == {'1.0.0', '2.0.0'} | ||
|
|
||
| key_alpha = 'alpha-lib+0.1.0' | ||
| key_beta_1 = 'beta-lib+1.0.0' | ||
| key_beta_2 = 'beta-lib+2.0.0' | ||
|
|
||
| assert key_alpha in metadata | ||
| assert key_beta_1 in metadata | ||
| assert key_beta_2 in metadata | ||
|
|
||
| # Check content (Path, Manifest) | ||
| # Note: metadata values are (parent_path, manifest_dict) | ||
|
|
||
| assert metadata[key_alpha][0].name == 'crate_a' | ||
| assert metadata[key_alpha][1]['package']['name'] == 'alpha-lib' | ||
|
|
||
| assert metadata[key_beta_1][0].name == 'crate_b_v1' | ||
| assert metadata[key_beta_1][1]['package']['name'] == 'beta-lib' | ||
|
|
||
| assert metadata[key_beta_2][0].name == 'crate_b_v2' | ||
| assert metadata[key_beta_2][1]['package']['name'] == 'beta-lib' | ||
|
|
||
|
|
||
| def test_get_available_crates_empty(tmp_path): | ||
| """Tests behavior when directory is empty or has no Cargo.toml files.""" | ||
| versions, metadata = _get_available_crates(tmp_path) | ||
| assert not versions | ||
| assert not metadata | ||
|
|
||
|
|
||
| def test_real_folder_integration(): | ||
| real_packages_path = Path(__file__).parent / 'packages' / 'upper_layer' | ||
|
|
||
| if not real_packages_path.exists(): | ||
| pytest.skip('Skipping: "packages" directory not found locally.') | ||
|
|
||
| versions, metadata = _get_available_crates(real_packages_path) | ||
|
|
||
| assert 'pkg-d' in versions | ||
| assert 'pkg-e' in versions | ||
|
|
||
| assert versions['pkg-d'] == {'0.0.0'} | ||
| assert versions['pkg-e'] == {'0.0.0'} | ||
|
|
||
| key_d = 'pkg-d+0.0.0' | ||
| key_e = 'pkg-e+0.0.0' | ||
|
|
||
| assert key_d in metadata | ||
| assert key_e in metadata | ||
|
|
||
| assert metadata[key_d][0].name == 'pkg-d-0.0.0' | ||
| assert metadata[key_d][1]['package']['name'] == 'pkg-d' | ||
|
|
||
| assert metadata[key_e][0].name == 'pkg-e-0.0.0' | ||
| assert metadata[key_e][1]['package']['name'] == 'pkg-e' |
Oops, something went wrong.
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.