test_test.dart 13.6 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/artifacts.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/io.dart';
11
import 'package:flutter_tools/src/cache.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
  testUsingContext('flutter test should run a test with a given tag', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag', automatedTestsDirectory, flutterTestDirectory,
        extraArguments: const <String>['--tags', 'include-tag']);
    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);
  });

  testUsingContext('flutter test should not run a test with excluded tag', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag', automatedTestsDirectory, flutterTestDirectory,
        extraArguments: const <String>['--exclude-tags', 'exclude-tag']);
    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);
  });

  testUsingContext('flutter test should run all tests when tags are unspecified', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag', automatedTestsDirectory, flutterTestDirectory);
    if (!(result.stdout as String).contains('+1 -1: Some tests failed')) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    expect(result.exitCode, 1);
  });

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  testUsingContext('flutter test should run a widgetTest with a given tag', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory,
        extraArguments: const <String>['--tags', 'include-tag']);
    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);
  });

  testUsingContext('flutter test should not run a widgetTest with excluded tag', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory,
        extraArguments: const <String>['--exclude-tags', 'exclude-tag']);
    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);
  });

  testUsingContext('flutter test should run all widgetTest when tags are unspecified', () async {
    Cache.flutterRoot = '../..';
    final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory);
    if (!(result.stdout as String).contains('+1 -1: Some tests failed')) {
      fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
    }
    expect(result.exitCode, 1);
  });

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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);
191
  });
192 193
}

194
Future<void> _testFile(
195 196 197 198 199 200
  String testName,
  String workingDirectory,
  String testDirectory, {
  Matcher exitCode,
  List<String> extraArguments = const <String>[],
}) async {
201
  exitCode ??= isNonZero;
202 203
  final String fullTestExpectation = globals.fs.path.join(testDirectory, '${testName}_expectation.txt');
  final File expectationFile = globals.fs.file(fullTestExpectation);
204
  if (!expectationFile.existsSync()) {
205
    fail('missing expectation file: $expectationFile');
206
  }
207

208
  while (_testExclusionLock != null) {
209
    await _testExclusionLock;
210
  }
211

212 213 214 215 216 217
  final ProcessResult exec = await _runFlutterTest(
    testName,
    workingDirectory,
    testDirectory,
    extraArguments: extraArguments,
  );
218

219
  expect(exec.exitCode, exitCode);
220
  final List<String> output = (exec.stdout as String).split('\n');
221
  if (output.first.startsWith('Waiting for another flutter command to release the startup lock...')) {
222
    output.removeAt(0);
223 224
  }
  if (output.first.startsWith('Running "flutter pub get" in')) {
225
    output.removeAt(0);
226
  }
227
  output.add('<<stderr>>');
228
  output.addAll((exec.stderr as String).split('\n'));
229
  final List<String> expectations = globals.fs.file(fullTestExpectation).readAsLinesSync();
230 231 232
  bool allowSkip = false;
  int expectationLineNumber = 0;
  int outputLineNumber = 0;
233
  bool haveSeenStdErrMarker = false;
234
  while (expectationLineNumber < expectations.length) {
235 236 237 238 239
    expect(
      output,
      hasLength(greaterThan(outputLineNumber)),
      reason: 'Failure in $testName to compare to $fullTestExpectation',
    );
240
    final String expectationLine = expectations[expectationLineNumber];
241
    String outputLine = output[outputLineNumber];
242 243 244 245 246 247
    if (expectationLine == '<<skip until matching line>>') {
      allowSkip = true;
      expectationLineNumber += 1;
      continue;
    }
    if (allowSkip) {
248
      if (!RegExp(expectationLine).hasMatch(outputLine)) {
249 250 251 252 253
        outputLineNumber += 1;
        continue;
      }
      allowSkip = false;
    }
254 255 256 257
    if (expectationLine == '<<stderr>>') {
      expect(haveSeenStdErrMarker, isFalse);
      haveSeenStdErrMarker = true;
    }
258 259 260 261 262 263 264 265 266 267 268 269
    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;
      }
    }

270
    expect(outputLine, matches(expectationLine), reason: 'Full output:\n- - - -----8<----- - - -\n${output.join("\n")}\n- - - -----8<----- - - -');
271 272 273 274
    expectationLineNumber += 1;
    outputLineNumber += 1;
  }
  expect(allowSkip, isFalse);
275
  if (!haveSeenStdErrMarker) {
276
    expect(exec.stderr, '');
277
  }
278
}
279

280 281 282 283
Future<ProcessResult> _runFlutterTest(
  String testName,
  String workingDirectory,
  String testDirectory, {
284
  List<String> extraArguments = const <String>[],
285 286
}) async {

287 288 289 290
  String testPath;
  if (testName == null) {
    // Test everything in the directory.
    testPath = testDirectory;
291
    final Directory directoryToTest = globals.fs.directory(testPath);
292
    if (!directoryToTest.existsSync()) {
293
      fail('missing test directory: $directoryToTest');
294
    }
295 296
  } else {
    // Test just a specific test file.
297 298
    testPath = globals.fs.path.join(testDirectory, '${testName}_test.dart');
    final File testFile = globals.fs.file(testPath);
299
    if (!testFile.existsSync()) {
300
      fail('missing test file: $testFile');
301
    }
302
  }
303

304
  final List<String> args = <String>[
305
    globals.fs.path.absolute(globals.fs.path.join('bin', 'flutter_tools.dart')),
306 307
    'test',
    '--no-color',
308
    '--no-version-check',
309
    ...extraArguments,
310
    testPath,
311
  ];
312

313
  while (_testExclusionLock != null) {
314
    await _testExclusionLock;
315
  }
316

317
  final Completer<void> testExclusionCompleter = Completer<void>();
318 319 320
  _testExclusionLock = testExclusionCompleter.future;
  try {
    return await Process.run(
321
      globals.artifacts.getArtifactPath(Artifact.engineDartBinary),
322 323
      args,
      workingDirectory: workingDirectory,
324 325
      stdoutEncoding: utf8,
      stderrEncoding: utf8,
326 327 328 329 330 331
    );
  } finally {
    _testExclusionLock = null;
    testExclusionCompleter.complete();
  }
}