Skip to content

Commit

Permalink
[flutter_tools] Fix null check in parsing web plugin from pubspec.yam…
Browse files Browse the repository at this point in the history
…l (#117939)

* fix null check in parsing web plugin yaml

* revert accidental diff

* remove comment
  • Loading branch information
christopherfujino authored Jan 4, 2023
1 parent 231855f commit 672fe20
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/flutter_tools/lib/src/platform_plugins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,19 @@ class WebPlugin extends PluginPlatform {
});

factory WebPlugin.fromYaml(String name, YamlMap yaml) {
assert(validate(yaml));
if (yaml['pluginClass'] is! String) {
throwToolExit('The plugin `$name` is missing the required field `pluginClass` in pubspec.yaml');
}
if (yaml['fileName'] is! String) {
throwToolExit('The plugin `$name` is missing the required field `fileName` in pubspec.yaml');
}
return WebPlugin(
name: name,
pluginClass: yaml['pluginClass'] as String,
fileName: yaml['fileName'] as String,
);
}

static bool validate(YamlMap yaml) {
if (yaml == null) {
return false;
}
return yaml['pluginClass'] is String && yaml['fileName'] is String;
}

static const String kConfigKey = 'web';

/// The name of the plugin.
Expand Down
23 changes: 23 additions & 0 deletions packages/flutter_tools/test/general.shard/plugin_parsing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,29 @@ void main() {
]);
});

testWithoutContext('Web plugin tool exits if fileName field missing', () {
final FileSystem fileSystem = MemoryFileSystem.test();
const String pluginYamlRaw =
'platforms:\n'
' web:\n'
' pluginClass: WebSamplePlugin\n';

final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
expect(
() => Plugin.fromYaml(
_kTestPluginName,
_kTestPluginPath,
pluginYaml,
null,
const <String>[],
fileSystem: fileSystem,
),
throwsToolExit(
message: 'The plugin `$_kTestPluginName` is missing the required field `fileName` in pubspec.yaml',
),
);
});

testWithoutContext('Windows assumes win32 when no variants are given', () {
final FileSystem fileSystem = MemoryFileSystem.test();
const String pluginYamlRaw =
Expand Down

0 comments on commit 672fe20

Please sign in to comment.