Skip to content

Commit 55696d1

Browse files
authored
Merge pull request #16 from TG1999/rust-tags
Add tag support for rust packages
2 parents d14a898 + bd22e3c commit 55696d1

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

package_registry/rust_versions.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import requests
18+
from urllib.parse import urlparse
19+
20+
21+
def build_url(package):
22+
"""
23+
Return a URL to access the list of versions using the Crates API
24+
"""
25+
base_url='https://crates.io/api/v1/crates'
26+
return '{}/{}'.format(base_url, package)
27+
28+
29+
def get_versions(package):
30+
"""
31+
Return a list of crate versions given the `package` from Crate packages
32+
"""
33+
34+
url = build_url(package)
35+
resp = requests.get(url)
36+
37+
tags = []
38+
39+
response = resp.json()
40+
41+
versions = response.get('versions',[])
42+
for version in versions:
43+
if 'num' in version:
44+
tags.append(version.get('num'))
45+
46+
return tags

tests/data/rust_versions_response.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/test_rust_versions.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import json
18+
from unittest import mock
19+
20+
from package_registry import rust_versions
21+
22+
23+
@mock.patch('package_registry.rust_versions.requests.get')
24+
def test_rust_versions_for_not_empty_list(mock_get):
25+
package = 'rand'
26+
with open('tests/data/rust_versions_response.json') as file:
27+
data = file.read()
28+
mock_get.return_value.json.return_value = json.loads(data)
29+
response = rust_versions.get_versions(package=package)
30+
tags = ['0.7.3', '0.7.2', '0.7.1', '0.7.0', '0.7.0-pre.2', '0.7.0-pre.1', '0.7.0-pre.0', '0.6.5', '0.6.4', '0.6.3', '0.6.2', '0.6.1', '0.6.0', '0.6.0-pre.1', '0.6.0-pre.0', '0.5.6', '0.5.5', '0.5.4', '0.5.3', '0.5.2', '0.5.1', '0.5.0', '0.5.0-pre.2', '0.5.0-pre.1', '0.5.0-pre.0', '0.4.6', '0.4.5', '0.4.4', '0.4.3', '0.4.2', '0.4.1', '0.4.0-pre.0', '0.3.23', '0.3.22', '0.3.21-pre.0', '0.3.20', '0.3.19', '0.3.18', '0.3.17', '0.3.16', '0.3.15', '0.3.14', '0.3.13', '0.3.12', '0.3.11', '0.3.10', '0.3.9', '0.3.8', '0.3.7', '0.3.6', '0.3.5', '0.3.4', '0.3.3', '0.3.2', '0.3.1', '0.3.0', '0.2.1', '0.2.0', '0.1.4', '0.1.3', '0.1.2', '0.1.1']
31+
for tag in tags:
32+
assert tag in response
33+
assert '0.7.9' not in response
34+
35+
def test_build_url():
36+
package = 'libc'
37+
expected = 'https://crates.io/api/v1/crates/libc'
38+
url = rust_versions.build_url(package=package)
39+
assert expected == url
40+
41+
@mock.patch('package_registry.rust_versions.requests.get')
42+
def test_rust_version_with_None(mock_get):
43+
package = None
44+
mock_get.return_value.json.return_value = {'errors': [{'detail': 'Not Found'}]}
45+
response = rust_versions.get_versions(package=package)
46+
assert [] == response
47+
48+
@mock.patch('package_registry.rust_versions.requests.get')
49+
def test_rust_version_with_empty_package_name(mock_get):
50+
package = ''
51+
mock_get.return_value.json.return_value = {'errors': [{'detail': 'Not Found'}]}
52+
response = rust_versions.get_versions(package=package)
53+
assert [] == response
54+
55+
@mock.patch('package_registry.rust_versions.requests.get')
56+
def test_rust_version_with_junk_package_name(mock_get):
57+
package = '!@#$%^*&*()"ABCD1234'
58+
mock_get.return_value.json.return_value = {'errors': [{'detail': 'Not Found'}]}
59+
response = rust_versions.get_versions(package=package)
60+
assert [] == response

0 commit comments

Comments
 (0)