From cd6931ec8c6f2010b7c39ce117211f274987a7a6 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Thu, 11 Oct 2018 16:04:37 +1100 Subject: [PATCH] Try and move local tests into main tests dir. Rename local tests. Save test settings to unique json files to avoid race conditions --- .travis.yml | 3 +- {tests_gui_local => tests}/GuiBaseTest.py | 3 +- tests/__init__.py | 1 + .../local_receive_mode_public_mode_test.py | 2 +- .../local_receive_mode_test.py | 2 +- .../local_share_mode_persistent_slug_test.py | 10 +- .../local_share_mode_public_mode_test.py | 2 +- .../local_share_mode_stay_open_test.py | 2 +- .../local_share_mode_test.py | 2 +- .../local_share_mode_timer_test.py | 2 +- tests_gui_local/__init__.py | 1 - tests_gui_local/conftest.py | 160 ------------------ tests_gui_local/run_unit_tests.sh | 5 - 13 files changed, 14 insertions(+), 181 deletions(-) rename {tests_gui_local => tests}/GuiBaseTest.py (99%) rename tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py => tests/local_receive_mode_public_mode_test.py (90%) rename tests_gui_local/onionshare_receive_mode_upload_test.py => tests/local_receive_mode_test.py (91%) rename tests_gui_local/onionshare_slug_persistent_test.py => tests/local_share_mode_persistent_slug_test.py (81%) rename tests_gui_local/onionshare_share_mode_download_test_public_mode.py => tests/local_share_mode_public_mode_test.py (90%) rename tests_gui_local/onionshare_share_mode_download_test_stay_open.py => tests/local_share_mode_stay_open_test.py (90%) rename tests_gui_local/onionshare_share_mode_download_test.py => tests/local_share_mode_test.py (91%) rename tests_gui_local/onionshare_timer_test.py => tests/local_share_mode_timer_test.py (91%) delete mode 100644 tests_gui_local/__init__.py delete mode 100644 tests_gui_local/conftest.py delete mode 100755 tests_gui_local/run_unit_tests.sh diff --git a/.travis.yml b/.travis.yml index e0b5b822..24255416 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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/ - - cd tests_gui_local/ && xvfb-run ./run_unit_tests.sh + - xvfb-run pytest --cov=onionshare tests/ diff --git a/tests_gui_local/GuiBaseTest.py b/tests/GuiBaseTest.py similarity index 99% rename from tests_gui_local/GuiBaseTest.py rename to tests/GuiBaseTest.py index e7d25031..7aec97f7 100644 --- a/tests_gui_local/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -20,7 +20,7 @@ from onionshare_gui.mode.receive_mode import ReceiveMode class GuiBaseTest(object): @staticmethod - def set_up(test_settings): + def set_up(test_settings, settings_filename='/tmp/testsettings.json'): '''Create GUI with given settings''' # Create our test file testfile = open('/tmp/test.txt', 'w') @@ -45,7 +45,6 @@ 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) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29b..1def7f5e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +from .GuiBaseTest import GuiBaseTest diff --git a/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py b/tests/local_receive_mode_public_mode_test.py similarity index 90% rename from tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py rename to tests/local_receive_mode_public_mode_test.py index 201402c2..0bc00833 100644 --- a/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py +++ b/tests/local_receive_mode_public_mode_test.py @@ -21,7 +21,7 @@ class ReceiveModePublicModeTest(unittest.TestCase): "public_mode": True, "receive_allow_receiver_shutdown": True } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ReceiveModePublicModeTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/onionshare_receive_mode_upload_test.py b/tests/local_receive_mode_test.py similarity index 91% rename from tests_gui_local/onionshare_receive_mode_upload_test.py rename to tests/local_receive_mode_test.py index 262c7aba..82d7529a 100644 --- a/tests_gui_local/onionshare_receive_mode_upload_test.py +++ b/tests/local_receive_mode_test.py @@ -20,7 +20,7 @@ class ReceiveModeTest(unittest.TestCase): test_settings = { "receive_allow_receiver_shutdown": True } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ReceiveModeTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/onionshare_slug_persistent_test.py b/tests/local_share_mode_persistent_slug_test.py similarity index 81% rename from tests_gui_local/onionshare_slug_persistent_test.py rename to tests/local_share_mode_persistent_slug_test.py index ab845f8e..cad01ed9 100644 --- a/tests_gui_local/onionshare_slug_persistent_test.py +++ b/tests/local_share_mode_persistent_slug_test.py @@ -23,23 +23,23 @@ class ShareModePersistentSlugTest(unittest.TestCase): "save_private_key": True, "close_after_first_download": False, } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModePersistentSlugTest.json') @classmethod def tearDownClass(cls): GuiBaseTest.tear_down() - @pytest.mark.run(order=1) + @pytest.mark.run(order=1000) def test_run_all_common_setup_tests(self): GuiBaseTest.run_all_common_setup_tests(self) - @pytest.mark.run(order=2) - def test_run_all_share_mode_tests(self): + @pytest.mark.run(order=1001) + def test_run_all_persistent_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=3) + @pytest.mark.run(order=1002) def test_have_same_slug(self): '''Test that we have the same slug''' self.assertEqual(self.gui.share_mode.server_status.web.slug, slug) diff --git a/tests_gui_local/onionshare_share_mode_download_test_public_mode.py b/tests/local_share_mode_public_mode_test.py similarity index 90% rename from tests_gui_local/onionshare_share_mode_download_test_public_mode.py rename to tests/local_share_mode_public_mode_test.py index e59b53f4..2aa3caea 100644 --- a/tests_gui_local/onionshare_share_mode_download_test_public_mode.py +++ b/tests/local_share_mode_public_mode_test.py @@ -20,7 +20,7 @@ class ShareModePublicModeTest(unittest.TestCase): test_settings = { "public_mode": True, } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModePublicModeTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/onionshare_share_mode_download_test_stay_open.py b/tests/local_share_mode_stay_open_test.py similarity index 90% rename from tests_gui_local/onionshare_share_mode_download_test_stay_open.py rename to tests/local_share_mode_stay_open_test.py index 9394c34a..0a2db984 100644 --- a/tests_gui_local/onionshare_share_mode_download_test_stay_open.py +++ b/tests/local_share_mode_stay_open_test.py @@ -20,7 +20,7 @@ class ShareModeStayOpenTest(unittest.TestCase): test_settings = { "close_after_first_download": False, } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeStayOpenTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/onionshare_share_mode_download_test.py b/tests/local_share_mode_test.py similarity index 91% rename from tests_gui_local/onionshare_share_mode_download_test.py rename to tests/local_share_mode_test.py index b24a3a78..ca1bed2c 100644 --- a/tests_gui_local/onionshare_share_mode_download_test.py +++ b/tests/local_share_mode_test.py @@ -19,7 +19,7 @@ class ShareModeTest(unittest.TestCase): def setUpClass(cls): test_settings = { } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/onionshare_timer_test.py b/tests/local_share_mode_timer_test.py similarity index 91% rename from tests_gui_local/onionshare_timer_test.py rename to tests/local_share_mode_timer_test.py index 60c616cc..ffa138a6 100644 --- a/tests_gui_local/onionshare_timer_test.py +++ b/tests/local_share_mode_timer_test.py @@ -21,7 +21,7 @@ class ShareModeTimerTest(unittest.TestCase): "public_mode": False, "shutdown_timeout": True, } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiBaseTest.set_up(test_settings, '/tmp/ShareModeTimerTest.json') @classmethod def tearDownClass(cls): diff --git a/tests_gui_local/__init__.py b/tests_gui_local/__init__.py deleted file mode 100644 index 7cf168eb..00000000 --- a/tests_gui_local/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .GuiBaseTest import GuiBaseTest diff --git a/tests_gui_local/conftest.py b/tests_gui_local/conftest.py deleted file mode 100644 index 8ac7efb8..00000000 --- a/tests_gui_local/conftest.py +++ /dev/null @@ -1,160 +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 - -@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) diff --git a/tests_gui_local/run_unit_tests.sh b/tests_gui_local/run_unit_tests.sh deleted file mode 100755 index 7d207a57..00000000 --- a/tests_gui_local/run_unit_tests.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -for test in `ls -1 | egrep ^onionshare_`; do - pytest $test -vvv || exit 1 -done