Always create the `AndroidDevice` object specific log directory. (#848)
Create upon access if `log_path` doesn't exist
diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py
index 40b47a4..7d5064a 100644
--- a/mobly/controllers/android_device.py
+++ b/mobly/controllers/android_device.py
@@ -491,8 +491,8 @@
def __init__(self, serial=''):
self._serial = str(serial)
# logging.log_path only exists when this is used in an Mobly test run.
- self._log_path_base = getattr(logging, 'log_path', '/tmp/logs')
- self._log_path = os.path.join(self._log_path_base,
+ _log_path_base = utils.abs_path(getattr(logging, 'log_path', '/tmp/logs'))
+ self._log_path = os.path.join(_log_path_base,
'AndroidDevice%s' % self._normalized_serial)
self._debug_tag = self._serial
self.log = AndroidDeviceLoggerAdapter(logging.getLogger(),
@@ -621,6 +621,8 @@
def log_path(self):
"""A string that is the path for all logs collected from this device.
"""
+ if not os.path.exists(self._log_path):
+ utils.create_dir(self._log_path)
return self._log_path
@log_path.setter
diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py
index a99f1e3..4aa5304 100755
--- a/tests/mobly/controllers/android_device_test.py
+++ b/tests/mobly/controllers/android_device_test.py
@@ -601,6 +601,7 @@
self, sanitize_filename_mock, get_log_file_timestamp_mock, MockFastboot,
MockAdbProxy):
mock_serial = 1
+ sanitize_filename_mock.return_value = '1'
ad = android_device.AndroidDevice(serial=mock_serial)
get_log_file_timestamp_mock.return_value = '07-22-2019_17-53-34-450'
filename = ad.generate_filename('MagicLog')
@@ -1098,8 +1099,7 @@
"""
expected_e = Exception('start failed.')
MockSnippetClient.initialize = mock.Mock(side_effect=expected_e)
- MockSnippetClient.stop = mock.Mock(
- side_effect=Exception('stop failed.'))
+ MockSnippetClient.stop = mock.Mock(side_effect=Exception('stop failed.'))
ad = android_device.AndroidDevice(serial='1')
try:
ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME)