Index: build/android/pylib/device/device_utils_test.py |
diff --git a/build/android/pylib/device/device_utils_test.py b/build/android/pylib/device/device_utils_test.py |
index 33aad913a38015a5c4e94d24b0bbadbfd420f413..430c443c4ae1b124d3643bb8231d898cd70cb897 100755 |
--- a/build/android/pylib/device/device_utils_test.py |
+++ b/build/android/pylib/device/device_utils_test.py |
@@ -67,6 +67,19 @@ class DeviceUtilsTest(unittest.TestCase): |
self.assertIsNone(d.old_interface.GetDevice()) |
+class MockTempFile(object): |
+ |
+ def __init__(self, name='/tmp/some/file'): |
+ self.file = mock.MagicMock(spec=file) |
+ self.file.name = name |
+ |
+ def __enter__(self): |
+ return self.file |
+ |
+ def __exit__(self, exc_type, exc_val, exc_tb): |
+ pass |
+ |
+ |
class _PatchedFunction(object): |
def __init__(self, patched=None, mocked=None): |
self.patched = patched |
@@ -1148,134 +1161,58 @@ class DeviceUtilsReadFileTest(DeviceUtilsOldImplTest): |
as_root=True) |
-class DeviceUtilsWriteFileTest(DeviceUtilsOldImplTest): |
+class DeviceUtilsWriteFileTest(DeviceUtilsNewImplTest): |
- def testWriteFile_basic(self): |
- mock_file = mock.MagicMock(spec=file) |
- mock_file.name = '/tmp/file/to.be.pushed' |
- mock_file.__enter__.return_value = mock_file |
- with mock.patch('tempfile.NamedTemporaryFile', |
- return_value=mock_file): |
- with self.assertCalls( |
- 'adb -s 0123456789abcdef push ' |
- '/tmp/file/to.be.pushed /test/file/written.to.device', |
- '100 B/s (100 bytes in 1.000s)\r\n'): |
- self.device.WriteFile('/test/file/written.to.device', |
- 'new test file contents') |
- mock_file.write.assert_called_once_with('new test file contents') |
- |
- def testWriteFile_asRoot_withRoot(self): |
- self.device.old_interface._external_storage = '/fake/storage/path' |
- self.device.old_interface._privileged_command_runner = ( |
- self.device.old_interface.RunShellCommand) |
- self.device.old_interface._protected_file_access_method_initialized = True |
- |
- mock_file = mock.MagicMock(spec=file) |
- mock_file.name = '/tmp/file/to.be.pushed' |
- mock_file.__enter__.return_value = mock_file |
- with mock.patch('tempfile.NamedTemporaryFile', |
- return_value=mock_file): |
- with self.assertCallsSequence( |
- cmd_ret=[ |
- # Create temporary contents file |
- (r"adb -s 0123456789abcdef shell " |
- "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
- "echo \$\?'", |
- '1\r\n'), |
- # Create temporary script file |
- (r"adb -s 0123456789abcdef shell " |
- "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
- "echo \$\?'", |
- '1\r\n'), |
- # Set contents file |
- (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
- '/fake/storage/path/temp_file-\d+\d+', |
- '100 B/s (100 bytes in 1.000s)\r\n'), |
- # Set script file |
- (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
- '/fake/storage/path/temp_file-\d+\d+', |
- '100 B/s (100 bytes in 1.000s)\r\n'), |
- # Call script |
- (r"adb -s 0123456789abcdef shell " |
- "'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
- # Remove device temporaries |
- (r"adb -s 0123456789abcdef shell " |
- "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
- (r"adb -s 0123456789abcdef shell " |
- "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
- comp=re.match): |
- self.device.WriteFile('/test/file/written.to.device', |
- 'new test file contents', as_root=True) |
- |
- def testWriteFile_asRoot_withSu(self): |
- self.device.old_interface._external_storage = '/fake/storage/path' |
- self.device.old_interface._privileged_command_runner = ( |
- self.device.old_interface.RunShellCommandWithSU) |
- self.device.old_interface._protected_file_access_method_initialized = True |
- |
- mock_file = mock.MagicMock(spec=file) |
- mock_file.name = '/tmp/file/to.be.pushed' |
- mock_file.__enter__.return_value = mock_file |
- with mock.patch('tempfile.NamedTemporaryFile', |
- return_value=mock_file): |
- with self.assertCallsSequence( |
- cmd_ret=[ |
- # Create temporary contents file |
- (r"adb -s 0123456789abcdef shell " |
- "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
- "echo \$\?'", |
- '1\r\n'), |
- # Create temporary script file |
- (r"adb -s 0123456789abcdef shell " |
- "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
- "echo \$\?'", |
- '1\r\n'), |
- # Set contents file |
- (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
- '/fake/storage/path/temp_file-\d+\d+', |
- '100 B/s (100 bytes in 1.000s)\r\n'), |
- # Set script file |
- (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
- '/fake/storage/path/temp_file-\d+\d+', |
- '100 B/s (100 bytes in 1.000s)\r\n'), |
- # Call script |
- (r"adb -s 0123456789abcdef shell " |
- "'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
- # Remove device temporaries |
- (r"adb -s 0123456789abcdef shell " |
- "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
- (r"adb -s 0123456789abcdef shell " |
- "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
- comp=re.match): |
- self.device.WriteFile('/test/file/written.to.device', |
- 'new test file contents', as_root=True) |
- |
- def testWriteFile_asRoot_rejected(self): |
- self.device.old_interface._privileged_command_runner = None |
- self.device.old_interface._protected_file_access_method_initialized = True |
- with self.assertRaises(device_errors.CommandFailedError): |
- self.device.WriteFile('/test/file/no.permissions.to.write', |
- 'new test file contents', as_root=True) |
- |
- |
-class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): |
+ def testWriteFile_withPush(self): |
+ tmp_host = MockTempFile('/tmp/file/on.host') |
+ with self.assertCalls( |
+ (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
+ self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
jbudorick
2014/11/11 23:16:03
Just to be clear: this not being a tuple means tha
perezju
2014/11/11 23:26:33
Yep, causes the call to return None.
|
+ self.device.WriteFile('/path/to/device/file', 'new file contents', |
+ force_push=True) |
+ tmp_host.file.write.assert_called_once_with('new file contents') |
+ |
+ def testWriteFile_withPushAndSU(self): |
+ tmp_host = MockTempFile('/tmp/file/on.host') |
+ with self.assertCalls( |
+ (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
+ (self.call.device.NeedsSU(), True), |
+ (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.device), |
+ MockTempFile('/external/path/tmp/on.device')), |
+ self.call.adb.Push('/tmp/file/on.host', '/external/path/tmp/on.device'), |
+ self.call.device.RunShellCommand( |
+ ['mv', '/external/path/tmp/on.device', '/path/to/device/file'], |
+ as_root=True, check_return=True)): |
+ self.device.WriteFile('/path/to/device/file', 'new file contents', |
+ as_root=True, force_push=True) |
+ tmp_host.file.write.assert_called_once_with('new file contents') |
+ |
+ def testWriteFile_withPush_rejected(self): |
+ tmp_host = MockTempFile('/tmp/file/on.host') |
+ with self.assertCalls( |
+ (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
+ (self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file'), |
+ self.CommandError())): |
+ with self.assertRaises(device_errors.CommandFailedError): |
+ self.device.WriteFile('/path/to/device/file', 'new file contents', |
+ force_push=True) |
+ tmp_host.file.write.assert_called_once_with('new file contents') |
- def testWriteTextFileTest_basic(self): |
+ def testWriteFile_withEcho(self): |
with self.assertCall( |
- self.call.adb.Shell('echo some.string > /test/file/to.write'), ''): |
- self.device.WriteTextFile('/test/file/to.write', 'some.string') |
+ self.call.adb.Shell('echo the.contents > /test/file/to.write'), ''): |
+ self.device.WriteFile('/test/file/to.write', 'the.contents') |
- def testWriteTextFileTest_quoted(self): |
+ def testWriteFile_withEchoAndQuotes(self): |
with self.assertCall( |
- self.call.adb.Shell("echo 'some other string' > '/test/file/to write'"), |
- ''): |
- self.device.WriteTextFile('/test/file/to write', 'some other string') |
+ self.call.adb.Shell("echo 'the contents' > '/test/file/to write'"), ''): |
+ self.device.WriteFile('/test/file/to write', 'the contents') |
- def testWriteTextFileTest_withSU(self): |
+ def testWriteFile_withEchoAndSU(self): |
with self.assertCalls( |
(self.call.device.NeedsSU(), True), |
- (self.call.adb.Shell('su -c echo string > /test/file'), '')): |
- self.device.WriteTextFile('/test/file', 'string', as_root=True) |
+ (self.call.adb.Shell('su -c echo contents > /test/file'), '')): |
+ self.device.WriteFile('/test/file', 'contents', as_root=True) |
class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |