| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Unit tests for the contents of device_temp_file.py. | 7 Unit tests for the contents of device_temp_file.py. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import logging | 10 import logging |
| 11 import os | 11 import os |
| 12 import sys | 12 import sys |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 from pylib import constants | 15 from pylib import constants |
| 16 from pylib.device import adb_wrapper | |
| 17 from pylib.device import device_errors | |
| 18 from pylib.utils import device_temp_file | 16 from pylib.utils import device_temp_file |
| 19 from pylib.utils import mock_calls | |
| 20 | 17 |
| 21 sys.path.append(os.path.join( | 18 sys.path.append(os.path.join( |
| 22 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 19 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 23 import mock # pylint: disable=F0401 | 20 import mock # pylint: disable=F0401 |
| 24 | 21 |
| 25 class DeviceTempFileTest(mock_calls.TestCase): | 22 class DeviceTempFileTest(unittest.TestCase): |
| 26 | 23 |
| 27 def setUp(self): | 24 def setUp(self): |
| 28 test_serial = '0123456789abcdef' | 25 self.device_utils = mock.MagicMock() |
| 29 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | |
| 30 self.adb.__str__ = mock.Mock(return_value=test_serial) | |
| 31 self.watchMethodCalls(self.call.adb) | |
| 32 | |
| 33 def mockShellCall(self, cmd_prefix, action=''): | |
| 34 """Expect an adb.Shell(cmd) call with cmd_prefix and do some action | |
| 35 | |
| 36 Args: | |
| 37 cmd_prefix: A string, the cmd of the received call is expected to have | |
| 38 this as a prefix. | |
| 39 action: If callable, an action to perform when the expected call is | |
| 40 received, otherwise a return value. | |
| 41 Returns: | |
| 42 An (expected_call, action) pair suitable for use in assertCalls. | |
| 43 """ | |
| 44 def check_and_return(cmd): | |
| 45 self.assertTrue( | |
| 46 cmd.startswith(cmd_prefix), | |
| 47 'command %r does not start with prefix %r' % (cmd, cmd_prefix)) | |
| 48 if callable(action): | |
| 49 return action(cmd) | |
| 50 else: | |
| 51 return action | |
| 52 return (self.call.adb.Shell(mock.ANY), check_and_return) | |
| 53 | |
| 54 def mockExistsTest(self, exists_result): | |
| 55 def action(cmd): | |
| 56 if exists_result: | |
| 57 return '' | |
| 58 else: | |
| 59 raise device_errors.AdbCommandFailedError( | |
| 60 cmd, 'File not found', 1, str(self.adb)) | |
| 61 return self.mockShellCall('test -e ', action) | |
| 62 | 26 |
| 63 def testTempFileNameAlreadyExists(self): | 27 def testTempFileNameAlreadyExists(self): |
| 64 with self.assertCalls( | 28 self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
| 65 self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), | 29 self.device_utils.FileExists.side_effect = [True, True, True, False] |
| 66 self.mockShellCall('test -d /sdcard'), | 30 |
| 67 self.mockExistsTest(True), | 31 tmpfile = device_temp_file.DeviceTempFile(self.device_utils) |
| 68 self.mockExistsTest(True), | 32 logging.debug('Temp file name: %s' % tmpfile.name) |
| 69 self.mockExistsTest(True), | 33 self.assertEqual(self.device_utils.FileExists.call_count, 4) |
| 70 self.mockExistsTest(False), | 34 self.device_utils.WriteFile.assert_called_with(tmpfile.name, '') |
| 71 self.mockShellCall('touch '), | |
| 72 self.mockShellCall('rm -f ')): | |
| 73 with device_temp_file.DeviceTempFile(self.adb) as tmpfile: | |
| 74 logging.debug('Temp file name: %s' % tmpfile.name) | |
| 75 | 35 |
| 76 def testTempFileLifecycle(self): | 36 def testTempFileLifecycle(self): |
| 77 with self.assertCalls( | 37 self.device_utils.GetExternalStoragePath.return_value = '/sdcard' |
| 78 self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), | 38 self.device_utils.FileExists.return_value = False |
| 79 self.mockShellCall('test -d /sdcard'), | 39 |
| 80 self.mockExistsTest(False), | 40 with device_temp_file.DeviceTempFile(self.device_utils) as tmpfile: |
| 81 self.mockShellCall('touch ')): | 41 filename = tmpfile.name |
| 82 tempFileContextManager = device_temp_file.DeviceTempFile(self.adb) | 42 self.assertEqual(self.device_utils.WriteFile.call_count, 1) |
| 83 with mock.patch.object(self.adb, 'Shell'): | 43 self.assertNotEqual(self.device_utils.RunShellCommand.call_args, |
| 84 with tempFileContextManager as tmpfile: | 44 mock.call(['rm', mock.ANY])) |
| 85 logging.debug('Temp file name: %s' % tmpfile.name) | 45 |
| 86 self.assertEquals(0, self.adb.Shell.call_count) | 46 self.assertEqual(self.device_utils.RunShellCommand.call_args, |
| 87 self.assertEquals(1, self.adb.Shell.call_count) | 47 mock.call(['rm', filename])) |
| 88 args, _ = self.adb.Shell.call_args | |
| 89 self.assertTrue(args[0].startswith('rm -f ')) | |
| 90 | 48 |
| 91 if __name__ == '__main__': | 49 if __name__ == '__main__': |
| 92 logging.getLogger().setLevel(logging.DEBUG) | 50 logging.getLogger().setLevel(logging.DEBUG) |
| 93 unittest.main(verbosity=2) | 51 unittest.main(verbosity=2) |
| OLD | NEW |