From 483f71ae2153db53a3c18fce826a5784a0dee243 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Thu, 10 Jun 2021 16:24:03 -0700 Subject: [PATCH] Migrate application_package to null safety (#83956) --- .../lib/src/ios/application_package.dart | 46 +++++++++---------- .../application_package_test.dart | 11 +++++ .../ios/ios_device_install_test.dart | 19 ++++---- .../ios/ios_device_start_prebuilt_test.dart | 1 + 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/application_package.dart b/packages/flutter_tools/lib/src/ios/application_package.dart index d6c3deca50..0d018a338d 100644 --- a/packages/flutter_tools/lib/src/ios/application_package.dart +++ b/packages/flutter_tools/lib/src/ios/application_package.dart @@ -2,10 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - -import 'package:meta/meta.dart'; - import '../application_package.dart'; import '../base/file_system.dart'; import '../build_info.dart'; @@ -13,15 +9,14 @@ import '../globals_null_migrated.dart' as globals; import '../project.dart'; import 'plist_parser.dart'; - /// Tests whether a [Directory] is an iOS bundle directory. bool _isBundleDirectory(Directory dir) => dir.path.endsWith('.app'); abstract class IOSApp extends ApplicationPackage { - IOSApp({@required String projectBundleId}) : super(id: projectBundleId); + IOSApp({required String projectBundleId}) : super(id: projectBundleId); /// Creates a new IOSApp from an existing app bundle or IPA. - factory IOSApp.fromPrebuiltApp(FileSystemEntity applicationBinary) { + static IOSApp? fromPrebuiltApp(FileSystemEntity applicationBinary) { final FileSystemEntityType entityType = globals.fs.typeSync(applicationBinary.path); if (entityType == FileSystemEntityType.notFound) { globals.printError( @@ -61,7 +56,7 @@ abstract class IOSApp extends ApplicationPackage { globals.printError('Invalid prebuilt iOS app. Does not contain Info.plist.'); return null; } - final String id = globals.plistParser.getValueFromFile( + final String? id = globals.plistParser.getValueFromFile( plistPath, PlistParser.kCFBundleIdentifierKey, ); @@ -77,7 +72,7 @@ abstract class IOSApp extends ApplicationPackage { ); } - static Future fromIosProject(IosProject project, BuildInfo buildInfo) { + static Future fromIosProject(IosProject project, BuildInfo buildInfo) async { if (!globals.platform.isMacOS) { return null; } @@ -106,26 +101,29 @@ abstract class IOSApp extends ApplicationPackage { /// Directory used by ios-deploy to store incremental installation metadata for /// faster second installs. - Directory get appDeltaDirectory; + Directory? get appDeltaDirectory; } class BuildableIOSApp extends IOSApp { - BuildableIOSApp(this.project, String projectBundleId, String hostAppBundleName) + BuildableIOSApp(this.project, String projectBundleId, String? hostAppBundleName) : _hostAppBundleName = hostAppBundleName, super(projectBundleId: projectBundleId); - static Future fromProject(IosProject project, BuildInfo buildInfo) async { - final String projectBundleId = await project.productBundleIdentifier(buildInfo); - final String hostAppBundleName = await project.hostAppBundleName(buildInfo); - return BuildableIOSApp(project, projectBundleId, hostAppBundleName); + static Future fromProject(IosProject project, BuildInfo buildInfo) async { + final String? hostAppBundleName = await project.hostAppBundleName(buildInfo); + final String? projectBundleId = await project.productBundleIdentifier(buildInfo); + if (projectBundleId != null) { + return BuildableIOSApp(project, projectBundleId, hostAppBundleName); + } + return null; } final IosProject project; - final String _hostAppBundleName; + final String? _hostAppBundleName; @override - String get name => _hostAppBundleName; + String? get name => _hostAppBundleName; @override String get simulatorBundlePath => _buildAppPath('iphonesimulator'); @@ -139,8 +137,8 @@ class BuildableIOSApp extends IOSApp { // Xcode uses this path for the final archive bundle location, // not a top-level output directory. // Specifying `build/ios/archive/Runner` will result in `build/ios/archive/Runner.xcarchive`. - String get archiveBundlePath - => globals.fs.path.join(getIosBuildDirectory(), 'archive', globals.fs.path.withoutExtension(_hostAppBundleName)); + String get archiveBundlePath => globals.fs.path.join(getIosBuildDirectory(), 'archive', + _hostAppBundleName == null ? 'Runner' : globals.fs.path.withoutExtension(_hostAppBundleName!)); // The output xcarchive bundle path `build/ios/archive/Runner.xcarchive`. String get archiveBundleOutputPath => @@ -156,19 +154,19 @@ class BuildableIOSApp extends IOSApp { class PrebuiltIOSApp extends IOSApp { PrebuiltIOSApp({ - this.bundleDir, + required this.bundleDir, this.bundleName, - @required String projectBundleId, + required String projectBundleId, }) : super(projectBundleId: projectBundleId); final Directory bundleDir; - final String bundleName; + final String? bundleName; @override - final Directory appDeltaDirectory = null; + final Directory? appDeltaDirectory = null; @override - String get name => bundleName; + String? get name => bundleName; @override String get simulatorBundlePath => _bundlePath; diff --git a/packages/flutter_tools/test/general.shard/application_package_test.dart b/packages/flutter_tools/test/general.shard/application_package_test.dart index ada96b3d9b..d0e0106a84 100644 --- a/packages/flutter_tools/test/general.shard/application_package_test.dart +++ b/packages/flutter_tools/test/general.shard/application_package_test.dart @@ -381,6 +381,17 @@ void main() { expect(iosApp, null); }, overrides: overrides); + + testUsingContext('returns null when there with no product identifier', () async { + globals.fs.file('pubspec.yaml').createSync(); + globals.fs.file('.packages').createSync(); + final Directory project = globals.fs.directory('ios/Runner.xcodeproj')..createSync(recursive: true); + project.childFile('project.pbxproj').createSync(); + final BuildableIOSApp iosApp = await IOSApp.fromIosProject( + FlutterProject.fromDirectory(globals.fs.currentDirectory).ios, null) as BuildableIOSApp; + + expect(iosApp, null); + }, overrides: overrides); }); group('FuchsiaApp', () { diff --git a/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart b/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart index c0475829eb..dce691e649 100644 --- a/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart @@ -30,14 +30,17 @@ const Map kDyLdLibEntry = { void main() { Artifacts artifacts; String iosDeployPath; + FileSystem fileSystem; + Directory bundleDirectory; setUp(() { artifacts = Artifacts.test(); + fileSystem = MemoryFileSystem.test(); + bundleDirectory = fileSystem.directory('bundle'); iosDeployPath = artifacts.getHostArtifact(HostArtifact.iosDeploy).path; }); testWithoutContext('IOSDevice.installApp calls ios-deploy correctly with USB', () async { - final FileSystem fileSystem = MemoryFileSystem.test(); final IOSApp iosApp = PrebuiltIOSApp( projectBundleId: 'app', bundleDir: fileSystem.currentDirectory, @@ -68,7 +71,6 @@ void main() { }); testWithoutContext('IOSDevice.installApp calls ios-deploy correctly with network', () async { - final FileSystem fileSystem = MemoryFileSystem.test(); final IOSApp iosApp = PrebuiltIOSApp( projectBundleId: 'app', bundleDir: fileSystem.currentDirectory, @@ -98,7 +100,7 @@ void main() { }); testWithoutContext('IOSDevice.uninstallApp calls ios-deploy correctly', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ iosDeployPath, @@ -121,7 +123,7 @@ void main() { group('isAppInstalled', () { testWithoutContext('catches ProcessException from ios-deploy', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ iosDeployPath, @@ -145,7 +147,7 @@ void main() { }); testWithoutContext('returns true when app is installed', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ iosDeployPath, @@ -169,7 +171,7 @@ void main() { }); testWithoutContext('returns false when app is not installed', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ iosDeployPath, @@ -195,7 +197,7 @@ void main() { }); testWithoutContext('returns false on command timeout or other error', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); const String stderr = '2020-03-26 17:48:43.484 ios-deploy[21518:5501783] [ !! ] Timed out waiting for device'; final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ @@ -224,7 +226,6 @@ void main() { }); testWithoutContext('IOSDevice.installApp catches ProcessException from ios-deploy', () async { - final FileSystem fileSystem = MemoryFileSystem.test(); final IOSApp iosApp = PrebuiltIOSApp( projectBundleId: 'app', bundleDir: fileSystem.currentDirectory, @@ -249,7 +250,7 @@ void main() { }); testWithoutContext('IOSDevice.uninstallApp catches ProcessException from ios-deploy', () async { - final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app', bundleDir: bundleDirectory); final FakeProcessManager processManager = FakeProcessManager.list([ FakeCommand(command: [ iosDeployPath, diff --git a/packages/flutter_tools/test/general.shard/ios/ios_device_start_prebuilt_test.dart b/packages/flutter_tools/test/general.shard/ios/ios_device_start_prebuilt_test.dart index 7fcf1bf466..efb96814d5 100644 --- a/packages/flutter_tools/test/general.shard/ios/ios_device_start_prebuilt_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/ios_device_start_prebuilt_test.dart @@ -93,6 +93,7 @@ void main() { final IOSApp iosApp = PrebuiltIOSApp( projectBundleId: 'app', bundleName: 'Runner', + bundleDir: MemoryFileSystem.test().directory('bundle'), ); device.portForwarder = devicePortForwarder;