diff --git a/packages/flutter_tools/test/integration.shard/swift_package_manager_test.dart b/packages/flutter_tools/test/integration.shard/swift_package_manager_test.dart index 2460fdece56..1aaa6a8336f 100644 --- a/packages/flutter_tools/test/integration.shard/swift_package_manager_test.dart +++ b/packages/flutter_tools/test/integration.shard/swift_package_manager_test.dart @@ -607,4 +607,89 @@ void main() { ); } }, skip: !platform.isMacOS); // [intended] Swift Package Manager only works on macos. + + test('Removing the last plugin updates the generated Swift package', () async { + final Directory workingDirectory = fileSystem.systemTempDirectory + .createTempSync('swift_package_manager_remove_last_plugin.'); + final String workingDirectoryPath = workingDirectory.path; + try { + await SwiftPackageManagerUtils.enableSwiftPackageManager( + flutterBin, + workingDirectoryPath, + ); + + // Create an app with a plugin. + final String appDirectoryPath = await SwiftPackageManagerUtils.createApp( + flutterBin, + workingDirectoryPath, + iosLanguage: 'swift', + platform: 'ios', + usesSwiftPackageManager: true, + options: ['--platforms=ios'], + ); + + final SwiftPackageManagerPlugin integrationTestPlugin = + SwiftPackageManagerUtils.integrationTestPlugin('ios'); + + SwiftPackageManagerUtils.addDependency( + appDirectoryPath: appDirectoryPath, + plugin: integrationTestPlugin, + ); + + // Build the app to generate the Swift package. + await SwiftPackageManagerUtils.buildApp( + flutterBin, + appDirectoryPath, + options: ['ios', '--config-only', '-v'], + ); + + // Verify the generated Swift package depends on the plugin. + final File generatedManifestFile = fileSystem + .directory(appDirectoryPath) + .childDirectory('ios') + .childDirectory('Flutter') + .childDirectory('ephemeral') + .childDirectory('Packages') + .childDirectory('FlutterGeneratedPluginSwiftPackage') + .childFile('Package.swift'); + + expect(generatedManifestFile.existsSync(), isTrue); + + String generatedManifest = generatedManifestFile.readAsStringSync(); + final String generatedSwiftDependency = ''' + dependencies: [ + .package(name: "integration_test", path: "${integrationTestPlugin.swiftPackagePlatformPath}") + ], +'''; + + expect(generatedManifest.contains(generatedSwiftDependency), isTrue); + + // Remove the plugin and rebuild the app to re-generate the Swift package. + SwiftPackageManagerUtils.removeDependency( + appDirectoryPath: appDirectoryPath, + plugin: integrationTestPlugin, + ); + + await SwiftPackageManagerUtils.buildApp( + flutterBin, + appDirectoryPath, + options: ['ios', '--config-only', '-v'], + ); + + // Verify the generated Swift package does not depend on the plugin. + expect(generatedManifestFile.existsSync(), isTrue); + + generatedManifest = generatedManifestFile.readAsStringSync(); + const String emptyDependencies = 'dependencies: [\n \n ],\n'; + + expect(generatedManifest.contains(generatedSwiftDependency), isFalse); + expect(generatedManifest.contains(emptyDependencies), isTrue); + } finally { + await SwiftPackageManagerUtils.disableSwiftPackageManager(flutterBin, workingDirectoryPath); + ErrorHandlingFileSystem.deleteIfExists( + workingDirectory, + recursive: true, + ); + } + }, skip: !platform.isMacOS); // [intended] Swift Package Manager only works on macos. } diff --git a/packages/flutter_tools/test/integration.shard/swift_package_manager_utils.dart b/packages/flutter_tools/test/integration.shard/swift_package_manager_utils.dart index 1dc52e35465..484627c943c 100644 --- a/packages/flutter_tools/test/integration.shard/swift_package_manager_utils.dart +++ b/packages/flutter_tools/test/integration.shard/swift_package_manager_utils.dart @@ -245,6 +245,24 @@ class SwiftPackageManagerUtils { ); } + static void removeDependency({ + required SwiftPackageManagerPlugin plugin, + required String appDirectoryPath, + }) { + final File pubspec = fileSystem.file( + fileSystem.path.join(appDirectoryPath, 'pubspec.yaml'), + ); + final String pubspecContent = pubspec.readAsStringSync(); + final String updatedPubspecContent = pubspecContent.replaceFirst( + '\n ${plugin.pluginName}:\n path: ${plugin.pluginPath}\n', + '\n', + ); + + expect(updatedPubspecContent, isNot(pubspecContent)); + + pubspec.writeAsStringSync(updatedPubspecContent); + } + static void disableSwiftPackageManagerByPubspec({ required String appDirectoryPath, }) { @@ -378,4 +396,5 @@ class SwiftPackageManagerPlugin { final String platform; String get exampleAppPath => fileSystem.path.join(pluginPath, 'example'); String get exampleAppPlatformPath => fileSystem.path.join(exampleAppPath, platform); + String get swiftPackagePlatformPath => fileSystem.path.join(pluginPath, platform, pluginName); }