fuchsia_tester.dart 3.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2015 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.

import 'dart:async';

import 'package:args/args.dart';
import 'package:process/process.dart';
import 'package:test/src/executable.dart'
    as test; // ignore: implementation_imports

import '../lib/src/base/common.dart';
import '../lib/src/base/config.dart';
import '../lib/src/base/context.dart';
import '../lib/src/base/file_system.dart';
import '../lib/src/base/io.dart';
import '../lib/src/base/logger.dart';
import '../lib/src/base/os.dart';
import '../lib/src/base/platform.dart';
20
import '../lib/src/base/terminal.dart';
21 22
import '../lib/src/cache.dart';
import '../lib/src/dart/package_map.dart';
23
import '../lib/src/disabled_usage.dart';
24 25 26 27 28 29
import '../lib/src/globals.dart';
import '../lib/src/test/flutter_platform.dart' as loader;
import '../lib/src/usage.dart';

// Note: this was largely inspired by lib/src/commands/test.dart.

30 31 32
const String _kOptionPackages = 'packages';
const String _kOptionShell = 'shell';
const String _kOptionTestDirectory = 'test-directory';
33 34 35 36 37 38 39 40 41
const List<String> _kRequiredOptions = const <String>[
  _kOptionPackages,
  _kOptionShell,
  _kOptionTestDirectory,
];

Future<Null> main(List<String> args) async {
  final AppContext executableContext = new AppContext();
  executableContext.setVariable(Logger, new StdoutLogger());
42
  await executableContext.runInZone(() {
43
    // Initialize the context with some defaults.
44 45
    // This list must be kept in sync with lib/executable.dart.
    context.putIfAbsent(Stdio, () => const Stdio());
46 47 48
    context.putIfAbsent(Platform, () => const LocalPlatform());
    context.putIfAbsent(FileSystem, () => const LocalFileSystem());
    context.putIfAbsent(ProcessManager, () => const LocalProcessManager());
49
    context.putIfAbsent(AnsiTerminal, () => new AnsiTerminal());
50 51 52 53
    context.putIfAbsent(Logger, () => new StdoutLogger());
    context.putIfAbsent(Cache, () => new Cache());
    context.putIfAbsent(Config, () => new Config());
    context.putIfAbsent(OperatingSystemUtils, () => new OperatingSystemUtils());
54
    context.putIfAbsent(Usage, () => new DisabledUsage());
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    return run(args);
  });
}

Iterable<String> _findTests(Directory directory) {
  return directory
      .listSync(recursive: true, followLinks: false)
      .where((FileSystemEntity entity) =>
          entity.path.endsWith('_test.dart') && fs.isFileSync(entity.path))
      .map((FileSystemEntity entity) => fs.path.absolute(entity.path));
}

Future<Null> run(List<String> args) async {
  final ArgParser parser = new ArgParser()
    ..addOption(_kOptionPackages, help: 'The .packages file')
    ..addOption(_kOptionShell, help: 'The Flutter shell binary')
71
    ..addOption(_kOptionTestDirectory, help: 'Directory containing the tests');
72 73 74 75 76 77
  final ArgResults argResults = parser.parse(args);
  if (_kRequiredOptions
      .any((String option) => !argResults.options.contains(option))) {
    printError('Missing option! All options must be specified.');
    exit(1);
  }
78 79 80 81 82 83 84
  final Directory tempDirectory =
      fs.systemTempDirectory.createTempSync('fuchsia_tester');
  try {
    Cache.flutterRoot = tempDirectory.path;
    final Directory testDirectory =
        fs.directory(argResults[_kOptionTestDirectory]);
    final Iterable<String> tests = _findTests(testDirectory);
85

86 87 88
    final List<String> testArgs = <String>[];
    testArgs.add('--');
    testArgs.addAll(tests);
89

90 91 92 93 94 95 96
    final String shellPath = argResults[_kOptionShell];
    if (!fs.isFileSync(shellPath)) {
      throwToolExit('Cannot find Flutter shell at $shellPath');
    }
    loader.installHook(
      shellPath: shellPath,
    );
97

98 99 100
    PackageMap.globalPackagesPath =
        fs.path.normalize(fs.path.absolute(argResults[_kOptionPackages]));
    fs.currentDirectory = testDirectory;
101

102
    await test.main(testArgs);
103
    exit(exitCode);
104 105
  } finally {
    tempDirectory.deleteSync(recursive: true);
106 107
  }
}