aboutsummaryrefslogtreecommitdiff
blob: 4db0fbf53bfe6202c2f7d8f0843a92c3cb60cc05 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import py
import sys, os, signal, thread, time
from dotviewer.conftest import option

SOURCE1 = r'''digraph _generated__graph {
subgraph _generated__ {
_generated__ [shape="box", label="generated", color="black", fillcolor="#a5e6f0", style="filled", width="0.75"];
edge [label="startblock", style="dashed", color="black", dir="forward", weight="5"];
_generated__ -> _generated____1
_generated____1 [shape="box", label="generated__1\ninputargs: v2720\n\n", color="black", fillcolor="white", style="filled", width="0.75"];
edge [label="v2720", style="solid", color="black", dir="forward", weight="5"];
_generated____1 -> _generated____2
_generated____2 [shape="octagon", label="generated__2\ninputargs: v2721\n\nv2722 = int_gt(v2721, (2))\l\lexitswitch: v2722", color="red", fillcolor="white", style="filled", width="0.75"];
edge [label="False: v2721", style="dotted", color="red", dir="forward", weight="5"];
_generated____2 -> _generated____3
edge [label="True: v2721", style="dotted", color="red", dir="forward", weight="5"];
_generated____2 -> _generated____4
_generated____3 [shape="octagon", label="generated__3\ninputargs: v2723\n\nv2724 = int_gt(v2723, (0))\l\lexitswitch: v2724", color="red", fillcolor="white", style="filled", width="0.75"];
edge [label="False: (22) v2723", style="dotted", color="red", dir="forward", weight="5"];
_generated____3 -> _generated____5
edge [label="True: v2723 v2724", style="dotted", color="red", dir="forward", weight="5"];
_generated____3 -> _generated____6
_generated____5 [shape="box", label="generated__5\ninputargs: v2727 v2725\n\nv2726 = int_sub(v2725, (1))\lv2728 = int_add(v2727, (1))\lv2729 = int_add(v2728, v2726)\l", color="black", fillcolor="white", style="filled", width="0.75"];
edge [label="v2729", style="solid", color="black", dir="forward", weight="5"];
_generated____5 -> _generated____7
_generated____7 [shape="box", label="generated__7\ninputargs: v2730\n\nreturn v2730", color="black", fillcolor="green", style="filled", width="0.75"];
_generated____6 [shape="box", label="generated__6\ninputargs: v2732 v2733\n\nv2731 = same_as((17))\l", color="black", fillcolor="white", style="filled", width="0.75"];
edge [label="v2731 v2732", style="solid", color="black", dir="forward", weight="5"];
_generated____6 -> _generated____5
_generated____4 [shape="box", label="generated__4\ninputargs: v2734\n\nv2735 = int_sub(v2734, (1))\lv2736 = int_add((55), v2735)\l", color="black", fillcolor="white", style="filled", width="0.75"];
edge [label="v2736", style="solid", color="black", dir="forward", weight="5"];
_generated____4 -> _generated____7
}
}
'''

SOURCE2=r'''digraph f {
  a; d; e; f; g; h; i; j; k; l;
  a -> d [penwidth=1, style="setlinewidth(1)"];
  d -> e [penwidth=2, style="setlinewidth(2)"];
  e -> f [penwidth=4, style="setlinewidth(4)"];
  f -> g [penwidth=8, style="setlinewidth(8)"];
  g -> h [penwidth=16, style="setlinewidth(16)"];
  h -> i [penwidth=32, style="setlinewidth(32)"];
  i -> j [penwidth=64, style="setlinewidth(64)"];
  j -> k [penwidth=128, style="setlinewidth(128)"];
  k -> l [penwidth=256, style="setlinewidth(256)"];
}'''


def setup_module(mod):
    if not option.pygame:
        py.test.skip("--pygame not enabled")
    udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3)
    udir.join('graph1.dot').write(SOURCE1)

    from dotviewer import graphclient
    mod.pkgdir = py.path.local(graphclient.this_dir)
    mod.udir = udir

    try:
        del os.environ['GRAPHSERVER']
    except KeyError:
        pass


