plugin_tests.dart 4.25 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';

12 13 14
/// Combines several TaskFunctions with trivial success value into one.
TaskFunction combine(List<TaskFunction> tasks) {
  return () async {
15
    for (final TaskFunction task in tasks) {
16 17 18 19 20
      final TaskResult result = await task();
      if (result.failed) {
        return result;
      }
    }
21
    return TaskResult.success(null);
22 23 24
  };
}

25 26
/// Defines task that creates new Flutter project, adds a local and remote
/// plugin, and then builds the specified [buildTarget].
27
class PluginTest {
28
  PluginTest(this.buildTarget, this.options, { this.pluginCreateEnvironment, this.appCreateEnvironment });
29

30
  final String buildTarget;
31
  final List<String> options;
32 33
  final Map<String, String> pluginCreateEnvironment;
  final Map<String, String> appCreateEnvironment;
34 35

  Future<TaskResult> call() async {
36 37
    final Directory tempDir =
        Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.');
38
    try {
39 40 41
      section('Create plugin');
      final _FlutterProject plugin = await _FlutterProject.create(
          tempDir, options,
42
          name: 'plugintest', template: 'plugin', environment: pluginCreateEnvironment);
43 44 45 46
      section('Test plugin');
      await plugin.test();
      section('Create Flutter app');
      final _FlutterProject app = await _FlutterProject.create(tempDir, options,
47
          name: 'plugintestapp', template: 'app', environment: appCreateEnvironment);
48
      try {
49 50 51 52 53 54 55 56
        section('Add plugins');
        await app.addPlugin('plugintest',
            pluginPath: path.join('..', 'plugintest'));
        await app.addPlugin('path_provider');
        section('Build app');
        await app.build(buildTarget);
        section('Test app');
        await app.test();
57
      } finally {
58 59
        await plugin.delete();
        await app.delete();
60
      }
61
      return TaskResult.success(null);
62
    } catch (e) {
63
      return TaskResult.failure(e.toString());
64
    } finally {
65
      rmTree(tempDir);
66 67 68 69
    }
  }
}

70 71
class _FlutterProject {
  _FlutterProject(this.parent, this.name);
72 73 74 75 76 77

  final Directory parent;
  final String name;

  String get rootPath => path.join(parent.path, name);

78
  Future<void> addPlugin(String plugin, {String pluginPath}) async {
79
    final File pubspec = File(path.join(rootPath, 'pubspec.yaml'));
80
    String content = await pubspec.readAsString();
81 82
    final String dependency =
        pluginPath != null ? '$plugin:\n    path: $pluginPath' : '$plugin:';
83 84
    content = content.replaceFirst(
      '\ndependencies:\n',
85
      '\ndependencies:\n  $dependency\n',
86 87 88 89
    );
    await pubspec.writeAsString(content, flush: true);
  }

90 91 92 93 94 95 96
  Future<void> test() async {
    await inDirectory(Directory(rootPath), () async {
      await flutter('test');
    });
  }

  static Future<_FlutterProject> create(
97 98 99 100 101 102 103
      Directory directory,
      List<String> options,
      {
        String name,
        String template,
        Map<String, String> environment,
      }) async {
104 105 106 107 108 109 110 111
    await inDirectory(directory, () async {
      await flutter(
        'create',
        options: <String>[
          '--template=$template',
          '--org',
          'io.flutter.devicelab',
          ...options,
112
          name,
113
        ],
114
        environment: environment,
115 116 117 118 119
      );
    });
    return _FlutterProject(directory, name);
  }

120
  Future<void> build(String target) async {
121
    await inDirectory(Directory(rootPath), () async {
122 123 124 125
      await flutter('build', options: <String>[target]);
    });
  }

126
  Future<void> delete() async {
127 128 129
    if (Platform.isWindows) {
      // A running Gradle daemon might prevent us from deleting the project
      // folder on Windows.
130 131 132 133 134
      final String wrapperPath =
          path.absolute(path.join(rootPath, 'android', 'gradlew.bat'));
      if (File(wrapperPath).existsSync()) {
        await exec(wrapperPath, <String>['--stop'], canFail: true);
      }
135
      // TODO(ianh): Investigating if flakiness is timing dependent.
136
      await Future<void>.delayed(const Duration(seconds: 10));
137
    }
138
    rmTree(parent);
139 140
  }
}