-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_github_tarballs.py
78 lines (64 loc) · 2.89 KB
/
test_github_tarballs.py
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
# Copyright 2012 SUSE Linux
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from contextlib import contextmanager, nested
import imp
import io
import unittest
from mock import patch
GITHUB_API = "https://api.github.com"
ghb = imp.load_source('ghb', 'github_tarballs')
class TestGitHubTarballs(unittest.TestCase):
def test_version_parse(self):
with mock_open(u"\nVersion: 2012.2.3+git.1355917214.0c8c2a3\n"):
self.assertEqual('0c8c2a3',
ghb.get_commit_from_spec('example_pkg'))
def test_version_parse_comment(self):
with mock_open(
u"\nVersion: 2012.2.3+git.1355917214.0c8c2a3 # oi comment\n"):
self.assertEqual('0c8c2a3',
ghb.get_commit_from_spec('example_pkg'))
def test_download_tarball(self):
with patch("urllib.urlretrieve") as urlretrieve:
ghb.download_tarball('https://example.com/target.tar.gz',
'filename')
self.assertEqual(
(("https://example.com/target.tar.gz",
"filename"),),
urlretrieve.call_args)
def test_get_changes(self):
response = io.StringIO(u'{"commits": [1, 2, 3]}')
response.code = 200
with nested(patch("ghb.get_commit_from_spec", return_value="1234"),
patch("ghb.github_credentials", return_value=""),
patch("urllib.urlopen", return_value=response)
) as (_, _, urlopen):
self.assertEqual({'commits': [1, 2, 3]},
ghb.get_changes("package", "owner", "repo",
"target"))
self.assertEqual(
((GITHUB_API + "/repos/owner/repo/compare/1234...target",),),
urlopen.call_args)
def test_github_credentials_empty_file(self):
with mock_open(u""):
self.assertEqual("", ghb.github_credentials())
def test_github_credentials_ioerror(self):
with patch("__builtin__.open", side_effect=IOError):
self.assertEqual("", ghb.github_credentials())
def test_github_credentials_read_from_file(self):
with mock_open(u"user:pass"):
self.assertEqual("user:pass@", ghb.github_credentials())
@contextmanager
def mock_open(contents):
with patch("__builtin__.open", return_value=io.StringIO(contents)):
yield