Skip to content

Commit 2770730

Browse files
author
Li
committed
#429: urls: Add test cases and update the code and commit to make more clear
1 parent 99f0a0d commit 2770730

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/packagedcode/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ def parse_repo_url(repo_url):
6868
[email protected]:foo/private.git
6969
"""
7070

71-
# TODO: Improve this and use outside of NPMs
7271
is_vcs_url = repo_url.startswith(VCS_URLS)
7372
if is_vcs_url:
74-
# TODO: ensure the .git suffix is present if needed
73+
# TODO: If we match http and https, we may should add more check in case if the url is not a repo one.
74+
# For example, check the domain name in the url...
7575
return repo_url
7676

7777
if repo_url.startswith('git@'):
7878
left, right = repo_url.split('@', 1)
7979
host, repo = right.split(':', 1)
80-
# may be we should
8180
if any(h in host for h in ['github', 'bitbucket', 'gitlab']):
8281
return 'https://%(host)s/%(repo)s' % locals()
8382
else:
@@ -88,9 +87,9 @@ def parse_repo_url(repo_url):
8887

8988
elif repo_url.startswith(('bitbucket:', 'gitlab:', 'github:')):
9089
hoster_urls = {
91-
'bitbucket:': 'https://bitbucket.org/%(repo)s',
92-
'github:': 'https://github.com/%(repo)s',
93-
'gitlab:': 'https://gitlab.com/%(repo)s',
90+
'bitbucket': 'https://bitbucket.org/%(repo)s',
91+
'github': 'https://github.com/%(repo)s',
92+
'gitlab': 'https://gitlab.com/%(repo)s',
9493
}
9594
hoster, repo = repo_url.split(':', 1)
9695
return hoster_urls[hoster] % locals()

tests/packagedcode/test_utils.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
3+
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
4+
# The ScanCode software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode require an acknowledgment.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# When you publish or redistribute any data created with ScanCode or any ScanCode
16+
# derivative work, you must accompany this data with the following acknowledgment:
17+
#
18+
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
19+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
20+
# ScanCode should be considered or used as legal advice. Consult an Attorney
21+
# for any legal advice.
22+
# ScanCode is a free software code scanning tool from nexB Inc. and others.
23+
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
24+
25+
from __future__ import absolute_import
26+
from __future__ import print_function
27+
28+
import os.path
29+
30+
from commoncode.testcase import FileBasedTesting
31+
from packagedcode.utils import parse_repo_url
32+
33+
34+
class TestUtilsPi(FileBasedTesting):
35+
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
36+
37+
def test_parse_url(self):
38+
inputs = ['gist:11081aaa281',
39+
'bitbucket:example/repo',
40+
'gitlab:another/repo',
41+
'expressjs/serve-static',
42+
'git://github.com/angular/di.js.git',
43+
'git://github.com/hapijs/boom',
44+
'[email protected]:balderdashy/waterline-criteria.git',
45+
'http://github.com/ariya/esprima.git',
46+
'http://github.com/isaacs/nopt',
47+
'https://github.com/chaijs/chai',
48+
'https://github.com/christkv/kerberos.git',
49+
'https://gitlab.com/foo/private.git',
50+
'[email protected]:foo/private.git'
51+
]
52+
results = []
53+
for input in inputs:
54+
results.append(parse_repo_url(input))
55+
expected = ['gist:11081aaa281',
56+
'https://bitbucket.org/example/repo',
57+
'https://gitlab.com/another/repo',
58+
'https://github.com/expressjs/serve-static',
59+
'git://github.com/angular/di.js.git',
60+
'git://github.com/hapijs/boom',
61+
'https://github.com/balderdashy/waterline-criteria.git',
62+
'http://github.com/ariya/esprima.git',
63+
'http://github.com/isaacs/nopt',
64+
'https://github.com/chaijs/chai',
65+
'https://github.com/christkv/kerberos.git',
66+
'https://gitlab.com/foo/private.git',
67+
'https://gitlab.com/foo/private.git'
68+
]
69+
self.assertEquals(expected, results)
70+

0 commit comments

Comments
 (0)