Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jun 30, 2020
1 parent 8dc5be4 commit 13b819d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
33 changes: 33 additions & 0 deletions tests/async_patchers.py
Expand Up @@ -15,6 +15,39 @@ async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)


def async_mock_open(read_data=""):
class AsyncMockFile:
def __init__(self, read_data):
self.read_data = read_data
_async_mock_open.written = read_data[:0]

def read(self, size=-1):
if size == -1:
ret = self.read_data
self.read_data = self.read_data[:0]
return ret

n = min(size, len(self.read_data))
ret = self.read_data[:n]
self.read_data = self.read_data[n:]
return ret

def write(self, b):
if _async_mock_open.written:
_async_mock_open.written += b
else:
_async_mock_open.written = b

@contextmanager
def _async_mock_open(*args, **kwargs):
try:
yield AsyncMockFile(read_data)
finally:
pass

return _async_mock_open


class FakeStreamWriter:
def close(self):
pass
Expand Down
34 changes: 34 additions & 0 deletions tests/patchers.py
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from mock import patch
import sys
import unittest
Expand All @@ -22,6 +23,39 @@
BULK_READ_LIST_WITH_AUTH_NEW_KEY = [MSG_CONNECT_WITH_AUTH1.pack(), MSG_CONNECT_WITH_AUTH1.data, MSG_CONNECT_WITH_AUTH_NEW_KEY2.pack(), MSG_CONNECT_WITH_AUTH_NEW_KEY2.data, MSG_CONNECT_WITH_AUTH_NEW_KEY3.pack(), MSG_CONNECT_WITH_AUTH_NEW_KEY3.data]


def mock_open(read_data=""):
class MockFile:
def __init__(self, read_data):
self.read_data = read_data
_mock_open.written = read_data[:0]

def read(self, size=-1):
if size == -1:
ret = self.read_data
self.read_data = self.read_data[:0]
return ret

n = min(size, len(self.read_data))
ret = self.read_data[:n]
self.read_data = self.read_data[n:]
return ret

def write(self, b):
if _mock_open.written:
_mock_open.written += b
else:
_mock_open.written = b

@contextmanager
def _mock_open(*args, **kwargs):
try:
yield MockFile(read_data)
finally:
pass

return _mock_open


class FakeSocket(object):
def __init__(self):
self._recv = b''
Expand Down
20 changes: 10 additions & 10 deletions tests/test_adb_device.py
Expand Up @@ -4,7 +4,7 @@
import time
import unittest

from mock import mock_open, patch
from mock import patch

from adb_shell import constants, exceptions
from adb_shell.adb_device import AdbDevice, AdbDeviceTcp, DeviceFile
Expand Down Expand Up @@ -529,7 +529,7 @@ def test_push_fail(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.WRTE, arg0=1, arg1=1, data=join_messages(FileSyncMessage(constants.FAIL, data=b''))))

with self.assertRaises(exceptions.PushFailedError), patch('adb_shell.adb_device.open', mock_open(read_data=filedata)):
with self.assertRaises(exceptions.PushFailedError), patch('adb_shell.adb_device.open', patchers.mock_open(read_data=filedata)):
self.device.push('TEST_FILE', '/data', mtime=mtime)

def test_push_file(self):
Expand All @@ -553,7 +553,7 @@ def test_push_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device.open', mock_open(read_data=filedata)):
with patch('adb_shell.adb_device.open', patchers.mock_open(read_data=filedata)):
self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

