test.dart 11.7 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6
import 'dart:io';
7

8
import 'package:path/path.dart' as path;
9 10

import 'run_command.dart';
11

12
typedef ShardRunner = Future<void> Function();
13

14 15 16 17
final String flutterRoot = path.dirname(path.dirname(path.dirname(path.fromUri(Platform.script))));
final String flutter = path.join(flutterRoot, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter');
final String dart = path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'dart.exe' : 'dart');
final String pub = path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'pub.bat' : 'pub');
18
final String pubCache = path.join(flutterRoot, '.pub-cache');
19
final List<String> flutterTestArgs = <String>[];
20

21
const Map<String, ShardRunner> _kShards = <String, ShardRunner>{
22
  'tests': _runTests,
23
  'tool_tests': _runToolTests,
24
  'aot_build_tests': _runAotBuildTests,
25 26 27
  'coverage': _runCoverage,
};

28 29
const Duration _kLongTimeout = Duration(minutes: 45);
const Duration _kShortTimeout = Duration(minutes: 5);
30

31
/// When you call this, you can pass additional arguments to pass custom
32
/// arguments to flutter test. For example, you might want to call this
33
/// script with the parameter --local-engine=host_debug_unopt to
34
/// use your own build of the engine.
35
///
36
/// To run the tool_tests part, run it with SHARD=tool_tests
37 38
///
/// For example:
39
/// SHARD=tool_tests bin/cache/dart-sdk/bin/dart dev/bots/test.dart
40
/// bin/cache/dart-sdk/bin/dart dev/bots/test.dart --local-engine=host_debug_unopt
41
Future<void> main(List<String> args) async {
42 43
  flutterTestArgs.addAll(args);

44 45
  final String shard = Platform.environment['SHARD'];
  if (shard != null) {
46 47 48 49 50
    if (!_kShards.containsKey(shard)) {
      print('Invalid shard: $shard');
      print('The available shards are: ${_kShards.keys.join(", ")}');
      exit(1);
    }
51
    print('${bold}SHARD=$shard$reset');
52 53 54 55 56
    await _kShards[shard]();
  } else {
    for (String currentShard in _kShards.keys) {
      print('${bold}SHARD=$currentShard$reset');
      await _kShards[currentShard]();
57
      print('');
58 59
    }
  }
60 61
}

62
Future<void> _runSmokeTests() async {
63 64
  // Verify that the tests actually return failure on failure and success on
  // success.
65
  final String automatedTests = path.join(flutterRoot, 'dev', 'automated_tests');
66 67 68
  // We run the "pass" and "fail" smoke tests first, and alone, because those
  // are particularly critical and sensitive. If one of these fails, there's no
  // point even trying the others.
69 70 71
  await _runFlutterTest(automatedTests,
    script: path.join('test_smoke_test', 'pass_test.dart'),
    printOutput: false,
72
    timeout: _kShortTimeout,
73 74
  );
  await _runFlutterTest(automatedTests,
75
    script: path.join('test_smoke_test', 'fail_test.dart'),
76 77
    expectFailure: true,
    printOutput: false,
78
    timeout: _kShortTimeout,
79
  );
80
  // We run the timeout tests individually because they are timing-sensitive.
81
  await _runFlutterTest(automatedTests,
82 83
    script: path.join('test_smoke_test', 'timeout_pass_test.dart'),
    expectFailure: false,
84
    printOutput: false,
85
    timeout: _kShortTimeout,
86
  );
87
  await _runFlutterTest(automatedTests,
88
    script: path.join('test_smoke_test', 'timeout_fail_test.dart'),
89 90 91 92
    expectFailure: true,
    printOutput: false,
    timeout: _kShortTimeout,
  );
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  // We run the remaining smoketests in parallel, because they each take some
  // time to run (e.g. compiling), so we don't want to run them in series,
  // especially on 20-core machines...
  await Future.wait<void>(
    <Future<void>>[
      _runFlutterTest(automatedTests,
        script: path.join('test_smoke_test', 'crash1_test.dart'),
        expectFailure: true,
        printOutput: false,
        timeout: _kShortTimeout,
      ),
      _runFlutterTest(automatedTests,
        script: path.join('test_smoke_test', 'crash2_test.dart'),
        expectFailure: true,
        printOutput: false,
        timeout: _kShortTimeout,
      ),
      _runFlutterTest(automatedTests,
        script: path.join('test_smoke_test', 'syntax_error_test.broken_dart'),
        expectFailure: true,
        printOutput: false,
        timeout: _kShortTimeout,
      ),
      _runFlutterTest(automatedTests,
        script: path.join('test_smoke_test', 'missing_import_test.broken_dart'),
        expectFailure: true,
        printOutput: false,
        timeout: _kShortTimeout,
      ),
      _runFlutterTest(automatedTests,
        script: path.join('test_smoke_test', 'disallow_error_reporter_modification_test.dart'),
        expectFailure: true,
        printOutput: false,
        timeout: _kShortTimeout,
      ),
128
      runCommand(flutter,
129 130
        <String>['drive', '--use-existing-app', '-t', path.join('test_driver', 'failure.dart')],
        workingDirectory: path.join(flutterRoot, 'packages', 'flutter_driver'),
131
        expectNonZeroExit: true,
132 133 134 135
        printOutput: false,
        timeout: _kShortTimeout,
      ),
    ],
136 137
  );

138 139
  // Verify that we correctly generated the version file.
  await _verifyVersion(path.join(flutterRoot, 'version'));
140 141
}

