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

import 'package:file/file.dart';
import 'package:file/memory.dart';
7
import 'package:flutter_tools/src/base/bot_detector.dart';
8
import 'package:flutter_tools/src/base/common.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/base/logger.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/cache.dart';
14
import 'package:flutter_tools/src/dart/pub.dart';
15
import 'package:flutter_tools/src/reporting/reporting.dart';
16 17
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
18
import 'package:fake_async/fake_async.dart';
19

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

void main() {
25
  setUpAll(() {
26
    Cache.flutterRoot = '';
27
  });
28

29 30 31 32
  tearDown(() {
    MockDirectory.findCache = false;
  });

33
  testWithoutContext('pub get 69', () async {
34
    String error;
35

36 37 38 39 40 41 42 43 44 45 46 47
    final MockProcessManager processMock = MockProcessManager(69);
    final BufferLogger logger = BufferLogger.test();
    final Pub pub = Pub(
      fileSystem: MockFileSystem(),
      logger: logger,
      processManager: processMock,
      usage: MockUsage(),
      platform: FakePlatform(
        environment: const <String, String>{},
      ),
      botDetector: const BotDetectorAlwaysNo(),
    );
48

49
    FakeAsync().run((FakeAsync time) {
Josh Soref's avatar
Josh Soref committed
50
      expect(processMock.lastPubEnvironment, isNull);
51
      expect(logger.statusText, '');
52
      pub.get(context: PubContext.flutterTests, checkLastModified: false).then((void value) {
53
        error = 'test completed unexpectedly';
54 55
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
56 57
      });
      time.elapse(const Duration(milliseconds: 500));
58
      expect(logger.statusText,
59
        'Running "flutter pub get" in /...\n'
60
        'pub get failed (server unavailable) -- attempting retry 1 in 1 second...\n',
61
      );
Josh Soref's avatar
Josh Soref committed
62
      expect(processMock.lastPubEnvironment, contains('flutter_cli:flutter_tests'));
63
      expect(processMock.lastPubCache, isNull);
64
      time.elapse(const Duration(milliseconds: 500));
65
      expect(logger.statusText,
66
        'Running "flutter pub get" in /...\n'
67 68
        'pub get failed (server unavailable) -- attempting retry 1 in 1 second...\n'
        'pub get failed (server unavailable) -- attempting retry 2 in 2 seconds...\n',
69 70
      );
      time.elapse(const Duration(seconds: 1));
71
      expect(logger.statusText,
72
        'Running "flutter pub get" in /...\n'
73 74
        'pub get failed (server unavailable) -- attempting retry 1 in 1 second...\n'
        'pub get failed (server unavailable) -- attempting retry 2 in 2 seconds...\n',
75 76
      );
      time.elapse(const Duration(seconds: 100)); // from t=0 to t=100
77
      expect(logger.statusText,
78
        'Running "flutter pub get" in /...\n'
79 80 81 82 83 84 85
        'pub get failed (server unavailable) -- attempting retry 1 in 1 second...\n'
        'pub get failed (server unavailable) -- attempting retry 2 in 2 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 3 in 4 seconds...\n' // at t=1
        'pub get failed (server unavailable) -- attempting retry 4 in 8 seconds...\n' // at t=5
        'pub get failed (server unavailable) -- attempting retry 5 in 16 seconds...\n' // at t=13
        'pub get failed (server unavailable) -- attempting retry 6 in 32 seconds...\n' // at t=29
        'pub get failed (server unavailable) -- attempting retry 7 in 64 seconds...\n', // at t=61
86 87
      );
      time.elapse(const Duration(seconds: 200)); // from t=0 to t=200
88
      expect(logger.statusText,
89
        'Running "flutter pub get" in /...\n'
90 91 92 93 94 95 96 97 98 99
        'pub get failed (server unavailable) -- attempting retry 1 in 1 second...\n'
        'pub get failed (server unavailable) -- attempting retry 2 in 2 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 3 in 4 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 4 in 8 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 5 in 16 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 6 in 32 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 7 in 64 seconds...\n'
        'pub get failed (server unavailable) -- attempting retry 8 in 64 seconds...\n' // at t=39
        'pub get failed (server unavailable) -- attempting retry 9 in 64 seconds...\n' // at t=103
        'pub get failed (server unavailable) -- attempting retry 10 in 64 seconds...\n', // at t=167
100 101
      );
    });
102
    expect(logger.errorText, isEmpty);
103
    expect(error, isNull);
104 105
  });

106 107 108 109 110 111 112 113 114 115
  testWithoutContext('pub get 66 shows message from pub', () async {
    final BufferLogger logger = BufferLogger.test();
    final Pub pub = Pub(
      platform: FakePlatform(environment: const <String, String>{}),
      fileSystem: MockFileSystem(),
      logger: logger,
      usage: MockUsage(),
      botDetector: const BotDetectorAlwaysNo(),
      processManager: MockProcessManager(66, stderr: 'err1\nerr2\nerr3\n', stdout: 'out1\nout2\nout3\n'),
    );
116
    try {
117
      await pub.get(context: PubContext.flutterTests, checkLastModified: false);
118 119 120 121
      throw AssertionError('pubGet did not fail');
    } on ToolExit catch (error) {
      expect(error.message, 'pub get failed (66; err3)');
    }
122
    expect(logger.statusText,
123 124 125 126 127
      'Running "flutter pub get" in /...\n'
      'out1\n'
      'out2\n'
      'out3\n'
    );
128
    expect(logger.errorText,
129 130 131 132 133 134
      'err1\n'
      'err2\n'
      'err3\n'
    );
  });

135
  testWithoutContext('pub cache in root is used', () async {
136
    String error;
137 138 139 140 141 142 143 144 145 146
    final MockProcessManager processMock = MockProcessManager(69);
    final MockFileSystem fsMock = MockFileSystem();
    final Pub pub = Pub(
      platform: FakePlatform(environment: const <String, String>{}),
      usage: MockUsage(),
      fileSystem: fsMock,
      logger: BufferLogger.test(),
      processManager: processMock,
      botDetector: const BotDetectorAlwaysNo(),
    );
147

148
    FakeAsync().run((FakeAsync time) {
149
      MockDirectory.findCache = true;
Josh Soref's avatar
Josh Soref committed
150
      expect(processMock.lastPubEnvironment, isNull);
151
      expect(processMock.lastPubCache, isNull);
152
      pub.get(context: PubContext.flutterTests, checkLastModified: false).then((void value) {
153 154 155 156 157
        error = 'test completed unexpectedly';
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
      });
      time.elapse(const Duration(milliseconds: 500));
158

159
      expect(processMock.lastPubCache, equals(fsMock.path.join(Cache.flutterRoot, '.pub-cache')));
160 161 162 163
      expect(error, isNull);
    });
  });

164 165 166 167 168 169 170 171 172 173 174 175 176 177
  testWithoutContext('pub cache in environment is used', () async {
    final MockProcessManager processMock = MockProcessManager(69);
    final Pub pub = Pub(
      fileSystem: MockFileSystem(),
      logger: BufferLogger.test(),
      processManager: processMock,
      usage: MockUsage(),
      botDetector: const BotDetectorAlwaysNo(),
      platform: FakePlatform(
        environment: const <String, String>{
          'PUB_CACHE': 'custom/pub-cache/path',
        },
      ),
    );
178

179
    FakeAsync().run((FakeAsync time) {
180
      MockDirectory.findCache = true;
Josh Soref's avatar
Josh Soref committed
181
      expect(processMock.lastPubEnvironment, isNull);
182
      expect(processMock.lastPubCache, isNull);
183 184

      String error;
185
      pub.get(context: PubContext.flutterTests, checkLastModified: false).then((void value) {
186 187 188 189 190
        error = 'test completed unexpectedly';
      }, onError: (dynamic thrownError) {
        error = 'test failed unexpectedly: $thrownError';
      });
      time.elapse(const Duration(milliseconds: 500));
191

192
      expect(processMock.lastPubCache, equals('custom/pub-cache/path'));
193 194
      expect(error, isNull);
    });
195
  });
196

197
  testWithoutContext('analytics sent on success', () async {
198
    final FileSystem fileSystem = MemoryFileSystem.test();
199 200
    final MockUsage usage = MockUsage();
    final Pub pub = Pub(
201
      fileSystem: fileSystem,
202 203 204 205 206 207 208 209 210 211
      logger: BufferLogger.test(),
      processManager: MockProcessManager(0),
      botDetector: const BotDetectorAlwaysNo(),
      usage: usage,
      platform: FakePlatform(
        environment: const <String, String>{
          'PUB_CACHE': 'custom/pub-cache/path',
        }
      ),
    );
212 213 214 215
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.dart_tool/package_config.json')
      ..createSync(recursive: true)
      ..writeAsStringSync('{"configVersion": 2,"packages": []}');
216

217 218 219
    await pub.get(
      context: PubContext.flutterTests,
      generateSyntheticPackage: true,
220
      checkLastModified: false,
221
    );
222 223

    verify(usage.sendEvent('pub-result', 'flutter-tests', label: 'success')).called(1);
224 225
  });

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  testWithoutContext('package_config_subset file is generated from packages and not timestamp', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final MockUsage usage = MockUsage();
    final Pub pub = Pub(
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      processManager: MockProcessManager(0),
      botDetector: const BotDetectorAlwaysNo(),
      usage: usage,
      platform: FakePlatform(
        environment: const <String, String>{
          'PUB_CACHE': 'custom/pub-cache/path',
        }
      ),
    );
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.dart_tool/package_config.json')
      ..createSync(recursive: true)
      ..writeAsStringSync('''
      {"configVersion": 2,"packages": [
        {
          "name": "flutter_tools",
          "rootUri": "../",
          "packageUri": "lib/",
          "languageVersion": "2.7"
        }
      ],"generated":"some-time"}
''');

    await pub.get(
      context: PubContext.flutterTests,
      generateSyntheticPackage: true,
258
      checkLastModified: false,
259 260 261 262 263 264 265 266 267 268 269 270 271
    );

    expect(
      fileSystem.file('.dart_tool/package_config_subset').readAsStringSync(),
      'flutter_tools\n'
      '2.7\n'
      'file:///\n'
      'file:///lib/\n'
      '2\n',
    );
  });


272
  testWithoutContext('analytics sent on failure', () async {
273
    MockDirectory.findCache = true;
274 275 276 277 278 279 280 281 282 283 284 285 286
    final MockUsage usage = MockUsage();
    final Pub pub = Pub(
      usage: usage,
      fileSystem: MockFileSystem(),
      logger: BufferLogger.test(),
      processManager: MockProcessManager(1),
      botDetector: const BotDetectorAlwaysNo(),
      platform: FakePlatform(
        environment: const <String, String>{
          'PUB_CACHE': 'custom/pub-cache/path',
        },
      ),
    );
287
    try {
288
      await pub.get(context: PubContext.flutterTests, checkLastModified: false);
289 290 291
    } on ToolExit {
      // Ignore.
    }
292 293

    verify(usage.sendEvent('pub-result', 'flutter-tests', label: 'failure')).called(1);
294 295
  });

296 297
  testWithoutContext('analytics sent on failed version solve', () async {
    final MockUsage usage = MockUsage();
298
    final FileSystem fileSystem = MemoryFileSystem.test();
299
    final Pub pub = Pub(
300
      fileSystem: fileSystem,
301 302 303 304 305 306 307 308 309 310 311 312 313
      logger: BufferLogger.test(),
      processManager: MockProcessManager(
        1,
        stderr: 'version solving failed',
      ),
      platform: FakePlatform(
        environment: <String, String>{
          'PUB_CACHE': 'custom/pub-cache/path',
        },
      ),
      usage: usage,
      botDetector: const BotDetectorAlwaysNo(),
    );
314
    fileSystem.file('pubspec.yaml').writeAsStringSync('name: foo');
315

316
    try {
317
      await pub.get(context: PubContext.flutterTests, checkLastModified: false);
318 319 320
    } on ToolExit {
      // Ignore.
    }
321 322

    verify(usage.sendEvent('pub-result', 'flutter-tests', label: 'version-solving-failed')).called(1);
323
  });
