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

import 'dart:async';
6
import 'dart:convert';
7

8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/io.dart';
10 11
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/dart/sdk.dart';
12
import 'package:flutter_tools/src/globals.dart' as globals;
13

14 15
import '../../src/common.dart';
import '../../src/context.dart';
16 17 18

// This test depends on some files in ///dev/automated_tests/flutter_test/*

19
Future<void> _testExclusionLock;
20

21
void main() {
22 23
  final String automatedTestsDirectory = globals.fs.path.join('..', '..', 'dev', 'automated_tests');
  final String flutterTestDirectory = globals.fs.path.join(automatedTestsDirectory, 'flutter_test');
24

25 26 27 28
  testUsingContext('flutter test should not have extraneous error messages', () async {
    Cache.flutterRoot = '../..';
    return _testFile('trivial_widget', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
  });
29

30 31 32 33
  testUsingContext('flutter test should report nice errors for exceptions thrown within testWidgets()', () async {
    Cache.flutterRoot = '../..';
    return _testFile('exception_handling', automatedTestsDirectory, flutterTestDirectory);
  });
34

35 36 37 38
  testUsingContext('flutter test should report a nice error when a guarded function was called without await', () async {
    Cache.flutterRoot = '../..';
    return _testFile('test_async_utils_guarded', automatedTestsDirectory, flutterTestDirectory);
  });
39

40 41 42 43
  testUsingContext('flutter test should report a nice error when an async function was called without await', () async {
    Cache.flutterRoot = '../..';
    return _testFile('test_async_utils_unguarded', automatedTestsDirectory, flutterTestDirectory);
  });
44

45 46 47 48
  testUsingContext('flutter test should report a nice error when a Ticker is left running', () async {
    Cache.flutterRoot = '../..';
    return _testFile('ticker', automatedTestsDirectory, flutterTestDirectory);
  });
49

50
  testUsingContext('flutter test should report a nice error when a pubspec.yaml is missing a flutter_test dependency', () async {
51
    final String missingDependencyTests = globals.fs.path.join('..', '..', 'dev', 'missing_dependency_tests');
52 53 54
    Cache.flutterRoot = '../..';
    return _testFile('trivial', missingDependencyTests, missingDependencyTests);
  });
55

56 57 58
  testUsingContext('flutter test should report which user created widget caused the error', () async {
    Cache.flutterRoot = '../..';
    return _testFile('print_user_created_ancestor', automatedTestsDirectory, flutterTestDirectory,
59
        extraArguments: const <String>['--track-widget-creation']);
60
  });
61

62 63 64 65 66
  testUsingContext('flutter test should report which user created widget caused the error - no flag', () async {
    Cache.flutterRoot = '../..';
    return _testFile('print_user_created_ancestor_no_flag', automatedTestsDirectory, flutterTestDirectory,
       extraArguments: const <String>['--no-track-widget-creation']);
  });
67

68 69 70 71 72
  testUsingContext('flutter test should report correct created widget caused the error', () async {
    Cache.flutterRoot = '../..';
    return _testFile('print_correct_local_widget', automatedTestsDirectory, flutterTestDirectory,
      extraArguments: const <String>['--track-widget-creation']);
  });
73

74 75 76 77
  testUsingContext('flutter test should can load assets within its own package', () async {
    Cache.flutterRoot = '../..';
    return _testFile('package_assets', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
  });
78

79 80 81 82 83 84 85 86 87
  testUsingContext('flutter test should run a test when its name matches a regexp', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering', automatedTestsDirectory, flutterTestDirectory,
      extraArguments: const <String>['--name', 'inc.*de']);
    if (!(result.stdout as String).contains('+1: All tests passed')) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    expect(result.exitCode, 0);
  });
88

89 90 91 92 93 94 95 96 97
  testUsingContext('flutter test should run a test when its name contains a string', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering', automatedTestsDirectory, flutterTestDirectory,
      extraArguments: const <String>['--plain-name', 'include']);
    if (!(result.stdout as String).contains('+1: All tests passed')) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    expect(result.exitCode, 0);
  });
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 128 129 130 131 132
  testUsingContext('flutter test should test runs to completion', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('trivial', automatedTestsDirectory, flutterTestDirectory,
      extraArguments: const <String>['--verbose']);
    final String stdout = result.stdout as String;
    if ((!stdout.contains('+1: All tests passed')) ||
        (!stdout.contains('test 0: starting shell process')) ||
        (!stdout.contains('test 0: deleting temporary directory')) ||
        (!stdout.contains('test 0: finished')) ||
        (!stdout.contains('test package returned with exit code 0'))) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    if ((result.stderr as String).isNotEmpty) {
      fail('unexpected error output from test:\n\n${result.stderr}\n-- end stderr --\n\n');
    }
    expect(result.exitCode, 0);
  });

  testUsingContext('flutter test should run all tests inside of a directory with no trailing slash', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest(null, automatedTestsDirectory, flutterTestDirectory + '/child_directory',
      extraArguments: const <String>['--verbose']);
    final String stdout = result.stdout as String;
    if ((!stdout.contains('+2: All tests passed')) ||
        (!stdout.contains('test 0: starting shell process')) ||
        (!stdout.contains('test 0: deleting temporary directory')) ||
        (!stdout.contains('test 0: finished')) ||
        (!stdout.contains('test package returned with exit code 0'))) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    if ((result.stderr as String).isNotEmpty) {
      fail('unexpected error output from test:\n\n${result.stderr}\n-- end stderr --\n\n');
    }
    expect(result.exitCode, 0);
133
  });
