File: test_tools.py

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (220 lines) | stat: -rw-r--r-- 5,792 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
Graphviz tools tests

The test cases in this file are sanity checks on the various tools in the
Graphviz suite. A failure of one of these indicates that some basic functional
property of one of the tools has been broken.
"""

import os
import platform
import re
import subprocess
import sys
import xml.etree.ElementTree as ET
from pathlib import Path

import pytest

sys.path.append(os.path.dirname(__file__))
from gvtest import (  # pylint: disable=wrong-import-position
    has_sandbox,
    remove_asan_summary,
    remove_xtype_warnings,
    run,
    run_raw,
    which,
)


@pytest.mark.parametrize(
    "tool",
    [
        "acyclic",
        "bcomps",
        "ccomps",
        "circo",
        "cluster",
        "diffimg",
        "dijkstra",
        "dot",
        "dot2gxl",
        "dot_builtins",
        "edgepaint",
        "fdp",
        "gc",
        "gml2gv",
        "graphml2gv",
        "gv2gml",
        "gv2gxl",
        "gvcolor",
        "gvedit",
        "gvgen",
        "gvmap",
        "gvmap.sh",
        "gvpack",
        "gvpr",
        "gxl2dot",
        "gxl2gv",
        "mingle",
        "mm2gv",
        "neato",
        "nop",
        "osage",
        "patchwork",
        "prune",
        "sccmap",
        "sfdp",
        "smyrna",
        "tred",
        "twopi",
        "unflatten",
        "vimdot",
    ],
)
def test_tools(tool):
    """
    check the help/usage output of the given tool looks correct
    """

    if which(tool) is None:
        pytest.skip(f"{tool} not available")

    # exec-ing a POSIX shell script as-is does not work on Windows
    if tool == "gvmap.sh" and platform.system() == "Windows":
        pytest.skip("gvmap.sh cannot be run directly on Windows")

    # Ensure that X fails to open display
    environ_copy = os.environ.copy()
    environ_copy.pop("DISPLAY", None)

    # Test usage
    with subprocess.Popen(
        [tool, "-?"],
        env=environ_copy,
        stdin=subprocess.DEVNULL,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
    ) as p:
        output, _ = p.communicate()
        ret = p.returncode

    assert ret == 0, f"`{tool} -?` failed. Output was: {output}"

    output = remove_asan_summary(output)
    output = remove_xtype_warnings(output)
    assert (
        re.match("usage", output, flags=re.IGNORECASE) is not None
    ), f"{tool} -? did not show usage. Output was: {output}"

    # Test unsupported option
    returncode = subprocess.call(
        [tool, "-$"],
        env=environ_copy,
    )

    assert returncode != 0, f"{tool} accepted unsupported option -$"


@pytest.mark.skipif(which("edgepaint") is None, reason="edgepaint not available")
@pytest.mark.parametrize(
    "arg",
    (
        "-accuracy=0.01",
        "-angle=15",
        "-random_seed=42",
        "-random_seed=-42",
        "-lightness=0,70",
        "-share_endpoint",
        f"-o {os.devnull}",
        "-color_scheme=accent7",
        "-v",
    ),
)
def test_edgepaint_options(arg: str):
    """
    edgepaint should correctly understand all its command line flags
    """

    # a basic graph that edgepaint can process
    input = (
        "digraph {\n"
        '  graph [bb="0,0,54,108"];\n'
        '  node [label="\\N"];\n'
        "  a       [height=0.5,\n"
        '           pos="27,90",\n'
        "           width=0.75];\n"
        "  b       [height=0.5,\n"
        '           pos="27,18",\n'
        "           width=0.75];\n"
        '  a -> b  [pos="e,27,36.104 27,71.697 27,63.983 27,54.712 27,46.112"];\n'
        "}"
    )

    # run edgepaint on this
    args = ["edgepaint"] + arg.split(" ")
    try:
        run(args, input=input)
    except subprocess.CalledProcessError as e:
        raise RuntimeError(f"edgepaint rejected command line option '{arg}'") from e


@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available")
def test_sandbox_noop():
    """check trivial functionality works when sandboxed"""
    sandbox = which("dot_sandbox")
    run_raw([sandbox, "-V"])


@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available")
def test_sandbox_basic():
    """check processing a simple graph when sandboxed"""
    sandbox = which("dot_sandbox")
    run([sandbox], input="graph { a -- b; }")


@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available")
def test_sandbox_render():
    """check rendering works when sandboxed"""
    sandbox = which("dot_sandbox")
    stdout = run([sandbox, "-Tsvg"], input="graph { a -- b; }")
    ET.fromstring(stdout)


@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available")
def test_sandbox_write(tmp_path: Path):
    """check file writing is prevented when sandboxed"""
    sandbox = which("dot_sandbox")
    proc = subprocess.run(
        [sandbox, "-o", "probe.dot"],
        input="graph { a -- b; }",
        text=True,
        cwd=tmp_path,
        check=False,
    )
    assert not (
        tmp_path / "probe.dot"
    ).exists(), "file writing within sandbox was not prevented"
    assert (
        proc.returncode != 0
    ), "failed file writing did not cause a non-zero exit status"


@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available")
def test_sandbox_write_2(tmp_path: Path):
    """check file writing via `-O` is prevented when sandboxed"""
    sandbox = which("dot_sandbox")
    proc = subprocess.run(
        [sandbox, "-Tsvg", "-O"],
        input="graph { a -- b; }",
        text=True,
        cwd=tmp_path,
        check=False,
    )
    assert not (
        tmp_path / "noname.gv.svg"
    ).exists(), "file writing within sandbox was not prevented"
    assert (
        proc.returncode != 0
    ), "failed file writing did not cause a non-zero exit status"