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 | |
16 from pylib.utils import device_temp_file | 18 from pylib.utils import device_temp_file |
19 from pylib.utils import mock_calls | |
17 | 20 |
18 sys.path.append(os.path.join( | 21 sys.path.append(os.path.join( |
19 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 22 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
20 import mock # pylint: disable=F0401 | 23 import mock # pylint: disable=F0401 |
21 | 24 |
22 class DeviceTempFileTest(unittest.TestCase): | 25 class DeviceTempFileTest(mock_calls.TestCase): |
23 | 26 |
24 def setUp(self): | 27 def setUp(self): |
25 self.device_utils = mock.MagicMock() | 28 test_serial = '0123456789abcdef' |
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) | |
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
| |
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) | |
26 | 62 |
27 def testTempFileNameAlreadyExists(self): | 63 def testTempFileNameAlreadyExists(self): |
28 self.device_utils.GetExternalStoragePath.return_value = '/sdcard' | 64 with self.assertCalls( |
29 self.device_utils.FileExists.side_effect = [True, True, True, False] | 65 self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), |
30 | 66 self.mockShellCall('test -d /sdcard'), |
31 tmpfile = device_temp_file.DeviceTempFile(self.device_utils) | 67 self.mockExistsTest(True), |
32 logging.debug('Temp file name: %s' % tmpfile.name) | 68 self.mockExistsTest(True), |
33 self.assertEqual(self.device_utils.FileExists.call_count, 4) | 69 self.mockExistsTest(True), |
34 self.device_utils.WriteFile.assert_called_with(tmpfile.name, '') | 70 self.mockExistsTest(False), |
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) | |
35 | 75 |
36 def testTempFileLifecycle(self): | 76 def testTempFileLifecycle(self): |
37 self.device_utils.GetExternalStoragePath.return_value = '/sdcard' | 77 with self.assertCalls( |
38 self.device_utils.FileExists.return_value = False | 78 self.mockShellCall('echo $EXTERNAL_STORAGE', '/sdcard'), |
39 | 79 self.mockShellCall('test -d /sdcard'), |
40 with device_temp_file.DeviceTempFile(self.device_utils) as tmpfile: | 80 self.mockExistsTest(False), |
41 filename = tmpfile.name | 81 self.mockShellCall('touch ')): |
42 self.assertEqual(self.device_utils.WriteFile.call_count, 1) | 82 tempFileContextManager = device_temp_file.DeviceTempFile(self.adb) |
43 self.assertNotEqual(self.device_utils.RunShellCommand.call_args, | 83 with mock.patch.object(self.adb, 'Shell'): |
44 mock.call(['rm', mock.ANY])) | 84 with tempFileContextManager as tmpfile: |
45 | 85 logging.debug('Temp file name: %s' % tmpfile.name) |
46 self.assertEqual(self.device_utils.RunShellCommand.call_args, | 86 self.assertEquals(0, self.adb.Shell.call_count) |
47 mock.call(['rm', filename])) | 87 self.assertEquals(1, self.adb.Shell.call_count) |
88 args, _ = self.adb.Shell.call_args | |
89 self.assertTrue(args[0].startswith('rm -f ')) | |
48 | 90 |
49 if __name__ == '__main__': | 91 if __name__ == '__main__': |
50 logging.getLogger().setLevel(logging.DEBUG) | 92 logging.getLogger().setLevel(logging.DEBUG) |
51 unittest.main(verbosity=2) | 93 unittest.main(verbosity=2) |
OLD | NEW |