Skip to content

Commit 63a43aa

Browse files
committed
fix(hansbug): remove named temporary file when rendering images
1 parent 49c17d9 commit 63a43aa

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

test/entry/cli/test_graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,6 @@ def test_file_with_invalid_permission(self):
245245
f'The output is:\n{result.output}'
246246
assert os.path.exists('test_graph.svg')
247247
if OS.windows:
248-
assert os.path.getsize('test_graph.svg') <= 2000
248+
assert 5000 <= os.path.getsize('test_graph.svg') <= 6500
249249
else:
250-
assert os.path.getsize('test_graph.svg') <= 1000
250+
assert 500 <= os.path.getsize('test_graph.svg') <= 1000

treevalue/entry/cli/graph.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def _save_source_code(g: Digraph, path: str):
6060
file.write(g.source)
6161

6262

63+
# Do not reopen an opened file in windows, it may cause PermissionDenied
64+
# See: https://stackoverflow.com/a/23212515/6995899
6365
def _save_image(g: Digraph, path: str, fmt: str):
64-
with tempfile.NamedTemporaryFile() as tmpfile:
65-
svg_file = g.render(tmpfile.name, format=fmt)
66+
with tempfile.TemporaryDirectory() as tmpdir:
67+
svg_file = g.render(os.path.join(tmpdir, 'source.gv'), format=fmt)
6668
shutil.copy(svg_file, path)
6769

6870

0 commit comments

Comments
 (0)