Revert "Try and move local tests into main tests dir. Rename local tests. Save test settings to unique json files to avoid race conditions"

This reverts commit 43eda6b9df.
This commit is contained in:
Miguel Jacq 2018-10-11 16:07:16 +11:00
parent 43eda6b9df
commit 418252f7c6
No known key found for this signature in database
GPG Key ID: EEA4341C6D97A0B6
13 changed files with 181 additions and 14 deletions

View File

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

View File

@ -1 +0,0 @@
from .GuiBaseTest import GuiBaseTest

View File

@ -20,7 +20,7 @@ from onionshare_gui.mode.receive_mode import ReceiveMode
class GuiBaseTest(object):
@staticmethod
def set_up(test_settings, settings_filename='/tmp/testsettings.json'):
def set_up(test_settings):
'''Create GUI with given settings'''
# Create our test file
testfile = open('/tmp/test.txt', 'w')
@ -45,6 +45,7 @@ class GuiBaseTest(object):
app = OnionShare(common, testonion, True, 0)
web = Web(common, False, True)
settings_filename = '/tmp/testsettings.json'
open(settings_filename, 'w').write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt'], settings_filename, True)

View File

@ -0,0 +1 @@
from .GuiBaseTest import GuiBaseTest

160
tests_gui_local/conftest.py Normal file
View File

@ -0,0 +1,160 @@
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
@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

@ -20,7 +20,7 @@ class ReceiveModeTest(unittest.TestCase):
test_settings = {
"receive_allow_receiver_shutdown": True
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ReceiveModeTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

@ -21,7 +21,7 @@ class ReceiveModePublicModeTest(unittest.TestCase):
"public_mode": True,
"receive_allow_receiver_shutdown": True
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ReceiveModePublicModeTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

@ -19,7 +19,7 @@ class ShareModeTest(unittest.TestCase):
def setUpClass(cls):
test_settings = {
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

@ -20,7 +20,7 @@ class ShareModePublicModeTest(unittest.TestCase):
test_settings = {
"public_mode": True,
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModePublicModeTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

@ -20,7 +20,7 @@ class ShareModeStayOpenTest(unittest.TestCase):
test_settings = {
"close_after_first_download": False,
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeStayOpenTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

@ -23,23 +23,23 @@ class ShareModePersistentSlugTest(unittest.TestCase):
"save_private_key": True,
"close_after_first_download": False,
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModePersistentSlugTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):
GuiBaseTest.tear_down()
@pytest.mark.run(order=1000)
@pytest.mark.run(order=1)
def test_run_all_common_setup_tests(self):
GuiBaseTest.run_all_common_setup_tests(self)
@pytest.mark.run(order=1001)
def test_run_all_persistent_share_mode_tests(self):
@pytest.mark.run(order=2)
def test_run_all_share_mode_tests(self):
GuiBaseTest.run_all_share_mode_tests(self, False, True)
global slug
slug = self.gui.share_mode.server_status.web.slug
@pytest.mark.run(order=1002)
@pytest.mark.run(order=3)
def test_have_same_slug(self):
'''Test that we have the same slug'''
self.assertEqual(self.gui.share_mode.server_status.web.slug, slug)

View File

@ -21,7 +21,7 @@ class ShareModeTimerTest(unittest.TestCase):
"public_mode": False,
"shutdown_timeout": True,
}
cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeTimerTest.json')
cls.gui = GuiBaseTest.set_up(test_settings)
@classmethod
def tearDownClass(cls):

View File

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