324

325 326 327
  testWithoutContext('Pub error handling', () async {
    final BufferLogger logger = BufferLogger.test();
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
328 329 330
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(
        command: const <String>[
331
          'bin/cache/dart-sdk/bin/pub',
332 333 334 335 336
          '--verbosity=warning',
          'get',
          '--no-precompile',
        ],
        onRun: () {
337
          fileSystem.file('.dart_tool/package_config.json')
338
            .setLastModifiedSync(DateTime(2002));
339 340 341 342
        }
      ),
      const FakeCommand(
        command: <String>[
343
          'bin/cache/dart-sdk/bin/pub',
344 345 346 347 348 349 350
          '--verbosity=warning',
          'get',
          '--no-precompile',
        ],
      ),
      FakeCommand(
        command: const <String>[
351
          'bin/cache/dart-sdk/bin/pub',
352 353 354 355 356
          '--verbosity=warning',
          'get',
          '--no-precompile',
        ],
        onRun: () {
357
          fileSystem.file('pubspec.yaml')
358
            .setLastModifiedSync(DateTime(2002));
359 360
        }
      ),
361 362
      const FakeCommand(
        command: <String>[
363
          'bin/cache/dart-sdk/bin/pub',
364 365 366 367 368
          '--verbosity=warning',
          'get',
          '--no-precompile',
        ],
      ),
369
    ]);