Expand All @@ -578,7 +578,7 @@ def test_push_file_mtime0(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device.open', mock_open(read_data=filedata)), patch('time.time', return_value=mtime):
with patch('adb_shell.adb_device.open', patchers.mock_open(read_data=filedata)), patch('time.time', return_value=mtime):
self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

Expand Down Expand Up @@ -612,7 +612,7 @@ def test_push_big_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device.open', mock_open(read_data=filedata)):
with patch('adb_shell.adb_device.open', patchers.mock_open(read_data=filedata)):
self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

Expand All @@ -637,7 +637,7 @@ def test_push_dir(self):
# Expected `bulk_write` values
#TODO

with patch('adb_shell.adb_device.open', mock_open(read_data=filedata)), patch('os.path.isdir', lambda x: x == 'TEST_DIR/'), patch('os.listdir', return_value=['TEST_FILE1', 'TEST_FILE2']):
with patch('adb_shell.adb_device.open', patchers.mock_open(read_data=filedata)), patch('os.path.isdir', lambda x: x == 'TEST_DIR/'), patch('os.listdir', return_value=['TEST_FILE1', 'TEST_FILE2']):
self.device.push('TEST_DIR/', '/data', mtime=mtime)

def test_pull_file(self):
Expand All @@ -659,9 +659,9 @@ def test_pull_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device.open', mock_open()) as m:
with patch('adb_shell.adb_device.open', patchers.mock_open()) as m:
self.device.pull('/data', 'TEST_FILE')
self.assertEqual(b''.join([bytes(call.args[0]) for call in m().write.mock_calls]), filedata)
self.assertEqual(m.written, filedata)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

def test_pull_big_file(self):
Expand All @@ -683,9 +683,9 @@ def test_pull_big_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device.open', mock_open()) as m:
with patch('adb_shell.adb_device.open', patchers.mock_open()) as m:
self.device.pull('/data', 'TEST_FILE')
self.assertEqual(b''.join([bytes(call.args[0]) for call in m().write.mock_calls]), filedata)
self.assertEqual(m.written, filedata)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

def test_stat(self):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_adb_device_async.py
Expand Up @@ -12,7 +12,7 @@
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

from . import patchers
from .async_patchers import PATCH_TCP_TRANSPORT_ASYNC, FakeTcpTransportAsync, async_patch
from .async_patchers import PATCH_TCP_TRANSPORT_ASYNC, FakeTcpTransportAsync, async_patch, async_mock_open
from .async_wrapper import awaiter
from .filesync_helpers import FileSyncMessage, FileSyncListMessage, FileSyncStatMessage
from .keygen_stub import open_priv_pub
Expand Down Expand Up @@ -566,7 +566,7 @@ async def test_push_fail(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.WRTE, arg0=1, arg1=1, data=join_messages(FileSyncMessage(constants.FAIL, data=b''))))

with self.assertRaises(exceptions.PushFailedError), patch('adb_shell.adb_device_async.open', mock_open(read_data=filedata)):
with self.assertRaises(exceptions.PushFailedError), patch('adb_shell.adb_device_async.open', async_mock_open(read_data=filedata)):
await self.device.push('TEST_FILE', '/data', mtime=mtime)

@awaiter
Expand All @@ -591,7 +591,7 @@ async def test_push_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device_async.open', mock_open(read_data=filedata)):
with patch('adb_shell.adb_device_async.open', async_mock_open(read_data=filedata)):
await self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(self.device._transport._bulk_write, expected_bulk_write)

Expand All @@ -617,7 +617,7 @@ async def test_push_file_mtime0(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device_async.open', mock_open(read_data=filedata)), patch('time.time', return_value=mtime):
with patch('adb_shell.adb_device_async.open', async_mock_open(read_data=filedata)), patch('time.time', return_value=mtime):
await self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(self.device._transport._bulk_write, expected_bulk_write)

Expand Down Expand Up @@ -652,7 +652,7 @@ async def test_push_big_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device_async.open', mock_open(read_data=filedata)):
with patch('adb_shell.adb_device_async.open', async_mock_open(read_data=filedata)):
await self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(self.device._transport._bulk_write, expected_bulk_write)

Expand All @@ -678,7 +678,7 @@ async def test_push_dir(self):
# Expected `bulk_write` values
#TODO

with patch('adb_shell.adb_device_async.open', mock_open(read_data=filedata)), patch('os.path.isdir', lambda x: x == 'TEST_DIR/'), patch('os.listdir', return_value=['TEST_FILE1', 'TEST_FILE2']):
with patch('adb_shell.adb_device_async.open', async_mock_open(read_data=filedata)), patch('os.path.isdir', lambda x: x == 'TEST_DIR/'), patch('os.listdir', return_value=['TEST_FILE1', 'TEST_FILE2']):
await self.device.push('TEST_DIR/', '/data', mtime=mtime)

@awaiter
Expand All @@ -701,9 +701,9 @@ async def test_pull_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device_async.open', mock_open()) as m:
with patch('adb_shell.adb_device_async.open', async_mock_open()) as m:
await self.device.pull('/data', 'TEST_FILE')
# self.assertEqual(b''.join([bytes(call.args[0]) for call in m().write.mock_calls]), filedata)
self.assertEqual(m.written, filedata)
self.assertEqual(self.device._transport._bulk_write, expected_bulk_write)

@awaiter
Expand All @@ -726,9 +726,9 @@ async def test_pull_big_file(self):
AdbMessage(command=constants.OKAY, arg0=1, arg1=1, data=b''),
AdbMessage(command=constants.CLSE, arg0=1, arg1=1, data=b''))

with patch('adb_shell.adb_device_async.open', mock_open()) as m:#, patch('os.path.exists', return_value=True):
with patch('adb_shell.adb_device_async.open', async_mock_open()) as m:
await self.device.pull('/data', 'TEST_FILE')
# self.assertEqual(b''.join([bytes(call.args[0]) for call in m().write.mock_calls]), filedata)
self.assertEqual(m.written, filedata)
self.assertEqual(self.device._transport._bulk_write, expected_bulk_write)

@awaiter
Expand Down

0 comments on commit 13b819d

Please sign in to comment.