From 17f301022be579ec953e9345a47c9f23145416bf Mon Sep 17 00:00:00 2001 From: Delirious Lettuce Date: Fri, 7 Jul 2017 22:51:09 -0600 Subject: [PATCH] Initial commit (all current pytest fixtures) --- test/conftest.py | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/test/conftest.py b/test/conftest.py index e69de29b..a66c7725 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -0,0 +1,116 @@ +import os +import shutil +import sys +import tempfile + +import pytest + +from onionshare import common + + +# 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 (and file inside) will + be deleted after fixture usage. + """ + + with tempfile.TemporaryDirectory() as tmp_dir: + 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_file_1024_delete(): + """ + Create a temporary file of a particular size (1024 bytes). + The temporary file will be deleted after fixture usage. + """ + + with tempfile.NamedTemporaryFile() as tmp_file: + tmp_file.write(b'*' * 1024) + tmp_file.flush() + yield tmp_file.name + + +# pytest > 2.9 only needs @pytest.fixture +@pytest.yield_fixture(scope='session') +def custom_zw(): + zw = common.ZipWriter( + zip_filename=common.random_string(4, 6), + processed_size_callback=lambda _: 'custom_callback' + ) + yield zw + zw.close() + os.remove(zw.zip_filename) + + +# pytest > 2.9 only needs @pytest.fixture +@pytest.yield_fixture(scope='session') +def default_zw(): + zw = common.ZipWriter() + yield zw + zw.close() + tmp_dir = os.path.dirname(zw.zip_filename) + shutil.rmtree(tmp_dir) + + +@pytest.fixture +def platform_darwin(monkeypatch): + monkeypatch.setattr('platform.system', lambda: 'Darwin') + + +@pytest.fixture +def platform_linux(monkeypatch): + monkeypatch.setattr('platform.system', lambda: 'Linux') + + +@pytest.fixture +def platform_windows(monkeypatch): + monkeypatch.setattr('platform.system', lambda: 'Windows') + + +@pytest.fixture +def set_debug_false(monkeypatch): + monkeypatch.setattr('onionshare.common.debug', False) + + +@pytest.fixture +def set_debug_true(monkeypatch): + monkeypatch.setattr('onionshare.common.debug', True) + + +@pytest.fixture +def sys_argv_sys_prefix(monkeypatch): + monkeypatch.setattr('sys.argv', [sys.prefix]) + + +@pytest.fixture +def sys_frozen(monkeypatch): + monkeypatch.setattr('sys.frozen', True, raising=False) + + +@pytest.fixture +def sys_meipass(monkeypatch): + monkeypatch.setattr( + 'sys._MEIPASS', os.path.expanduser('~'), raising=False) + + +@pytest.fixture +def sys_onionshare_dev_mode(monkeypatch): + monkeypatch.setattr('sys.onionshare_dev_mode', True, raising=False) + + +@pytest.fixture +def time_time_100(monkeypatch): + monkeypatch.setattr('time.time', lambda: 100) + + +@pytest.fixture +def time_strftime(monkeypatch): + monkeypatch.setattr('time.strftime', lambda _: 'Jun 06 2013 11:05:00')