TestSubCommand inherit from UpdateSubCommand.

Sharing code between these two classes will make it easier to work on
extracting an XmlGenerator class from the YamlParser.

Change-Id: If93227d0b4fca4d7bb80de2341d1e83f1e3ddc79
This commit is contained in:
Wayne Warren 2016-01-02 14:37:19 -08:00
parent f85cf31826
commit 4350690b61
3 changed files with 41 additions and 47 deletions

View File

@ -15,28 +15,23 @@
import logging
import sys
import time
from jenkins_jobs.builder import Builder
from jenkins_jobs.parser import YamlParser
import jenkins_jobs.cli.subcommand.base as base
import jenkins_jobs.cli.subcommand.update as update
logger = logging.getLogger(__name__)
class TestSubCommand(base.BaseSubCommand):
class TestSubCommand(update.UpdateSubCommand):
def parse_args(self, subparser):
test = subparser.add_parser('test')
self.parse_option_recursive_exclude(test)
test.add_argument(
'path',
help='''colon-separated list of paths to YAML files or
directories''',
nargs='?',
default=sys.stdin)
self.parse_arg_path(test)
self.parse_arg_names(test)
test.add_argument(
'-p',
dest='plugins_info_path',
@ -47,27 +42,9 @@ class TestSubCommand(base.BaseSubCommand):
dest='output_dir',
default=sys.stdout,
help='path to output XML')
test.add_argument(
'name',
help='name(s) of job(s)', nargs='*')
def execute(self, options, jjb_config):
builder = Builder(jjb_config)
logger.info("Updating jobs in {0} ({1})".format(
options.path, options.name))
orig = time.time()
builder, xml_jobs = self._generate_xmljobs(options, jjb_config)
# Generate XML
parser = YamlParser(jjb_config, builder.plugins_list)
parser.load_files(options.path)
parser.expandYaml(options.name)
parser.generateXML()
jobs = parser.jobs
step = time.time()
logging.debug('%d XML files generated in %ss',
len(jobs), str(step - orig))
builder.update_jobs(parser.xml_jobs, output=options.output_dir,
n_workers=1)
builder.update_jobs(xml_jobs, output=options.output_dir, n_workers=1)

View File

@ -14,6 +14,7 @@
# under the License.
import logging
import sys
import time
from jenkins_jobs.builder import Builder
@ -26,18 +27,28 @@ logger = logging.getLogger(__name__)
class UpdateSubCommand(base.BaseSubCommand):
def parse_arg_path(self, parser):
parser.add_argument(
'path',
nargs='?',
default=sys.stdin,
help='''colon-separated list of paths to YAML files or
directories''')
def parse_arg_names(self, parser):
parser.add_argument(
'names',
help='name(s) of job(s)', nargs='*')
def parse_args(self, subparser):
update = subparser.add_parser('update')
self.parse_option_recursive_exclude(update)
update.add_argument(
'path',
help='''colon-separated list of paths to YAML files or
directories''')
update.add_argument(
'names',
help='name(s) of job(s)', nargs='*')
self.parse_arg_path(update)
self.parse_arg_names(update)
update.add_argument(
'--delete-old',
action='store_true',
@ -52,13 +63,9 @@ class UpdateSubCommand(base.BaseSubCommand):
help='''number of workers to use, 0 for autodetection and 1 for
just one worker.''')
def execute(self, options, jjb_config):
def _generate_xmljobs(self, options, jjb_config=None):
builder = Builder(jjb_config)
if options.n_workers < 0:
raise JenkinsJobsException(
'Number of workers must be equal or greater than 0')
logger.info("Updating jobs in {0} ({1})".format(
options.path, options.names))
orig = time.time()
@ -74,11 +81,21 @@ class UpdateSubCommand(base.BaseSubCommand):
logging.debug('%d XML files generated in %ss',
len(jobs), str(step - orig))
return builder, parser.xml_jobs
def execute(self, options, jjb_config):
if options.n_workers < 0:
raise JenkinsJobsException(
'Number of workers must be equal or greater than 0')
builder, xml_jobs = self._generate_xmljobs(options, jjb_config)
jobs, num_updated_jobs = builder.update_jobs(
parser.xml_jobs,
xml_jobs,
n_workers=options.n_workers)
logger.info("Number of jobs updated: %d", num_updated_jobs)
if options.delete_old:
n = builder.delete_old_managed(keep=parser.xml_jobs)
n = builder.delete_old_managed(keep=xml_jobs)
logger.info("Number of jobs deleted: %d", n)

View File

@ -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.cli.subcommand.test.YamlParser.generateXML')
@mock.patch('jenkins_jobs.cli.subcommand.update.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.cli.subcommand.test.YamlParser.generateXML')
@mock.patch('jenkins_jobs.cli.subcommand.update.YamlParser.generateXML')
@mock.patch('jenkins_jobs.parser.ModuleRegistry')
def test_bogus_plugins_info_stub_option(self, registry_mock,
generateXML_mock):