From: Fred L. D. <fd...@us...> - 2003-07-10 16:57:09
|
Update of /cvsroot/cvs-syncmail/syncmail In directory sc8-pr-cvs1:/tmp/cvs-serv13697 Modified Files: Tag: new-config-branch syncmail tests.py Log Message: Be more careful not to set an option more than once from the command line. Index: tests.py =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/Attic/tests.py,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -d -r1.1.2.2 -r1.1.2.3 --- tests.py 10 Jul 2003 12:56:11 -0000 1.1.2.2 +++ tests.py 10 Jul 2003 16:52:29 -0000 1.1.2.3 @@ -5,23 +5,52 @@ import os +from cStringIO import StringIO + # Since there's no .py extension, we need to load syncmail magically # so we don't trigger the main() function: __name__ = "notmain" execfile("syncmail") -VARS = {"FOO": "<whack!>"} - def eq(a, b, msg=None): if msg is None: msg = "%s != %s" % (`a`, `b`) - assert a == b, msg + if not a == b: + raise AssertionError(msg) + +def raises(exctype, f, *args, **kw): + try: + apply(f, args, kw) + except exctype: + pass + else: + raise AssertionError("expected exception") + +VARS = {"FOO": "<whack!>"} +# "variable" replacement replace = Replacer(VARS) eq(replace("abc$FOO-def${SPLAT}"), "abc<whack!>-def") eq(replace("$FOO"), "<whack!>") +eq(replace("$ FOO"), "$ FOO") +eq(replace("${FOO}"), "<whack!>") +eq(replace(" ${FOO} "), " <whack!> ") + +# load_cmdline() +eq(load_cmdline([]), ({}, [])) + +sys.stderr = StringIO() +sys.stdout = sys.stderr +try: + raises(SystemExit, load_cmdline, ['-qq']) + raises(SystemExit, load_cmdline, ['--config=filename', '--no-config']) + raises(SystemExit, load_cmdline, ['--no-config', '--no-config']) +finally: + sys.stderr = sys.__stderr__ + sys.stdout = sys.__stdout__ +# load_configuration() config = load_configuration([]) eq(config.contextlines, 2) eq(config.verbose, 1) @@ -41,6 +70,7 @@ eq(config.smtp_server, MAILHOST) eq(config.smtp_port, 8025) +# OptionLookup dicts = [ {'common': '1', 'first': 'one'}, {'common': '2', 'second': 'two'}, Index: syncmail =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/syncmail,v retrieving revision 1.36.2.4 retrieving revision 1.36.2.5 diff -u -d -r1.36.2.4 -r1.36.2.5 --- syncmail 10 Jul 2003 14:26:35 -0000 1.36.2.4 +++ syncmail 10 Jul 2003 16:52:29 -0000 1.36.2.5 @@ -522,33 +522,36 @@ 'reply-to=', 'help', 'quiet']) except getopt.error, msg: usage(2, msg) - cmdline = {"config-file": "syncmail.conf"} + cmdline = {} + def set(option, value, cmdline=cmdline): + if cmdline.has_key(option): + usage(2, "can't set option more than once") + cmdline[option] = value for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt == '--cvsroot': - cmdline['cvsroot'] = arg + set('cvsroot', arg) elif opt == "--config": - cmdline['config-file'] = arg + set('config-file', arg) elif opt == '--no-config': - if cmdline.has_key('config-file'): - del cmdline['config-file'] + set('config-file', '') elif opt in ('-C', '--context'): - cmdline['context-lines'] = arg + set('context-lines', arg) elif opt == '-c': - cmdline['diff-type'] = 'context' + set('diff-type', 'context') elif opt == '-u': - cmdline['diff-type'] = 'unified' + set('diff-type', 'unified') elif opt in ('-S', '--subject-prefix'): - cmdline['subject-prefix'] = string.strip(arg) + set('subject-prefix', string.strip(arg)) elif opt in ('-R', '--reply-to'): - cmdline['reply-to'] = arg + set('reply-to', arg) elif opt in ('-q', '--quiet'): - cmdline['verbose'] = 'false' + set('verbose', 'false') elif opt in ('-f', '--fromhost'): - cmdline['from-host'] = arg + set('from-host', arg) elif opt in ('-m', '--mailhost'): - cmdline['smtp-server'] = arg + set('smtp-server', arg) return cmdline, args class Options: @@ -576,7 +579,11 @@ def load_configuration(args, branch=None): cmdline, args = load_cmdline(args) - cfgfile = cmdline.get('config-file') + if cmdline.has_key('config-file'): + cfgfile = cmdline['config-file'] + del cmdline['config-file'] + else: + cfgfile = "syncmail.conf" # cfgfile is specified relative to the CVSROOT directory; we need # to transform the path appropriately, since that won't be the # current directory when we try to read it. In fact, a wrong |