Unverified Commit a1c185f3 authored by Collin Jackson's avatar Collin Jackson Committed by GitHub

Fix plugin template app's tests (#39080)

* Fix plugin template

* Add test of plugin template working
parent 19cdb21c
...@@ -23,8 +23,8 @@ TaskFunction combine(List<TaskFunction> tasks) { ...@@ -23,8 +23,8 @@ TaskFunction combine(List<TaskFunction> tasks) {
}; };
} }
/// Defines task that creates new Flutter project, adds a plugin, and then /// Defines task that creates new Flutter project, adds a local and remote
/// builds the specified [buildTarget]. /// plugin, and then builds the specified [buildTarget].
class PluginTest { class PluginTest {
PluginTest(this.buildTarget, this.options); PluginTest(this.buildTarget, this.options);
...@@ -32,19 +32,32 @@ class PluginTest { ...@@ -32,19 +32,32 @@ class PluginTest {
final List<String> options; final List<String> options;
Future<TaskResult> call() async { Future<TaskResult> call() async {
section('Create Flutter project'); final Directory tempDir =
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.'); Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.');
try { try {
final FlutterProject project = await FlutterProject.create(tempDir, options); section('Create plugin');
final _FlutterProject plugin = await _FlutterProject.create(
tempDir, options,
name: 'plugintest', template: 'plugin');
section('Test plugin');
await plugin.test();
section('Create Flutter app');
final _FlutterProject app = await _FlutterProject.create(tempDir, options,
name: 'plugintestapp', template: 'app');
try { try {
if (buildTarget == 'ios') if (buildTarget == 'ios')
await prepareProvisioningCertificates(project.rootPath); await prepareProvisioningCertificates(app.rootPath);
section('Add plugin'); section('Add plugins');
await project.addPlugin('path_provider'); await app.addPlugin('plugintest',
section('Build'); pluginPath: path.join('..', 'plugintest'));
await project.build(buildTarget); await app.addPlugin('path_provider');
section('Build app');
await app.build(buildTarget);
section('Test app');
await app.test();
} finally { } finally {
await project.delete(); await plugin.delete();
await app.delete();
} }
return TaskResult.success(null); return TaskResult.success(null);
} catch (e) { } catch (e) {
...@@ -55,34 +68,50 @@ class PluginTest { ...@@ -55,34 +68,50 @@ class PluginTest {
} }
} }
class FlutterProject { class _FlutterProject {
FlutterProject(this.parent, this.name); _FlutterProject(this.parent, this.name);
final Directory parent; final Directory parent;
final String name; final String name;
static Future<FlutterProject> create(Directory directory, List<String> options) async {
await inDirectory(directory, () async {
await flutter(
'create',
options: <String>['--template=app', '--org', 'io.flutter.devicelab', ...options, 'plugintest'],
);
});
return FlutterProject(directory, 'plugintest');
}
String get rootPath => path.join(parent.path, name); String get rootPath => path.join(parent.path, name);
Future<void> addPlugin(String plugin) async { Future<void> addPlugin(String plugin, {String pluginPath}) async {
final File pubspec = File(path.join(rootPath, 'pubspec.yaml')); final File pubspec = File(path.join(rootPath, 'pubspec.yaml'));
String content = await pubspec.readAsString(); String content = await pubspec.readAsString();
final String dependency =
pluginPath != null ? '$plugin:\n path: $pluginPath' : '$plugin:';
content = content.replaceFirst( content = content.replaceFirst(
'\ndependencies:\n', '\ndependencies:\n',
'\ndependencies:\n $plugin:\n', '\ndependencies:\n $dependency\n',
); );
await pubspec.writeAsString(content, flush: true); await pubspec.writeAsString(content, flush: true);
} }
Future<void> test() async {
await inDirectory(Directory(rootPath), () async {
await flutter('test');
});
}
static Future<_FlutterProject> create(
Directory directory, List<String> options,
{String name, String template}) async {
await inDirectory(directory, () async {
await flutter(
'create',
options: <String>[
'--template=$template',
'--org',
'io.flutter.devicelab',
...options,
name
],
);
});
return _FlutterProject(directory, name);
}
Future<void> build(String target) async { Future<void> build(String target) async {
await inDirectory(Directory(rootPath), () async { await inDirectory(Directory(rootPath), () async {
await flutter('build', options: <String>[target]); await flutter('build', options: <String>[target]);
...@@ -93,11 +122,11 @@ class FlutterProject { ...@@ -93,11 +122,11 @@ class FlutterProject {
if (Platform.isWindows) { if (Platform.isWindows) {
// A running Gradle daemon might prevent us from deleting the project // A running Gradle daemon might prevent us from deleting the project
// folder on Windows. // folder on Windows.
await exec( final String wrapperPath =
path.absolute(path.join(rootPath, 'android', 'gradlew.bat')), path.absolute(path.join(rootPath, 'android', 'gradlew.bat'));
<String>['--stop'], if (File(wrapperPath).existsSync()) {
canFail: true, await exec(wrapperPath, <String>['--stop'], canFail: true);
); }
// TODO(ianh): Investigating if flakiness is timing dependent. // TODO(ianh): Investigating if flakiness is timing dependent.
await Future<void>.delayed(const Duration(seconds: 10)); await Future<void>.delayed(const Duration(seconds: 10));
} }
......
...@@ -5,6 +5,8 @@ import 'package:{{projectName}}/{{projectName}}.dart'; ...@@ -5,6 +5,8 @@ import 'package:{{projectName}}/{{projectName}}.dart';
void main() { void main() {
const MethodChannel channel = MethodChannel('{{projectName}}'); const MethodChannel channel = MethodChannel('{{projectName}}');
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() { setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async { channel.setMockMethodCallHandler((MethodCall methodCall) async {
return '42'; return '42';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment