Skip to content

Commit

Permalink
[Swift Package Manager] Test removing the last Flutter plugin (flutte…
Browse files Browse the repository at this point in the history
…r#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#11819 (comment)
  • Loading branch information
loic-sharma authored Aug 16, 2024
1 parent bced008 commit 15876ff
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) {
Expand Down Expand Up @@ -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);
}

0 comments on commit 15876ff

Please sign in to comment.