142
Future<void> _runToolTests() async {
143 144
  await _runSmokeTests();

145 146 147 148
  await _pubRunTest(
    path.join(flutterRoot, 'packages', 'flutter_tools'),
    enableFlutterToolAsserts: true,
  );
149 150 151 152

  print('${bold}DONE: All tests successful.$reset');
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/// Verifies that AOT builds of some examples apps finish
/// without crashing. It does not actually launch the AOT-built
/// apps. That happens later in the devicelab. This is just
/// a smoke-test.
Future<void> _runAotBuildTests() async {
  await _flutterBuildAot(path.join('examples', 'hello_world'));
  await _flutterBuildAot(path.join('examples', 'flutter_gallery'));
  await _flutterBuildAot(path.join('examples', 'flutter_view'));

  print('${bold}DONE: All AOT build tests successful.$reset');
}

Future<void> _flutterBuildAot(String relativePathToApplication) {
  return runCommand(flutter,
    <String>['build', 'aot'],
    workingDirectory: path.join(flutterRoot, relativePathToApplication),
    expectNonZeroExit: false,
    timeout: _kShortTimeout,
  );
}

174
Future<void> _runTests() async {
175
  await _runSmokeTests();
176

177
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter'));
178 179 180 181 182
  // Only packages/flutter/test/widgets/widget_inspector_test.dart really
  // needs to be run with --track-widget-creation but it is nice to run
  // all of the tests in package:flutter with the flag to ensure that
  // the Dart kernel transformer triggered by the flag does not break anything.
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter'), options: <String>['--track-widget-creation']);
183 184 185 186
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_localizations'));
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_driver'));
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_test'));
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'fuchsia_remote_debug_protocol'));
187
  await _pubRunTest(path.join(flutterRoot, 'dev', 'bots'));
188
  await _pubRunTest(path.join(flutterRoot, 'dev', 'devicelab'));
189
  await _pubRunTest(path.join(flutterRoot, 'dev', 'snippets'));
190
  await _runFlutterTest(path.join(flutterRoot, 'dev', 'integration_tests', 'android_semantics_testing'));
191 192 193 194 195 196
  await _runFlutterTest(path.join(flutterRoot, 'dev', 'manual_tests'));
  await _runFlutterTest(path.join(flutterRoot, 'dev', 'tools', 'vitool'));
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'hello_world'));
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'layers'));
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'stocks'));
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'flutter_gallery'));
197 198 199
  // Regression test to ensure that code outside of package:flutter can run
  // with --track-widget-creation.
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'flutter_gallery'), options: <String>['--track-widget-creation']);
200
  await _runFlutterTest(path.join(flutterRoot, 'examples', 'catalog'));
201 202 203 204

  print('${bold}DONE: All tests successful.$reset');
}

