From 104a1e27cee2d5524460d26c83d3e920cd88b2e9 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Mon, 31 Dec 2007 02:10:56 +0000 Subject: Add unit testing for new option parsing library (Full Coverage) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4141 ce84e21b-d406-0410-9b95-82705330c041 --- src/lib/Options.py | 25 ++++++++------- testsuite/TestOptions.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 testsuite/TestOptions.py diff --git a/src/lib/Options.py b/src/lib/Options.py index 4e9a0a494..77e5bd718 100644 --- a/src/lib/Options.py +++ b/src/lib/Options.py @@ -13,6 +13,7 @@ class Option(object): if not self.__cfp: self.__cfp = ConfigParser.ConfigParser() self.__cfp.readfp(open(self.cfpath)) + return self.__cfp cfp = property(getCFP) def getValue(self): @@ -29,7 +30,7 @@ class Option(object): self.cmd = cmd if cmd and (cmd[0] != '-' or len(cmd) != 2): raise OptionFailure("Poorly formed command %s" % cmd) - self.odesg = odesc + self.odesc = odesc self.env = env self.cf = cf self.cook = cook @@ -54,9 +55,9 @@ class Option(object): def parse(self, opts, rawopts): if self.cmd and opts: # processing getopted data - optinfo = [opt[1] for opt in opts if opt[0] == option[0]] + optinfo = [opt[1] for opt in opts if opt[0] == self.cmd] if optinfo: - self._value = optinfo + self._value = optinfo[0] return if self.cmd and self.cmd in rawopts: self._value = rawopts[rawopts.index(self.cmd) + 1] @@ -67,10 +68,8 @@ class Option(object): return if self.cf: try: - if self.cf in locations: - self._value = self.cfp.get(*locations[self.cf]) - else: - self._value = self.cfp.get(*self.cf) + self._value = self.cfp.get(*self.cf) + return except: pass self._value = self.default @@ -94,11 +93,11 @@ class OptionSet(dict): ret = {} if do_getopt: try: - opts, args = getopt.getopt(argv, self.buildHelpGetopt(), []) + opts, args = getopt.getopt(argv, self.buildGetopt(), []) except getopt.GetoptError, err: self.helpExit(err) if '-h' in argv: - self.helpExit(err) + self.helpExit('', 0) for key in self.keys(): option = self[key] if do_getopt: @@ -112,11 +111,11 @@ class OptionSet(dict): class OptionParser(OptionSet): '''OptionParser bootstraps option parsing, getting the value of the config file''' def __init__(self, args): - self.Bootstrap = OptionSet(['configfile', Option('config file path', - '/etc/bcfg2.conf', - cmd='-C')]) + self.Bootstrap = OptionSet([('configfile', Option('config file path', + '/etc/bcfg2.conf', + cmd='-C'))]) self.Bootstrap.parse(sys.argv[1:], do_getopt=False) - if self.Bootstrap['configfile'] != '/etc/bcfg2.conf': + if self.Bootstrap['configfile'] != Option.cfpath: Option.cfpath = self.Bootstrap['configfile'] Option.__cfp = False OptionSet.__init__(self, args) diff --git a/testsuite/TestOptions.py b/testsuite/TestOptions.py new file mode 100644 index 000000000..9d4ed06a5 --- /dev/null +++ b/testsuite/TestOptions.py @@ -0,0 +1,79 @@ +import os, sys +import Bcfg2.Options + +class TestOption(object): + def test__init(self): + o = Bcfg2.Options.Option('foo', False, cmd='-F') + try: + p = Bcfg2.Options.Option('foo', False, cmd='--F') + assert False + except Bcfg2.Options.OptionFailure: + pass + + def test_parse(self): + o = Bcfg2.Options.Option('foo', 'test4', cmd='-F', env='TEST2', + odesc='bar', cf=('communication', 'password')) + o.parse([], ['-F', 'test']) + assert o.value == 'test' + o.parse([('-F', 'test2')], []) + assert o._value == 'test2' + os.environ['TEST2'] = 'test3' + o.parse([], []) + assert o._value == 'test3' + del os.environ['TEST2'] + o.parse([], []) + print o._value + assert o._value == 'foobat' + o.cf = False + o.parse([], []) + assert o._value == 'test4' + + def test_cook(self): + cooker = lambda x: 1 + o = Bcfg2.Options.Option('foo', 'test4', cook=cooker) + o.parse([], []) + assert o.value == 1 + + +class TestOptionSet(object): + def test_buildGetopt(self): + opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')), + ('bar', Bcfg2.Options.Option('foo', 'test2')), + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + os = Bcfg2.Options.OptionSet(opts) + res = os.buildGetopt() + assert 'H:' in res and 'G' in res and len(res) == 3 + + def test_parse(self): + opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')), + ('bar', Bcfg2.Options.Option('foo', 'test2')), + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + os = Bcfg2.Options.OptionSet(opts) + try: + os.parse(['-G', '-H']) + assert False + except SystemExit: + pass + opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-h')), + ('bar', Bcfg2.Options.Option('foo', 'test2')), + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + os2 = Bcfg2.Options.OptionSet(opts) + try: + os2.parse(['-h']) + assert False + except SystemExit: + pass + +class TestOptionParser(object): + def test__init(self): + opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-h')), + ('bar', Bcfg2.Options.Option('foo', 'test2')), + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + os1 = Bcfg2.Options.OptionParser(opts) + assert Bcfg2.Options.Option.cfpath == '/etc/bcfg2.conf' + sys.argv = ['foo', '-C', '/usr/local/etc/bcfg2.conf'] + os2 = Bcfg2.Options.OptionParser(opts) + assert Bcfg2.Options.Option.cfpath == '/usr/local/etc/bcfg2.conf' + sys.argv = [] + os3 = Bcfg2.Options.OptionParser(opts) + assert Bcfg2.Options.Option.cfpath == '/etc/bcfg2.conf' -- cgit v1.2.3-1-g7c22