[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: https://github.com/flutter/flutter/issues/11819#issuecomment-2289782626
This commit is contained in:
Loïc Sharma 2024-08-16 07:03:13 -07:00 committed by GitHub
parent bced008679
commit 15876ff6ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 104 additions and 0 deletions

View File

@ -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: <String>['--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: <String>['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: <String>['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.
}

View File

@ -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);
}