File: twilltestlib.py

package info (click to toggle)
twill 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,472 kB
  • ctags: 1,916
  • sloc: python: 12,686; java: 98; makefile: 18; sh: 2
file content (114 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys, subprocess

try:
    import pkg_resources
except ImportError:
    raise Exception("you must have setuptools installed to run the tests")

pkg_resources.require('quixote>=2.3')

from quixote.server.simple_server import run
from cStringIO import StringIO
import os
import socket
import urllib

_server_url = None

def get_url():
    if _server_url is None:
        raise Exception("server has not yet been started")
    return _server_url

testdir = os.path.dirname(__file__)
print 'testdir is:', testdir
sys.path.insert(0, os.path.abspath(os.path.join(testdir, '..')))

import twill

def cd_testdir():
    global cwd
    cwd = os.getcwd()
    os.chdir(testdir)

def pop_testdir():
    global cwd
    os.chdir(cwd)

def execute_twill_script(filename, inp=None, initial_url=None):
    global testdir

    if inp:
        inp_fp = StringIO(inp)
        old, sys.stdin = sys.stdin, inp_fp

    scriptfile = os.path.join(testdir, filename)
    try:
        twill.execute_file(filename, initial_url=initial_url)
    finally:
        if inp:
            sys.stdin = old

def execute_twill_shell(filename, inp=None, initial_url=None,
                        fail_on_unknown=False):
    # use filename as the stdin *for the shell object only*
    scriptfile = os.path.join(testdir, filename)
    
    cmd_inp = open(scriptfile).read()
    cmd_inp += '\nquit\n'
    cmd_inp = StringIO(cmd_inp)

    # use inp as the std input for the actual script commands.
    if inp:
        inp_fp = StringIO(inp)
        old, sys.stdin = sys.stdin, inp_fp

    try:
        try:
            s = twill.shell.TwillCommandLoop(initial_url=initial_url,
                                             stdin=cmd_inp,
                                             fail_on_unknown=fail_on_unknown)
            s.cmdloop()
        except SystemExit:
            pass
    finally:
        if inp:
            sys.stdin = old
    

def run_server(create_fn, PORT=None):
    """
    Run a Quixote simple_server on localhost:PORT with subprocess.
    All output is captured & thrown away.

    The parent process returns the URL on which the server is running.
    """
    import time, tempfile
    global _server_url

    if PORT is None:
        PORT = int(os.environ.get('TWILL_TEST_PORT', '8080'))

    outfd = tempfile.mkstemp('twilltst')[0]
	
    print 'STARTING:', sys.executable, 'tests/twilltestserver.py', os.getcwd()
    process = subprocess.Popen([sys.executable, '-u', 'twilltestserver.py'],
                               stderr=subprocess.STDOUT,
                               stdout=outfd)
   
    time.sleep(1)

    _server_url = 'http://localhost:%d/' % (PORT,)
	
def kill_server():
    """
    Kill the previously started Quixote server.
    """
    global _server_url
    if _server_url != None:
       try:
          fp = urllib.urlopen('%sexit' % (_server_url,))
       except:
          pass

    _server_url = None