Create temporary file/directory without deleting them after

This commit is contained in:
Delirious Lettuce 2017-07-07 23:16:37 -06:00
parent cf05204e65
commit 99bc9ea25b

View File

@ -10,11 +10,24 @@ from onionshare import common
# pytest > 2.9 only needs @pytest.fixture # pytest > 2.9 only needs @pytest.fixture
@pytest.yield_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 tmp_dir = tempfile.mkdtemp()
be deleted after fixture usage. 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: with tempfile.TemporaryDirectory() as tmp_dir:
@ -24,6 +37,16 @@ def temp_dir_1024_delete():
yield tmp_dir 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 > 2.9 only needs @pytest.fixture
@pytest.yield_fixture() @pytest.yield_fixture()
def temp_file_1024_delete(): def temp_file_1024_delete():