Improve Dart plugin registration handling (#122046)

Improve Dart plugin registration handling
This commit is contained in:
stuartmorgan
2023-03-09 13:33:38 -08:00
committed by GitHub
parent 7e000b2977
commit 62116f998b
3 changed files with 279 additions and 144 deletions

View File

@@ -38,7 +38,7 @@ void main() {
});
group('resolvePlatformImplementation', () {
testWithoutContext('selects implementation from direct dependency', () async {
testWithoutContext('selects uncontested implementation from direct dependency', () async {
final Set<String> directDependencies = <String>{
'url_launcher_linux',
'url_launcher_macos',
@@ -76,25 +76,30 @@ void main() {
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'undirect_dependency_plugin',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'windows': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginWindows',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
resolvePlatformImplementation(<Plugin>[
expect(resolutions.length, equals(2));
expect(resolutions[0].toMap(), equals(
<String, String>{
'pluginName': 'url_launcher_linux',
'dartClass': 'UrlLauncherPluginLinux',
'platform': 'linux',
})
);
expect(resolutions[1].toMap(), equals(
<String, String>{
'pluginName': 'url_launcher_macos',
'dartClass': 'UrlLauncherPluginMacOS',
'platform': 'macos',
})
);
});
testWithoutContext('selects uncontested implementation from transitive dependency', () async {
final Set<String> directDependencies = <String>{
'url_launcher_macos',
};
final List<PluginInterfaceResolution> resolutions = resolvePlatformImplementation(<Plugin>[
Plugin.fromYaml(
'url_launcher_macos',
'',
@@ -111,21 +116,37 @@ void main() {
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'transitive_dependency_plugin',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'windows': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginWindows',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
expect(resolutions.length, equals(2));
expect(resolutions[0].toMap(), equals(
<String, String>{
'pluginName': 'url_launcher_linux',
'dartClass': 'UrlLauncherPluginLinux',
'platform': 'linux',
'pluginName': 'url_launcher_macos',
'dartClass': 'UrlLauncherPluginMacOS',
'platform': 'macos',
})
);
expect(resolutions[1].toMap(), equals(
<String, String>{
'pluginName': 'url_launcher_macos',
'dartClass': 'UrlLauncherPluginMacOS',
'platform': 'macos',
'pluginName': 'transitive_dependency_plugin',
'dartClass': 'UrlLauncherPluginWindows',
'platform': 'windows',
})
);
});
@@ -301,6 +322,25 @@ void main() {
fileSystem: fs,
appDependencies: directDependencies,
),
// Include three possible implementations, one before and one after
// to ensure that the selection is working as intended, not just by
// coincidence of order.
Plugin.fromYaml(
'another_url_launcher_linux',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UnofficialUrlLauncherPluginLinux',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'url_launcher_linux',
'',
@@ -317,6 +357,22 @@ void main() {
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'yet_another_url_launcher_linux',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UnofficialUrlLauncherPluginLinux2',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
expect(resolutions.length, equals(1));
expect(resolutions[0].toMap(), equals(
@@ -439,71 +495,6 @@ void main() {
);
});
testWithoutContext('selects user selected implementation despites default implementation', () async {
final Set<String> directDependencies = <String>{
'user_selected_url_launcher_implementation',
'url_launcher',
};
final List<PluginInterfaceResolution> resolutions = resolvePlatformImplementation(<Plugin>[
Plugin.fromYaml(
'url_launcher',
'',
YamlMap.wrap(<String, dynamic>{
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'default_package': 'url_launcher_linux',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'url_launcher_linux',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginLinux',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'user_selected_url_launcher_implementation',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginLinux',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
expect(resolutions.length, equals(1));
expect(resolutions[0].toMap(), equals(
<String, String>{
'pluginName': 'user_selected_url_launcher_implementation',
'dartClass': 'UrlLauncherPluginLinux',
'platform': 'linux',
})
);
});
testUsingContext('provides error when user selected multiple implementations', () async {
final Set<String> directDependencies = <String>{
'url_launcher_linux_1',
@@ -545,22 +536,27 @@ void main() {
),
]);
expect(
testLogger.errorText,
'Plugin `url_launcher_linux_2` implements an interface for `linux`, which was already implemented by plugin `url_launcher_linux_1`.\n'
'To fix this issue, remove either dependency from pubspec.yaml.'
'\n\n'
);
},
throwsToolExit(
message: 'Please resolve the errors',
));
expect(
testLogger.errorText,
'Plugin url_launcher:linux has conflicting direct dependency implementations:\n'
' url_launcher_linux_1\n'
' url_launcher_linux_2\n'
'To fix this issue, remove all but one of these dependencies from pubspec.yaml.'
'\n\n'
);
});
testUsingContext('provides all errors when user selected multiple implementations', () async {
final Set<String> directDependencies = <String>{
'url_launcher_linux_1',
'url_launcher_linux_2',
'url_launcher_windows_1',
'url_launcher_windows_2',
};
expect(() {
resolvePlatformImplementation(<Plugin>[
@@ -596,18 +592,109 @@ void main() {
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'url_launcher_windows_1',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'windows': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginWindows1',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'url_launcher_windows_2',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'windows': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginWindows2',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
expect(
testLogger.errorText,
'Plugin `url_launcher_linux_2` implements an interface for `linux`, which was already implemented by plugin `url_launcher_linux_1`.\n'
'To fix this issue, remove either dependency from pubspec.yaml.'
'\n\n'
);
},
throwsToolExit(
message: 'Please resolve the errors',
));
expect(
testLogger.errorText,
'Plugin url_launcher:linux has conflicting direct dependency implementations:\n'
' url_launcher_linux_1\n'
' url_launcher_linux_2\n'
'To fix this issue, remove all but one of these dependencies from pubspec.yaml.'
'\n\n'
'Plugin url_launcher:windows has conflicting direct dependency implementations:\n'
' url_launcher_windows_1\n'
' url_launcher_windows_2\n'
'To fix this issue, remove all but one of these dependencies from pubspec.yaml.'
'\n\n'
);
});
testUsingContext('provides error when user needs to select among multiple implementations', () async {
final Set<String> directDependencies = <String>{};
expect(() {
resolvePlatformImplementation(<Plugin>[
Plugin.fromYaml(
'url_launcher_linux_1',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginLinux1',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
Plugin.fromYaml(
'url_launcher_linux_2',
'',
YamlMap.wrap(<String, dynamic>{
'implements': 'url_launcher',
'platforms': <String, dynamic>{
'linux': <String, dynamic>{
'dartPluginClass': 'UrlLauncherPluginLinux2',
},
},
}),
null,
<String>[],
fileSystem: fs,
appDependencies: directDependencies,
),
]);
},
throwsToolExit(
message: 'Please resolve the errors',
));
expect(
testLogger.errorText,
'Plugin url_launcher:linux has multiple possible implementations:\n'
' url_launcher_linux_1\n'
' url_launcher_linux_2\n'
'To fix this issue, add one of these dependencies to pubspec.yaml.'
'\n\n'
);
});
});
@@ -994,8 +1081,11 @@ void createFakeDartPlugins(
) {
final Directory fakePubCache = fs.systemTempDirectory.childDirectory('cache');
final File packagesFile = flutterProject.directory
.childFile('.packages')
..createSync(recursive: true);
.childFile('.packages');
if (packagesFile.existsSync()) {
packagesFile.deleteSync();
}
packagesFile.createSync(recursive: true);
for (final MapEntry<String, String> entry in plugins.entries) {
final String name = fs.path.basename(entry.key);