Skip to content

Commit b27f4a2

Browse files
committed
+ test_set_branch
1 parent 3e33da0 commit b27f4a2

2 files changed

Lines changed: 60 additions & 12 deletions

File tree

jenkins_cli/cli.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,14 @@ def set_branch(self, args):
141141
job_name = self._check_job(args.job_name)
142142
xml = self.jenkins.get_job_config(job_name)
143143
root = ElementTree.fromstring(xml.encode('utf-8'))
144-
scm = root.find('scm')
145-
new_xml = None
146-
if scm is not None:
147-
revision = scm.find('revision')
148-
if revision is not None:
149-
revision.text = args.branch_name
150-
new_xml = ElementTree.tostring(root)
151-
self.jenkins.reconfig_job(job_name, new_xml)
152-
print('Done')
153-
if new_xml is None:
154-
print("Can not set revision info")
144+
scm_name, branch_node = self._get_scm_name_and_node(root)
145+
if branch_node is not None:
146+
branch_node.text = args.branch_name
147+
new_xml = ElementTree.tostring(root)
148+
self.jenkins.reconfig_job(job_name, new_xml)
149+
print('Done')
150+
else:
151+
print("Can't set branch name")
155152

156153
def start(self, args):
157154
for job in args.job_name:

tests/test_cli.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from argparse import Namespace
44
from xml.etree import ElementTree
5+
from datetime import datetime
56

67
from pyfakefs import fake_filesystem_unittest
78
import mock
@@ -200,7 +201,57 @@ def test_info(self, patched_get_job_name, patched_get_job_info, patched_get_job_
200201
patched_get_job_config.return_value = EMPTY_SCM_XML
201202
JenkinsCli(self.args).info(self.args)
202203
arg = JenkinsCli.INFO_TEMPLATE % ('Not Built', 'Not Built', 'Not Built', 'Not Built', 'No', 'UnknownSCM', 'Unknown branch')
203-
self.patched_print.assert_has_calls([mock.call(arg)])
204+
self.patched_print.assert_called_once_with(arg)
205+
self.patched_print.reset_mock()
206+
207+
ts = 1456870299
208+
job_info = {'lastBuild': {'fullDisplayName': 'FDN (cur)',
209+
'result': 'Done',
210+
'timestamp': ts * 1000,
211+
'building': True},
212+
'lastSuccessfulBuild': {'fullDisplayName': 'FDN (last)'}}
213+
patched_get_job_info.return_value = job_info
214+
patched_get_job_config.return_value = GIT_SCM_XML
215+
JenkinsCli(self.args).info(self.args)
216+
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(ts), 'Yes', 'Git', 'cli-tests')
217+
self.patched_print.assert_called_once_with(arg)
218+
self.patched_print.reset_mock()
219+
220+
job_info['building'] = False
221+
patched_get_job_info.return_value = job_info
222+
patched_get_job_config.return_value = HG_SCM_XML
223+
JenkinsCli(self.args).info(self.args)
224+
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(ts), 'Yes', 'Mercurial', 'v123')
225+
self.patched_print.assert_called_once_with(arg)
226+
227+
@mock.patch.object(jenkins.Jenkins, 'reconfig_job')
228+
@mock.patch.object(jenkins.Jenkins, 'get_job_config')
229+
@mock.patch.object(jenkins.Jenkins, 'get_job_name', return_value='Job1')
230+
def test_set_branch(self, patched_get_job_name, patched_get_job_config, patched_reconfig_job):
231+
patched_get_job_config.return_value = EMPTY_SCM_XML
232+
self.args.job_name = 'Job1'
233+
self.args.branch_name = 'b1'
234+
JenkinsCli(self.args).set_branch(self.args)
235+
self.assertFalse(patched_reconfig_job.called)
236+
self.patched_print.assert_called_once_with("Can't set branch name")
237+
self.patched_print.reset_mock()
204238

239+
patched_get_job_config.return_value = GIT_SCM_XML
240+
JenkinsCli(self.args).set_branch(self.args)
241+
self.assertEqual(patched_reconfig_job.call_args[0][0], 'Job1')
242+
self.assertIn('b1', patched_reconfig_job.call_args[0][1])
243+
self.assertNotIn('cli-tests', patched_reconfig_job.call_args[1])
244+
self.patched_print.assert_called_once_with("Done")
245+
patched_reconfig_job.reset_mock()
246+
self.patched_print.reset_mock()
247+
248+
patched_get_job_config.return_value = HG_SCM_XML
249+
JenkinsCli(self.args).set_branch(self.args)
250+
self.assertEqual(patched_reconfig_job.call_args[0][0], 'Job1')
251+
self.assertIn('b1', patched_reconfig_job.call_args[0][1])
252+
self.assertNotIn('v123', patched_reconfig_job.call_args[1])
253+
self.patched_print.assert_called_once_with("Done")
254+
patched_reconfig_job.reset_mock()
255+
self.patched_print.reset_mock()
205256
if __name__ == '__main__':
206257
unittest.main()

0 commit comments

Comments
 (0)