Skip to content

Commit 5dd6d7b

Browse files
author
Jakub Ruzicka
committed
tests: fix gerrit query related unit tests
These tests were merged in a hurry and I think they never worked. I fixed the tests and adjusted utils.tidy_ssh_user to match expected behavior in an edge case. Change-Id: I3011e0dc7b453db8d29be947b4b7c01201eec6fa
1 parent d7123f0 commit 5dd6d7b

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

rdopkg/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def tidy_ssh_user(url=None, user=None):
77
# is there a user already ?
88
match = re.compile('ssh://([^@]+)@.+').match(url)
99
if match:
10-
ssh_user = match.groups()[0]
10+
ssh_user = match.group(1)
1111
if user and ssh_user != user:
1212
# assume prevalence of argument
13-
url.replace(ssh_user + '@',
14-
user + '@')
13+
url = url.replace(re.escape(ssh_user) + '@',
14+
user + '@')
1515
elif user:
1616
url = 'ssh://' + \
1717
user + '@' + \

tests/test_utils.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@
33

44
def test_url_tidying():
55
# Do nothing when nothing is provided
6-
assert(None, utils.tidy_ssh_user())
6+
assert utils.tidy_ssh_user() is None
77
# Do nothing when url is not provided
8-
assert(None, utils.tidy_ssh_user(url=None, user='kyle'))
8+
assert utils.tidy_ssh_user(url=None, user='kyle') is None
99
# Do nothing when url doesn't start with ssh
1010
uri = 'http://mygitrepo/myproject.git'
11-
assert(uri, utils.tidy_ssh_user(uri))
12-
assert(uri, utils.tidy_ssh_user(uri, user='stan'))
11+
assert utils.tidy_ssh_user(uri) == uri
12+
assert utils.tidy_ssh_user(uri, user='stan') == uri
1313
# Do nothing if url starts with ssh, has no user, and no user provided
1414
uri = 'ssh://%smygitrepo/myproject.git'
15-
assert(uri % '', utils.tidy_ssh_user(uri % ''))
15+
assert utils.tidy_ssh_user(uri % '') == uri % ''
1616
# Add missing user if provided
17-
assert(uri % 'kenny@', utils.tidy_ssh_user(uri % '', user='kenny'))
17+
assert utils.tidy_ssh_user(uri % '', user='kenny') == uri % 'kenny@'
1818
# keep default user if no user provided
19-
assert(uri % 'cartman@', utils.tidy_ssh_user(uri % 'cartman@'))
19+
assert utils.tidy_ssh_user(uri % 'cartman@') == uri % 'cartman@'
2020
# change default user if user provided
21-
assert(uri % 'butters@',
22-
utils.tidy_ssh_user(uri % 'timmy@', user='butters'))
21+
assert utils.tidy_ssh_user(uri % 'timmy@', user='dude') == uri % 'dude@'
2322

2423

25-
def test_GerritQuery(monkeypatch):
24+
def test_gerrit_query(monkeypatch):
2625
mock_result = """{"project":"ironic","branch":"liberty-patches","topic":"p1","id":"Ie0db0345c9d5f9ad3d2ec4880e084cd38d06a6dc","number":"368","subject":"Set default DB location","owner":{"name":"Fabien Boucher","email":"fabien.dot.boucher@gmail.com","username":"morucci"},"url":"http://rpmfactory.beta.rdoproject.org/r/368","commitMessage":"Set default DB location","createdOn":1453933391,"lastUpdated":1456332266,"open":true,"status":"NEW"}
2726
{"type":"stats","rowCount":1,"runTimeMilliseconds":4,"moreChanges":true}"""
2827

@@ -31,9 +30,8 @@ def mock_run(*args, **kwargs):
3130
monkeypatch.setattr(utils.cmd, 'run', mock_run)
3231
g = utils.cmd.GerritQuery('gerrithost', '29418')
3332
r = g('project:ironic')
34-
assert(True, isinstance(r, dict))
35-
assert("Ie0db0345c9d5f9ad3d2ec4880e084cd38d06a6dc",
36-
r['id'])
33+
assert isinstance(r, dict) == True
34+
assert r['id'] == "Ie0db0345c9d5f9ad3d2ec4880e084cd38d06a6dc"
3735
mock_result = """{"type":"stats","rowCount":0,"runTimeMilliseconds":4,"moreChanges":false}"""
3836
r = g('project:ironic')
39-
assert(None, r)
37+
assert r is None

0 commit comments

Comments
 (0)