From 137f0751b21d9bc2028966e0ed682fd634adc9cd Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Wed, 29 Nov 2017 08:22:05 -0800 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20IOS=5FSIMULATOR=5FHOME=20?= =?UTF-8?q?environment=20variable=20in=20IOSSimulat=E2=80=A6=20(#13236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for IOS_SIMULATOR_HOME environment variable in IOSSimulator.logFilePath flutter_tools can be run on environments where the user's HOME directory is not the root of the iOS simulators' configs. This change adds support for such environments by allowing the caller to set the simulator root directory via an environment variable. --- .../flutter_tools/lib/src/ios/simulators.dart | 35 ++++++++++--------- .../test/ios/simulators_test.dart | 25 +++++++++++-- packages/flutter_tools/test/src/context.dart | 3 +- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index a80485ce12..5b6112fd5a 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -415,7 +415,9 @@ class IOSSimulator extends Device { } String get logFilePath { - return fs.path.join(homeDirPath, 'Library', 'Logs', 'CoreSimulator', id, 'system.log'); + return platform.environment.containsKey('IOS_SIMULATOR_LOG_FILE_PATH') + ? platform.environment['IOS_SIMULATOR_LOG_FILE_PATH'].replaceAll('%{id}', id) + : fs.path.join(homeDirPath, 'Library', 'Logs', 'CoreSimulator', id, 'system.log'); } @override @@ -424,6 +426,13 @@ class IOSSimulator extends Device { @override Future get sdkNameAndVersion async => category; + final RegExp _iosSdkRegExp = new RegExp(r'iOS (\d+)'); + + Future get sdkMajorVersion async { + final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion); + return int.parse(sdkMatch.group(1) ?? 11); + } + @override DeviceLogReader getLogReader({ApplicationPackage app}) { assert(app is IOSApp); @@ -444,10 +453,12 @@ class IOSSimulator extends Device { } } - void ensureLogsExists() { - final File logFile = fs.file(logFilePath); - if (!logFile.existsSync()) - logFile.writeAsBytesSync([]); + Future ensureLogsExists() async { + if (await sdkMajorVersion < 11) { + final File logFile = fs.file(logFilePath); + if (!logFile.existsSync()) + logFile.writeAsBytesSync([]); + } } bool get _xcodeVersionSupportsScreenshot { @@ -463,15 +474,10 @@ class IOSSimulator extends Device { } } -final RegExp _iosSdkRegExp = new RegExp(r'iOS (\d+)'); - /// Launches the device log reader process on the host. Future launchDeviceLogTool(IOSSimulator device) async { - final Match sdkMatch = _iosSdkRegExp.firstMatch(await device.sdkNameAndVersion); - final int majorVersion = int.parse(sdkMatch.group(1) ?? 11); - // Versions of iOS prior to iOS 11 log to the simulator syslog file. - if (majorVersion < 11) + if (await device.sdkMajorVersion < 11) return runCommand(['tail', '-n', '0', '-F', device.logFilePath]); // For iOS 11 and above, use /usr/bin/log to tail process logs. @@ -482,11 +488,8 @@ Future launchDeviceLogTool(IOSSimulator device) async { } Future launchSystemLogTool(IOSSimulator device) async { - final Match sdkMatch = _iosSdkRegExp.firstMatch(await device.sdkNameAndVersion); - final int majorVersion = int.parse(sdkMatch.group(1) ?? 11); - // Versions of iOS prior to 11 tail the simulator syslog file. - if (majorVersion < 11) + if (await device.sdkMajorVersion < 11) return runCommand(['tail', '-n', '0', '-F', '/private/var/log/system.log']); // For iOS 11 and later, all relevant detail is in the device log. @@ -520,7 +523,7 @@ class _IOSSimulatorLogReader extends DeviceLogReader { Future _start() async { // Device log. - device.ensureLogsExists(); + await device.ensureLogsExists(); _deviceProcess = await launchDeviceLogTool(device); _deviceProcess.stdout.transform(UTF8.decoder).transform(const LineSplitter()).listen(_onDeviceLine); _deviceProcess.stderr.transform(UTF8.decoder).transform(const LineSplitter()).listen(_onDeviceLine); diff --git a/packages/flutter_tools/test/ios/simulators_test.dart b/packages/flutter_tools/test/ios/simulators_test.dart index 182b4ae21b..8cca0dbeb3 100644 --- a/packages/flutter_tools/test/ios/simulators_test.dart +++ b/packages/flutter_tools/test/ios/simulators_test.dart @@ -18,8 +18,29 @@ class MockProcessManager extends Mock implements ProcessManager {} class MockProcess extends Mock implements Process {} void main() { - final FakePlatform osx = new FakePlatform.fromPlatform(const LocalPlatform()); - osx.operatingSystem = 'macos'; + FakePlatform osx; + + setUp(() { + osx = new FakePlatform.fromPlatform(const LocalPlatform()); + osx.operatingSystem = 'macos'; + }); + + group('logFilePath', () { + testUsingContext('defaults to rooted from HOME', () { + osx.environment['HOME'] = '/foo/bar'; + expect(new IOSSimulator('123').logFilePath, '/foo/bar/Library/Logs/CoreSimulator/123/system.log'); + }, overrides: { + Platform: () => osx, + }, testOn: 'posix'); + + testUsingContext('respects IOS_SIMULATOR_LOG_FILE_PATH', () { + osx.environment['HOME'] = '/foo/bar'; + osx.environment['IOS_SIMULATOR_LOG_FILE_PATH'] = '/baz/qux/%{id}/system.log'; + expect(new IOSSimulator('456').logFilePath, '/baz/qux/456/system.log'); + }, overrides: { + Platform: () => osx, + }); + }); group('compareIosVersions', () { test('compares correctly', () { diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index f75f10133a..db567d7b9a 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -68,6 +68,7 @@ void testUsingContext(String description, dynamic testMethod(), { Timeout timeout, Map overrides: const {}, ContextInitializer initializeContext: _defaultInitializeContext, + String testOn, bool skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this }) { @@ -124,7 +125,7 @@ void testUsingContext(String description, dynamic testMethod(), { rethrow; } - }, timeout: timeout, skip: skip); + }, timeout: timeout, testOn: testOn, skip: skip); } void _printBufferedErrors(AppContext testContext) {