From 5487070f66ce6184f0d2ec49052f9320fc6beaa7 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Fri, 25 Oct 2019 15:00:05 -0700 Subject: [PATCH] Don't html-escape in the plugin registrant templates. (#43448) * Don't html-escape in the plugin registrant templates. Fixes #43382 * Add test --- packages/flutter_tools/lib/src/plugins.dart | 2 +- .../test/general.shard/plugins_test.dart | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index e1ed29852d5b..cae49b0cad6c 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -20,7 +20,7 @@ import 'project.dart'; void _renderTemplateToFile(String template, dynamic context, String filePath) { final String renderedTemplate = - mustache.Template(template).renderString(context); + mustache.Template(template, htmlEscapeValues: false).renderString(context); final File file = fs.file(filePath); file.createSync(recursive: true); file.writeAsStringSync(renderedTemplate); diff --git a/packages/flutter_tools/test/general.shard/plugins_test.dart b/packages/flutter_tools/test/general.shard/plugins_test.dart index 7e4089084e23..68a059451f53 100644 --- a/packages/flutter_tools/test/general.shard/plugins_test.dart +++ b/packages/flutter_tools/test/general.shard/plugins_test.dart @@ -21,6 +21,7 @@ void main() { MockIosProject iosProject; MockMacOSProject macosProject; MockAndroidProject androidProject; + MockWebProject webProject; File packagesFile; Directory dummyPackageDirectory; @@ -44,6 +45,10 @@ void main() { when(flutterProject.android).thenReturn(androidProject); when(androidProject.pluginRegistrantHost).thenReturn(flutterProject.directory.childDirectory('android').childDirectory('app')); when(androidProject.hostAppGradleRoot).thenReturn(flutterProject.directory.childDirectory('android')); + webProject = MockWebProject(); + when(flutterProject.web).thenReturn(webProject); + when(webProject.libDirectory).thenReturn(flutterProject.directory.childDirectory('lib')); + when(webProject.existsSync()).thenReturn(true); // Set up a simple .packages file for all the tests to use, pointing to one package. dummyPackageDirectory = fs.directory('/pubcache/apackage/lib/'); @@ -392,6 +397,54 @@ plugin3:${pluginUsingOldEmbeddingDir.childDirectory('lib').uri.toString()} ProcessManager: () => FakeProcessManager.any(), FeatureFlags: () => featureFlags, }); + + testUsingContext('Registrant for web doesn\'t escape slashes in imports', () async { + when(flutterProject.isModule).thenReturn(true); + when(featureFlags.isWebEnabled).thenReturn(true); + + // injectPlugins will crash if there is no AndroidManifest + final File androidManifest = flutterProject.directory + .childDirectory('android') + .childFile('AndroidManifest.xml') + ..createSync(recursive: true) + ..writeAsStringSync(kAndroidManifestUsingOldEmbedding); + when(androidProject.appManifestFile).thenReturn(androidManifest); + + final Directory webPluginWithNestedFile = + fs.systemTempDirectory.createTempSync('web_plugin_with_nested'); + webPluginWithNestedFile.childFile('pubspec.yaml').writeAsStringSync(''' +flutter: + plugin: + platforms: + web: + pluginClass: WebPlugin + fileName: src/web_plugin.dart +'''); + webPluginWithNestedFile + .childDirectory('lib') + .childDirectory('src') + .childFile('web_plugin.dart') + ..createSync(recursive: true); + + flutterProject.directory + .childFile('.packages') + .writeAsStringSync(''' +web_plugin_with_nested:${webPluginWithNestedFile.childDirectory('lib').uri.toString()} +'''); + + await injectPlugins(flutterProject); + + final File registrant = flutterProject.directory + .childDirectory('lib') + .childFile('generated_plugin_registrant.dart'); + + expect(registrant.existsSync(), isTrue); + expect(registrant.readAsStringSync(), contains("import 'package:web_plugin_with_nested/src/web_plugin.dart';")); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + FeatureFlags: () => featureFlags, + }); }); } @@ -401,3 +454,4 @@ class MockFlutterProject extends Mock implements FlutterProject {} class MockIosProject extends Mock implements IosProject {} class MockMacOSProject extends Mock implements MacOSProject {} class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {} +class MockWebProject extends Mock implements WebProject {}