Index: build/android/pylib/utils/device_temp_file_test.py |
diff --git a/build/android/pylib/utils/device_temp_file_test.py b/build/android/pylib/utils/device_temp_file_test.py |
index 0eb80a86f42b355849801f7c166ab40ff3c354bb..b0d23b12685fe04b7ada456307c6e6627f7727ea 100755 |
--- a/build/android/pylib/utils/device_temp_file_test.py |
+++ b/build/android/pylib/utils/device_temp_file_test.py |
@@ -13,80 +13,38 @@ |
import unittest |
from pylib import constants |
-from pylib.device import adb_wrapper |
-from pylib.device import device_errors |
from pylib.utils import device_temp_file |
-from pylib.utils import mock_calls |
sys.path.append(os.path.join( |
constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
import mock # pylint: disable=F0401 |
-class DeviceTempFileTest(mock_calls.TestCase): |
+class DeviceTempFileTest(unittest.TestCase): |
def setUp(self): |
- test_serial = '0123456789abcdef' |
- self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
- self.adb.__str__ = mock.Mock(return_value=test_serial) |
- self.watchMethodCalls(self.call.adb) |
- |
- def mockShellCall(self, cmd_prefix, action=''): |
- """Expect an adb.Shell(cmd) call with cmd_prefix and do some action |
- |
- Args: |
- cmd_prefix: A string, the cmd of the received call is expected to have |
- this as a prefix. |
- action: If callable, an action to perform when the expected call is |
- received, otherwise a return value. |
- Returns: |
- An (expected_call, action) pair suitable for use in assertCalls. |
- """ |
- def check_and_return(cmd): |
- self.assertTrue( |
- cmd.startswith(cmd_prefix), |
- 'command %r does not start with prefix %r' % (cmd, cmd_prefix)) |
- if callable(action): |
- return action(cmd) |
- else: |
- return action |
- return (self.call.adb.Shell(mock.ANY), check_and_return) |
- |
- def mockExistsTest(self, exists_result): |
- def action(cmd): |
- if exists_result: |
- return '' |
- else: |
- raise device_errors.AdbCommandFailedError( |
- cmd, 'File not found', 1, str(self.adb)) |
- return self.mockShellCall('test -e ', action) |
+ self.device_utils = mock.MagicMock() |
def testTempFileNameAlreadyExists(self): |
- with self.assertCalls( |
- self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), |
- self.mockShellCall('test -d /sdcard'), |
- self.mockExistsTest(True), |
- self.mockExistsTest(True), |
- self.mockExistsTest(True), |
- self.mockExistsTest(False), |
- self.mockShellCall('touch '), |
- self.mockShellCall('rm -f ')): |
- with device_temp_file.DeviceTempFile(self.adb) as tmpfile: |
- logging.debug('Temp file name: %s' % tmpfile.name) |
+ self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
+ self.device_utils.FileExists.side_effect = [True, True, True, False] |
+ |
+ tmpfile = device_temp_file.DeviceTempFile(self.device_utils) |
+ logging.debug('Temp file name: %s' % tmpfile.name) |
+ self.assertEqual(self.device_utils.FileExists.call_count, 4) |
+ self.device_utils.WriteFile.assert_called_with(tmpfile.name, '') |
def testTempFileLifecycle(self): |
- with self.assertCalls( |
- self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), |
- self.mockShellCall('test -d /sdcard'), |
- self.mockExistsTest(False), |
- self.mockShellCall('touch ')): |
- tempFileContextManager = device_temp_file.DeviceTempFile(self.adb) |
- with mock.patch.object(self.adb, 'Shell'): |
- with tempFileContextManager as tmpfile: |
- logging.debug('Temp file name: %s' % tmpfile.name) |
- self.assertEquals(0, self.adb.Shell.call_count) |
- self.assertEquals(1, self.adb.Shell.call_count) |
- args, _ = self.adb.Shell.call_args |
- self.assertTrue(args[0].startswith('rm -f ')) |
+ self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
+ self.device_utils.FileExists.return_value = False |
+ |
+ with device_temp_file.DeviceTempFile(self.device_utils) as tmpfile: |
+ filename = tmpfile.name |
+ self.assertEqual(self.device_utils.WriteFile.call_count, 1) |
+ self.assertNotEqual(self.device_utils.RunShellCommand.call_args, |
+ mock.call(['rm', mock.ANY])) |
+ |
+ self.assertEqual(self.device_utils.RunShellCommand.call_args, |
+ mock.call(['rm', filename])) |
if __name__ == '__main__': |
logging.getLogger().setLevel(logging.DEBUG) |