Merge branch 'topic/CheckPragmaNoCover' into develop

This commit is contained in:
Vincent Jacques
2013-03-28 21:52:49 +01:00
11 changed files with 77 additions and 27 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ import GithubException
class _NotSetType:
def __repr__(self):
return "NotSet" # pragma no cover
return "NotSet"
NotSet = _NotSetType()
+8 -10
View File
@@ -25,8 +25,8 @@ atLeastPython3 = sys.hexversion >= 0x03000000
if atLeastPython26:
import json
else: # pragma no cover
import simplejson as json # pragma no cover
else: # pragma no cover (Covered by all tests with Python 2.5)
import simplejson as json # pragma no cover (Covered by all tests with Python 2.5)
import GithubException
@@ -49,7 +49,7 @@ class Requester:
if password is not None:
login = login_or_token
if atLeastPython3:
self.__authorizationHeader = "Basic " + base64.b64encode((login + ":" + password).encode("utf-8")).decode("utf-8").replace('\n', '') # pragma no cover
self.__authorizationHeader = "Basic " + base64.b64encode((login + ":" + password).encode("utf-8")).decode("utf-8").replace('\n', '') # pragma no cover (Covered by Authentication.testAuthorizationHeaderWithXxx with Python 3)
else:
self.__authorizationHeader = "Basic " + base64.b64encode(login + ":" + password).replace('\n', '')
elif login_or_token is not None:
@@ -70,7 +70,7 @@ class Requester:
elif o.scheme == "http":
self.__connectionClass = self.__httpConnectionClass
else:
assert False, "Unknown URL scheme" # pragma no cover
assert False, "Unknown URL scheme"
self.rate_limiting = (5000, 5000)
self.FIX_REPO_GET_GIT_REF = True
self.per_page = per_page
@@ -97,8 +97,8 @@ class Requester:
if len(data) == 0:
return None
else:
if atLeastPython3 and isinstance(data, bytes): # pragma no branch
data = data.decode("utf-8") # pragma no cover
if atLeastPython3 and isinstance(data, bytes): # pragma no branch (Covered by Issue142.testDecodeJson with Python 3)
data = data.decode("utf-8") # pragma no cover (Covered by Issue142.testDecodeJson with Python 3)
return json.loads(data)
def requestJson(self, verb, url, parameters, input):
@@ -201,9 +201,9 @@ class Requester:
def __createConnection(self):
kwds = {}
if not atLeastPython3: # pragma no branch
if not atLeastPython3: # pragma no branch (Branch useful only with Python 3)
kwds["strict"] = True # Useless in Python3, would generate a deprecation warning
if atLeastPython26: # pragma no branch
if atLeastPython26: # pragma no branch (Branch useful only with Python 2.5)
kwds["timeout"] = self.__timeout # Did not exist before Python2.6
return self.__connectionClass(host=self.__hostname, port=self.__port, **kwds)
@@ -215,6 +215,4 @@ class Requester:
requestHeaders["Authorization"] = "Basic (login and password removed)"
elif requestHeaders["Authorization"].startswith("token"):
requestHeaders["Authorization"] = "token (oauth token removed)"
else: # pragma no cover
requestHeaders["Authorization"] = "Unknown authorization removed"
logger.debug("%s %s://%s%s %s %s ==> %i %s %s", str(verb), self.__scheme, self.__hostname, str(url), str(requestHeaders), str(input), status, str(responseHeaders), str(output))
+1 -1
View File
@@ -30,7 +30,7 @@ from InputGitAuthor import InputGitAuthor
from InputGitTreeElement import InputGitTreeElement
def enable_console_debug_logging(): # pragma no cover
def enable_console_debug_logging(): # pragma no cover (Function useful only outside test environment)
"""
This function sets up a very simple logging configuration (log everything on standard output) that is useful for troubleshooting.
"""
+16
View File
@@ -38,3 +38,19 @@ class Authentication(Framework.BasicTestCase):
def testUserAgent(self):
g = github.Github(user_agent="PyGithubTester")
self.assertEqual(g.get_user("jacquev6").name, "Vincent Jacques")
def testAuthorizationHeaderWithLogin(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("fake_login", "fake_password")
try:
g.get_user().name
except github.GithubException:
pass
def testAuthorizationHeaderWithToken(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk")
try:
g.get_user().name
except github.GithubException:
pass
+1 -1
View File
@@ -36,7 +36,7 @@ class ContentFile(Framework.TestCase):
self.assertEqual(self.file.name, "ReadMe.md")
self.assertEqual(self.file.path, "ReadMe.md")
if atLeastPython3:
self.assertEqual(len(base64.b64decode(bytearray(self.file.content, "utf-8"))), 7531) # pragma no cover
self.assertEqual(len(base64.b64decode(bytearray(self.file.content, "utf-8"))), 7531) # pragma no cover (Covered with Python 3)
else:
self.assertEqual(len(base64.b64decode(self.file.content)), 7531)
self.assertEqual(self.file.sha, "5628799a7d517a4aaa0c1a7004d07569cd154df0")
+5
View File
@@ -28,6 +28,11 @@ class Enterprise(Framework.BasicTestCase):
g = github.Github(self.login, self.password, base_url="http://my.enterprise.com")
self.assertListKeyEqual(g.get_user().get_repos(), lambda r: r.name, ["TestPyGithub", "django", "PyGithub", "developer.github.com", "acme-public-website", "C4Planner", "Hacking", "vincent-jacques.net", "Contests", "Candidates", "Tests", "DrawTurksHead", "DrawSyntax", "QuadProgMm", "Boost.HierarchicalEnum", "ViDE"])
def testUnknownUrlScheme(self):
with self.assertRaises(AssertionError) as cm:
github.Github(self.login, self.password, base_url="foobar://my.enterprise.com")
self.assertEqual(cm.exception.args[0], "Unknown URL scheme")
def testLongUrl(self):
g = github.Github(self.login, self.password, base_url="http://my.enterprise.com/path/to/github")
repos = g.get_user().get_repos()
+3 -3
View File
@@ -57,7 +57,7 @@ class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
def testUnknownUser(self):
@@ -71,7 +71,7 @@ class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
def testBadAuthentication(self):
@@ -85,5 +85,5 @@ class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "401 {u'message': u'Bad credentials'}")
else:
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
+15 -11
View File
@@ -27,8 +27,8 @@ atMostPython32 = sys.hexversion < 0x03030000
if atLeastPython26:
import json
else: # pragma no cover
import simplejson as json # pragma no cover
else: # pragma no cover (Covered by all tests with Python 2.5)
import simplejson as json # pragma no cover (Covered by all tests with Python 2.5)
def readLine(file):
@@ -53,15 +53,19 @@ class FakeHttpResponse:
def fixAuthorizationHeader(headers):
if "Authorization" in headers:
if headers["Authorization"].startswith("token "):
if headers["Authorization"].endswith("ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk"):
# This special case is here to test the real Authorization header
# sent by PyGithub. It would have avoided issue https://github.com/jacquev6/PyGithub/issues/153
# because we would have seen that Python 3 was not generating the same
# header as Python 2
pass
elif headers["Authorization"].startswith("token "):
headers["Authorization"] = "token private_token_removed"
elif headers["Authorization"].startswith("Basic "):
headers["Authorization"] = "Basic login_and_password_removed"
else: # pragma no cover
assert False
class RecordingConnection: # pragma no cover
class RecordingConnection: # pragma no cover (Class useful only when recording new tests, not used during automated tests)
def __init__(self, file, protocol, host, port, *args, **kwds):
self.__file = file
self.__protocol = protocol
@@ -103,14 +107,14 @@ class RecordingConnection: # pragma no cover
self.__file.write(line + "\n")
class RecordingHttpConnection(RecordingConnection): # pragma no cover
class RecordingHttpConnection(RecordingConnection): # pragma no cover (Class useful only when recording new tests, not used during automated tests)
_realConnection = httplib.HTTPConnection
def __init__(self, file, *args, **kwds):
RecordingConnection.__init__(self, file, "http", *args, **kwds)
class RecordingHttpsConnection(RecordingConnection): # pragma no cover
class RecordingHttpsConnection(RecordingConnection): # pragma no cover (Class useful only when recording new tests, not used during automated tests)
_realConnection = httplib.HTTPSConnection
def __init__(self, file, *args, **kwds):
@@ -175,7 +179,7 @@ class BasicTestCase(unittest.TestCase):
unittest.TestCase.setUp(self)
self.__fileName = ""
self.__file = None
if self.recordMode: # pragma no cover
if self.recordMode: # pragma no cover (Branch useful only when recording new tests, not used during automated tests)
github.Requester.Requester.injectConnectionClasses(
lambda ignored, *args, **kwds: RecordingHttpConnection(self.__openFile("wb"), *args, **kwds),
lambda ignored, *args, **kwds: RecordingHttpsConnection(self.__openFile("wb"), *args, **kwds)
@@ -216,7 +220,7 @@ class BasicTestCase(unittest.TestCase):
def __closeReplayFileIfNeeded(self):
if self.__file is not None:
if not self.recordMode: # pragma no branch
if not self.recordMode: # pragma no branch (Branch useful only when recording new tests, not used during automated tests)
self.assertEqual(readLine(self.__file), "")
self.__file.close()
@@ -235,5 +239,5 @@ class TestCase(BasicTestCase):
self.g = github.Github(self.login, self.password)
def activateRecordMode(): # pragma no cover
def activateRecordMode(): # pragma no cover (Function useful only when recording new tests, not used during automated tests)
BasicTestCase.recordMode = True
+5
View File
@@ -17,6 +17,8 @@ import datetime
import Framework
import github
class Github(Framework.TestCase):
def testGetGists(self):
@@ -107,3 +109,6 @@ class Github(Framework.TestCase):
t = self.g.get_gitignore_template("C++")
self.assertEqual(t.name, "C++")
self.assertEqual(t.source, "# Compiled Object files\n*.slo\n*.lo\n*.o\n\n# Compiled Dynamic libraries\n*.so\n*.dylib\n\n# Compiled Static libraries\n*.lai\n*.la\n*.a\n")
def testStringOfNotSet(self):
self.assertEqual(str(github.GithubObject.NotSet), "NotSet")
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/user
{'Authorization': 'Basic ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk'}
null
401
[('status', '401 Unauthorized'), ('content-length', '29'), ('x-github-media-type', 'github.beta; format=json'), ('server', 'GitHub.com'), ('connection', 'keep-alive'), ('date', 'Thu, 28 Mar 2013 20:14:22 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"message":"Bad credentials"}
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/user
{'Authorization': 'token ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk'}
null
403
[('status', '403 Forbidden'), ('content-length', '50'), ('x-github-media-type', 'github.beta; format=json'), ('server', 'GitHub.com'), ('connection', 'keep-alive'), ('date', 'Thu, 28 Mar 2013 20:15:00 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"message":"Max number of login attempt exceeded"}