Add tests to handle command-line arguments
This commit is contained in:
parent
bde4b1bf10
commit
7eb8015aee
@ -381,7 +381,7 @@ class ArgHandler(object):
|
||||
self.__import_action()
|
||||
return None
|
||||
|
||||
def handle_args_cli(self, cleanup=True):
|
||||
def handle_args_cli(self, cleanup=True, should_exit=True):
|
||||
"""
|
||||
Depending on the given arguments, import or open data, launch
|
||||
session, write files, and/or perform actions.
|
||||
@ -398,19 +398,28 @@ class ArgHandler(object):
|
||||
|
||||
print(_("%(full_DB_path)s with name \"%(f_t_name)s\"")
|
||||
% {'full_DB_path' : dirname, 'f_t_name' : name})
|
||||
sys.exit(0)
|
||||
if should_exit:
|
||||
sys.exit(0)
|
||||
else:
|
||||
return
|
||||
|
||||
# Handle the "-L" List Family Trees in detail option.
|
||||
if self.list_more:
|
||||
self.dbman.print_family_tree_summaries()
|
||||
sys.exit(0)
|
||||
if should_exit:
|
||||
sys.exit(0)
|
||||
else:
|
||||
return
|
||||
|
||||
# Handle the "-t" List Family Trees, tab delimited option.
|
||||
if self.list_table:
|
||||
print(_('Gramps Family Trees:'))
|
||||
summary_list = self.dbman.family_tree_summary()
|
||||
if not summary_list:
|
||||
sys.exit(0)
|
||||
if should_exit:
|
||||
sys.exit(0)
|
||||
else:
|
||||
return
|
||||
# We have to construct the line elements together, to avoid
|
||||
# insertion of blank spaces when print on the same line is used
|
||||
line_list = [_("Family Tree")]
|
||||
@ -426,7 +435,10 @@ class ArgHandler(object):
|
||||
# translators: used in French+Russian, ignore otherwise
|
||||
line_list += [(_('"%s"') % summary[item])]
|
||||
print("\t".join(line_list))
|
||||
sys.exit(0)
|
||||
if should_exit:
|
||||
sys.exit(0)
|
||||
else:
|
||||
return
|
||||
|
||||
self.__open_action()
|
||||
self.__import_action()
|
||||
@ -447,7 +459,8 @@ class ArgHandler(object):
|
||||
if cleanup:
|
||||
self.cleanup()
|
||||
print(_("Exiting."), file=sys.stderr)
|
||||
sys.exit(0)
|
||||
if should_exit:
|
||||
sys.exit(0)
|
||||
|
||||
def cleanup(self):
|
||||
print(_("Cleaning up."), file=sys.stderr)
|
||||
|
@ -27,6 +27,11 @@ import re
|
||||
import subprocess
|
||||
|
||||
from gramps.gen.const import TEMP_DIR
|
||||
from gramps.gen.dbstate import DbState
|
||||
from ..grampscli import CLIManager
|
||||
from ..user import User
|
||||
from ..arghandler import ArgHandler
|
||||
from ..argparser import ArgParser
|
||||
|
||||
test_ged = """0 HEAD
|
||||
1 SOUR min1r.ged min 1-rec
|
||||
@ -42,6 +47,9 @@ test_ged = """0 HEAD
|
||||
ddir = os.path.dirname(__file__)
|
||||
min1r = os.path.join(ddir, "min1r.ged")
|
||||
out_ged = os.path.join(ddir, "test_out.ged")
|
||||
example_copy = os.path.join(ddir, "copy.gramps")
|
||||
example = os.path.join(ddir, "..", "..", "..",
|
||||
"example", "gramps", "data.gramps")
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@ -155,6 +163,35 @@ class UnicodeTest(unittest.TestCase):
|
||||
self.assertEqual(self.newtitle, title, "Compare titles %s and %s" %
|
||||
(repr(self.newtitle), repr(title)))
|
||||
|
||||
class CLITest(unittest.TestCase):
|
||||
def tearDown(self):
|
||||
if os.path.exists(example_copy):
|
||||
os.remove(example_copy)
|
||||
|
||||
def setUp(self):
|
||||
self.tearDown()
|
||||
self.dbstate = DbState()
|
||||
#we need a manager for the CLI session
|
||||
self.user = User(auto_accept=True, quiet=False)
|
||||
self.climanager = CLIManager(self.dbstate, setloader=True, user=self.user)
|
||||
#load the plugins
|
||||
self.climanager.do_reg_plugins(self.dbstate, uistate=None)
|
||||
|
||||
def test1_cli(self):
|
||||
# handle the arguments
|
||||
argparser = ArgParser([None, "-C", "Test", "--import", example])
|
||||
argparser.need_gui() # initializes some variables
|
||||
handler = ArgHandler(self.dbstate, argparser, self.climanager)
|
||||
# create a manager to manage the database
|
||||
handler.handle_args_cli(should_exit=False)
|
||||
|
||||
def test2_cli(self):
|
||||
# handle the arguments
|
||||
argparser = ArgParser([None, "-O", "Test", "--export", example_copy])
|
||||
argparser.need_gui() # initializes some variables
|
||||
handler = ArgHandler(self.dbstate, argparser, self.climanager)
|
||||
# create a manager to manage the database
|
||||
handler.handle_args_cli(should_exit=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user