-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_d2.py
79 lines (62 loc) · 2.47 KB
/
test_d2.py
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
import pytest
from d2_python import D2
import os
from pathlib import Path
import tempfile
from unittest import mock
import subprocess
@pytest.fixture
def d2():
return D2()
def test_binary_detection(d2):
"""Test if the D2 binary exists and is executable in the expected location"""
assert os.path.exists(d2.binary_path)
assert os.access(d2.binary_path, os.X_OK)
def test_wrapper_vs_binary_output(d2, tmp_path):
"""
Verify that the wrapper produces identical output to direct binary usage.
This ensures our wrapper doesn't modify the D2 output in any way.
"""
diagram = "x -> y"
wrapper_output = tmp_path / "wrapper.svg"
binary_output = tmp_path / "binary.svg"
input_file = tmp_path / "test.d2"
# Generate output using wrapper with string input
d2.render(diagram, str(wrapper_output))
# Generate output using binary directly with file input
input_file.write_text(diagram)
subprocess.run([d2.binary_path, str(input_file), str(binary_output)], check=True)
# Compare binary outputs to ensure they're identical
assert wrapper_output.read_bytes() == binary_output.read_bytes()
def test_temp_file_cleanup(d2, tmp_path):
"""
Verify that temporary files are properly cleaned up after rendering.
Checks that no .d2 files are left in the temp directory.
"""
temp_files_before = list(Path(tempfile.gettempdir()).glob("*.d2"))
with mock.patch('subprocess.run') as mock_run:
mock_run.return_value = mock.Mock(returncode=0)
d2.render("x -> y", str(tmp_path / "output.svg"))
temp_files_after = list(Path(tempfile.gettempdir()).glob("*.d2"))
assert temp_files_before == temp_files_after
def test_command_construction(d2, tmp_path):
"""
Test that the wrapper correctly constructs D2 command with all options.
Verifies that each option (theme, layout, pad, format) is properly included
in the command passed to the binary.
"""
with mock.patch('subprocess.run') as mock_run:
mock_run.return_value = mock.Mock(returncode=0)
d2.render(
"x -> y",
str(tmp_path / "output.svg"),
theme='dark',
layout='elk',
pad=200,
format='png'
)
cmd = mock_run.call_args[0][0]
assert all(x in cmd for x in ['--theme', 'dark'])
assert all(x in cmd for x in ['--layout', 'elk'])
assert all(x in cmd for x in ['--pad', '200'])
assert all(x in cmd for x in ['--format', 'png'])