44
55
66import asyncio
7+ from contextlib import asynccontextmanager
78import os
89import sys
910import unittest
1617from ppadb .sync_async import SyncAsync
1718
1819from .async_wrapper import awaiter
19- from .patchers import FakeStreamReader , FakeStreamWriter , async_patch
20+ from .patchers import FakeStreamReader , FakeStreamWriter , async_mock_open , async_patch
2021
2122
2223PNG_IMAGE = b'\x89 PNG\r \n \x1a \n \x00 \x00 \x00 \r IHDR\x00 \x00 \x00 \n \x00 \x00 \x00 \n \x08 \x06 \x00 \x00 \x00 \x8d 2\xcf \xbd \x00 \x00 \x00 \x04 sBIT\x08 \x08 \x08 \x08 |\x08 d\x88 \x00 \x00 \x00 \t pHYs\x00 \x00 \x0f a\x00 \x00 \x0f a\x01 \xa8 ?\xa7 i\x00 \x00 \x00 \x0e IDAT\x18 \x95 c`\x18 \x05 \x83 \x13 \x00 \x00 \x01 \x9a \x00 \x01 \x16 \xca \xd3 i\x00 \x00 \x00 \x00 IEND\xae B`\x82 '
2324
2425PNG_IMAGE_NEEDS_REPLACING = PNG_IMAGE [:5 ] + b'\r ' + PNG_IMAGE [5 :]
2526
27+ FILEDATA = b'Ohayou sekai.\n Good morning world!'
28+
2629
2730class TestDevice (unittest .TestCase ):
2831 @awaiter
@@ -72,7 +75,7 @@ def progress(*args, **kwargs):
7275 pass
7376
7477 filedata = b'Ohayou sekai.\n Good morning world!'
75- with patch ('os.path.exists' , return_value = True ), patch ('os.path.isfile' , return_value = True ), patch ('ppadb.device_async.open' , mock_open ( read_data = filedata )), patch ( ' os.stat' , return_value = os .stat_result ((123 ,) * 10 )), patch ('ppadb.sync_async.open' , mock_open ( read_data = filedata )):
78+ with patch ('os.path.exists' , return_value = True ), patch ('os.path.isfile' , return_value = True ), patch ('os.stat' , return_value = os .stat_result ((123 ,) * 10 )), patch ('ppadb.sync_async.aiofiles. open' , async_mock_open ( FILEDATA )):
7679 with async_patch ('asyncio.open_connection' , return_value = (FakeStreamReader (), FakeStreamWriter ())):
7780 with async_patch ('{}.FakeStreamReader.read' .format (__name__ ), side_effect = [b'OKAY' , b'OKAY' , b'OKAY' , PNG_IMAGE_NEEDS_REPLACING , b'' , b'OKAY' ]):
7881 await self .device .push ('src' , 'dest' , progress = progress )
@@ -87,14 +90,14 @@ async def test_push_dir(self):
8790 async def test_pull (self ):
8891 with async_patch ('asyncio.open_connection' , return_value = (FakeStreamReader (), FakeStreamWriter ())):
8992 with async_patch ('{}.FakeStreamReader.read' .format (__name__ ), side_effect = [b'OKAY' , b'OKAY' , b'DATA' , SyncAsync ._little_endian (4 ), b'TEST' , b'DONE' , b'OKAY' ]):
90- with patch ('ppadb.sync_async.open' , mock_open ( )):
93+ with patch ('ppadb.sync_async.aiofiles. open' , async_mock_open ( FILEDATA )):
9194 await self .device .pull ('src' , 'dest' )
9295
9396 @awaiter
9497 async def test_pull_fail (self ):
9598 with async_patch ('asyncio.open_connection' , return_value = (FakeStreamReader (), FakeStreamWriter ())):
9699 with async_patch ('{}.FakeStreamReader.read' .format (__name__ ), side_effect = [b'OKAY' , b'OKAY' , b'FAIL' , SyncAsync ._little_endian (4 ), b'TEST' , b'DONE' , b'OKAY' ]):
97- with patch ('ppadb.sync_async.open' , mock_open ( )):
100+ with patch ('ppadb.sync_async.aiofiles. open' , async_mock_open ( FILEDATA )):
98101 await self .device .pull ('src' , 'dest' )
99102
100103
0 commit comments