test.dart 6.41 KB
Newer Older
1
import 'dart:async';
2
import 'dart:io';
3 4 5 6 7 8 9 10 11 12 13 14 15

import 'package:path/path.dart' as p;

String flutterRoot = p.dirname(p.dirname(p.dirname(p.fromUri(Platform.script))));
String flutter = p.join(flutterRoot, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter');
String dart = p.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'dart.exe' : 'dart');
String flutterTestArgs = Platform.environment['FLUTTER_TEST_ARGS'];

/// When you call this, you can set FLUTTER_TEST_ARGS to pass custom
/// arguments to flutter test. For example, you might want to call this
/// script using FLUTTER_TEST_ARGS=--local-engine=host_debug_unopt to
/// use your own build of the engine.
Future<Null> main() async {
16 17 18 19 20
  if (Platform.environment['SHARD'] == 'docs') {
    print('\x1B[32mDONE: test.dart does nothing in the docs shard.\x1B[0m');
  } else if (Platform.environment['SHARD'] == 'analyze') {
    // Analyze all the Dart code in the repo.
    await _runFlutterAnalyze(flutterRoot,
21
      options: <String>['--flutter-repo'],
22
    );
23

24 25 26 27 28 29 30 31 32 33 34 35
    await _runCmd(dart, <String>[p.join(flutterRoot, 'dev', 'tools', 'mega_gallery.dart')],
      workingDirectory: flutterRoot,
    );
    await _runFlutterAnalyze(p.join(flutterRoot, 'dev', 'benchmarks', 'mega_gallery'),
      options: <String>['--watch', '--benchmark'],
    );

    print('\x1B[32mDONE: Analysis successful.\x1B[0m');
  } else {
    // Verify that the tests actually return failure on failure and success on success.
    String automatedTests = p.join(flutterRoot, 'dev', 'automated_tests');
    await _runFlutterTest(automatedTests,
36 37 38
      script: p.join('test_smoke_test', 'fail_test.dart'),
      expectFailure: true,
      printOutput: false,
39 40
    );
    await _runFlutterTest(automatedTests,
41 42
      script: p.join('test_smoke_test', 'pass_test.dart'),
      printOutput: false,
43 44
    );
    await _runFlutterTest(automatedTests,
45 46 47
      script: p.join('test_smoke_test', 'crash1_test.dart'),
      expectFailure: true,
      printOutput: false,
48 49
    );
    await _runFlutterTest(automatedTests,
50 51 52
      script: p.join('test_smoke_test', 'crash2_test.dart'),
      expectFailure: true,
      printOutput: false,
53 54
    );
    await _runFlutterTest(automatedTests,
55 56 57
      script: p.join('test_smoke_test', 'syntax_error_test.broken_dart'),
      expectFailure: true,
      printOutput: false,
58 59
    );
    await _runFlutterTest(automatedTests,
60 61 62
      script: p.join('test_smoke_test', 'missing_import_test.broken_dart'),
      expectFailure: true,
      printOutput: false,
63 64
    );
    await _runCmd(flutter, <String>['drive', '--use-existing-app', '-t', p.join('test_driver', 'failure.dart')],
65 66 67
      workingDirectory: p.join(flutterRoot, 'packages', 'flutter_driver'),
      expectFailure: true,
      printOutput: false,
68
    );
69

70 71 72
    List<String> coverageFlags = <String>[];
    if (Platform.environment['TRAVIS'] != null && Platform.environment['TRAVIS_PULL_REQUEST'] == 'false')
      coverageFlags.add('--coverage');
73

74 75
    // Run tests.
    await _runFlutterTest(p.join(flutterRoot, 'packages', 'flutter'),
76
      options: coverageFlags,
77 78 79 80 81
    );
    await _runAllDartTests(p.join(flutterRoot, 'packages', 'flutter_driver'));
    await _runFlutterTest(p.join(flutterRoot, 'packages', 'flutter_test'));
    await _runFlutterTest(p.join(flutterRoot, 'packages', 'flutter_markdown'));
    await _runAllDartTests(p.join(flutterRoot, 'packages', 'flutter_tools'),
82
      environment: <String, String>{ 'FLUTTER_ROOT': flutterRoot },
83
    );
84

85 86 87 88 89 90
    await _runAllDartTests(p.join(flutterRoot, 'dev', 'devicelab'));
    await _runFlutterTest(p.join(flutterRoot, 'dev', 'manual_tests'));
    await _runFlutterTest(p.join(flutterRoot, 'examples', 'hello_world'));
    await _runFlutterTest(p.join(flutterRoot, 'examples', 'layers'));
    await _runFlutterTest(p.join(flutterRoot, 'examples', 'stocks'));
    await _runFlutterTest(p.join(flutterRoot, 'examples', 'flutter_gallery'));
91

92 93
    print('\x1B[32mDONE: All tests successful.\x1B[0m');
  }
94 95 96
}

Future<Null> _runCmd(String executable, List<String> arguments, {
97 98 99 100 101
  String workingDirectory,
  Map<String, String> environment,
  bool expectFailure: false,
  bool printOutput: true,
  bool skip: false,
102 103 104 105 106 107 108 109 110 111
}) async {
  String cmd = '${p.relative(executable)} ${arguments.join(' ')}';
  String relativeWorkingDir = p.relative(workingDirectory);
  if (skip) {
    _printProgress('SKIPPING', relativeWorkingDir, cmd);
    return null;
  }
  _printProgress('RUNNING', relativeWorkingDir, cmd);

  Process process = await Process.start(executable, arguments,
112 113
    workingDirectory: workingDirectory,
    environment: environment,
114 115 116 117 118 119 120 121 122 123
  );

  if (printOutput) {
    stdout.addStream(process.stdout);
    stderr.addStream(process.stderr);
  }

  int exitCode = await process.exitCode;
  if ((exitCode == 0) == expectFailure) {
    print(
124 125 126
      '\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m\n'
      '\x1B[1mERROR:\x1B[31m Last command exited with $exitCode (expected: ${expectFailure ? 'non-zero' : 'zero'}).\x1B[0m\n'
      '\x1B[31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1B[0m'
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    );
    exit(1);
  }
}

Future<Null> _runFlutterTest(String workingDirectory, {
    String script,
    bool expectFailure: false,
    bool printOutput: true,
    List<String> options: const <String>[],
    bool skip: false,
}) {
  List<String> args = <String>['test']..addAll(options);
  if (flutterTestArgs != null)
    args.add(flutterTestArgs);
  if (script != null)
    args.add(script);
  return _runCmd(flutter, args,
145 146 147 148
    workingDirectory: workingDirectory,
    expectFailure: expectFailure,
    printOutput: printOutput,
    skip: skip || Platform.isWindows, // TODO(goderbauer): run on Windows when sky_shell is available
149 150 151 152
  );
}

Future<Null> _runAllDartTests(String workingDirectory, {
153
  Map<String, String> environment,
154 155 156
}) {
  List<String> args = <String>['--checked', p.join('test', 'all.dart')];
  return _runCmd(dart, args,
157 158
    workingDirectory: workingDirectory,
    environment: environment,
159 160 161 162
  );
}

Future<Null> _runFlutterAnalyze(String workingDirectory, {
163
  List<String> options: const <String>[]
164 165
}) {
  return _runCmd(flutter, <String>['analyze']..addAll(options),
166
    workingDirectory: workingDirectory,
167 168 169 170 171 172
  );
}

void _printProgress(String action, String workingDir, String cmd) {
  print('>>> $action in \x1B[36m$workingDir\x1B[0m: \x1B[33m$cmd\x1B[0m');
}