pub_get_test.dart 8.85 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'dart:async';

import 'package:file/file.dart';
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/cache.dart';
10
import 'package:flutter_tools/src/base/context.dart';
11
import 'package:flutter_tools/src/base/io.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/dart/pub.dart';
14

15 16 17 18 19
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:quiver/testing/async.dart';
import 'package:test/test.dart';

20
import '../src/common.dart';
21 22 23
import '../src/context.dart';

void main() {
24 25 26 27
  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

28 29
  testUsingContext('pub get 69', () async {
    String error;
30 31 32

    final MockProcessManager processMock = context.getVariable(ProcessManager);

33
    new FakeAsync().run((FakeAsync time) {
34
      expect(processMock.lastPubEnvironmment, isNull);
35
      pubGet(context: PubContext.flutterTests, checkLastModified: false).then((Null value) {
36
        error = 'test completed unexpectedly';
37 38
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
39 40 41 42 43 44 45
      });
      expect(testLogger.statusText, '');
      time.elapse(const Duration(milliseconds: 500));
      expect(testLogger.statusText,
        'Running "flutter packages get" in /...\n'
        'pub get failed (69) -- attempting retry 1 in 1 second...\n'
      );
46
      expect(processMock.lastPubEnvironmment, contains('flutter_cli:flutter_tests'));
47
      expect(processMock.lastPubCache, isNull);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      time.elapse(const Duration(milliseconds: 500));
      expect(testLogger.statusText,
        'Running "flutter packages get" in /...\n'
        'pub get failed (69) -- attempting retry 1 in 1 second...\n'
        'pub get failed (69) -- attempting retry 2 in 2 seconds...\n'
      );
      time.elapse(const Duration(seconds: 1));
      expect(testLogger.statusText,
        'Running "flutter packages get" in /...\n'
        'pub get failed (69) -- attempting retry 1 in 1 second...\n'
        'pub get failed (69) -- attempting retry 2 in 2 seconds...\n'
      );
      time.elapse(const Duration(seconds: 100)); // from t=0 to t=100
      expect(testLogger.statusText,
        'Running "flutter packages get" in /...\n'
        'pub get failed (69) -- attempting retry 1 in 1 second...\n'
        'pub get failed (69) -- attempting retry 2 in 2 seconds...\n'
        'pub get failed (69) -- attempting retry 3 in 4 seconds...\n' // at t=1
        'pub get failed (69) -- attempting retry 4 in 8 seconds...\n' // at t=5
        'pub get failed (69) -- attempting retry 5 in 16 seconds...\n' // at t=13
        'pub get failed (69) -- attempting retry 6 in 32 seconds...\n' // at t=29
        'pub get failed (69) -- attempting retry 7 in 64 seconds...\n' // at t=61
      );
      time.elapse(const Duration(seconds: 200)); // from t=0 to t=200
      expect(testLogger.statusText,
        'Running "flutter packages get" in /...\n'
        'pub get failed (69) -- attempting retry 1 in 1 second...\n'
        'pub get failed (69) -- attempting retry 2 in 2 seconds...\n'
        'pub get failed (69) -- attempting retry 3 in 4 seconds...\n'
        'pub get failed (69) -- attempting retry 4 in 8 seconds...\n'
        'pub get failed (69) -- attempting retry 5 in 16 seconds...\n'
        'pub get failed (69) -- attempting retry 6 in 32 seconds...\n'
        'pub get failed (69) -- attempting retry 7 in 64 seconds...\n'
        'pub get failed (69) -- attempting retry 8 in 64 seconds...\n' // at t=39
        'pub get failed (69) -- attempting retry 9 in 64 seconds...\n' // at t=103
        'pub get failed (69) -- attempting retry 10 in 64 seconds...\n' // at t=167
      );
    });
    expect(testLogger.errorText, isEmpty);
    expect(error, isNull);
  }, overrides: <Type, Generator>{
    ProcessManager: () => new MockProcessManager(69),
    FileSystem: () => new MockFileSystem(),
91 92 93 94 95 96 97 98 99
    Platform: () => new FakePlatform(
      environment: <String, String>{},
    ),
  });

  testUsingContext('pub cache in root is used', () async {
    String error;

    final MockProcessManager processMock = context.getVariable(ProcessManager);
100
    final MockFileSystem fsMock = context.getVariable(FileSystem);
101 102 103 104 105

    new FakeAsync().run((FakeAsync time) {
      MockDirectory.findCache = true;
      expect(processMock.lastPubEnvironmment, isNull);
      expect(processMock.lastPubCache, isNull);
106
      pubGet(context: PubContext.flutterTests, checkLastModified: false).then((Null value) {
107 108 109 110 111
        error = 'test completed unexpectedly';
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
      });
      time.elapse(const Duration(milliseconds: 500));
112
      expect(processMock.lastPubCache, equals(fsMock.path.join(Cache.flutterRoot, '.pub-cache')));
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      expect(error, isNull);
    });
  }, overrides: <Type, Generator>{
    ProcessManager: () => new MockProcessManager(69),
    FileSystem: () => new MockFileSystem(),
    Platform: () => new FakePlatform(
      environment: <String, String>{},
    ),
  });

  testUsingContext('pub cache in environment is used', () async {
    String error;

    final MockProcessManager processMock = context.getVariable(ProcessManager);

    new FakeAsync().run((FakeAsync time) {
129
      MockDirectory.findCache = true;
130 131
      expect(processMock.lastPubEnvironmment, isNull);
      expect(processMock.lastPubCache, isNull);
132
      pubGet(context: PubContext.flutterTests, checkLastModified: false).then((Null value) {
133 134 135 136 137
        error = 'test completed unexpectedly';
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
      });
      time.elapse(const Duration(milliseconds: 500));
138
      expect(processMock.lastPubCache, equals('custom/pub-cache/path'));
139 140 141 142 143 144
      expect(error, isNull);
    });
  }, overrides: <Type, Generator>{
    ProcessManager: () => new MockProcessManager(69),
    FileSystem: () => new MockFileSystem(),
    Platform: () => new FakePlatform(
145
      environment: <String, String>{'PUB_CACHE': 'custom/pub-cache/path'},
146
    ),
147 148 149 150 151 152 153 154 155 156
  });
}

typedef void StartCallback(List<dynamic> command);

class MockProcessManager implements ProcessManager {
  MockProcessManager(this.fakeExitCode);

  final int fakeExitCode;

157
  String lastPubEnvironmment;
158
  String lastPubCache;
159

160 161 162 163 164 165 166 167 168
  @override
  Future<Process> start(
    List<dynamic> command, {
    String workingDirectory,
    Map<String, String> environment,
    bool includeParentEnvironment: true,
    bool runInShell: false,
    ProcessStartMode mode: ProcessStartMode.NORMAL,
  }) {
169
    lastPubEnvironmment = environment['PUB_ENVIRONMENT'];
170
    lastPubCache = environment['PUB_CACHE'];
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    return new Future<Process>.value(new MockProcess(fakeExitCode));
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}

class MockProcess implements Process {
  MockProcess(this.fakeExitCode);

  final int fakeExitCode;

  @override
  Stream<List<int>> get stdout => new MockStream<List<int>>();

  @override
  Stream<List<int>> get stderr => new MockStream<List<int>>();

  @override
  Future<int> get exitCode => new Future<int>.value(fakeExitCode);

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}

class MockStream<T> implements Stream<T> {
  @override
  Stream<S> transform<S>(StreamTransformer<T, S> streamTransformer) => new MockStream<S>();

  @override
  Stream<T> where(bool test(T event)) => new MockStream<T>();

  @override
  StreamSubscription<T> listen(void onData(T event), {Function onError, void onDone(), bool cancelOnError}) {
    return new MockStreamSubscription<T>();
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}

class MockStreamSubscription<T> implements StreamSubscription<T> {
  @override
  Future<E> asFuture<E>([E futureValue]) => new Future<E>.value();

  @override
  Future<Null> cancel() => null;

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}


class MockFileSystem extends MemoryFileSystem {
  @override
  File file(dynamic path) {
    return new MockFile();
  }
229 230 231 232 233

  @override
  Directory directory(dynamic path) {
    return new MockDirectory(path);
  }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

class MockFile implements File {
  @override
  Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) async {
    return new MockRandomAccessFile();
  }

  @override
  bool existsSync() => true;

  @override
  DateTime lastModifiedSync() => new DateTime(0);

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
class MockDirectory implements Directory {
  static bool findCache = false;

  MockDirectory(this.path);

  @override
  final String path;

  @override
  bool existsSync() => findCache && path.endsWith('.pub-cache');

  @override
  dynamic noSuchMethod(Invocation invocation) => null;
}

267
class MockRandomAccessFile extends Mock implements RandomAccessFile {}