Test tito via CLI class rather than shell commands.

Should allow for much better error checking.
This commit is contained in:
Devan Goodwin 2010-05-24 00:40:39 -03:00
parent 79f58c17c1
commit a0cdf98405
4 changed files with 34 additions and 44 deletions

View file

@ -19,4 +19,4 @@ import sys
from tito.cli import CLI from tito.cli import CLI
if __name__ == "__main__": if __name__ == "__main__":
CLI().main() CLI().main(sys.argv)

View file

@ -36,4 +36,4 @@ os.environ['TITO_SRC_BIN_DIR'] = SRC_BIN_DIR
from tito.cli import CLI from tito.cli import CLI
if __name__ == "__main__": if __name__ == "__main__":
CLI().main() CLI().main(sys.argv)

View file

@ -88,18 +88,18 @@ class CLI(object):
options together. options together.
""" """
def main(self): def main(self, argv):
if len(sys.argv) < 2 or not sys.argv[1] in CLI_MODULES.keys(): print("argv = %s" % argv)
if len(argv) < 1 or not argv[0] in CLI_MODULES.keys():
self._usage() self._usage()
sys.exit(1) sys.exit(1)
module_class = CLI_MODULES[sys.argv[1]] module_class = CLI_MODULES[argv[0]]
module = module_class() module = module_class()
module.main() module.main(argv)
def _usage(self): def _usage(self):
print("Usage: %s MODULENAME --help" % print("Usage: tito MODULENAME --help")
(os.path.basename(sys.argv[0])))
print("Supported modules:") print("Supported modules:")
print(" tag - Tag package releases.") print(" tag - Tag package releases.")
print(" build - Build packages.") print(" build - Build packages.")
@ -141,12 +141,12 @@ class BaseCliModule(object):
% default_output_dir) % default_output_dir)
def main(self): def main(self, argv):
(self.options, args) = self.parser.parse_args() (self.options, args) = self.parser.parse_args(argv)
self._validate_options() self._validate_options()
if len(sys.argv) < 2: if len(argv) < 1:
print(self.parser.error("Must supply an argument. " print(self.parser.error("Must supply an argument. "
"Try -h for help.")) "Try -h for help."))
@ -352,8 +352,8 @@ class BuildModule(BaseCliModule):
"(i.e. runs 'make new-sources') Must be " "(i.e. runs 'make new-sources') Must be "
"used until 'sources' file is committed to CVS.")) "used until 'sources' file is committed to CVS."))
def main(self): def main(self, argv):
BaseCliModule.main(self) BaseCliModule.main(self, argv)
build_dir = os.path.normpath(os.path.abspath(self.options.output_dir)) build_dir = os.path.normpath(os.path.abspath(self.options.output_dir))
package_name = get_project_name(tag=self.options.tag) package_name = get_project_name(tag=self.options.tag)
@ -461,8 +461,8 @@ class TagModule(BaseCliModule):
self.parser.add_option("--undo", "-u", dest="undo", action="store_true", self.parser.add_option("--undo", "-u", dest="undo", action="store_true",
help="Undo the most recent (un-pushed) tag.") help="Undo the most recent (un-pushed) tag.")
def main(self): def main(self, argv):
BaseCliModule.main(self) BaseCliModule.main(self, argv)
if self.global_config.has_option(GLOBALCONFIG_SECTION, if self.global_config.has_option(GLOBALCONFIG_SECTION,
"block_tagging"): "block_tagging"):
@ -500,7 +500,7 @@ class InitModule(BaseCliModule):
def __init__(self): def __init__(self):
BaseCliModule.__init__(self, "usage: %prog init [options]") BaseCliModule.__init__(self, "usage: %prog init [options]")
def main(self): def main(self, argv):
# DO NOT CALL BaseCliModule.main(self) # DO NOT CALL BaseCliModule.main(self)
# we are initializing tito to work in this module and # we are initializing tito to work in this module and
# calling main will result in a configuration error. # calling main will result in a configuration error.
@ -576,7 +576,7 @@ class ReportModule(BaseCliModule):
"which packages are in need of a re-tag.", "which packages are in need of a re-tag.",
)) ))
def main(self): def main(self, argv):
BaseCliModule.main(self) BaseCliModule.main(self)
if self.options.untagged_report: if self.options.untagged_report:

