From 99bc9ea25be29a07a64b0936e64a7d99c4f1e7ae Mon Sep 17 00:00:00 2001 From: Delirious Lettuce Date: Fri, 7 Jul 2017 23:16:37 -0600 Subject: [PATCH] Create temporary file/directory without deleting them after --- test/conftest.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index a66c7725..147c7e99 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -10,11 +10,24 @@ from onionshare import common # pytest > 2.9 only needs @pytest.fixture @pytest.yield_fixture() -def temp_dir_1024_delete(): +def temp_dir_1024(): + """ Create a temporary directory that has a single file of a + particular size (1024 bytes). """ - Create a temporary directory that has a single file of a particular - size (1024 bytes). The temporary directory (and file inside) will - be deleted after fixture usage. + + tmp_dir = tempfile.mkdtemp() + tmp_file, tmp_file_path = tempfile.mkstemp(dir=tmp_dir) + with open(tmp_file, 'wb') as f: + f.write(b'*' * 1024) + yield tmp_dir + + +# pytest > 2.9 only needs @pytest.fixture +@pytest.yield_fixture() +def temp_dir_1024_delete(): + """ Create a temporary directory that has a single file of a + particular size (1024 bytes). The temporary directory (including + the file inside) will be deleted after fixture usage. """ with tempfile.TemporaryDirectory() as tmp_dir: @@ -24,6 +37,16 @@ def temp_dir_1024_delete(): yield tmp_dir +# pytest > 2.9 only needs @pytest.fixture +@pytest.yield_fixture() +def temp_file_1024(): + """ Create a temporary file of a particular size (1024 bytes). """ + + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: + tmp_file.write(b'*' * 1024) + yield tmp_file.name + + # pytest > 2.9 only needs @pytest.fixture @pytest.yield_fixture() def temp_file_1024_delete():