def setup(package): twilltestlib.cd_testdir() twilltestlib.run_server(twilltestserver.create_publisher) url = twilltestlib.get_url() from twill.commands import go, find from twill import logconfig logconfig.set_handler_for_stream(open(os.devnull, 'w')) try: go(url) find("These are the twill tests") except: raise Exception("\n\n***\n\nHello! The twill test server is not running or cannot be reached; please free port 8080 (or set TWILL_TEST_PORT to something else), and clear your proxy settings too!\n\n***\n\n")
def main(): global twillargs, interactive import sys from twill import TwillCommandLoop, execute_file, __version__ from twill.utils import gather_filenames from optparse import OptionParser from cStringIO import StringIO ### # make sure that the current working directory is in the path. does this # work on Windows?? if not '.' in sys.path: sys.path.append('.') ### #### OPTIONS parser = OptionParser() parser.add_option('-q', '--quiet', action="store_true", dest="quiet", help = 'do not show normal output') parser.add_option('-i', '--interactive', action="store_true", dest="interact", help = 'drop into an interactive shell after running files (if any)') parser.add_option('-f', '--fail', action="store_true", dest="fail", help = 'fail exit on first file to fail') parser.add_option('-n', '--never-fail', action="store_true", dest="never_fail", help = 'continue executing scripts past errors') parser.add_option('-v', '--version', action="store_true", dest="show_version", help = 'show version information and exit') parser.add_option('-u', '--url', nargs=1, action="store", dest="url", help="start at the given URL before each script") parser.add_option('-l', '--loglevel', nargs=1, action="store", dest="loglevel", help="set the log level") parser.add_option('-L', '--logfile', nargs=1, action="store", dest="logfile", help="use logfile as output for log, or discards log if value is 'none'") parser.add_option('-o', '--output', nargs=1, action="store", dest="outfile", help="print output to outfile, or discards output if value is 'none'") parser.add_option('-t', '--tidy', nargs=1, action="store", dest="tidycmd", help="uses tidycmd as the tidy command") #### # parse arguments. sysargs = sys.argv[1:] if '--' in sysargs: found = False for last in range(len(sysargs) - 1, -1, -1): if sysargs[last] == '--': found = True break if found: twillargs = sysargs[last + 1:] sysargs = sysargs[:last] (options, args) = parser.parse_args(sysargs) if options.show_version: print('twill version %s.' % __version__) sys.exit(0) if options.loglevel: if options.loglevel not in logconfig.loglevels: sys.exit("valid log levels are " + ", ".join(logconfig.loglevels.keys())) logconfig.logger.setLevel(logconfig.loglevels[options.loglevel]) if options.logfile: try: path = options.logfile if options.logfile != 'none' else os.devnull logfile = open(path, 'w') logfile.write('') logconfig.set_handler_for_stream(logfile) except IOError as e: sys.exit("Invalid logfile '%s': %s", options.logfile, e) if options.outfile: try: path = options.outfile if options.outfile != 'none' else os.devnull outfile = open(path, 'w') outfile.write('') sys.stdout = outfile except IOError as e: sys.exit("Invalid output file '%s': %s", options.logfile, e) if options.quiet: if options.interact: logger.critical("interactive mode is incompatible with -q") sys.exit(1) if not args: sys.exit("interactive mode is incompatible with -q") old_stdout = sys.stdout sys.stdout = open(os.devnull, 'w') logconfig.set_handler_for_stream(sys.stdout) if options.tidycmd: config.tidy_cmd = options.tidycmd # If run from the command line, find & run any scripts put on the command # line. If none, drop into an interactive AutoShell. failed = False if len(args): success = [] failure = [] filenames = gather_filenames(args) for filename in filenames: logger.info('>> EXECUTING FILE %s', filename) try: interactive = False execute_file(filename, initial_url=options.url, never_fail=options.never_fail) success.append(filename) except Exception, e: if options.fail: # import pdb # _, _, tb = sys.exc_info() # pdb.post_mortem(tb) raise else: logger.error('** UNHANDLED EXCEPTION: %s', str(e)) logger.debug(traceback.format_exc()) failure.append(filename) logger.info('--') logger.info('%d of %d files SUCCEEDED.', len(success), len(success) + len(failure)) if len(failure): logger.error('Failed:\n\t') logger.error("\n\t".join(failure)) failed = True