8000 Testing is hard ;) · n1k0/github3.py@8b08bcf · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div class="d-none">

Commit 8b08bcf

Browse files
committed
Testing is hard ;)
1 parent b113199 commit 8b08bcf

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: python
22
python:
33
- 2.6
44
- 2.7
5+
- 3.1
56
- 3.2
67
- pypy
78
# command to install dependencies, e.g. pip install -r requirements.txt
@@ -17,3 +18,7 @@ notifications:
1718
branches:
1819
except:
1920
- uricore
21+
22+
matrix:
23+
allow_failures:
24+
- python: 3.1

github3/repos.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from base64 import b64decode
1010
from requests import post
11+
from collections import Callable
1112
from github3.events import Event
1213
from github3.issues import Issue, IssueEvent, Label, Milestone, issue_params
1314
from github3.git import Blob, Commit, Reference, Tag, Tree
@@ -158,12 +159,12 @@ def archive(self, format, path='', ref='master'):
158159
url = self._build_url(format, ref, base_url=self._api)
159160
resp = self._get(url, allow_redirects=True, prefetch=False)
160161

161-
fd = None
162-
file_like = False
163-
if resp and resp.ok:
162+
pre_opened = False
163+
if resp and self._boolean(resp, 200, 404):
164+
fd = None
164165
if path:
165-
if callable(getattr(path, 'write', None)):
166-
file_like = True
166+
if isinstance(getattr(path, 'write', None), Callable):
167+
pre_opened = True
167168
fd = path
168169
else:
169170
fd = open(path, 'wb')
@@ -174,7 +175,7 @@ def archive(self, format, path='', ref='master'):
174175
for chunk in resp.iter_content():
175176
fd.write(chunk)
176177

177-
if not file_like:
178+
if not pre_opened:
178179
fd.close()
179180

180181
written = True
@@ -1411,7 +1412,7 @@ def saveas(self, path=''):
14111412

14121413
resp = self._get(self.html_url, allow_redirects=True, prefetch=False)
14131414
if self._boolean(resp, 200, 404):
1414-
if callable(getattr(path, 'write', None)):
1415+
if isinstance(getattr(path, 'write', None), Callable):
14151416
file_like = True
14161417
fd = path
14171418
else:

tests/json/archive

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
archive_data

tests/test_repos.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import github3
23
from tests.utils import (generate_response, expect, BaseCase, load)
34

@@ -26,7 +27,34 @@ def test_add_collaborator(self):
2627
self.mock_assertions()
2728

2829
def test_archive(self):
29-
pass
30+
headers = {'content-disposition': 'filename=foo'}
31+
self.request.return_value = generate_response('archive', 200,
32+
**headers)
33+
self.args = ('get', self.api + 'tarball/master')
34+
self.conf.update({'prefetch': False})
35+
36+
expect(self.repo.archive(None)).is_False()
37+
38+
expect(os.path.isfile('foo')).is_False()
39+
expect(self.repo.archive('tarball')).is_True()
40+
expect(os.path.isfile('foo')).is_True()
41+
os.unlink('foo')
42+
self.mock_assertions()
43+
44+
self.request.return_value.raw.seek(0)
45+
self.request.return_value._content_consumed = False
46+
47+
expect(os.path.isfile('path_to_file')).is_False()
48+
expect(self.repo.archive('tarball', 'path_to_file')).is_True()
49+
expect(os.path.isfile('path_to_file')).is_True()
50+
os.unlink('path_to_file')
51+
52+
self.request.return_value.raw.seek(0)
53+
self.request.return_value._content_consumed = False
54+
55+
self.args = ('get', self.api + 'zipball/randomref')
56+
expect(self.repo.archive('zipball', ref='randomref')).is_True()
57+
os.unlink('foo')
3058

3159
def test_blob(self):
3260
self.request.return_value = generate_response('blob')

tests/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import github3
33
import expecter
44
import json
5+
import sys
56
from mock import patch, call
67
from io import BytesIO
78
from unittest import TestCase
@@ -17,7 +18,10 @@ def generate_response(path_name, status_code=200, enc='utf-8', _iter=False,
1718
if _iter:
1819
content = path(path_name).read().strip()
1920
content = '[{0}]'.format(content)
20-
r.raw = BytesIO(content.encode('utf-8'))
21+
r.raw = BytesIO(content.encode())
22+
elif sys.version_info > (3, 0):
23+
content = path(path_name).read().strip()
24+
r.raw = BytesIO(content.encode())
2125
else:
2226
r.raw = path(path_name)
2327
else:

0 commit comments

Comments
 (0)
0