View file

@ -1,5 +1,5 @@
# #
# Copyright (c) 2008-2009 Red Hat, Inc. # Copyright (c) 2008-2010 Red Hat, Inc.
# #
# This software is licensed to you under the GNU General Public License, # This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or # version 2 (GPLv2). There is NO WARRANTY for this software, express or
@ -23,6 +23,7 @@ import unittest
from tito.common import check_tag_exists, commands, run_command, \ from tito.common import check_tag_exists, commands, run_command, \
get_latest_tagged_version get_latest_tagged_version
from tito.cli import CLI
# A location where we can safely create a test git repository. # A location where we can safely create a test git repository.
@ -79,19 +80,9 @@ rm -rf %{buildroot}
%changelog %changelog
""" """
def run_tito(argstring): def tito(argstring):
""" Run the tito script from source with given arguments. """ """ Run Tito from source with given arguments. """
tito_path = 'tito' # assume it's on PATH by default CLI().main(argstring.split(' '))
if 'TITO_SRC_BIN_DIR' in os.environ:
bin_dir = os.environ['TITO_SRC_BIN_DIR']
tito_path = os.path.join(bin_dir, 'tito')
tito_cmd = "%s %s" % (tito_path, argstring)
print("Running: %s" % tito_cmd)
(status, output) = commands.getstatusoutput(tito_cmd)
print output
if status > 0:
print "Tito command failed, output:"
raise Exception()
def cleanup_temp_git(): def cleanup_temp_git():
""" Delete the test directory if it exists. """ """ Delete the test directory if it exists. """
@ -109,11 +100,10 @@ def create_single_project_git():
This is *ALL* you should count on in these tests. This is *ALL* you should count on in these tests.
""" """
run_command('mkdir -p %s' % TEST_DIR)
run_command('mkdir -p %s' % SINGLE_GIT) run_command('mkdir -p %s' % SINGLE_GIT)
os.chdir(SINGLE_GIT) os.chdir(SINGLE_GIT)
run_command('git init') run_command('git init')
run_tito("init") tito("init")
run_command('echo "offline = true" >> rel-eng/tito.props') run_command('echo "offline = true" >> rel-eng/tito.props')
run_command('git add rel-eng/tito.props') run_command('git add rel-eng/tito.props')
run_command('git commit -m "Setting offline"') run_command('git commit -m "Setting offline"')
@ -134,7 +124,7 @@ def create_multi_project_git():
run_command('mkdir -p %s' % MULTI_GIT) run_command('mkdir -p %s' % MULTI_GIT)
os.chdir(MULTI_GIT) os.chdir(MULTI_GIT)
run_command('git init') run_command('git init')
run_tito("init") tito("init")
run_command('echo "offline = true" >> rel-eng/tito.props') run_command('echo "offline = true" >> rel-eng/tito.props')
run_command('git add rel-eng/tito.props') run_command('git add rel-eng/tito.props')
run_command('git commit -m "Setting offline"') run_command('git commit -m "Setting offline"')
@ -181,7 +171,7 @@ def create_git_project(full_pkg_dir, pkg_name):
run_command('git commit -a -m "Initial commit for %s."' % pkg_name) run_command('git commit -a -m "Initial commit for %s."' % pkg_name)
# Create initial 0.0.1 tag: # Create initial 0.0.1 tag:
run_tito("tag --keep-version --accept-auto-changelog --debug") tito("tag --keep-version --accept-auto-changelog --debug")
def release_bumped(initial_version, new_version): def release_bumped(initial_version, new_version):
first_release = initial_version.split('-')[-1] first_release = initial_version.split('-')[-1]
@ -230,7 +220,7 @@ class SingleProjectTaggerTests(unittest.TestCase):
def test_initial_tag(self): def test_initial_tag(self):
""" Test creating an initial tag. """ """ Test creating an initial tag. """
run_tito("tag --accept-auto-changelog --debug") tito("tag --accept-auto-changelog --debug")
check_tag_exists("%s-0.0.2-1" % TEST_PKG_1, offline=True) check_tag_exists("%s-0.0.2-1" % TEST_PKG_1, offline=True)
@ -249,7 +239,7 @@ class MultiProjectTaggerTests(unittest.TestCase):
def test_release_tagger(self): def test_release_tagger(self):
os.chdir(os.path.join(MULTI_GIT, TEST_PKG_2_DIR)) os.chdir(os.path.join(MULTI_GIT, TEST_PKG_2_DIR))
start_ver = get_latest_tagged_version(TEST_PKG_2) start_ver = get_latest_tagged_version(TEST_PKG_2)
run_tito('tag --debug --accept-auto-changelog') tito('tag --debug --accept-auto-changelog')
new_ver = get_latest_tagged_version(TEST_PKG_2) new_ver = get_latest_tagged_version(TEST_PKG_2)
self.assertTrue(release_bumped(start_ver, new_ver)) self.assertTrue(release_bumped(start_ver, new_ver))
@ -261,7 +251,7 @@ class MultiProjectTaggerTests(unittest.TestCase):
run_command("git mv tito.props build.py.props") run_command("git mv tito.props build.py.props")
run_command('git commit -a -m "Rename to build.py.props"') run_command('git commit -a -m "Rename to build.py.props"')
run_tito('tag --debug --accept-auto-changelog') tito('tag --debug --accept-auto-changelog')
new_ver = get_latest_tagged_version(TEST_PKG_2) new_ver = get_latest_tagged_version(TEST_PKG_2)
self.assertTrue(release_bumped(start_ver, new_ver)) self.assertTrue(release_bumped(start_ver, new_ver))
@ -272,23 +262,23 @@ class SingleProjectBuilderTests(unittest.TestCase):
os.chdir(SINGLE_GIT) os.chdir(SINGLE_GIT)
def test_latest_tgz(self): def test_latest_tgz(self):
run_tito("build --tgz -o %s" % SINGLE_GIT) tito("build --tgz -o %s" % SINGLE_GIT)
def test_tag_tgz(self): def test_tag_tgz(self):
run_tito("build --tgz --tag=%s-0.0.1-1 -o %s" % (TEST_PKG_1, tito("build --tgz --tag=%s-0.0.1-1 -o %s" % (TEST_PKG_1,
SINGLE_GIT)) SINGLE_GIT))
self.assertTrue(os.path.exists(os.path.join(SINGLE_GIT, self.assertTrue(os.path.exists(os.path.join(SINGLE_GIT,
"%s-0.0.1.tar.gz" % TEST_PKG_1))) "%s-0.0.1.tar.gz" % TEST_PKG_1)))
def test_latest_srpm(self): def test_latest_srpm(self):
run_tito("build --srpm") tito("build --srpm")
def test_tag_srpm(self): def test_tag_srpm(self):
run_tito("build --srpm --tag=%s-0.0.1-1 -o SINGLE_GIT" % TEST_PKG_1) tito("build --srpm --tag=%s-0.0.1-1 -o SINGLE_GIT" % TEST_PKG_1)
def test_latest_rpm(self): def test_latest_rpm(self):
run_tito("build --rpm -o %s" % SINGLE_GIT) tito("build --rpm -o %s" % SINGLE_GIT)
def test_tag_rpm(self): def test_tag_rpm(self):
run_tito("build --rpm --tag=%s-0.0.1-1 -o %s" % (TEST_PKG_1, tito("build --rpm --tag=%s-0.0.1-1 -o %s" % (TEST_PKG_1,
SINGLE_GIT)) SINGLE_GIT))