205
Future<void> _runCoverage() async {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  final File coverageFile = File(path.join(flutterRoot, 'packages', 'flutter', 'coverage', 'lcov.info'));
  if (!coverageFile.existsSync()) {
    print('${red}Coverage file not found.$reset');
    print('Expected to find: ${coverageFile.absolute}');
    print('This file is normally obtained by running `flutter update-packages`.');
    exit(1);
  }
  coverageFile.deleteSync();
  await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter'),
    options: const <String>['--coverage'],
  );
  if (!coverageFile.existsSync()) {
    print('${red}Coverage file not found.$reset');
    print('Expected to find: ${coverageFile.absolute}');
    print('This file should have been generated by the `flutter test --coverage` script, but was not.');
    exit(1);
  }

  print('${bold}DONE: Coverage collection successful.$reset');
225 226
}

227
Future<void> _pubRunTest(
228 229
  String workingDirectory, {
  String testPath,
230
  bool enableFlutterToolAsserts = false
231
}) {
232
  final List<String> args = <String>['run', 'test', '-rcompact', '-j1'];
233 234
  if (!hasColor)
    args.add('--no-color');
235 236
  if (testPath != null)
    args.add(testPath);
237
  final Map<String, String> pubEnvironment = <String, String>{};
238
  if (Directory(pubCache).existsSync()) {
239 240
    pubEnvironment['PUB_CACHE'] = pubCache;
  }
241 242 243 244 245 246 247 248
  if (enableFlutterToolAsserts) {
    // If an existing env variable exists append to it, but only if
    // it doesn't appear to already include enable-asserts.
    String toolsArgs = Platform.environment['FLUTTER_TOOL_ARGS'] ?? '';
    if (!toolsArgs.contains('--enable-asserts'))
        toolsArgs += ' --enable-asserts';
    pubEnvironment['FLUTTER_TOOL_ARGS'] = toolsArgs.trim();
  }
249
  return runCommand(
250 251 252 253
    pub, args,
    workingDirectory: workingDirectory,
    environment: pubEnvironment,
  );
254 255
}

256 257 258 259
class EvalResult {
  EvalResult({
    this.stdout,
    this.stderr,
260
    this.exitCode = 0,
261 262 263 264
  });

  final String stdout;
  final String stderr;
265
  final int exitCode;
266 267
}

268
Future<void> _runFlutterTest(String workingDirectory, {
269
  String script,
270 271 272 273 274
  bool expectFailure = false,
  bool printOutput = true,
  List<String> options = const <String>[],
  bool skip = false,
  Duration timeout = _kLongTimeout,
275
}) {
276
  final List<String> args = <String>['test']..addAll(options);
277
  if (flutterTestArgs != null && flutterTestArgs.isNotEmpty)
278
    args.addAll(flutterTestArgs);
279 280 281 282 283 284 285 286 287 288 289 290
  if (script != null) {
    final String fullScriptPath = path.join(workingDirectory, script);
    if (!FileSystemEntity.isFileSync(fullScriptPath)) {
      print('Could not find test: $fullScriptPath');
      print('Working directory: $workingDirectory');
      print('Script: $script');
      if (!printOutput)
        print('This is one of the tests that does not normally print output.');
      if (skip)
        print('This is one of the tests that is normally skipped in this configuration.');
      exit(1);
    }
291
    args.add(script);
292
  }
293
  return runCommand(flutter, args,
294
    workingDirectory: workingDirectory,
295
    expectNonZeroExit: expectFailure,
296
    printOutput: printOutput,
297
    skip: skip,
298
    timeout: timeout,
299 300 301
  );
}

302
Future<void> _verifyVersion(String filename) async {
303
  if (!File(filename).existsSync()) {
304
    print('$redLine');
305
    print('The version logic failed to create the Flutter version file.');
306
    print('$redLine');
307 308
    exit(1);
  }
309
  final String version = await File(filename).readAsString();
310
  if (version == '0.0.0-unknown') {
311
    print('$redLine');
312
    print('The version logic failed to determine the Flutter version.');
313
    print('$redLine');
314 315
    exit(1);
  }
316
  final RegExp pattern = RegExp(r'^[0-9]+\.[0-9]+\.[0-9]+(-pre\.[0-9]+)?$');
317
  if (!version.contains(pattern)) {
318
    print('$redLine');
319
    print('The version logic generated an invalid version string.');
320
    print('$redLine');
321 322
    exit(1);
  }
323
}