370 371 372 373 374 375
    final Pub pub = Pub(
      usage: MockUsage(),
      fileSystem: fileSystem,
      logger: logger,
      processManager: processManager,
      platform: FakePlatform(
376 377 378
        operatingSystem: 'linux', // so that the command executed is consistent
        environment: <String, String>{},
      ),
379 380 381 382 383 384 385 386 387 388
      botDetector: const BotDetectorAlwaysNo()
    );

    // the good scenario: .packages is old, pub updates the file.
    fileSystem.file('.dart_tool/package_config.json')
      ..createSync(recursive: true)
      ..setLastModifiedSync(DateTime(2000));
    fileSystem.file('pubspec.yaml')
      ..createSync()
      ..setLastModifiedSync(DateTime(2001));
389
    await pub.get(context: PubContext.flutterTests, checkLastModified: true); // pub sets date of .packages to 2002
390 391 392 393 394 395 396 397 398 399 400

    expect(logger.statusText, 'Running "flutter pub get" in /...\n');
    expect(logger.errorText, isEmpty);
    expect(fileSystem.file('pubspec.yaml').lastModifiedSync(), DateTime(2001)); // because nothing should touch it
    logger.clear();

    // bad scenario 1: pub doesn't update file; doesn't matter, because we do instead
    fileSystem.file('.dart_tool/package_config.json')
      .setLastModifiedSync(DateTime(2000));
    fileSystem.file('pubspec.yaml')
      .setLastModifiedSync(DateTime(2001));
401
    await pub.get(context: PubContext.flutterTests, checkLastModified: true); // pub does nothing
402 403 404 405 406

    expect(logger.statusText, 'Running "flutter pub get" in /...\n');
    expect(logger.errorText, isEmpty);
    expect(fileSystem.file('pubspec.yaml').lastModifiedSync(), DateTime(2001)); // because nothing should touch it
    logger.clear();
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

    // bad scenario 2: pub changes pubspec.yaml instead
    fileSystem.file('.dart_tool/package_config.json')
      .setLastModifiedSync(DateTime(2000));
    fileSystem.file('pubspec.yaml')
      .setLastModifiedSync(DateTime(2001));
    try {
      await pub.get(context: PubContext.flutterTests, checkLastModified: true);
      expect(true, isFalse, reason: 'pub.get did not throw');
    } on ToolExit catch (error) {
      expect(error.message, '/: unexpected concurrent modification of pubspec.yaml while running pub.');
    }
    expect(logger.statusText, 'Running "flutter pub get" in /...\n');
    expect(logger.errorText, isEmpty);
    expect(fileSystem.file('pubspec.yaml').lastModifiedSync(), DateTime(2002)); // because fake pub above touched it

    // bad scenario 3: pubspec.yaml was created in the future
    fileSystem.file('.dart_tool/package_config.json')
      .setLastModifiedSync(DateTime(2000));
    fileSystem.file('pubspec.yaml')
      .setLastModifiedSync(DateTime(9999));
    assert(DateTime(9999).isAfter(DateTime.now()));

    await pub.get(context: PubContext.flutterTests, checkLastModified: true); // pub does nothing

    expect(logger.statusText, contains('Running "flutter pub get" in /...\n'));
    expect(logger.errorText, startsWith(
      'Warning: File "/pubspec.yaml" was created in the future. Optimizations that rely on '
      'comparing time stamps will be unreliable. Check your system clock for accuracy.\n'
      'The timestamp was:'
    ));
    logger.clear();
439 440 441 442 443
  });
}

class BotDetectorAlwaysNo implements BotDetector {
  const BotDetectorAlwaysNo();
444

445
  @override
446
  Future<bool> get isRunningOnBot async => false;
447 448
}

449
typedef StartCallback = void Function(List<dynamic> command);
450 451

class MockProcessManager implements ProcessManager {
452
  MockProcessManager(this.fakeExitCode, {
453
    this.stdout = '',
454 455
    this.stderr = '',
  });
456 457

  final int fakeExitCode;
458
  final String stdout;
459
  final String stderr;
460

Josh Soref's avatar
Josh Soref committed
461
  String lastPubEnvironment;
462
  String lastPubCache;
463

464 465 466 467 468
  @override
  Future<Process> start(
    List<dynamic> command, {
    String workingDirectory,
    Map<String, String> environment,
469 470
    bool includeParentEnvironment = true,
    bool runInShell = false,
471
    ProcessStartMode mode = ProcessStartMode.normal,
472
  }) {
Josh Soref's avatar
Josh Soref committed
473
    lastPubEnvironment = environment['PUB_ENVIRONMENT'];
474
    lastPubCache = environment['PUB_CACHE'];
475 476
    return Future<Process>.value(mocks.createMockProcess(
      exitCode: fakeExitCode,
477
      stdout: stdout,
478 479
      stderr: stderr,
    ));
480 481 482 483 484 485
  }

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

486
class MockFileSystem extends ForwardingFileSystem {
487
  MockFileSystem() : super(MemoryFileSystem.test());
488

489 490
  @override
  File file(dynamic path) {
491
    return MockFile();
492
  }
493 494 495

  @override
  Directory directory(dynamic path) {
496
    return MockDirectory(path as String);
497
  }
498 499 500 501
}

class MockFile implements File {
  @override
502
  Future<RandomAccessFile> open({ FileMode mode = FileMode.read }) async {
503
    return MockRandomAccessFile();
504 505 506 507 508 509
  }

  @override
  bool existsSync() => true;

  @override
510
  DateTime lastModifiedSync() => DateTime(0);
511 512 513 514 515

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

516 517 518 519 520 521
class MockDirectory implements Directory {
  MockDirectory(this.path);

  @override
  final String path;

522 523
  static bool findCache = false;

524 525 526 527 528 529 530
  @override
  bool existsSync() => findCache && path.endsWith('.pub-cache');

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

531
class MockRandomAccessFile extends Mock implements RandomAccessFile {}
532 533

class MockUsage extends Mock implements Usage {}