Move 'delete' yaml parsing to JenkinsJobs.execute.

This is the final piece of YamlParser in jenkins_jobs.builder, so also
remove the imports and make trivial fixes to test methods to prevent
borked test cases.

Change-Id: Ida543fa14c70447ae506092ec8afd5cb6caaa629
This commit is contained in:
Wayne Warren 2016-01-01 23:24:38 -08:00
parent 3ce6618381
commit 7373201882
3 changed files with 19 additions and 16 deletions
jenkins_jobs
tests/cmd/subcommands

@ -32,7 +32,6 @@ import jenkins
from jenkins_jobs.constants import MAGIC_MANAGE_STRING
from jenkins_jobs.parallel import parallelize
from jenkins_jobs.parser import YamlParser
from jenkins_jobs import utils
@ -250,7 +249,7 @@ class Builder(object):
if self.jenkins.is_managed(job['name']):
logger.info("Removing obsolete jenkins job {0}"
.format(job['name']))
self.delete_job(job['name'])
self.delete_job([job['name']])
deleted_jobs += 1
else:
logger.info("Not deleting unmanaged jenkins job %s",
@ -259,16 +258,7 @@ class Builder(object):
logger.debug("Keeping job %s", job['name'])
return deleted_jobs
def delete_job(self, jobs_glob, fn=None):
self.parser = YamlParser(self.jjb_config, self.plugins_list)
if fn:
self.parser.load_files(fn)
self.parser.expandYaml([jobs_glob])
jobs = [j['name'] for j in self.parser.jobs]
else:
jobs = [jobs_glob]
def delete_job(self, jobs):
if jobs is not None:
logger.info("Removing jenkins job(s): %s" % ", ".join(jobs))
for job in jobs:

@ -133,8 +133,21 @@ class JenkinsJobs(object):
builder = Builder(self.jjb_config)
if options.command == 'delete':
for job in options.name:
builder.delete_job(job, options.path)
parser = YamlParser(self.jjb_config, builder.plugins_list)
fn = options.path
for jobs_glob in options.name:
parser = YamlParser(self.jjb_config, builder.plugins_list)
if fn:
parser.load_files(fn)
parser.expandYaml([jobs_glob])
jobs = [j['name'] for j in parser.jobs]
else:
jobs = [jobs_glob]
builder.delete_job(jobs)
elif options.command == 'delete-all':
if not utils.confirm(

@ -130,7 +130,7 @@ class TestTests(CmdTestsBase):
e = self.assertRaises(UnicodeError, jenkins_jobs.execute)
self.assertIn("'ascii' codec can't encode character", str(e))
@mock.patch('jenkins_jobs.builder.YamlParser.generateXML')
@mock.patch('jenkins_jobs.cli.entry.YamlParser.generateXML')
@mock.patch('jenkins_jobs.parser.ModuleRegistry')
def test_plugins_info_stub_option(self, registry_mock, generateXML_mock):
"""
@ -154,7 +154,7 @@ class TestTests(CmdTestsBase):
registry_mock.assert_called_with(mock.ANY,
plugins_info_list)
@mock.patch('jenkins_jobs.builder.YamlParser.generateXML')
@mock.patch('jenkins_jobs.cli.entry.YamlParser.generateXML')
@mock.patch('jenkins_jobs.parser.ModuleRegistry')
def test_bogus_plugins_info_stub_option(self, registry_mock,
generateXML_mock):