Skip to content

Commit 0323bcc

Browse files
committed
Use fixture to cleanup test generated files
1 parent 7d02d18 commit 0323bcc

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

test_main.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
import frontend as ft
22
import ulid
33
from pathlib import Path
4+
import pytest
5+
6+
7+
@pytest.fixture()
8+
def file_cleanup(request):
9+
"""
10+
Gives me the ability to defer the file cleanup, in the test body, by
11+
appending the paths to this list.
12+
"""
13+
paths_to_cleanup = []
14+
15+
def cleanup():
16+
for p in paths_to_cleanup:
17+
p.unlink()
18+
19+
request.addfinalizer(cleanup)
20+
return paths_to_cleanup
21+
422

523
def test_load_comments():
624
comments = ft.get_comments_for_slug("example", [".",])
725
assert len(comments) == 1
826

9-
def test_create_comment():
27+
def test_create_comment(file_cleanup):
1028
comments = ft.get_comments_for_slug("example", [".",])
1129

1230
comment_fname = str(ulid.new())
1331
path = ft.create_new_comment("Leon", "leon@gmail.com", "Do you dream, Elliot?", comment_fname, ["example",])
32+
file_cleanup.append(path)
1433

1534
comments = ft.get_comments_for_slug("example", [".",])
1635

1736
assert comments[1].created_by == "Leon"
1837
assert len(comments) == 2
1938

20-
# Cleanup after yourself!
21-
path.unlink()
22-
2339
def test_comment_api_from_path():
2440
p = Path("comments", "example", "01H7A86Y8EM4DM3FM25EWPHB8W.txt")
2541
c = ft.Comment.from_path(p)
@@ -38,7 +54,7 @@ def test_comment_api_into_file_author_fail():
3854

3955
comments = ft.get_comments_for_slug("example", [".",])
4056

41-
def test_comment_api_into_file():
57+
def test_comment_api_into_file(file_cleanup):
4258
comments = ft.get_comments_for_slug("example", [".",])
4359

4460
comment_fname = str(ulid.new())
@@ -49,12 +65,9 @@ def test_comment_api_into_file():
4965
comment.paragraphs = ["How do I know which one's for me?",]
5066

5167
result_path = comment.dump_into_file(["example",], comment_fname)
68+
file_cleanup.append(result_path)
5269

5370
comments = ft.get_comments_for_slug("example", [".",])
5471

5572
assert len(comments[-1].paragraphs) == 1
5673
assert comments[-1].created_by == "Elliot"
57-
58-
result_path.unlink()
59-
60-

0 commit comments

Comments
 (0)