forked from nephila/giturlparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rewrite.py
More file actions
110 lines (98 loc) · 4.7 KB
/
Copy pathtest_rewrite.py
File metadata and controls
110 lines (98 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import unittest
from giturlparse import parse
# Test data
REWRITE_URLS = (
# GitHub SSH
("git@github.com:Org/Repo.git", "ssh", "git@github.com:Org/Repo.git"),
("git@github.com:Org/Repo.git/", "ssh", "git@github.com:Org/Repo.git"),
("git@github.com/Org/Repo", "ssh", "git@github.com:Org/Repo.git"),
("git@github.com/Org/Repo/", "ssh", "git@github.com:Org/Repo.git"),
("git@github.com:Org/Repo.git", "https", "https://github.com/Org/Repo.git"),
("git@github.com:Org/Repo.git", "git", "git://github.com/Org/Repo.git"),
# GitHub HTTPS
("https://github.com/Org/Repo.git", "ssh", "git@github.com:Org/Repo.git"),
("https://github.com/Org/Repo.git", "https", "https://github.com/Org/Repo.git"),
("https://github.com/Org/Repo.git", "git", "git://github.com/Org/Repo.git"),
# GitHub GIT
("git://github.com/Org/Repo.git", "ssh", "git@github.com:Org/Repo.git"),
("git://github.com/Org/Repo.git", "https", "https://github.com/Org/Repo.git"),
("git://github.com/Org/Repo.git", "git", "git://github.com/Org/Repo.git"),
(
"git://github.com/Org/Repo/blob/master/dir/subdir/path",
"git",
"git://github.com/Org/Repo.git/blob/master/dir/subdir/path",
),
# BitBucket SSH
("git@bitbucket.org:Org/Repo.git", "ssh", "git@bitbucket.org:Org/Repo.git"),
("git@bitbucket.org:Org/Repo.git", "https", "https://Org@bitbucket.org/Org/Repo.git"),
# BitBucket HTTPS
("https://Org@bitbucket.org/Org/Repo.git", "ssh", "git@bitbucket.org:Org/Repo.git"),
("https://Org@bitbucket.org/Org/Repo.git", "https", "https://Org@bitbucket.org/Org/Repo.git"),
# Assembla GIT
("git://git.assembla.com/SomeRepoID.git", "ssh", "git@git.assembla.com:SomeRepoID.git"),
("git://git.assembla.com/SomeRepoID.git", "git", "git://git.assembla.com/SomeRepoID.git"),
# Assembla SSH
("git@git.assembla.com:SomeRepoID.git", "ssh", "git@git.assembla.com:SomeRepoID.git"),
("git@git.assembla.com:SomeRepoID.git", "git", "git://git.assembla.com/SomeRepoID.git"),
# FriendCode HTTPS
("https://friendco.de/Aaron@user/test-repo.git", "https", "https://friendco.de/Aaron@user/test-repo.git"),
# Generic
("git://git.buildroot.net/buildroot", "https", "https://git.buildroot.net/buildroot.git"),
("https://git.buildroot.net/buildroot", "git", "git://git.buildroot.net/buildroot.git"),
("https://git.buildroot.net/buildroot", "ssh", "git@git.buildroot.net:buildroot.git"),
# Gitlab SSH
("git@host.org:Org/Repo.git", "ssh", "git@host.org:Org/Repo.git"),
("git@host.org:9999/Org/Repo.git", "ssh", "git@host.org:9999/Org/Repo.git"),
("git@host.org:Org/Repo.git", "https", "https://host.org/Org/Repo.git"),
("git@host.org:9999/Org/Repo.git", "https", "https://host.org/Org/Repo.git"),
# Gitlab HTTPS
("https://host.org/Org/Repo.git", "ssh", "git@host.org:Org/Repo.git"),
("https://host.org/Org/Repo.git", "https", "https://host.org/Org/Repo.git"),
("https://host.org/Org/Group/Repo.git", "ssh", "git@host.org:Org/Group/Repo.git"),
(
"https://host.org/Org/Group/Repo/-/blob/master/file.py",
"ssh",
"git@host.org:Org/Group/Repo.git/-/blob/master/file.py",
),
(
"https://host.org/Org/Group/Repo/blob/master/file.py",
"ssh",
"git@host.org:Org/Group/Repo.git/blob/master/file.py",
),
)
INVALID_PARSE_URLS = (
("SSH Bad Username", "gitx@github.com:Org/Repo.git"),
("SSH No Repo", "git@github.com:Org"),
("HTTPS No Repo", "https://github.com/Org"),
("GIT No Repo", "git://github.com/Org"),
)
REWRITE_COMPONENTS = {
"name": {
"source": "git@github.com:Org/Repo.git",
"value": "New-repo",
"expected": "git@github.com:Org/New-repo.git",
},
"owner": {
"source": "git@github.com:Org/Repo.git",
"value": "New-Org",
"expected": "git@github.com:New-Org/Repo.git",
},
}
class UrlRewriteTestCase(unittest.TestCase):
def _test_rewrite(self, source, protocol, expected):
parsed = parse(source)
self.assertTrue(parsed.valid, "Invalid Url: %s" % source)
return self.assertEqual(parse(source).format(protocol), expected)
def test_rewrites(self):
for data in REWRITE_URLS:
self._test_rewrite(*data)
def _test_rewrite_components(self, field, data):
parsed = parse(data["source"])
setattr(parsed, field, data["value"])
self.assertTrue(parsed.valid, "Invalid Url: %s" % data["source"])
return self.assertEqual(parsed.url, data["expected"])
def test_rewrite_components(self):
for field, data in REWRITE_COMPONENTS.items():
self._test_rewrite_components(field, data)
# Test Suite
suite = unittest.TestLoader().loadTestsFromTestCase(UrlRewriteTestCase)