onionshare/tests/conftest.py

209 lines
5.4 KiB
Python
Raw Normal View History

import sys
2019-10-13 00:01:25 -04:00
# Force tests to look for resources in the source code tree
sys.onionshare_dev_mode = True
# Let OnionShare know the tests are running, to avoid colliding with settings files
sys.onionshare_test_mode = True
import os
import shutil
import tempfile
import pytest
2018-09-30 20:35:58 -04:00
from onionshare import common, web, settings, strings
2019-10-13 00:01:25 -04:00
# The temporary directory for CLI tests
test_temp_dir = None
def pytest_addoption(parser):
parser.addoption(
"--rungui", action="store_true", default=False, help="run GUI tests"
)
parser.addoption(
"--runtor", action="store_true", default=False, help="run tor tests"
)
def pytest_collection_modifyitems(config, items):
if not config.getoption("--runtor"):
# --runtor given in cli: do not skip tor tests
skip_tor = pytest.mark.skip(reason="need --runtor option to run")
for item in items:
if "tor" in item.keywords:
item.add_marker(skip_tor)
2019-10-13 00:01:25 -04:00
if not config.getoption("--rungui"):
# --rungui given in cli: do not skip GUI tests
skip_gui = pytest.mark.skip(reason="need --rungui option to run")
for item in items:
if "gui" in item.keywords:
item.add_marker(skip_gui)
@pytest.fixture
def temp_dir():
"""Creates a persistent temporary directory for the CLI tests to use"""
global test_temp_dir
if not test_temp_dir:
test_temp_dir = tempfile.mkdtemp()
return test_temp_dir
@pytest.fixture
def temp_dir_1024(temp_dir):
""" Create a temporary directory that has a single file of a
particular size (1024 bytes).
"""
new_temp_dir = tempfile.mkdtemp(dir=temp_dir)
tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir)
2019-10-13 00:01:25 -04:00
with open(tmp_file, "wb") as f:
f.write(b"*" * 1024)
return new_temp_dir
# pytest > 2.9 only needs @pytest.fixture
@pytest.yield_fixture
def temp_dir_1024_delete(temp_dir):
""" 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(dir=temp_dir) as new_temp_dir:
tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir)
2019-10-13 00:01:25 -04:00
with open(tmp_file, "wb") as f:
f.write(b"*" * 1024)
yield new_temp_dir
@pytest.fixture
def temp_file_1024(temp_dir):
""" Create a temporary file of a particular size (1024 bytes). """
with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file:
2019-10-13 00:01:25 -04:00
tmp_file.write(b"*" * 1024)
return tmp_file.name
# pytest > 2.9 only needs @pytest.fixture
@pytest.yield_fixture
def temp_file_1024_delete(temp_dir):
"""
Create a temporary file of a particular size (1024 bytes).
The temporary file will be deleted after fixture usage.
"""
with tempfile.NamedTemporaryFile(dir=temp_dir) as tmp_file:
2019-10-13 00:01:25 -04:00
tmp_file.write(b"*" * 1024)
tmp_file.flush()
yield tmp_file.name
# pytest > 2.9 only needs @pytest.fixture
2019-10-13 00:01:25 -04:00
@pytest.yield_fixture(scope="session")
def custom_zw():
zw = web.share_mode.ZipWriter(
2018-03-13 05:22:26 -04:00
common.Common(),
zip_filename=common.Common.random_string(4, 6),
2019-10-13 00:01:25 -04:00
processed_size_callback=lambda _: "custom_callback",
)
yield zw
zw.close()
os.remove(zw.zip_filename)
# pytest > 2.9 only needs @pytest.fixture
2019-10-13 00:01:25 -04:00
@pytest.yield_fixture(scope="session")
def default_zw():
zw = web.share_mode.ZipWriter(common.Common())
yield zw
zw.close()
tmp_dir = os.path.dirname(zw.zip_filename)
try:
shutil.rmtree(tmp_dir, ignore_errors=True)
except:
pass
2017-07-08 02:17:20 -04:00
@pytest.fixture
def locale_en(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("en_US", "UTF-8"))
2017-07-08 02:17:20 -04:00
@pytest.fixture
def locale_fr(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("fr_FR", "UTF-8"))
2017-07-08 02:17:20 -04:00
@pytest.fixture
def locale_invalid(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("xx_XX", "UTF-8"))
2017-07-08 02:17:20 -04:00
@pytest.fixture
def locale_ru(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("ru_RU", "UTF-8"))
2017-07-08 02:17:20 -04:00
@pytest.fixture
def platform_darwin(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("platform.system", lambda: "Darwin")
@pytest.fixture # (scope="session")
def platform_linux(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("platform.system", lambda: "Linux")
@pytest.fixture
def platform_windows(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("platform.system", lambda: "Windows")
@pytest.fixture
def sys_argv_sys_prefix(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("sys.argv", [sys.prefix])
@pytest.fixture
def sys_frozen(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("sys.frozen", True, raising=False)
@pytest.fixture
def sys_meipass(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("sys._MEIPASS", os.path.expanduser("~"), raising=False)
@pytest.fixture # (scope="session")
def sys_onionshare_dev_mode(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("sys.onionshare_dev_mode", True, raising=False)
@pytest.fixture
def time_time_100(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("time.time", lambda: 100)
@pytest.fixture
def time_strftime(monkeypatch):
2019-10-13 00:01:25 -04:00
monkeypatch.setattr("time.strftime", lambda _: "Jun 06 2013 11:05:00")
2018-03-13 05:22:26 -04:00
@pytest.fixture
def common_obj():
return common.Common()
2019-10-13 00:01:25 -04:00
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
2019-10-13 00:01:25 -04:00
_common.version = "DUMMY_VERSION_1.2.3"
strings.load_strings(_common)
return settings.Settings(_common)