diff --git a/packages/flutter_tools/lib/src/ios/core_devices.dart b/packages/flutter_tools/lib/src/ios/core_devices.dart index aa39adbf66..0d3bcdd2a9 100644 --- a/packages/flutter_tools/lib/src/ios/core_devices.dart +++ b/packages/flutter_tools/lib/src/ios/core_devices.dart @@ -5,6 +5,7 @@ import 'package:meta/meta.dart'; import 'package:process/process.dart'; +import '../base/error_handling_io.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; @@ -101,7 +102,7 @@ class IOSCoreDeviceControl { _logger.printError('Error executing devicectl: $err'); return []; } finally { - tempDirectory.deleteSync(recursive: true); + ErrorHandlingFileSystem.deleteIfExists(tempDirectory, recursive: true); } } diff --git a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart index e6363ceb91..ef2752b604 100644 --- a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart +++ b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart @@ -111,7 +111,7 @@ void main() { exceptionHandler = FileExceptionHandler(); }); - testWithoutContext('bypasses error handling when withAllowedFailure is used', () { + testWithoutContext('bypasses error handling when noExitOnFailure is used', () { final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle), platform: windowsPlatform, @@ -123,9 +123,9 @@ void main() { FileSystemOp.write, FileSystemException('', file.path, const OSError('', kUserPermissionDenied)), ); - + final Matcher throwsNonToolExit = throwsA(isNot(isA())); expect(() => ErrorHandlingFileSystem.noExitOnFailure( - () => file.writeAsStringSync('')), throwsException); + () => file.writeAsStringSync('')), throwsNonToolExit); // nesting does not unconditionally re-enable errors. expect(() { @@ -133,7 +133,7 @@ void main() { ErrorHandlingFileSystem.noExitOnFailure(() { }); file.writeAsStringSync(''); }); - }, throwsException); + }, throwsNonToolExit); // Check that state does not leak. expect(() => file.writeAsStringSync(''), throwsToolExit()); diff --git a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart index d820e1a13b..a8aecf4540 100644 --- a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart @@ -5,6 +5,7 @@ import 'package:file/memory.dart'; import 'package:file_testing/file_testing.dart'; import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/version.dart'; import 'package:flutter_tools/src/ios/core_devices.dart'; @@ -35,7 +36,7 @@ void main() { version: Version(14, 0, 0), ); xcode = Xcode.test( - processManager: FakeProcessManager.any(), + processManager: fakeProcessManager, xcodeProjectInterpreter: xcodeProjectInterpreter, ); deviceControl = IOSCoreDeviceControl( @@ -86,6 +87,7 @@ void main() { setUp(() { logger = BufferLogger.test(); fakeProcessManager = FakeProcessManager.empty(); + // TODO(fujino): make this FakeProcessManager.empty() xcode = Xcode.test(processManager: FakeProcessManager.any()); deviceControl = IOSCoreDeviceControl( logger: logger, @@ -1322,6 +1324,37 @@ invalid JSON }); group('list devices', () { + testWithoutContext('Handles FileSystemException deleting temp directory', () async { + final Directory tempDir = fileSystem.systemTempDirectory + .childDirectory('core_devices.rand0'); + final File tempFile = tempDir.childFile('core_device_list.json'); + final List args = [ + 'xcrun', + 'devicectl', + 'list', + 'devices', + '--timeout', + '5', + '--json-output', + tempFile.path, + ]; + fakeProcessManager.addCommand(FakeCommand( + command: args, + onRun: () { + // Simulate that this command threw and simulataneously the OS + // deleted the temp directory + expect(tempFile, exists); + tempDir.deleteSync(recursive: true); + expect(tempFile, isNot(exists)); + throw ProcessException(args.first, args.sublist(1)); + }, + )); + + await deviceControl.getCoreDevices(); + expect(logger.errorText, contains('Error executing devicectl: ProcessException')); + expect(fakeProcessManager, hasNoRemainingExpectations); + }); + testWithoutContext('No devices', () async { const String deviceControlOutput = ''' {