diff --git a/.travis.yml b/.travis.yml index 24255416..e0b5b822 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index 1def7f5e..e69de29b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +0,0 @@ -from .GuiBaseTest import GuiBaseTest diff --git a/tests/GuiBaseTest.py b/tests_gui_local/GuiBaseTest.py similarity index 99% rename from tests/GuiBaseTest.py rename to tests_gui_local/GuiBaseTest.py index 7aec97f7..e7d25031 100644 --- a/tests/GuiBaseTest.py +++ b/tests_gui_local/GuiBaseTest.py @@ -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) diff --git a/tests_gui_local/__init__.py b/tests_gui_local/__init__.py new file mode 100644 index 00000000..7cf168eb --- /dev/null +++ b/tests_gui_local/__init__.py @@ -0,0 +1 @@ +from .GuiBaseTest import GuiBaseTest diff --git a/tests_gui_local/conftest.py b/tests_gui_local/conftest.py new file mode 100644 index 00000000..8ac7efb8 --- /dev/null +++ b/tests_gui_local/conftest.py @@ -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) diff --git a/tests/local_receive_mode_test.py b/tests_gui_local/onionshare_receive_mode_upload_test.py similarity index 91% rename from tests/local_receive_mode_test.py rename to tests_gui_local/onionshare_receive_mode_upload_test.py index 82d7529a..262c7aba 100644 --- a/tests/local_receive_mode_test.py +++ b/tests_gui_local/onionshare_receive_mode_upload_test.py @@ -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): diff --git a/tests/local_receive_mode_public_mode_test.py b/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py similarity index 90% rename from tests/local_receive_mode_public_mode_test.py rename to tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py index 0bc00833..201402c2 100644 --- a/tests/local_receive_mode_public_mode_test.py +++ b/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py @@ -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): diff --git a/tests/local_share_mode_test.py b/tests_gui_local/onionshare_share_mode_download_test.py similarity index 91% rename from tests/local_share_mode_test.py rename to tests_gui_local/onionshare_share_mode_download_test.py index ca1bed2c..b24a3a78 100644 --- a/tests/local_share_mode_test.py +++ b/tests_gui_local/onionshare_share_mode_download_test.py @@ -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): diff --git a/tests/local_share_mode_public_mode_test.py b/tests_gui_local/onionshare_share_mode_download_test_public_mode.py similarity index 90% rename from tests/local_share_mode_public_mode_test.py rename to tests_gui_local/onionshare_share_mode_download_test_public_mode.py index 2aa3caea..e59b53f4 100644 --- a/tests/local_share_mode_public_mode_test.py +++ b/tests_gui_local/onionshare_share_mode_download_test_public_mode.py @@ -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): diff --git a/tests/local_share_mode_stay_open_test.py b/tests_gui_local/onionshare_share_mode_download_test_stay_open.py similarity index 90% rename from tests/local_share_mode_stay_open_test.py rename to tests_gui_local/onionshare_share_mode_download_test_stay_open.py index 0a2db984..9394c34a 100644 --- a/tests/local_share_mode_stay_open_test.py +++ b/tests_gui_local/onionshare_share_mode_download_test_stay_open.py @@ -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): diff --git a/tests/local_share_mode_persistent_slug_test.py b/tests_gui_local/onionshare_slug_persistent_test.py similarity index 81% rename from tests/local_share_mode_persistent_slug_test.py rename to tests_gui_local/onionshare_slug_persistent_test.py index cad01ed9..ab845f8e 100644 --- a/tests/local_share_mode_persistent_slug_test.py +++ b/tests_gui_local/onionshare_slug_persistent_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, '/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) diff --git a/tests/local_share_mode_timer_test.py b/tests_gui_local/onionshare_timer_test.py similarity index 91% rename from tests/local_share_mode_timer_test.py rename to tests_gui_local/onionshare_timer_test.py index ffa138a6..60c616cc 100644 --- a/tests/local_share_mode_timer_test.py +++ b/tests_gui_local/onionshare_timer_test.py @@ -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): diff --git a/tests_gui_local/run_unit_tests.sh b/tests_gui_local/run_unit_tests.sh new file mode 100755 index 00000000..7d207a57 --- /dev/null +++ b/tests_gui_local/run_unit_tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +for test in `ls -1 | egrep ^onionshare_`; do + pytest $test -vvv || exit 1 +done