From 13b819dbfd9ed4c7cc238931a9745a223947057d Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Mon, 29 Jun 2020 20:53:48 -0700 Subject: [PATCH] Fix tests --- tests/async_patchers.py | 33 +++++++++++++++++++++++++++++++++ tests/patchers.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_adb_device.py | 20 ++++++++++---------- tests/test_adb_device_async.py | 20 ++++++++++---------- 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/tests/async_patchers.py b/tests/async_patchers.py index 664376e..a1bdda0 100644 --- a/tests/async_patchers.py +++ b/tests/async_patchers.py @@ -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 diff --git a/tests/patchers.py b/tests/patchers.py index 9b849ef..d77807c 100644 --- a/tests/patchers.py +++ b/tests/patchers.py @@ -1,3 +1,4 @@ +from contextlib import contextmanager from mock import patch import sys import unittest @@ -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'' diff --git a/tests/test_adb_device.py b/tests/test_adb_device.py index 978c037..2163c5a 100644 --- a/tests/test_adb_device.py +++ b/tests/test_adb_device.py @@ -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 @@ -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): @@ -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) @@ -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) @@ -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) @@ -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): @@ -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): @@ -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): diff --git a/tests/test_adb_device_async.py b/tests/test_adb_device_async.py index 5c177fe..8f7362d 100644 --- a/tests/test_adb_device_async.py +++ b/tests/test_adb_device_async.py @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -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