| 1 |
""" |
| 2 |
Graphviz tools tests |
| 3 |
|
| 4 |
The test cases in this file are sanity checks on the various tools in the |
| 5 |
Graphviz suite. A failure of one of these indicates that some basic functional |
| 6 |
property of one of the tools has been broken. |
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import platform |
| 11 |
import re |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
import xml.etree.ElementTree as ET |
| 15 |
from pathlib import Path |
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
sys.path.append(os.path.dirname(__file__)) |
| 20 |
from gvtest import ( # pylint: disable=wrong-import-position |
| 21 |
has_sandbox, |
| 22 |
remove_asan_summary, |
| 23 |
remove_xtype_warnings, |
| 24 |
run, |
| 25 |
run_raw, |
| 26 |
which, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize( |
| 31 |
"tool", |
| 32 |
[ |
| 33 |
"acyclic", |
| 34 |
"bcomps", |
| 35 |
"ccomps", |
| 36 |
"circo", |
| 37 |
"cluster", |
| 38 |
"diffimg", |
| 39 |
"dijkstra", |
| 40 |
"dot", |
| 41 |
"dot2gxl", |
| 42 |
"dot_builtins", |
| 43 |
"edgepaint", |
| 44 |
"fdp", |
| 45 |
"gc", |
| 46 |
"gml2gv", |
| 47 |
"graphml2gv", |
| 48 |
"gv2gml", |
| 49 |
"gv2gxl", |
| 50 |
"gvcolor", |
| 51 |
"gvedit", |
| 52 |
"gvgen", |
| 53 |
"gvmap", |
| 54 |
"gvmap.sh", |
| 55 |
"gvpack", |
| 56 |
"gvpr", |
| 57 |
"gxl2dot", |
| 58 |
"gxl2gv", |
| 59 |
"mingle", |
| 60 |
"mm2gv", |
| 61 |
"neato", |
| 62 |
"nop", |
| 63 |
"osage", |
| 64 |
"patchwork", |
| 65 |
"prune", |
| 66 |
"sccmap", |
| 67 |
"sfdp", |
| 68 |
"smyrna", |
| 69 |
"tred", |
| 70 |
"twopi", |
| 71 |
"unflatten", |
| 72 |
"vimdot", |
| 73 |
], |
| 74 |
) |
| 75 |
def test_tools(tool): |
| 76 |
""" |
| 77 |
check the help/usage output of the given tool looks correct |
| 78 |
""" |
| 79 |
|
| 80 |
if which(tool) is None: |
| 81 |
pytest.skip(f"{tool} not available") |
| 82 |
|
| 83 |
# exec-ing a POSIX shell script as-is does not work on Windows |
| 84 |
if tool == "gvmap.sh" and platform.system() == "Windows": |
| 85 |
pytest.skip("gvmap.sh cannot be run directly on Windows") |
| 86 |
|
| 87 |
# Ensure that X fails to open display |
| 88 |
environ_copy = os.environ.copy() |
| 89 |
environ_copy.pop("DISPLAY", None) |
| 90 |
|
| 91 |
# Test usage |
| 92 |
with subprocess.Popen( |
| 93 |
[tool, "-?"], |
| 94 |
env=environ_copy, |
| 95 |
stdin=subprocess.DEVNULL, |
| 96 |
stdout=subprocess.PIPE, |
| 97 |
stderr=subprocess.STDOUT, |
| 98 |
text=True, |
| 99 |
) as p: |
| 100 |
output, _ = p.communicate() |
| 101 |
ret = p.returncode |
| 102 |
|
| 103 |
assert ret == 0, f"`{tool} -?` failed. Output was: {output}" |
| 104 |
|
| 105 |
output = remove_asan_summary(output) |
| 106 |
output = remove_xtype_warnings(output) |
| 107 |
assert ( |
| 108 |
re.match("usage", output, flags=re.IGNORECASE) is not None |
| 109 |
), f"{tool} -? did not show usage. Output was: {output}" |
| 110 |
|
| 111 |
# Test unsupported option |
| 112 |
returncode = subprocess.call( |
| 113 |
[tool, "-$"], |
| 114 |
env=environ_copy, |
| 115 |
) |
| 116 |
|
| 117 |
assert returncode != 0, f"{tool} accepted unsupported option -$" |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.skipif(which("edgepaint") is None, reason="edgepaint not available") |
| 121 |
@pytest.mark.parametrize( |
| 122 |
"arg", |
| 123 |
( |
| 124 |
"-accuracy=0.01", |
| 125 |
"-angle=15", |
| 126 |
"-random_seed=42", |
| 127 |
"-random_seed=-42", |
| 128 |
"-lightness=0,70", |
| 129 |
"-share_endpoint", |
| 130 |
f"-o {os.devnull}", |
| 131 |
"-color_scheme=accent7", |
| 132 |
"-v", |
| 133 |
), |
| 134 |
) |
| 135 |
def test_edgepaint_options(arg: str): |
| 136 |
""" |
| 137 |
edgepaint should correctly understand all its command line flags |
| 138 |
""" |
| 139 |
|
| 140 |
# a basic graph that edgepaint can process |
| 141 |
input = ( |
| 142 |
"digraph {\n" |
| 143 |
' graph [bb="0,0,54,108"];\n' |
| 144 |
' node [label="\\N"];\n' |
| 145 |
" a [height=0.5,\n" |
| 146 |
' pos="27,90",\n' |
| 147 |
" width=0.75];\n" |
| 148 |
" b [height=0.5,\n" |
| 149 |
' pos="27,18",\n' |
| 150 |
" width=0.75];\n" |
| 151 |
' a -> b [pos="e,27,36.104 27,71.697 27,63.983 27,54.712 27,46.112"];\n' |
| 152 |
"}" |
| 153 |
) |
| 154 |
|
| 155 |
# run edgepaint on this |
| 156 |
args = ["edgepaint"] + arg.split(" ") |
| 157 |
try: |
| 158 |
run(args, input=input) |
| 159 |
except subprocess.CalledProcessError as e: |
| 160 |
raise RuntimeError(f"edgepaint rejected command line option '{arg}'") from e |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available") |
| 164 |
def test_sandbox_noop(): |
| 165 |
"""check trivial functionality works when sandboxed""" |
| 166 |
sandbox = which("dot_sandbox") |
| 167 |
run_raw([sandbox, "-V"]) |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available") |
| 171 |
def test_sandbox_basic(): |
| 172 |
"""check processing a simple graph when sandboxed""" |
| 173 |
sandbox = which("dot_sandbox") |
| 174 |
run([sandbox], input="graph { a -- b; }") |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available") |
| 178 |
def test_sandbox_render(): |
| 179 |
"""check rendering works when sandboxed""" |
| 180 |
sandbox = which("dot_sandbox") |
| 181 |
stdout = run([sandbox, "-Tsvg"], input="graph { a -- b; }") |
| 182 |
ET.fromstring(stdout) |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available") |
| 186 |
def test_sandbox_write(tmp_path: Path): |
| 187 |
"""check file writing is prevented when sandboxed""" |
| 188 |
sandbox = which("dot_sandbox") |
| 189 |
proc = subprocess.run( |
| 190 |
[sandbox, "-o", "probe.dot"], |
| 191 |
input="graph { a -- b; }", |
| 192 |
text=True, |
| 193 |
cwd=tmp_path, |
| 194 |
check=False, |
| 195 |
) |
| 196 |
assert not ( |
| 197 |
tmp_path / "probe.dot" |
| 198 |
).exists(), "file writing within sandbox was not prevented" |
| 199 |
assert ( |
| 200 |
proc.returncode != 0 |
| 201 |
), "failed file writing did not cause a non-zero exit status" |
| 202 |
|
| 203 |
|
| 204 |
@pytest.mark.skipif(not has_sandbox(), reason="no supported sandbox available") |
| 205 |
def test_sandbox_write_2(tmp_path: Path): |
| 206 |
"""check file writing via `-O` is prevented when sandboxed""" |
| 207 |
sandbox = which("dot_sandbox") |
| 208 |
proc = subprocess.run( |
| 209 |
[sandbox, "-Tsvg", "-O"], |
| 210 |
input="graph { a -- b; }", |
| 211 |
text=True, |
| 212 |
cwd=tmp_path, |
| 213 |
check=False, |
| 214 |
) |
| 215 |
assert not ( |
| 216 |
tmp_path / "noname.gv.svg" |
| 217 |
).exists(), "file writing within sandbox was not prevented" |
| 218 |
assert ( |
| 219 |
proc.returncode != 0 |
| 220 |
), "failed file writing did not cause a non-zero exit status" |