Chromium Code Reviews| 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 b0d23b12685fe04b7ada456307c6e6627f7727ea..0eb80a86f42b355849801f7c166ab40ff3c354bb 100755 |
| --- a/build/android/pylib/utils/device_temp_file_test.py |
| +++ b/build/android/pylib/utils/device_temp_file_test.py |
| @@ -13,38 +13,80 @@ import sys |
| 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(unittest.TestCase): |
| +class DeviceTempFileTest(mock_calls.TestCase): |
| def setUp(self): |
| - self.device_utils = mock.MagicMock() |
| + 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 testTempFileNameAlreadyExists(self): |
| - self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
| - self.device_utils.FileExists.side_effect = [True, True, True, False] |
| + def mockShellCall(self, cmd_prefix, action=''): |
| + """Expect an adb.Shell(cmd) call with cmd_prefix and do some action |
| - 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, '') |
| + 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) |
|
perezju
2014/11/25 16:25:33
John, have a look. I'm not quite sure if this is t
jbudorick
2014/11/25 17:05:26
Not quite -- I think I was interested in something
|
| - def testTempFileLifecycle(self): |
| - self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
| - self.device_utils.FileExists.return_value = False |
| + 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) |
| - 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])) |
| + 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.assertEqual(self.device_utils.RunShellCommand.call_args, |
| - mock.call(['rm', filename])) |
| + 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 ')) |
| if __name__ == '__main__': |
| logging.getLogger().setLevel(logging.DEBUG) |