134 135
}

136
Future<void> _testFile(
137 138 139 140 141 142
  String testName,
  String workingDirectory,
  String testDirectory, {
  Matcher exitCode,
  List<String> extraArguments = const <String>[],
}) async {
143
  exitCode ??= isNonZero;
144 145
  final String fullTestExpectation = globals.fs.path.join(testDirectory, '${testName}_expectation.txt');
  final File expectationFile = globals.fs.file(fullTestExpectation);
146
  if (!expectationFile.existsSync()) {
147
    fail('missing expectation file: $expectationFile');
148
  }
149

150
  while (_testExclusionLock != null) {
151
    await _testExclusionLock;
152
  }
153

154 155 156 157 158 159
  final ProcessResult exec = await _runFlutterTest(
    testName,
    workingDirectory,
    testDirectory,
    extraArguments: extraArguments,
  );
160

161
  expect(exec.exitCode, exitCode);
162
  final List<String> output = (exec.stdout as String).split('\n');
163
  if (output.first == 'Waiting for another flutter command to release the startup lock...') {
164
    output.removeAt(0);
165 166
  }
  if (output.first.startsWith('Running "flutter pub get" in')) {
167
    output.removeAt(0);
168
  }
169
  output.add('<<stderr>>');
170
  output.addAll((exec.stderr as String).split('\n'));
171
  final List<String> expectations = globals.fs.file(fullTestExpectation).readAsLinesSync();
172 173 174
  bool allowSkip = false;
  int expectationLineNumber = 0;
  int outputLineNumber = 0;
175
  bool haveSeenStdErrMarker = false;
176
  while (expectationLineNumber < expectations.length) {
177 178 179 180 181
    expect(
      output,
      hasLength(greaterThan(outputLineNumber)),
      reason: 'Failure in $testName to compare to $fullTestExpectation',
    );
182
    final String expectationLine = expectations[expectationLineNumber];
183
    String outputLine = output[outputLineNumber];
184 185 186 187 188 189
    if (expectationLine == '<<skip until matching line>>') {
      allowSkip = true;
      expectationLineNumber += 1;
      continue;
    }
    if (allowSkip) {
190
      if (!RegExp(expectationLine).hasMatch(outputLine)) {
191 192 193 194 195
        outputLineNumber += 1;
        continue;
      }
      allowSkip = false;
    }
196 197 198 199
    if (expectationLine == '<<stderr>>') {
      expect(haveSeenStdErrMarker, isFalse);
      haveSeenStdErrMarker = true;
    }
200 201 202 203 204 205 206 207 208 209 210 211
    if (!RegExp(expectationLine).hasMatch(outputLine) && outputLineNumber + 1 < output.length) {
      // Check if the RegExp can match the next two lines in the output so
      // that it is possible to write expectations that still hold even if a
      // line is wrapped slightly differently due to for example a file name
      // being longer on one platform than another.
      final String mergedLines = '$outputLine\n${output[outputLineNumber+1]}';
      if (RegExp(expectationLine).hasMatch(mergedLines)) {
        outputLineNumber += 1;
        outputLine = mergedLines;
      }
    }

212
    expect(outputLine, matches(expectationLine), reason: 'Full output:\n- - - -----8<----- - - -\n${output.join("\n")}\n- - - -----8<----- - - -');
213 214 215 216
    expectationLineNumber += 1;
    outputLineNumber += 1;
  }
  expect(allowSkip, isFalse);
217
  if (!haveSeenStdErrMarker) {
218
    expect(exec.stderr, '');
219
  }
220
}
221

222 223 224 225
Future<ProcessResult> _runFlutterTest(
  String testName,
  String workingDirectory,
  String testDirectory, {
226
  List<String> extraArguments = const <String>[],
227 228
}) async {

229 230 231 232
  String testPath;
  if (testName == null) {
    // Test everything in the directory.
    testPath = testDirectory;
233
    final Directory directoryToTest = globals.fs.directory(testPath);
234
    if (!directoryToTest.existsSync()) {
235
      fail('missing test directory: $directoryToTest');
236
    }
237 238
  } else {
    // Test just a specific test file.
239 240
    testPath = globals.fs.path.join(testDirectory, '${testName}_test.dart');
    final File testFile = globals.fs.file(testPath);
241
    if (!testFile.existsSync()) {
242
      fail('missing test file: $testFile');
243
    }
244
  }
245

246 247
  final List<String> args = <String>[
    ...dartVmFlags,
248
    globals.fs.path.absolute(globals.fs.path.join('bin', 'flutter_tools.dart')),
249 250
    'test',
    '--no-color',
251
    ...extraArguments,
252
    testPath,
253
  ];
254

255
  while (_testExclusionLock != null) {
256
    await _testExclusionLock;
257
  }
258

259
  final Completer<void> testExclusionCompleter = Completer<void>();
260 261 262
  _testExclusionLock = testExclusionCompleter.future;
  try {
    return await Process.run(
263
      globals.fs.path.join(dartSdkPath, 'bin', 'dart'),
264 265
      args,
      workingDirectory: workingDirectory,
266 267
      stdoutEncoding: utf8,
      stderrEncoding: utf8,
268 269 270 271 272 273
    );
  } finally {
    _testExclusionLock = null;
    testExclusionCompleter.complete();
  }
}