From: Fred L. D. <fd...@us...> - 2003-07-11 04:26:38
|
Update of /cvsroot/cvs-syncmail/syncmail/tests In directory sc8-pr-cvs1:/tmp/cvs-serv12297/tests Added Files: Tag: new-config-branch syncmail.py test_options.py Log Message: Move the tests to a subdir, and make them use a proxy module that deals with loading code from the syncmail script. This provides better namespace isolation between syncmail and the tests. --- NEW FILE: test_options.py --- """Tests for the helper functions in syncmail.""" # These tests assume that the syncmail script is in the current directory. import os import sys from cStringIO import StringIO import syncmail def eq(a, b, msg=None): if msg is None: msg = "%s != %s" % (`a`, `b`) 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 = syncmail.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(syncmail.load_cmdline([]), ({}, [])) sys.stderr = StringIO() sys.stdout = sys.stderr try: raises(SystemExit, syncmail.load_cmdline, ['-qq']) raises(SystemExit, syncmail.load_cmdline, ['--config=filename', '--no-config']) raises(SystemExit, syncmail.load_cmdline, ['--no-config', '--no-config']) finally: sys.stderr = sys.__stderr__ sys.stdout = sys.__stdout__ # load_configuration() config = syncmail.load_configuration([]) eq(config.contextlines, 2) eq(config.verbose, 1) eq(config.smtp_server, syncmail.MAILHOST) eq(config.smtp_port, syncmail.MAILPORT) config = syncmail.load_configuration(['-q', '--mailhost=smtp.example.com']) eq(config.verbose, 0) eq(config.smtp_server, "smtp.example.com") eq(config.smtp_port, syncmail.MAILPORT) config = syncmail.load_configuration(['--mailhost=smtp.example.com:8025']) eq(config.smtp_server, "smtp.example.com") eq(config.smtp_port, 8025) config = syncmail.load_configuration(['--mailhost=:8025']) eq(config.smtp_server, syncmail.MAILHOST) eq(config.smtp_port, 8025) # OptionLookup dicts = [ {'common': '1', 'first': 'one'}, {'common': '2', 'second': 'two'}, {'common': '3', 'third': 'three'}, {'common': '4', 'fourth': 'four'}, ] options = syncmail.OptionLookup(dicts) eq(options.get('common'), '1') eq(options.get('first'), 'one') eq(options.get('second'), 'two') eq(options.get('third'), 'three') eq(options.get('fourth'), 'four') eq(options.get('missing'), None) eq(options.get('common'), '1') eq(options.get('third'), 'three') options = syncmail.OptionLookup([{"foo": "bar", "branch": "$BRANCH", "hostname": "$HOSTNAME"}], "my-branch") eq(options.get("branch"), "my-branch") eq(options.get("hostname"), os.environ.get("HOSTNAME", syncmail.getfqdn())) --- NEW FILE: syncmail.py --- """Module which loads the syncmail script as a module.""" import os __here = os.path.dirname(os.path.abspath(__file__)) __script = os.path.join(os.path.dirname(__here), "syncmail") del os del __here execfile(__script) del __script |