diff --git a/github/Legacy.py b/github/Legacy.py index de55eb61..89c9862a 100644 --- a/github/Legacy.py +++ b/github/Legacy.py @@ -59,17 +59,17 @@ def convertUser(attributes): "login": attributes["login"], "url": "/users/" + attributes["login"], } - if "gravatar_id" in attributes: + if "gravatar_id" in attributes: # pragma no branch convertedAttributes["gravatar_id"] = attributes["gravatar_id"] - if "followers" in attributes: + if "followers" in attributes: # pragma no branch convertedAttributes["followers"] = attributes["followers"] - if "repos" in attributes: + if "repos" in attributes: # pragma no branch convertedAttributes["public_repos"] = attributes["repos"] - if "name" in attributes: + if "name" in attributes: # pragma no branch convertedAttributes["name"] = attributes["name"] - if "created_at" in attributes: + if "created_at" in attributes: # pragma no branch convertedAttributes["created_at"] = attributes["created_at"] - if "location" in attributes: + if "location" in attributes: # pragma no branch convertedAttributes["location"] = attributes["location"] return convertedAttributes @@ -79,35 +79,35 @@ def convertRepo(attributes): "owner": {"login": attributes["owner"], "url": "/users/" + attributes["owner"]}, "url": "/repos/" + attributes["owner"] + "/" + attributes["name"], } - if "pushed_at" in attributes: + if "pushed_at" in attributes: # pragma no branch convertedAttributes["pushed_at"] = attributes["pushed_at"] - if "homepage" in attributes: + if "homepage" in attributes: # pragma no branch convertedAttributes["homepage"] = attributes["homepage"] - if "created_at" in attributes: + if "created_at" in attributes: # pragma no branch convertedAttributes["created_at"] = attributes["created_at"] - if "watchers" in attributes: + if "watchers" in attributes: # pragma no branch convertedAttributes["watchers"] = attributes["watchers"] - if "has_downloads" in attributes: + if "has_downloads" in attributes: # pragma no branch convertedAttributes["has_downloads"] = attributes["has_downloads"] - if "fork" in attributes: + if "fork" in attributes: # pragma no branch convertedAttributes["fork"] = attributes["fork"] - if "has_issues" in attributes: + if "has_issues" in attributes: # pragma no branch convertedAttributes["has_issues"] = attributes["has_issues"] - if "has_wiki" in attributes: + if "has_wiki" in attributes: # pragma no branch convertedAttributes["has_wiki"] = attributes["has_wiki"] - if "forks" in attributes: + if "forks" in attributes: # pragma no branch convertedAttributes["forks"] = attributes["forks"] - if "size" in attributes: + if "size" in attributes: # pragma no branch convertedAttributes["size"] = attributes["size"] - if "private" in attributes: + if "private" in attributes: # pragma no branch convertedAttributes["private"] = attributes["private"] - if "open_issues" in attributes: + if "open_issues" in attributes: # pragma no branch convertedAttributes["open_issues"] = attributes["open_issues"] - if "description" in attributes: + if "description" in attributes: # pragma no branch convertedAttributes["description"] = attributes["description"] - if "language" in attributes: + if "language" in attributes: # pragma no branch convertedAttributes["language"] = attributes["language"] - if "name" in attributes: + if "name" in attributes: # pragma no branch convertedAttributes["name"] = attributes["name"] return convertedAttributes @@ -118,18 +118,18 @@ def convertIssue(attributes): "url": "/repos" + urlparse.urlparse(attributes["html_url"]).path, "user": {"login": attributes["user"], "url": "/users/" + attributes["user"]}, } - if "labels" in attributes: + if "labels" in attributes: # pragma no branch convertedAttributes["labels"] = [{"name": label} for label in attributes["labels"]] - if "title" in attributes: + if "title" in attributes: # pragma no branch convertedAttributes["title"] = attributes["title"] - if "created_at" in attributes: + if "created_at" in attributes: # pragma no branch convertedAttributes["created_at"] = attributes["created_at"] - if "comments" in attributes: + if "comments" in attributes: # pragma no branch convertedAttributes["comments"] = attributes["comments"] - if "body" in attributes: + if "body" in attributes: # pragma no branch convertedAttributes["body"] = attributes["body"] - if "updated_at" in attributes: + if "updated_at" in attributes: # pragma no branch convertedAttributes["updated_at"] = attributes["updated_at"] - if "state" in attributes: + if "state" in attributes: # pragma no branch convertedAttributes["state"] = attributes["state"] return convertedAttributes diff --git a/github/Requester.py b/github/Requester.py index 1f3214f8..17cc9070 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -122,7 +122,7 @@ class Requester: requestHeaders["Authorization"] = "Basic (login and password removed)" elif requestHeaders["Authorization"].startswith("token"): requestHeaders["Authorization"] = "token (oauth token removed)" - else: + 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)) diff --git a/github/__init__.py b/github/__init__.py index 641c0ee5..9485b2cc 100644 --- a/github/__init__.py +++ b/github/__init__.py @@ -20,7 +20,7 @@ from InputGitAuthor import InputGitAuthor from InputGitTreeElement import InputGitTreeElement -def enable_console_debug_logging(): +def enable_console_debug_logging(): # pragma no cover logger = logging.getLogger("github") logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler()) diff --git a/github/tests/Exceptions.py b/github/tests/Exceptions.py index b30cff58..b95602f4 100644 --- a/github/tests/Exceptions.py +++ b/github/tests/Exceptions.py @@ -21,10 +21,11 @@ atLeastPython26 = sys.hexversion >= 0x02060000 class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we do not use self.assertRaises with only one argument def testInvalidInput(self): + raised = False try: self.g.get_user().create_key("Bad key", "xxx") - self.fail("Should have raised") except github.GithubException, exception: + raised = True self.assertEqual(exception.status, 422) self.assertEqual( exception.data, @@ -43,40 +44,47 @@ class Exceptions(Framework.TestCase): # To stay compatible with Python 2.6, we if atLeastPython26: self.assertEqual(str(exception), "422 {u\'message\': u\'Validation Failed\', u\'errors\': [{u\'field\': u\'key\', u\'message\': u\"key is invalid. It must begin with \'ssh-rsa\' or \'ssh-dss\'. Check that you\'re copying the public half of the key\", u\'code\': u\'custom\', u\'resource\': u\'PublicKey\'}]}") else: - self.assertEqual(str(exception), "422 {\'message\': \'Validation Failed\', \'errors\': [{\'field\': \'key\', \'message\': \"key is invalid. It must begin with \'ssh-rsa\' or \'ssh-dss\'. Check that you\'re copying the public half of the key\", \'code\': \'custom\', \'resource\': \'PublicKey\'}]}") + self.assertEqual(str(exception), "422 {\'message\': \'Validation Failed\', \'errors\': [{\'field\': \'key\', \'message\': \"key is invalid. It must begin with \'ssh-rsa\' or \'ssh-dss\'. Check that you\'re copying the public half of the key\", \'code\': \'custom\', \'resource\': \'PublicKey\'}]}") # pragma no cover + self.assertTrue(raised) def testUnknownObject(self): + raised = False try: self.g.get_user().get_repo("Xxx") - self.fail("Should have raised") except github.GithubException, exception: + raised = True self.assertEqual(exception.status, 404) self.assertEqual(exception.data, {"message": "Not Found"}) if atLeastPython26: self.assertEqual(str(exception), "404 {u'message': u'Not Found'}") else: - self.assertEqual(str(exception), "404 {'message': 'Not Found'}") + self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover + self.assertTrue(raised) def testUnknownUser(self): + raised = False try: self.g.get_user("ThisUserShouldReallyNotExist") - self.fail("Should have raised") except github.GithubException, exception: + raised = True self.assertEqual(exception.status, 404) self.assertEqual(exception.data, {"message": "Not Found"}) if atLeastPython26: self.assertEqual(str(exception), "404 {u'message': u'Not Found'}") else: - self.assertEqual(str(exception), "404 {'message': 'Not Found'}") + self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover + self.assertTrue(raised) def testBadAuthentication(self): + raised = False try: github.Github("BadUser", "BadPassword").get_user().login - self.fail("Should have raised") except github.GithubException, exception: + raised = True self.assertEqual(exception.status, 401) self.assertEqual(exception.data, {"message": "Bad credentials"}) if atLeastPython26: self.assertEqual(str(exception), "401 {u'message': u'Bad credentials'}") else: - self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") + self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover + self.assertTrue(raised) diff --git a/github/tests/Framework.py b/github/tests/Framework.py index ae8c3878..af528f5a 100644 --- a/github/tests/Framework.py +++ b/github/tests/Framework.py @@ -39,11 +39,11 @@ def fixAuthorizationHeader(headers): headers["Authorization"] = "token private_token_removed" elif headers["Authorization"].startswith("Basic "): headers["Authorization"] = "Basic login_and_password_removed" - else: + else: # pragma no cover assert False -class RecordingConnection: +class RecordingConnection: # pragma no cover def __init__(self, file, protocol, host, port, *args, **kwds): self.__file = file self.__protocol = protocol @@ -76,14 +76,14 @@ class RecordingConnection: return self.__cnx.close() -class RecordingHttpConnection(RecordingConnection): +class RecordingHttpConnection(RecordingConnection): # pragma no cover _realConnection = httplib.HTTPConnection def __init__(self, file, *args, **kwds): RecordingConnection.__init__(self, file, "http", *args, **kwds) -class RecordingHttpsConnection(RecordingConnection): +class RecordingHttpsConnection(RecordingConnection): # pragma no cover _realConnection = httplib.HTTPSConnection def __init__(self, file, *args, **kwds): @@ -130,7 +130,7 @@ class BasicTestCase(unittest.TestCase): unittest.TestCase.setUp(self) self.__fileName = "" self.__file = None - if self.recordMode: + if self.recordMode: # pragma no cover github.Requester.Requester.injectConnectionClasses( lambda ignored, *args, **kwds: RecordingHttpConnection(self.__openFile("wb"), *args, **kwds), lambda ignored, *args, **kwds: RecordingHttpsConnection(self.__openFile("wb"), *args, **kwds) @@ -165,7 +165,7 @@ class BasicTestCase(unittest.TestCase): def __closeReplayFileIfNeeded(self): if self.__file is not None: - if not self.recordMode: + if not self.recordMode: # pragma no branch self.assertEqual(self.__file.readline(), "") self.__file.close() @@ -184,5 +184,5 @@ class TestCase(BasicTestCase): self.g = github.Github(self.login, self.password) -def activateRecordMode(): +def activateRecordMode(): # pragma no cover BasicTestCase.recordMode = True diff --git a/github/tests/Issue.py b/github/tests/Issue.py index 95eec520..91db99fc 100644 --- a/github/tests/Issue.py +++ b/github/tests/Issue.py @@ -95,6 +95,6 @@ class Issue(Framework.TestCase): question = self.repo.get_label("Question") self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"]) self.issue.delete_labels() - self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, []) + self.assertListKeyEqual(self.issue.get_labels(), None, []) self.issue.set_labels(bug, question) self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Question"]) diff --git a/github/tests/PaginatedList.py b/github/tests/PaginatedList.py index 00c45db1..0191b3fd 100644 --- a/github/tests/PaginatedList.py +++ b/github/tests/PaginatedList.py @@ -66,7 +66,7 @@ class PaginatedList(Framework.TestCase): def testInterruptedIteration(self): # No asserts, but checks that only three pages are fetched l = 0 - for element in self.list: + for element in self.list: # pragma no branch (exits only by break) l += 1 if l == 75: break @@ -74,7 +74,7 @@ class PaginatedList(Framework.TestCase): def testInterruptedIterationInSlice(self): # No asserts, but checks that only three pages are fetched l = 0 - for element in self.list[:100]: + for element in self.list[:100]: # pragma no branch (exits only by break) l += 1 if l == 75: break diff --git a/github/tests/Repository.py b/github/tests/Repository.py index b2833109..9458c816 100644 --- a/github/tests/Repository.py +++ b/github/tests/Repository.py @@ -409,9 +409,11 @@ class Repository(Framework.TestCase): self.assertEqual(commit, None) def testMergeWithConflict(self): + raised = False try: commit = self.repo.merge("branchForBase", "branchForHead") - self.fail("Should have raised") except github.GithubException, exception: + raised = True self.assertEqual(exception.status, 409) self.assertEqual(exception.data, {"message": "Merge conflict"}) + self.assertTrue(raised) diff --git a/github/tests/Team.py b/github/tests/Team.py index c2469886..7f32d6f3 100644 --- a/github/tests/Team.py +++ b/github/tests/Team.py @@ -30,24 +30,24 @@ class Team(Framework.TestCase): def testMembers(self): user = self.g.get_user("jacquev6") - self.assertListKeyEqual(self.team.get_members(), lambda u: u.login, []) + self.assertListKeyEqual(self.team.get_members(), None, []) self.assertFalse(self.team.has_in_members(user)) self.team.add_to_members(user) self.assertListKeyEqual(self.team.get_members(), lambda u: u.login, ["jacquev6"]) self.assertTrue(self.team.has_in_members(user)) self.team.remove_from_members(user) - self.assertListKeyEqual(self.team.get_members(), lambda u: u.login, []) + self.assertListKeyEqual(self.team.get_members(), None, []) self.assertFalse(self.team.has_in_members(user)) def testRepos(self): repo = self.org.get_repo("FatherBeaver") - self.assertListKeyEqual(self.team.get_repos(), lambda r: r.name, []) + self.assertListKeyEqual(self.team.get_repos(), None, []) self.assertFalse(self.team.has_in_repos(repo)) self.team.add_to_repos(repo) self.assertListKeyEqual(self.team.get_repos(), lambda r: r.name, ["FatherBeaver"]) self.assertTrue(self.team.has_in_repos(repo)) self.team.remove_from_repos(repo) - self.assertListKeyEqual(self.team.get_repos(), lambda r: r.name, []) + self.assertListKeyEqual(self.team.get_repos(), None, []) self.assertFalse(self.team.has_in_repos(repo)) def testEditWithoutArguments(self): diff --git a/github/tests/__init__.py b/github/tests/__init__.py index 8108507e..ba9e1b20 100644 --- a/github/tests/__init__.py +++ b/github/tests/__init__.py @@ -17,4 +17,4 @@ import AllTests def run(): - unittest.main(module=AllTests, argv=["Dummy Script Name"]) + return unittest.main(module=AllTests, argv=["Dummy Script Name"], exit=False).result diff --git a/github/tests/__main__.py b/github/tests/__main__.py index 802c3f69..c6f8d2ca 100644 --- a/github/tests/__main__.py +++ b/github/tests/__main__.py @@ -1,3 +1,16 @@ +# Copyright 2012 Vincent Jacques +# vincent@vincent-jacques.net + +# This file is part of PyGithub. http://vincent-jacques.net/PyGithub + +# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License +# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see . + import sys import unittest diff --git a/setup.py b/setup.py index db1f1232..6b783931 100755 --- a/setup.py +++ b/setup.py @@ -15,6 +15,8 @@ from distutils.core import setup, Command import textwrap +import sys +import glob class test( Command ): user_options = [] @@ -26,8 +28,29 @@ class test( Command ): pass def run( self ): + try: + import coverage + analyseCoverage = True + except ImportError: + print "Unable to import coverage. Running tests without coverage analysis" + analyseCoverage = False + if analyseCoverage: + cov = coverage.coverage(branch=True) + cov.start() + import github.tests - github.tests.run() + testsResult = github.tests.run() + + ok = len( testsResult.failures ) == 0 and len( testsResult.errors ) == 0 + if analyseCoverage: + cov.stop() + for f in glob.glob( "github/*.py" ): + ok = ok and len( cov.analysis2( f )[ 3 ] ) == 0 + cov.report(file=sys.stdout, include="github/*") + if ok: + exit( 0 ) + else: + exit( 1 ) setup( name = "PyGithub",