mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-11-04 14:30:30 +03:00
install: reliable algorithm for picking devices from adb output
Versions of this algorithm are used elsewhere: * https://github.com/openatx/adbutils/blob/master/adbutils/_adb.py
This commit is contained in:
parent
f1b110942a
commit
681d705da0
2 changed files with 193 additions and 42 deletions
|
|
@ -5,9 +5,11 @@
|
|||
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())), '..')
|
||||
|
|
@ -16,13 +18,17 @@ print('localmodule: ' + localmodule)
|
|||
if localmodule not in sys.path:
|
||||
sys.path.insert(0, localmodule)
|
||||
|
||||
import fdroidserver.common
|
||||
import fdroidserver.install
|
||||
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)
|
||||
|
|
@ -35,6 +41,140 @@ class InstallTest(unittest.TestCase):
|
|||
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())
|
||||
|
||||
@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 = fdroidserver.install.download_fdroid_apk()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue