common.dart 5.01 KB
Newer Older
1 2 3 4
// Copyright 2016 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 6
import 'dart:async';

7
import 'package:args/command_runner.dart';
8 9
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
import 'package:test/test.dart' as test_package show TypeMatcher;
10

11
import 'package:flutter_tools/src/base/common.dart';
12
import 'package:flutter_tools/src/base/file_system.dart';
13
import 'package:flutter_tools/src/base/platform.dart';
14
import 'package:flutter_tools/src/base/process.dart';
15
import 'package:flutter_tools/src/commands/create.dart';
16 17 18
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';

19 20 21 22 23 24
export 'package:test/test.dart' hide TypeMatcher, isInstanceOf; // Defines a 'package:test' shim.

/// A matcher that compares the type of the actual value to the type argument T.
// TODO(ianh): Remove this once https://github.com/dart-lang/matcher/issues/98 is fixed
Matcher isInstanceOf<T>() => new test_package.TypeMatcher<T>(); // ignore: prefer_const_constructors, https://github.com/dart-lang/sdk/issues/32544

25 26 27 28 29 30 31 32 33 34 35
void tryToDelete(Directory directory) {
  // This should not be necessary, but it turns out that
  // on Windows it's common for deletions to fail due to
  // bogus (we think) "access denied" errors.
  try {
    directory.deleteSync(recursive: true);
  } on FileSystemException catch (error) {
    print('Failed to delete ${directory.path}: $error');
  }
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/// Gets the path to the root of the Flutter repository.
///
/// This will first look for a `FLUTTER_ROOT` environment variable. If the
/// environment variable is set, it will be returned. Otherwise, this will
/// deduce the path from `platform.script`.
String getFlutterRoot() {
  if (platform.environment.containsKey('FLUTTER_ROOT'))
    return platform.environment['FLUTTER_ROOT'];

  Error invalidScript() => new StateError('Invalid script: ${platform.script}');

  Uri scriptUri;
  switch (platform.script.scheme) {
    case 'file':
      scriptUri = platform.script;
      break;
    case 'data':
53 54
      final RegExp flutterTools = new RegExp(r'(file://[^"]*[/\\]flutter_tools[/\\][^"]+\.dart)', multiLine: true);
      final Match match = flutterTools.firstMatch(Uri.decodeFull(platform.script.path));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      if (match == null)
        throw invalidScript();
      scriptUri = Uri.parse(match.group(1));
      break;
    default:
      throw invalidScript();
  }

  final List<String> parts = fs.path.split(fs.path.fromUri(scriptUri));
  final int toolsIndex = parts.indexOf('flutter_tools');
  if (toolsIndex == -1)
    throw invalidScript();
  final String toolsPath = fs.path.joinAll(parts.sublist(0, toolsIndex + 1));
  return fs.path.normalize(fs.path.join(toolsPath, '..', '..'));
}

71
CommandRunner<Null> createTestCommandRunner([FlutterCommand command]) {
72
  final FlutterCommandRunner runner = new FlutterCommandRunner();
73 74 75
  if (command != null)
    runner.addCommand(command);
  return runner;
76
}
77 78 79 80 81

/// Updates [path] to have a modification time [seconds] from now.
void updateFileModificationTime(String path,
                                DateTime baseTime,
                                int seconds) {
82
  final DateTime modificationTime = baseTime.add(new Duration(seconds: seconds));
83
  fs.file(path).setLastModifiedSync(modificationTime);
84
}
85

86
/// Matcher for functions that throw [ToolExit].
87 88 89 90 91 92 93
Matcher throwsToolExit({int exitCode, String message}) {
  Matcher matcher = isToolExit;
  if (exitCode != null)
    matcher = allOf(matcher, (ToolExit e) => e.exitCode == exitCode);
  if (message != null)
    matcher = allOf(matcher, (ToolExit e) => e.message.contains(message));
  return throwsA(matcher);
94 95 96
}

/// Matcher for [ToolExit]s.
97
final Matcher isToolExit = isInstanceOf<ToolExit>();
98 99 100 101 102 103 104 105 106

/// Matcher for functions that throw [ProcessExit].
Matcher throwsProcessExit([dynamic exitCode]) {
  return exitCode == null
      ? throwsA(isProcessExit)
      : throwsA(allOf(isProcessExit, (ProcessExit e) => e.exitCode == exitCode));
}

/// Matcher for [ProcessExit]s.
107
final Matcher isProcessExit = isInstanceOf<ProcessExit>();
108

109 110
/// Creates a flutter project in the [temp] directory using the
/// [arguments] list if specified, or `--no-pub` if not.
111
/// Returns the path to the flutter project.
112 113
Future<String> createProject(Directory temp, {List<String> arguments}) async {
  arguments ??= <String>['--no-pub'];
114 115 116
  final String projectPath = fs.path.join(temp.path, 'flutter_project');
  final CreateCommand command = new CreateCommand();
  final CommandRunner<Null> runner = createTestCommandRunner(command);
117
  await runner.run(<String>['create']..addAll(arguments)..add(projectPath));
118
  return projectPath;
119 120 121
}

/// Test case timeout for tests involving remote calls to `pub get` or similar.
122
const Timeout allowForRemotePubInvocation = Timeout.factor(10.0);
123 124 125

/// Test case timeout for tests involving creating a Flutter project with
/// `--no-pub`. Use [allowForRemotePubInvocation] when creation involves `pub`.
126
const Timeout allowForCreateFlutterProject = Timeout.factor(3.0);