From 8b65582009a27255a54f11b6580d58a91dc82f1b Mon Sep 17 00:00:00 2001 From: Sateesh Kumar Date: Sun, 8 Dec 2013 08:54:32 +0530 Subject: [PATCH] Make version a property --- README.rst | 2 +- jenkinsapi/jenkins.py | 12 +++++------- jenkinsapi_tests/unittests/test_jenkins.py | 11 ++++------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index bfc4a12..cdb3d78 100644 --- a/README.rst +++ b/README.rst @@ -89,7 +89,7 @@ JenkinsAPI is intended to map the objects in Jenkins (e.g. Builds, Views, Jobs) >>> import jenkinsapi >>> from jenkinsapi.jenkins import Jenkins >>> J = Jenkins('http://localhost:8080') - >>> J.get_version() + >>> J.version 1.542 >>> J.keys() # Jenkins objects appear to be dict-like, mapping keys (job-names) to ['foo', 'test_jenkinsapi'] diff --git a/jenkinsapi/jenkins.py b/jenkinsapi/jenkins.py index 11c65d8..2a6ed61 100644 --- a/jenkinsapi/jenkins.py +++ b/jenkinsapi/jenkins.py @@ -21,7 +21,7 @@ from jenkinsapi.queue import Queue from jenkinsapi.fingerprint import Fingerprint from jenkinsapi.jenkinsbase import JenkinsBase from jenkinsapi.utils.requester import Requester -from jenkinsapi.custom_exceptions import UnknownJob, BadURL +from jenkinsapi.custom_exceptions import UnknownJob log = logging.getLogger(__name__) @@ -319,13 +319,11 @@ class Jenkins(JenkinsBase): url = '%s/computer/%s' % (self.baseurl, nodename) return Executors(url, nodename, self) - def get_version(self): + @property + def version(self): """ Return version number of Jenkins """ response = self.requester.get_and_confirm_status(self.baseurl) - try: - return response.headers['X-jenkins'] - except KeyError: - msg = '%s is not a valid Jenkins URL.' % (self.baseurl) - raise BadURL(msg) + version_key = 'X-Jenkins' + return response.headers.get(version_key,'0.0') diff --git a/jenkinsapi_tests/unittests/test_jenkins.py b/jenkinsapi_tests/unittests/test_jenkins.py index 0ff7ab9..2af6058 100644 --- a/jenkinsapi_tests/unittests/test_jenkins.py +++ b/jenkinsapi_tests/unittests/test_jenkins.py @@ -309,17 +309,17 @@ class TestJenkins(unittest.TestCase): class MockResponse(object): def __init__(self): self.headers = {} - self.headers['X-jenkins'] = '1.542' + self.headers['X-Jenkins'] = '1.542' mock_requester = Requester(username='foouser', password='foopassword') mock_requester.get_and_confirm_status = mock.MagicMock(return_value=MockResponse()) J = Jenkins('http://localhost:8080/', username='foouser', password='foopassword', requester=mock_requester) - self.assertEquals('1.542', J.get_version()) + self.assertEquals('1.542', J.version) @mock.patch.object(JenkinsBase, '_poll') @mock.patch.object(Jenkins, '_poll') - def test_get_version_exception(self, _base_poll, _poll): + def test_get_version_nonexistent(self, _base_poll, _poll): class MockResponse(object): def __init__(self): self.headers = {} @@ -329,10 +329,7 @@ class TestJenkins(unittest.TestCase): J = Jenkins(base_url, username='foouser', password='foopassword', requester=mock_requester) - with self.assertRaises(BadURL) as context: - J.get_version() - msg = '%s is not a valid Jenkins URL.' % (base_url) - self.assertEqual(context.exception.message, msg) + self.assertEquals('0.0', J.version) class TestJenkinsURLs(unittest.TestCase): -- 2.7.4