Skip to content

Commit 15876ff

Browse files
authored
[Swift Package Manager] Test removing the last Flutter plugin (#153519)
The Flutter tool has a bug where removing the last Flutter plugin does not correctly update the CocoaPods integration. This adds a test to ensure that the generated Swift package is properly updated when the last Flutter plugin is removed. See: flutter/flutter#11819 (comment)
1 parent bced008 commit 15876ff

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

packages/flutter_tools/test/integration.shard/swift_package_manager_test.dart

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,4 +607,89 @@ void main() {
607607
);
608608
}
609609
}, skip: !platform.isMacOS); // [intended] Swift Package Manager only works on macos.
610+
611+
test('Removing the last plugin updates the generated Swift package', () async {
612+
final Directory workingDirectory = fileSystem.systemTempDirectory
613+
.createTempSync('swift_package_manager_remove_last_plugin.');
614+
final String workingDirectoryPath = workingDirectory.path;
615+
try {
616+
await SwiftPackageManagerUtils.enableSwiftPackageManager(
617+
flutterBin,
618+
workingDirectoryPath,
619+
);
620+
621+
// Create an app with a plugin.
622+
final String appDirectoryPath = await SwiftPackageManagerUtils.createApp(
623+
flutterBin,
624+
workingDirectoryPath,
625+
iosLanguage: 'swift',
626+
platform: 'ios',
627+
usesSwiftPackageManager: true,
628+
options: <String>['--platforms=ios'],
629+
);
630+
631+
final SwiftPackageManagerPlugin integrationTestPlugin =
632+
SwiftPackageManagerUtils.integrationTestPlugin('ios');
633+
634+
SwiftPackageManagerUtils.addDependency(
635+
appDirectoryPath: appDirectoryPath,
636+
plugin: integrationTestPlugin,
637+
);
638+
639+
// Build the app to generate the Swift package.
640+
await SwiftPackageManagerUtils.buildApp(
641+
flutterBin,
642+
appDirectoryPath,
643+
options: <String>['ios', '--config-only', '-v'],
644+
);
645+
646+
// Verify the generated Swift package depends on the plugin.
647+
final File generatedManifestFile = fileSystem
648+
.directory(appDirectoryPath)
649+
.childDirectory('ios')
650+
.childDirectory('Flutter')
651+
.childDirectory('ephemeral')
652+
.childDirectory('Packages')
653+
.childDirectory('FlutterGeneratedPluginSwiftPackage')
654+
.childFile('Package.swift');
655+
656+
expect(generatedManifestFile.existsSync(), isTrue);
657+
658+
String generatedManifest = generatedManifestFile.readAsStringSync();
659+
final String generatedSwiftDependency = '''
660+
dependencies: [
661+
.package(name: "integration_test", path: "${integrationTestPlugin.swiftPackagePlatformPath}")
662+
],
663+
''';
664+
665+
expect(generatedManifest.contains(generatedSwiftDependency), isTrue);
666+
667+
// Remove the plugin and rebuild the app to re-generate the Swift package.
668+
SwiftPackageManagerUtils.removeDependency(
669+
appDirectoryPath: appDirectoryPath,
670+
plugin: integrationTestPlugin,
671+
);
672+
673+
await SwiftPackageManagerUtils.buildApp(
674+
flutterBin,
675+
appDirectoryPath,
676+
options: <String>['ios', '--config-only', '-v'],
677+
);
678+
679+
// Verify the generated Swift package does not depend on the plugin.
680+
expect(generatedManifestFile.existsSync(), isTrue);
681+
682+
generatedManifest = generatedManifestFile.readAsStringSync();
683+
const String emptyDependencies = 'dependencies: [\n \n ],\n';
684+
685+
expect(generatedManifest.contains(generatedSwiftDependency), isFalse);
686+
expect(generatedManifest.contains(emptyDependencies), isTrue);
687+
} finally {
688+
await SwiftPackageManagerUtils.disableSwiftPackageManager(flutterBin, workingDirectoryPath);
689+
ErrorHandlingFileSystem.deleteIfExists(
690+
workingDirectory,
691+
recursive: true,
692+
);
693+
}
694+
}, skip: !platform.isMacOS); // [intended] Swift Package Manager only works on macos.
610695
}

packages/flutter_tools/test/integration.shard/swift_package_manager_utils.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ class SwiftPackageManagerUtils {
245245
);
246246
}
247247

248+
static void removeDependency({
249+
required SwiftPackageManagerPlugin plugin,
250+
required String appDirectoryPath,
251+
}) {
252+
final File pubspec = fileSystem.file(
253+
fileSystem.path.join(appDirectoryPath, 'pubspec.yaml'),
254+
);
255+
final String pubspecContent = pubspec.readAsStringSync();
256+
final String updatedPubspecContent = pubspecContent.replaceFirst(
257+
'\n ${plugin.pluginName}:\n path: ${plugin.pluginPath}\n',
258+
'\n',
259+
);
260+
261+
expect(updatedPubspecContent, isNot(pubspecContent));
262+
263+
pubspec.writeAsStringSync(updatedPubspecContent);
264+
}
265+
248266
static void disableSwiftPackageManagerByPubspec({
249267
required String appDirectoryPath,
250268
}) {
@@ -378,4 +396,5 @@ class SwiftPackageManagerPlugin {
378396
final String platform;
379397
String get exampleAppPath => fileSystem.path.join(pluginPath, 'example');
380398
String get exampleAppPlatformPath => fileSystem.path.join(exampleAppPath, platform);
399+
String get swiftPackagePlatformPath => fileSystem.path.join(pluginPath, platform, pluginName);
381400
}

0 commit comments

Comments
 (0)