def test_dotviewer():
    print "=== dotviewer.py graph1.dot"
    err = os.system('"%s" "%s"' % (pkgdir.join('dotviewer.py'),
                                   udir.join('graph1.dot')))
    assert err == 0
    os.system('dot -Tplain "%s" > "%s"' % (udir.join('graph1.dot'),
                                           udir.join('graph1.plain')))
    print "=== dotviewer.py graph1.plain"
    err = os.system('"%s" "%s"' % (pkgdir.join('dotviewer.py'),
                                   udir.join('graph1.plain')))
    assert err == 0

def test_display_dot_file():
    FILES = [    # one thread per file - each thread calls display_dot_file()
        'graph1.dot',
        'clock.dot']
    locks = []
    errors = []
    done = []
    write_clock_file()
    thread.start_new_thread(update_clock_file, (done,))
    for filename in FILES:
        lock = thread.allocate_lock()
        lock.acquire()
        locks.append(lock)
        thread.start_new_thread(display1, (filename, lock, errors))
    # wait for all threads to finish
    for lock in locks:
        lock.acquire()
    done.append(True)
    if errors:
        exc, value, tb = errors[0]
        raise exc, value, tb

def write_clock_file():
    filename = udir.join('clock.dot')
    tmpname = udir.join('clock.dot~')
    currenttime = time.ctime()
    tmpname.write('''digraph clock {
        node1 [shape="box", label="%s", color="black", fillcolor="#a5e6f0", style="filled", width="0.75"];
        node2 [shape="box", label="try reloading!"];
    }''' % currenttime)
    if sys.platform.startswith('win'):    # XXX really necessary?
        os.unlink(str(filename))
    os.rename(str(tmpname), str(filename))

def update_clock_file(done):
    time.sleep(1)
    while not done:
        write_clock_file()
        time.sleep(1)

def display1(filename, lock, errors):
    try:
        try:
            from dotviewer.graphclient import display_dot_file
            print "=== display_dot_file(%s) with GRAPHSERVER=%s" % (
                filename, os.environ.get('GRAPHSERVER', ''),)
            display_dot_file(udir.join(filename))
            print "=== display_dot_file finished"
        except:
            errors.append(sys.exc_info())
    finally:
        lock.release()

def test_graphserver():
    import socket
    s = socket.socket()
    s.listen(1)
    host, port = s.getsockname()     # pick a random free port
    s.close()

    if hasattr(sys, 'pypy_objspaceclass'):
        python = 'python'
    else:
        python = sys.executable

    cmdargs = [python, str(pkgdir.join('graphserver.py')),
               str(port)]
    print '* starting:', ' '.join(cmdargs)
    pid = os.spawnv(os.P_NOWAIT, cmdargs[0], cmdargs)
    try:
        time.sleep(1)    # hack - wait a bit to make sure the server is up
        os.environ['GRAPHSERVER'] = '%s:%d' % (host, port)
        try:
            test_display_dot_file()
        finally:
            del os.environ['GRAPHSERVER']
    finally:
        os.kill(pid, signal.SIGTERM)

def test_colors():
    from dotviewer import graphpage, graphclient
    class MyPage(graphpage.DotFileGraphPage):
        def compute(self, dotfile):
            super(MyPage, self).compute(dotfile)
            self.links = {'v2721': 'Hello world',
                          'v2720': ('Something green', (0, 192, 0)),
                          }
    dotfile = udir.join('graph1.dot')
    page = MyPage(str(dotfile))
    graphclient.display_page(page)

def test_fixedfont():
    from dotviewer import graphpage, graphclient
    class MyPage(graphpage.DotFileGraphPage):
        fixedfont = True
    dotfile = udir.join('graph1.dot')
    page = MyPage(str(dotfile))
    page.fixedfont = True
    graphclient.display_page(page)

def test_linewidth():
    udir.join("graph2.dot").write(SOURCE2)
    from dotviewer import graphpage, graphclient
    dotfile = udir.join('graph2.dot')
    page = graphpage.DotFileGraphPage(str(dotfile))
    graphclient.display_page(page)

def test_ensure_readable():
    from dotviewer import graphpage, graphclient
    tmpname = udir.join("graph5.dot")
    tmpname.write('''digraph f {
        node1 [shape="box", label="readable???", color="black", fillcolor="black", style="filled"];
    }''')
    page = graphpage.DotFileGraphPage(str(tmpname))
    graphclient.display_page(page)