Move GUI tests into tests/ dir and fix conftest related stuff so everything loads what it needs and passes

This commit is contained in:
Miguel Jacq 2018-10-13 10:35:09 +11:00
parent 2a00656fd6
commit e3459a5136
31 changed files with 25 additions and 189 deletions

View File

@ -19,5 +19,4 @@ before_script:
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# run CLI tests and local GUI tests
script:
- pytest --cov=onionshare tests/
- xvfb-run pytest tests_gui/
- xvfb-run pytest --cov=onionshare --cov=onionshare_gui -vvv tests/

View File

@ -10,6 +10,22 @@ import pytest
from onionshare import common, web, settings, strings
def pytest_addoption(parser):
parser.addoption(
"--runtor", action="store_true", default=False, help="run tor tests"
)
def pytest_collection_modifyitems(config, items):
if config.getoption("--runtor"):
# --runtor given in cli: do not skip tor tests
return
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)
@pytest.fixture
def temp_dir_1024():
""" Create a temporary directory that has a single file of a
@ -151,13 +167,11 @@ def time_strftime(monkeypatch):
@pytest.fixture
def common_obj():
_common = common.Common()
_common.settings = settings.Settings(_common)
strings.load_strings(_common)
return _common
return common.Common()
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
_common.version = 'DUMMY_VERSION_1.2.3'
strings.load_strings(_common)
return settings.Settings(_common)

View File

@ -23,7 +23,7 @@ import types
import pytest
from onionshare import strings
from onionshare.settings import Settings
# # Stub get_resource_path so it finds the correct path while running tests
# def get_resource_path(filename):
@ -40,6 +40,7 @@ class TestLoadStrings:
def test_load_strings_defaults_to_english(
self, common_obj, locale_en, sys_onionshare_dev_mode):
""" load_strings() loads English by default """
common_obj.settings = Settings(common_obj)
strings.load_strings(common_obj)
assert strings._('preparing_files') == "Compressing files."
@ -47,6 +48,7 @@ class TestLoadStrings:
def test_load_strings_loads_other_languages(
self, common_obj, locale_fr, sys_onionshare_dev_mode):
""" load_strings() loads other languages in different locales """
common_obj.settings = Settings(common_obj)
common_obj.settings.set('locale', 'fr')
strings.load_strings(common_obj)
assert strings._('preparing_files') == "Préparation des fichiers à partager."
@ -55,5 +57,6 @@ class TestLoadStrings:
self, common_obj, locale_invalid, sys_onionshare_dev_mode):
""" load_strings() raises a KeyError for an invalid locale """
with pytest.raises(KeyError):
common_obj.settings = Settings(common_obj)
common_obj.settings.set('locale', 'XX')
strings.load_strings(common_obj)

View File

@ -31,6 +31,7 @@ import tempfile
import pytest
from onionshare.common import Common
from onionshare import strings
from onionshare.web import Web
from onionshare.settings import Settings
@ -41,7 +42,7 @@ RANDOM_STR_REGEX = re.compile(r'^[a-z2-7]+$')
def web_obj(common_obj, mode, num_files=0):
""" Creates a Web object, in either share mode or receive mode, ready for testing """
common_obj.load_settings()
strings.load_strings(common_obj)
web = Web(common_obj, False, mode)
web.generate_slug()
web.stay_open = True

View File

View File

@ -1,176 +0,0 @@
import sys
# Force tests to look for resources in the source code tree
sys.onionshare_dev_mode = True
import os
import shutil
import tempfile
import pytest
from onionshare import common, web, settings
def pytest_addoption(parser):
parser.addoption(
"--runtor", action="store_true", default=False, help="run tor tests"
)
def pytest_collection_modifyitems(config, items):
if config.getoption("--runtor"):
# --runtor given in cli: do not skip tor tests
return
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)
@pytest.fixture
def temp_dir_1024():
""" Create a temporary directory that has a single file of a
particular size (1024 bytes).
"""
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)
return 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:
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.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)
return tmp_file.name
# 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 = web.share_mode.ZipWriter(
common.Common(),
zip_filename=common.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 = web.share_mode.ZipWriter(common.Common())
yield zw
zw.close()
tmp_dir = os.path.dirname(zw.zip_filename)
shutil.rmtree(tmp_dir)
@pytest.fixture
def locale_en(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('en_US', 'UTF-8'))
@pytest.fixture
def locale_fr(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('fr_FR', 'UTF-8'))
@pytest.fixture
def locale_invalid(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('xx_XX', 'UTF-8'))
@pytest.fixture
def locale_ru(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('ru_RU', 'UTF-8'))
@pytest.fixture
def platform_darwin(monkeypatch):
monkeypatch.setattr('platform.system', lambda: 'Darwin')
@pytest.fixture # (scope="session")
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 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 # (scope="session")
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')
@pytest.fixture
def common_obj():
return common.Common()
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
_common.version = 'DUMMY_VERSION_1.2.3'
return settings.Settings(_common)

View File

@ -1,5 +0,0 @@
#!/bin/bash
for test in `ls -1 | egrep ^local_`; do
pytest $test -vvv || exit 1
done