def __init__(self, repo, api_url, bleeding_edge = False, interval = 3600): self.has_update = False self.__timer = None if not bleeding_edge: self.__updater = StableUpdater(repo, api_url) else: self.__updater = BleedingEdgeUpdater(repo, api_url) self.__interval = interval
def test_init_bleeding_egde_update(self): """ Make sure that the BleedingEdgeUpdater is used, if detailed. """ self.mox.StubOutWithMock(BleedingEdgeUpdater, "__init__") BleedingEdgeUpdater.__init__(self.__repo, self.__remote_url) self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url, True)
def test_getting_origin_head_hash_no_response(self): """ If urlopen returns None, None should be returned. """ self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(json, "loads") urllib2.urlopen(self.__remote_url).AndReturn(None) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(None, bleeding_updater.get_origin_head_sha())
def test_has_git_when_has_git(self): """ has_git should return True if the git library indicates git v1 as being available. """ self.mox.StubOutWithMock(git.LocalRepository, "getGitVersion") # This first call is __init__ retrieving HEAD commit hash git.LocalRepository.getGitVersion().AndReturn("1.7.5.4") git.LocalRepository.getGitVersion().AndReturn("1.7.5.4") self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertTrue(updater.has_git())
def test_github_does_not_have_new_version(self): """ If the github query returns a SHA key that is equal to the local master HEAD key, check shall indicate that there is no new version available. """ mock_commit = self.mox.CreateMockAnything() mock_commit.hash = self.__mock_local_head_hash self.mox.StubOutWithMock(git.LocalRepository, "getHead") self.mox.StubOutWithMock(BleedingEdgeUpdater, "is_repo") self.mox.StubOutWithMock(BleedingEdgeUpdater, "has_git") self.mox.StubOutWithMock(BleedingEdgeUpdater, "get_origin_head_sha") BleedingEdgeUpdater.is_repo().AndReturn(True) BleedingEdgeUpdater.has_git().AndReturn(True) git.LocalRepository.getHead().AndReturn(mock_commit) BleedingEdgeUpdater.get_origin_head_sha().AndReturn( self.__mock_local_head_hash) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertTrue(not updater.check())
def test_getting_origin_head_hash_urlopen_exception(self): """ If urlopen raises an exception (timeout, invalid url, etc.), None should be returned. """ self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(json, "loads") urllib2.urlopen(self.__remote_url).AndRaise(urllib2.URLError("na-ah")) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(None, bleeding_updater.get_origin_head_sha())
def test_has_git_when_is_not_repo(self): """ If project root is not a repo, has_git should return False, rather than raising an exception, as it requires a local repository in order to query git for version(?!). """ self.mox.StubOutWithMock(BleedingEdgeUpdater, "is_repo") self.mox.StubOutWithMock(git.LocalRepository, "getGitVersion") BleedingEdgeUpdater.is_repo().AndReturn(False) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertTrue(not updater.has_git())
def test_has_git_when_does_not_have_git(self): """ has_git should return False if the git library is unable to locate git v1. """ self.mox.StubOutWithMock(git.LocalRepository, "getGitVersion") # This first call is __init__ retrieving HEAD commit hash git.LocalRepository.getGitVersion().AndReturn("1.7.5.4") git.LocalRepository.getGitVersion().AndRaise( git.exceptions.GitException("dang nabit")) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertTrue(not updater.has_git())
def test_is_repo_when_is_repo(self): """ If project root contains a .git directory, make sure that is_repo indicates that. """ self.mox.StubOutWithMock(os.path, "isdir") # This first call is __init__ checking is_repo in order to create a # LocalRepository attribute. os.path.isdir(os.path.join(self.__project_root, ".git")).AndReturn(True) os.path.isdir(os.path.join(self.__project_root, ".git")).AndReturn(True) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertTrue(updater.is_repo())
def test_getting_origin_head_hash_no_html(self): """ If we fail to read the urlopen query, None shall be returned. """ mock_response = self.mox.CreateMockAnything() self.mox.StubOutWithMock(urllib2, "urlopen") urllib2.urlopen(self.__remote_url).AndReturn(mock_response) mock_response.read().AndReturn(None) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(None, bleeding_updater.get_origin_head_sha())
def test_getting_local_commit_hash_no_repo(self): """ If project root is not a repo, local_head_commit_hash attribute should equal None. """ mock_commit = self.mox.CreateMockAnything() mock_commit.hash = self.__mock_local_head_hash self.mox.StubOutWithMock(git.LocalRepository, "getHead") self.mox.StubOutWithMock(BleedingEdgeUpdater, "is_repo") self.mox.StubOutWithMock(BleedingEdgeUpdater, "has_git") BleedingEdgeUpdater.is_repo().AndReturn(False) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEqual(None, updater.local_head_commit_hash)
def test_getting_origin_head_hash_no_json(self): """ In the case of the read HTML not containing proper json, None shall be returned. """ mock_response = self.mox.CreateMockAnything() mock_html = "no json for you" self.mox.StubOutWithMock(urllib2, "urlopen") urllib2.urlopen(self.__remote_url).AndReturn(mock_response) mock_response.read().AndReturn(mock_html) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(None, bleeding_updater.get_origin_head_sha())
class UpdateNotifyer(object): """ UpdateNotifyer is responsible for querying the selected updater at the desired interval, and if an update is found, the bot status is updated to reflect this. """ def __init__(self, repo, api_url, bleeding_edge = False, interval = 3600): self.has_update = False self.__timer = None if not bleeding_edge: self.__updater = StableUpdater(repo, api_url) else: self.__updater = BleedingEdgeUpdater(repo, api_url) self.__interval = interval def start(self): """ Start the UpdateNotifyer service. """ self.__timer = threading.Timer(self.__interval, self.timeout) self.__timer.start() def stop(self): """ Stop the UpdateNotifyer service. """ self.__timer.cancel() def timeout(self): """ Called upon by the Timer when self.__interval has elapsed. """ had_update = self.has_update self.has_update = self.__updater.check() if self.has_update and not had_update: cli = Client() new_version = self.__updater.get_update_version() if new_version: cli.change_status(u"update available: %s" % new_version[:7]) self.start()
def test_getting_local_commit_hash(self): """ If project root is a git repo, and git is avaialable, tha local_head_commit_hash attribute should contain the SHA of HEAD after having initialized BleedingEdgeUpdater. """ mock_commit = self.mox.CreateMockAnything() mock_commit.hash = self.__mock_local_head_hash self.mox.StubOutWithMock(git.LocalRepository, "getHead") self.mox.StubOutWithMock(BleedingEdgeUpdater, "is_repo") self.mox.StubOutWithMock(BleedingEdgeUpdater, "has_git") BleedingEdgeUpdater.is_repo().AndReturn(True) BleedingEdgeUpdater.has_git().AndReturn(True) git.LocalRepository.getHead().AndReturn(mock_commit) self.mox.ReplayAll() updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(self.__mock_local_head_hash, updater.local_head_commit_hash)
def test_getting_origin_head_hash_no_sha_in_json(self): """ If the json response from github does not contain a SHA key, None shall be returned. """ mock_response = self.mox.CreateMockAnything() mock_html = self.mox.CreateMockAnything() mock_json_dict = {'foo': 'bar'} self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(json, "loads") urllib2.urlopen(self.__remote_url).AndReturn(mock_response) mock_response.read().AndReturn(mock_html) json.loads(mock_html).AndReturn(mock_json_dict) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(None, bleeding_updater.get_origin_head_sha())
def test_getting_origin_head_hash(self): """ Sunshine case when querying github for origin/master HEAD SHA. """ mock_response = self.mox.CreateMockAnything() mock_html = self.mox.CreateMockAnything() mock_json_dict = self.mox.CreateMockAnything() self.mox.StubOutWithMock(urllib2, "urlopen") self.mox.StubOutWithMock(json, "loads") urllib2.urlopen(self.__remote_url).AndReturn(mock_response) mock_response.read().AndReturn(mock_html) json.loads(mock_html).AndReturn(mock_json_dict) mock_json_dict["sha"].AndReturn(self.__mock_origin_head_sha) self.mox.ReplayAll() bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url) self.assertEquals(self.__mock_origin_head_sha, bleeding_updater.get_origin_head_sha())