mirror of
https://gitlab.com/fdroid/fdroidserver.git
synced 2024-11-15 03:20:10 +01:00
244 lines
8.6 KiB
Python
Executable File
244 lines
8.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
import inspect
|
|
import os
|
|
import sys
|
|
import textwrap
|
|
import unittest
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, patch
|
|
|
|
localmodule = os.path.realpath(
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
|
)
|
|
print('localmodule: ' + localmodule)
|
|
if localmodule not in sys.path:
|
|
sys.path.insert(0, localmodule)
|
|
|
|
import fdroidserver
|
|
from fdroidserver import common, install
|
|
from fdroidserver.exception import BuildException, FDroidException
|
|
|
|
|
|
class InstallTest(unittest.TestCase):
|
|
'''fdroidserver/install.py'''
|
|
|
|
def tearDown(self):
|
|
common.config = None
|
|
|
|
def test_devices(self):
|
|
config = dict()
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
fdroidserver.common.config = config
|
|
config['adb'] = fdroidserver.common.find_sdk_tools_cmd('adb')
|
|
self.assertTrue(os.path.exists(config['adb']))
|
|
self.assertTrue(os.path.isfile(config['adb']))
|
|
devices = fdroidserver.install.devices()
|
|
self.assertIsInstance(devices, list, 'install.devices() did not return a list!')
|
|
for device in devices:
|
|
self.assertIsInstance(device, str)
|
|
|
|
def test_devices_fail(self):
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
common.config['adb'] = '/bin/false'
|
|
with self.assertRaises(FDroidException):
|
|
fdroidserver.install.devices()
|
|
|
|
def test_devices_fail_nonexistent(self):
|
|
"""This is mostly just to document this strange difference in behavior"""
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
common.config['adb'] = '/nonexistent'
|
|
with self.assertRaises(BuildException):
|
|
fdroidserver.install.devices()
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_none(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = 'List of devices attached\n\n'
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual([], fdroidserver.install.devices())
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_one(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = 'List of devices attached\n05995813\tdevice\n\n'
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual(['05995813'], fdroidserver.install.devices())
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_many(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = textwrap.dedent(
|
|
"""* daemon not running; starting now at tcp:5037
|
|
* daemon started successfully
|
|
List of devices attached
|
|
RZCT809FTQM device
|
|
05995813 device
|
|
emulator-5556 device
|
|
emulator-5554 unauthorized
|
|
0a388e93 no permissions (missing udev rules? user is in the plugdev group); see [http://developer.android.com/tools/device.html]
|
|
986AY133QL device
|
|
09301JEC215064 device
|
|
015d165c3010200e device
|
|
4DCESKVGUC85VOTO device
|
|
|
|
"""
|
|
)
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual(
|
|
[
|
|
'RZCT809FTQM',
|
|
'05995813',
|
|
'emulator-5556',
|
|
'986AY133QL',
|
|
'09301JEC215064',
|
|
'015d165c3010200e',
|
|
'4DCESKVGUC85VOTO',
|
|
],
|
|
fdroidserver.install.devices(),
|
|
)
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_error(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = textwrap.dedent(
|
|
"""* daemon not running. starting it now on port 5037 *
|
|
* daemon started successfully *
|
|
** daemon still not running
|
|
error: cannot connect to daemon
|
|
"""
|
|
)
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual([], fdroidserver.install.devices())
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_no_permissions(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = textwrap.dedent(
|
|
"""List of devices attached
|
|
???????????????? no permissions
|
|
"""
|
|
)
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual([], fdroidserver.install.devices())
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_unauthorized(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = textwrap.dedent(
|
|
"""List of devices attached
|
|
aeef5e4e unauthorized
|
|
"""
|
|
)
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual([], fdroidserver.install.devices())
|
|
|
|
@patch('fdroidserver.common.SdkToolsPopen')
|
|
def test_devices_with_mock_no_permissions_with_serial(self, mock_SdkToolsPopen):
|
|
p = Mock()
|
|
mock_SdkToolsPopen.return_value = p
|
|
p.output = textwrap.dedent(
|
|
"""List of devices attached
|
|
4DCESKVGUC85VOTO no permissions (missing udev rules? user is in the plugdev group); see [http://developer.android.com/tools/device.html]
|
|
|
|
"""
|
|
)
|
|
p.returncode = 0
|
|
common.config = dict()
|
|
common.fill_config_defaults(common.config)
|
|
self.assertEqual([], fdroidserver.install.devices())
|
|
|
|
@staticmethod
|
|
def _download_raise(privacy_mode):
|
|
raise Exception('fake failed download')
|
|
|
|
@patch('fdroidserver.install.download_apk')
|
|
@patch('fdroidserver.install.download_fdroid_apk')
|
|
def test_install_fdroid_apk_privacy_mode_true(
|
|
self, download_fdroid_apk, download_apk
|
|
):
|
|
download_apk.side_effect = self._download_raise
|
|
download_fdroid_apk.side_effect = self._download_raise
|
|
fdroidserver.common.config = {'jarsigner': 'fakepath'}
|
|
install.install_fdroid_apk(privacy_mode=True)
|
|
download_apk.assert_not_called()
|
|
download_fdroid_apk.assert_called_once()
|
|
|
|
@patch('fdroidserver.install.download_apk')
|
|
@patch('fdroidserver.install.download_fdroid_apk')
|
|
def test_install_fdroid_apk_privacy_mode_false(
|
|
self, download_fdroid_apk, download_apk
|
|
):
|
|
download_apk.side_effect = self._download_raise
|
|
download_fdroid_apk.side_effect = self._download_raise
|
|
fdroidserver.common.config = {'jarsigner': 'fakepath'}
|
|
install.install_fdroid_apk(privacy_mode=False)
|
|
download_apk.assert_not_called()
|
|
download_fdroid_apk.assert_called_once()
|
|
|
|
@patch('fdroidserver.install.download_apk')
|
|
@patch('fdroidserver.install.download_fdroid_apk')
|
|
@patch('locale.getlocale', lambda: ('zh_CN', 'UTF-8'))
|
|
def test_install_fdroid_apk_privacy_mode_locale_auto(
|
|
self, download_fdroid_apk, download_apk
|
|
):
|
|
download_apk.side_effect = self._download_raise
|
|
download_fdroid_apk.side_effect = self._download_raise
|
|
fdroidserver.common.config = {'jarsigner': 'fakepath'}
|
|
install.install_fdroid_apk(privacy_mode=None)
|
|
download_apk.assert_not_called()
|
|
download_fdroid_apk.assert_called_once()
|
|
|
|
@patch('fdroidserver.net.download_using_mirrors', lambda m: 'testvalue')
|
|
def test_download_fdroid_apk_smokecheck(self):
|
|
self.assertEqual('testvalue', install.download_fdroid_apk())
|
|
|
|
@unittest.skipUnless(os.getenv('test_download_fdroid_apk'), 'requires net access')
|
|
def test_download_fdroid_apk(self):
|
|
f = install.download_fdroid_apk()
|
|
self.assertTrue(Path(f).exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
import argparse
|
|
from testcommon import parse_args_for_test
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"-v",
|
|
"--verbose",
|
|
action="store_true",
|
|
default=False,
|
|
help="Spew out even more information than normal",
|
|
)
|
|
fdroidserver.install.options = parse_args_for_test(parser, sys.argv)
|
|
|
|
newSuite = unittest.TestSuite()
|
|
newSuite.addTest(unittest.makeSuite(InstallTest))
|
|
unittest.main(failfast=False)
|