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

5 6
// @dart = 2.8

7 8 9 10 11 12
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=1000"
@Tags(<String>['no-shuffle'])

13
import 'dart:async';
14
import 'dart:convert';
15 16

import 'package:args/command_runner.dart';
17
import 'package:file/memory.dart';
18
import 'package:flutter_tools/src/base/bot_detector.dart';
19
import 'package:flutter_tools/src/base/error_handling_io.dart';
20
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
21
import 'package:flutter_tools/src/base/io.dart';
22
import 'package:flutter_tools/src/base/platform.dart';
23
import 'package:flutter_tools/src/cache.dart';
24
import 'package:flutter_tools/src/commands/packages.dart';
25
import 'package:flutter_tools/src/dart/pub.dart';
26
import 'package:flutter_tools/src/globals.dart' as globals;
27

28 29
import '../../src/common.dart';
import '../../src/context.dart';
30
import '../../src/fake_process_manager.dart';
31
import '../../src/fakes.dart';
32
import '../../src/test_flutter_command_runner.dart';
33 34

void main() {
35 36
  Cache.disableLocking();
  group('packages get/upgrade', () {
37
    Directory tempDir;
38 39

    setUp(() {
40
      tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
41 42 43
    });

    tearDown(() {
44
      tryToDelete(tempDir);
45 46
    });

47
    Future<String> createProjectWithPlugin(String plugin, { List<String> arguments }) async {
48
      final String projectPath = await createProject(tempDir, arguments: arguments);
49
      final File pubspec = globals.fs.file(globals.fs.path.join(projectPath, 'pubspec.yaml'));
50
      String content = await pubspec.readAsString();
51 52 53 54 55 56 57 58
      final List<String> contentLines = LineSplitter.split(content).toList();
      final int depsIndex = contentLines.indexOf('dependencies:');
      expect(depsIndex, isNot(-1));
      contentLines.replaceRange(depsIndex, depsIndex + 1, <String>[
        'dependencies:',
        '  $plugin:',
      ]);
      content = contentLines.join('\n');
59 60 61
      await pubspec.writeAsString(content, flush: true);
      return projectPath;
    }
62

63
    Future<PackagesCommand> runCommandIn(String projectPath, String verb, { List<String> args }) async {
64
      final PackagesCommand command = PackagesCommand();
65
      final CommandRunner<void> runner = createTestCommandRunner(command);
66 67 68 69 70 71
      await runner.run(<String>[
        'packages',
        verb,
        ...?args,
        projectPath,
      ]);
72
      return command;
73 74
    }

75
    void expectExists(String projectPath, String relPath) {
76
      expect(
77
        globals.fs.isFileSync(globals.fs.path.join(projectPath, relPath)),
78 79 80 81 82 83 84 85
        true,
        reason: '$projectPath/$relPath should exist, but does not',
      );
    }

    void expectContains(String projectPath, String relPath, String substring) {
      expectExists(projectPath, relPath);
      expect(
86
        globals.fs.file(globals.fs.path.join(projectPath, relPath)).readAsStringSync(),
87
        contains(substring),
88
        reason: '$projectPath/$relPath has unexpected content',
89 90 91 92 93
      );
    }

    void expectNotExists(String projectPath, String relPath) {
      expect(
94
        globals.fs.isFileSync(globals.fs.path.join(projectPath, relPath)),
95 96 97 98 99 100 101 102
        false,
        reason: '$projectPath/$relPath should not exist, but does',
      );
    }

    void expectNotContains(String projectPath, String relPath, String substring) {
      expectExists(projectPath, relPath);
      expect(
103
        globals.fs.file(globals.fs.path.join(projectPath, relPath)).readAsStringSync(),
104 105 106 107 108
        isNot(contains(substring)),
        reason: '$projectPath/$relPath has unexpected content',
      );
    }

109
    const List<String> pubOutput = <String>[
110 111 112 113
      '.packages',
      'pubspec.lock',
    ];

114
    const List<String> pluginRegistrants = <String>[
115 116 117 118 119
      'ios/Runner/GeneratedPluginRegistrant.h',
      'ios/Runner/GeneratedPluginRegistrant.m',
      'android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
    ];

120
    const List<String> modulePluginRegistrants = <String>[
121 122 123 124 125
      '.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.h',
      '.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.m',
      '.android/Flutter/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
    ];

126
    const List<String> pluginWitnesses = <String>[
127 128 129 130
      '.flutter-plugins',
      'ios/Podfile',
    ];

131
    const List<String> modulePluginWitnesses = <String>[
132 133 134 135
      '.flutter-plugins',
      '.ios/Podfile',
    ];

136
    const Map<String, String> pluginContentWitnesses = <String, String>{
137 138
      'ios/Flutter/Debug.xcconfig': '#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"',
      'ios/Flutter/Release.xcconfig': '#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"',
139 140
    };

141
    const Map<String, String> modulePluginContentWitnesses = <String, String>{
142 143
      '.ios/Config/Debug.xcconfig': '#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"',
      '.ios/Config/Release.xcconfig': '#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"',
144 145
    };

146
    void expectDependenciesResolved(String projectPath) {
147
      for (final String output in pubOutput) {
148 149 150 151 152
        expectExists(projectPath, output);
      }
    }

    void expectZeroPluginsInjected(String projectPath) {
153
      for (final String registrant in modulePluginRegistrants) {
154 155 156 157 158
        expectExists(projectPath, registrant);
      }
      for (final String witness in pluginWitnesses) {
        expectNotExists(projectPath, witness);
      }
159
      modulePluginContentWitnesses.forEach((String witness, String content) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        expectNotContains(projectPath, witness, content);
      });
    }

    void expectPluginInjected(String projectPath) {
      for (final String registrant in pluginRegistrants) {
        expectExists(projectPath, registrant);
      }
      for (final String witness in pluginWitnesses) {
        expectExists(projectPath, witness);
      }
      pluginContentWitnesses.forEach((String witness, String content) {
        expectContains(projectPath, witness, content);
      });
    }

176 177
    void expectModulePluginInjected(String projectPath) {
      for (final String registrant in modulePluginRegistrants) {
178 179
        expectExists(projectPath, registrant);
      }
180
      for (final String witness in modulePluginWitnesses) {
181 182
        expectExists(projectPath, witness);
      }
183
      modulePluginContentWitnesses.forEach((String witness, String content) {
184 185 186 187
        expectContains(projectPath, witness, content);
      });
    }

188 189 190
    void removeGeneratedFiles(String projectPath) {
      final Iterable<String> allFiles = <List<String>>[
        pubOutput,
191
        modulePluginRegistrants,
192
        pluginWitnesses,
193
      ].expand<String>((List<String> list) => list);
194
      for (final String path in allFiles) {
195
        final File file = globals.fs.file(globals.fs.path.join(projectPath, path));
196
        ErrorHandlingFileSystem.deleteIfExists(file);
197
      }
198 199
    }

200
    testUsingContext('get fetches packages', () async {
201 202
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
203 204 205 206 207 208
      removeGeneratedFiles(projectPath);

      await runCommandIn(projectPath, 'get');

      expectDependenciesResolved(projectPath);
      expectZeroPluginsInjected(projectPath);
209
    }, overrides: <Type, Generator>{
210 211 212 213 214 215 216 217
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
218
    });
219

220
    testUsingContext('get --offline fetches packages', () async {
221 222
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
223 224 225 226 227 228
      removeGeneratedFiles(projectPath);

      await runCommandIn(projectPath, 'get', args: <String>['--offline']);

      expectDependenciesResolved(projectPath);
      expectZeroPluginsInjected(projectPath);
229
    }, overrides: <Type, Generator>{
230 231 232 233 234 235 236 237
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
238
    });
239

240
    testUsingContext('set no plugins as usage value', () async {
241 242 243 244 245 246 247
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
      removeGeneratedFiles(projectPath);

      final PackagesCommand command = await runCommandIn(projectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

248
      expect((await getCommand.usageValues).commandPackagesNumberPlugins, 0);
249
    }, overrides: <Type, Generator>{
250 251 252 253 254 255 256 257
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
258
    });
259

260 261 262 263 264 265 266 267 268 269
    testUsingContext('set the number of plugins as usage value', () async {
      final String projectPath = await createProject(
        tempDir,
        arguments: <String>['--template=plugin', '--no-pub', '--platforms=ios,android,macos,windows'],
      );
      final String exampleProjectPath = globals.fs.path.join(projectPath, 'example');

      final PackagesCommand command = await runCommandIn(exampleProjectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

270
      expect((await getCommand.usageValues).commandPackagesNumberPlugins, 1);
271 272 273 274 275 276 277 278 279 280 281
    }, overrides: <Type, Generator>{
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
    });

282 283 284 285 286 287 288 289
    testUsingContext('indicate that the project is not a module in usage value', () async {
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub']);
      removeGeneratedFiles(projectPath);

      final PackagesCommand command = await runCommandIn(projectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

290
      expect((await getCommand.usageValues).commandPackagesProjectModule, false);
291
    }, overrides: <Type, Generator>{
292 293 294 295 296 297 298 299
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
300
    });
301 302 303 304 305 306 307 308 309

    testUsingContext('indicate that the project is a module in usage value', () async {
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
      removeGeneratedFiles(projectPath);

      final PackagesCommand command = await runCommandIn(projectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

310
      expect((await getCommand.usageValues).commandPackagesProjectModule, true);
311
    }, overrides: <Type, Generator>{
312 313 314 315 316 317 318 319
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
320
    });
321

322 323 324 325 326
    testUsingContext('indicate that Android project reports v1 in usage value', () async {
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub']);
      removeGeneratedFiles(projectPath);

327 328 329 330 331 332 333 334 335
      final File androidManifest = globals.fs.file(globals.fs.path.join(
        projectPath,
        'android/app/src/main/AndroidManifest.xml',
      ));
      final String updatedAndroidManifestString =
          androidManifest.readAsStringSync().replaceAll('android:value="2"', 'android:value="1"');

      androidManifest.writeAsStringSync(updatedAndroidManifestString);

336 337 338
      final PackagesCommand command = await runCommandIn(projectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

339
      expect((await getCommand.usageValues).commandPackagesAndroidEmbeddingVersion, 'v1');
340
    }, overrides: <Type, Generator>{
341 342 343 344 345 346 347 348
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
349 350 351 352 353 354 355 356 357 358
    });

    testUsingContext('indicate that Android project reports v2 in usage value', () async {
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub']);
      removeGeneratedFiles(projectPath);

      final PackagesCommand command = await runCommandIn(projectPath, 'get');
      final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;

359
      expect((await getCommand.usageValues).commandPackagesAndroidEmbeddingVersion, 'v2');
360
    }, overrides: <Type, Generator>{
361 362 363 364 365 366 367 368
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
369 370
    });

371
    testUsingContext('upgrade fetches packages', () async {
372 373
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
374 375 376 377 378 379
      removeGeneratedFiles(projectPath);

      await runCommandIn(projectPath, 'upgrade');

      expectDependenciesResolved(projectPath);
      expectZeroPluginsInjected(projectPath);
380
    }, overrides: <Type, Generator>{
381
      Stdio: () => FakeStdio()..stdout.terminalColumns = 80,
382 383 384 385 386 387 388 389
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
390
    });
391 392

    testUsingContext('get fetches packages and injects plugin', () async {
393 394
      final String projectPath = await createProjectWithPlugin('path_provider',
        arguments: <String>['--no-pub', '--template=module']);
395 396 397
      removeGeneratedFiles(projectPath);

      await runCommandIn(projectPath, 'get');
398

399
      expectDependenciesResolved(projectPath);
400
      expectModulePluginInjected(projectPath);
401
    }, overrides: <Type, Generator>{
402 403 404 405 406 407 408 409
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
410
    });
411

412 413
    testUsingContext('get fetches packages and injects plugin in plugin project', () async {
      final String projectPath = await createProject(
414
        tempDir,
415
        arguments: <String>['--template=plugin', '--no-pub', '--platforms=ios,android'],
416
      );
417
      final String exampleProjectPath = globals.fs.path.join(projectPath, 'example');
418 419 420 421 422 423 424 425 426 427 428
      removeGeneratedFiles(projectPath);
      removeGeneratedFiles(exampleProjectPath);

      await runCommandIn(projectPath, 'get');

      expectDependenciesResolved(projectPath);

      await runCommandIn(exampleProjectPath, 'get');

      expectDependenciesResolved(exampleProjectPath);
      expectPluginInjected(exampleProjectPath);
429
    }, overrides: <Type, Generator>{
430 431 432 433 434 435 436 437
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
438
    });
439
  });
440 441

  group('packages test/pub', () {
442
    FakeProcessManager processManager;
443
    FakeStdio mockStdio;
444 445

    setUp(() {
446
      processManager = FakeProcessManager.empty();
447
      mockStdio = FakeStdio()..stdout.terminalColumns = 80;
448 449
    });

450
    testUsingContext('test without bot', () async {
451
      Cache.flutterRoot = '';
452
      globals.fs.directory('/packages/flutter_tools').createSync(recursive: true);
453 454
      globals.fs.file('pubspec.yaml').createSync();
      processManager.addCommand(
455
        const FakeCommand(command: <String>['/bin/cache/dart-sdk/bin/dart', '__deprecated_pub', 'run', 'test']),
456
      );
457
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
458

459
      expect(processManager, hasNoRemainingExpectations);
460
    }, overrides: <Type, Generator>{
461
      FileSystem: () => MemoryFileSystem.test(),
462
      Platform: () => FakePlatform(environment: <String, String>{}),
463
      ProcessManager: () => processManager,
464
      Stdio: () => mockStdio,
465
      BotDetector: () => const FakeBotDetector(false),
466 467 468 469 470 471 472 473
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
474 475 476
    });

    testUsingContext('test with bot', () async {
477 478 479
      Cache.flutterRoot = '';
      globals.fs.file('pubspec.yaml').createSync();
      processManager.addCommand(
480
        const FakeCommand(command: <String>['/bin/cache/dart-sdk/bin/dart', '__deprecated_pub', '--trace', 'run', 'test']),
481
      );
482
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
483

484
      expect(processManager, hasNoRemainingExpectations);
485
    }, overrides: <Type, Generator>{
486
      FileSystem: () => MemoryFileSystem.test(),
487
      Platform: () => FakePlatform(environment: <String, String>{}),
488
      ProcessManager: () => processManager,
489
      Stdio: () => mockStdio,
490
      BotDetector: () => const FakeBotDetector(true),
491 492 493 494 495 496 497 498
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
499 500
    });

501 502 503 504 505 506
    testUsingContext('run pass arguments through to pub', () async {
      Cache.flutterRoot = '';
      globals.fs.file('pubspec.yaml').createSync();
      final IOSink stdin = IOSink(StreamController<List<int>>().sink);
      processManager.addCommand(
        FakeCommand(command: const <String>[
507
          '/bin/cache/dart-sdk/bin/dart', '__deprecated_pub', 'run', '--foo', 'bar'],
508 509 510 511
          stdin: stdin,
        ),
      );
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', '--verbose', 'pub', 'run', '--foo', 'bar']);
512

513
      expect(processManager, hasNoRemainingExpectations);
514
    }, overrides: <Type, Generator>{
515
      FileSystem: () => MemoryFileSystem.test(),
516
      Platform: () => FakePlatform(environment: <String, String>{}),
517
      ProcessManager: () => processManager,
518
      Stdio: () => mockStdio,
519 520 521 522 523 524 525 526
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
527
    });
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    testUsingContext('token pass arguments through to pub', () async {
      Cache.flutterRoot = '';
      globals.fs.file('pubspec.yaml').createSync();
      final IOSink stdin = IOSink(StreamController<List<int>>().sink);
      processManager.addCommand(
        FakeCommand(command: const <String>[
          '/bin/cache/dart-sdk/bin/dart', '__deprecated_pub', 'token', 'list'],
          stdin: stdin,
        ),
      );
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', '--verbose', 'pub', 'token', 'list']);

      expect(processManager, hasNoRemainingExpectations);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem.test(),
      Platform: () => FakePlatform(environment: <String, String>{}),
      ProcessManager: () => processManager,
      Stdio: () => mockStdio,
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
    });

557 558 559 560
    testUsingContext('upgrade does not check for pubspec.yaml if -h/--help is passed', () async {
      Cache.flutterRoot = '';
      processManager.addCommand(
        FakeCommand(command: const <String>[
561
          '/bin/cache/dart-sdk/bin/dart', '__deprecated_pub', 'upgrade', '-h'],
562 563 564 565 566 567 568 569
          stdin:  IOSink(StreamController<List<int>>().sink),
        ),
      );
      await createTestCommandRunner(PackagesCommand()).run(<String>['pub', 'upgrade', '-h']);

      expect(processManager, hasNoRemainingExpectations);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem.test(),
570
      Platform: () => FakePlatform(environment: <String, String>{}),
571 572 573 574 575 576 577 578 579 580 581
      ProcessManager: () => processManager,
      Stdio: () => mockStdio,
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
    });
582 583
  });
}