-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add support to fetch purl #179
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
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,41 @@ Fetch some package metadata and get a ``fetchcode.packagedcode_models.Package`` | |
>>> list(package.info('pkg:rubygems/files')) | ||
[Package(type='rubygems', namespace=None, name='files', version=None)] | ||
|
||
Fetch a purl and get a ``fetchcode.fetch.Response`` object back:: | ||
|
||
>>> from fetchcode import fetch | ||
>>> f = fetch('pkg:swift/github.com/Alamofire/[email protected]') | ||
>>> f.location | ||
'/tmp/tmp_cm02xsg' | ||
>>> f.content_type | ||
'application/zip' | ||
>>> f.url | ||
'https://github.com/Alamofire/Alamofire/archive/5.4.3.zip' | ||
|
||
Ecosystems supported for fetching a purl from fetchcode: | ||
|
||
- alpm | ||
- apk | ||
- bitbucket | ||
- cargo | ||
- composer | ||
- conda | ||
- cpan | ||
- cran | ||
- deb | ||
- gem | ||
- generic | ||
- github | ||
- golang | ||
- hackage | ||
- hex | ||
- luarocks | ||
- maven | ||
- npm | ||
- nuget | ||
- pub | ||
- pypi | ||
- swift | ||
|
||
License | ||
-------- | ||
|
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
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 |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
import pytest | ||
|
||
from fetchcode import fetch | ||
from fetchcode import resolve_purl | ||
from fetchcode import resolve_url_from_purl | ||
|
||
|
||
@mock.patch("fetchcode.requests.get") | ||
|
@@ -63,3 +65,123 @@ def test_fetch_with_scheme_not_present(): | |
url = "abc://speedtest/1KB.zip" | ||
response = fetch(url=url) | ||
assert "Not a supported/known scheme." == e_info | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
@mock.patch("fetchcode.fetch_http") | ||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_fetch_purl_with_fetchcode(mock_fetch_json_response, mock_fetch_http, mock_http_exists): | ||
mock_fetch_http.return_value = "mocked_purl_response" | ||
mock_http_exists.return_value = True | ||
mock_fetch_json_response.return_value = { | ||
"urls": [{"url": "https://example.com/sample-1.0.0.zip"}] | ||
} | ||
|
||
response = fetch("pkg:pypi/[email protected]") | ||
|
||
assert response == "mocked_purl_response" | ||
mock_http_exists.assert_called_once() | ||
mock_fetch_http.assert_called_once() | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
@mock.patch("fetchcode.fetch_http") | ||
def test_fetch_purl_with_purl2url(mock_fetch_http, mock_http_exists): | ||
mock_fetch_http.return_value = "mocked_purl_response" | ||
mock_http_exists.return_value = True | ||
|
||
response = fetch("pkg:alpm/[email protected]") | ||
|
||
assert response == "mocked_purl_response" | ||
mock_http_exists.assert_called_once() | ||
mock_fetch_http.assert_called_once() | ||
|
||
|
||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_fetch_invalid_purl(mock_fetch_json_response): | ||
mock_fetch_json_response.return_value = {} | ||
|
||
with pytest.raises(Exception, match="No download URL found for invalid-package version 1.0.0"): | ||
fetch("pkg:pypi/[email protected]") | ||
|
||
|
||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_fetch_invalid_purl(mock_fetch_json_response): | ||
mock_fetch_json_response.return_value = {} | ||
|
||
with pytest.raises(Exception, match="No download URL found for invalid-package version 1.0.0"): | ||
fetch("pkg:pypi/[email protected]") | ||
|
||
|
||
def test_fetch_unsupported_scheme(): | ||
with pytest.raises(Exception, match="Not a supported/known scheme"): | ||
fetch("s3://bucket/object") | ||
|
||
|
||
def test_resolve_url_from_purl_invalid(): | ||
with pytest.raises(ValueError, match="Could not resolve PURL to a valid URL."): | ||
fetch("pkg:invalid/[email protected]") | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
def test_resolve_url_from_purl_using_purl2url(mock_http_exists): | ||
mock_http_exists.return_value = True | ||
|
||
url, _ = resolve_url_from_purl("pkg:swift/github.com/Alamofire/[email protected]") | ||
assert url == "https://github.com/Alamofire/Alamofire/archive/5.4.3.zip" | ||
mock_http_exists.assert_called_once_with( | ||
"https://github.com/Alamofire/Alamofire/archive/5.4.3.zip" | ||
) | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_resolve_url_from_purl_using_fetchcode(mock_fetch_json_response, mock_http_exists): | ||
mock_http_exists.return_value = True | ||
mock_fetch_json_response.return_value = { | ||
"urls": [{"url": "https://example.com/sample-1.0.0.zip"}] | ||
} | ||
|
||
url, _ = resolve_url_from_purl("pkg:pypi/[email protected]") | ||
assert url == "https://example.com/sample-1.0.0.zip" | ||
mock_http_exists.assert_called_once_with("https://example.com/sample-1.0.0.zip") | ||
|
||
|
||
def test_resolve_purl_invalid(): | ||
assert resolve_purl("pkg:invalid/[email protected]") is None | ||
|
||
|
||
def test_resolve_purl_using_purl2url(): | ||
url = resolve_purl("pkg:pub/[email protected]") | ||
assert url == "https://pub.dev/api/archives/http-0.13.3.tar.gz" | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
def test_resolve_purl_using_purl2url_url_does_not_exists(mock_http_exists): | ||
mock_http_exists.return_value = False | ||
url = resolve_purl("pkg:pub/[email protected]") | ||
assert url is None | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_resolve_purl_using_fetchcode(mock_fetch_json_response, mock_http_exists): | ||
mock_fetch_json_response.return_value = { | ||
"urls": [{"url": "https://example.com/sample-1.0.0.zip"}] | ||
} | ||
mock_http_exists.return_value = True | ||
url = resolve_purl("pkg:pypi/[email protected]") | ||
assert url == "https://example.com/sample-1.0.0.zip" | ||
|
||
|
||
@mock.patch("fetchcode._http_exists") | ||
@mock.patch("fetchcode.pypi.fetch_json_response") | ||
def test_resolve_purl_using_fetchcode_url_does_not_exists( | ||
mock_fetch_json_response, mock_http_exists | ||
): | ||
mock_fetch_json_response.return_value = { | ||
"urls": [{"url": "https://example.com/sample-1.0.0.zip"}] | ||
} | ||
mock_http_exists.return_value = False | ||
url = resolve_purl("pkg:pypi/[email protected]") | ||
assert url is None |
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.