packages_test.dart 20.3 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
import 'dart:async';
8
import 'dart:convert';
9 10

import 'package:args/command_runner.dart';
11
import 'package:file/memory.dart';
12
import 'package:flutter_tools/src/base/bot_detector.dart';
13
import 'package:flutter_tools/src/base/error_handling_io.dart';
14
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
15
import 'package:flutter_tools/src/base/io.dart';
16
import 'package:flutter_tools/src/base/platform.dart';
17
import 'package:flutter_tools/src/cache.dart';
18
import 'package:flutter_tools/src/commands/packages.dart';
19
import 'package:flutter_tools/src/dart/pub.dart';
20
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
21
import 'package:flutter_tools/src/reporting/reporting.dart';
22

23 24
import '../../src/common.dart';
import '../../src/context.dart';
25
import '../../src/fake_process_manager.dart';
26
import '../../src/fakes.dart';
27
import '../../src/test_flutter_command_runner.dart';
28 29

void main() {
30 31
  Cache.disableLocking();
  group('packages get/upgrade', () {
32
    Directory tempDir;
33 34

    setUp(() {
35
      tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
36 37 38
    });

    tearDown(() {
39
      tryToDelete(tempDir);
40 41
    });

42
    Future<String> createProjectWithPlugin(String plugin, { List<String> arguments }) async {
43
      final String projectPath = await createProject(tempDir, arguments: arguments);
44
      final File pubspec = globals.fs.file(globals.fs.path.join(projectPath, 'pubspec.yaml'));
45
      String content = await pubspec.readAsString();
46 47 48 49 50 51 52 53
      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');
54 55 56
      await pubspec.writeAsString(content, flush: true);
      return projectPath;
    }
57

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

70
    void expectExists(String projectPath, String relPath) {
71
      expect(
72
        globals.fs.isFileSync(globals.fs.path.join(projectPath, relPath)),
73 74 75 76 77 78 79 80
        true,
        reason: '$projectPath/$relPath should exist, but does not',
      );
    }

    void expectContains(String projectPath, String relPath, String substring) {
      expectExists(projectPath, relPath);
      expect(
81
        globals.fs.file(globals.fs.path.join(projectPath, relPath)).readAsStringSync(),
82
        contains(substring),
83
        reason: '$projectPath/$relPath has unexpected content',
84 85 86 87 88
      );
    }

    void expectNotExists(String projectPath, String relPath) {
      expect(
89
        globals.fs.isFileSync(globals.fs.path.join(projectPath, relPath)),
90 91 92 93 94 95 96 97
        false,
        reason: '$projectPath/$relPath should not exist, but does',
      );
    }

    void expectNotContains(String projectPath, String relPath, String substring) {
      expectExists(projectPath, relPath);
      expect(
98
        globals.fs.file(globals.fs.path.join(projectPath, relPath)).readAsStringSync(),
99 100 101 102 103
        isNot(contains(substring)),
        reason: '$projectPath/$relPath has unexpected content',
      );
    }

104
    const List<String> pubOutput = <String>[
105 106 107 108
      '.packages',
      'pubspec.lock',
    ];

109
    const List<String> pluginRegistrants = <String>[
110 111 112 113 114
      'ios/Runner/GeneratedPluginRegistrant.h',
      'ios/Runner/GeneratedPluginRegistrant.m',
      'android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
    ];

115
    const List<String> modulePluginRegistrants = <String>[
116 117 118 119 120
      '.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.h',
      '.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.m',
      '.android/Flutter/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
    ];

121
    const List<String> pluginWitnesses = <String>[
122 123 124 125
      '.flutter-plugins',
      'ios/Podfile',
    ];

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

131
    const Map<String, String> pluginContentWitnesses = <String, String>{
132 133
      '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"',
134 135
    };

136
    const Map<String, String> modulePluginContentWitnesses = <String, String>{
137 138
      '.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"',
139 140
    };

141
    void expectDependenciesResolved(String projectPath) {
142
      for (final String output in pubOutput) {
143 144 145 146 147
        expectExists(projectPath, output);
      }
    }

    void expectZeroPluginsInjected(String projectPath) {
148
      for (final String registrant in modulePluginRegistrants) {
149 150 151 152 153
        expectExists(projectPath, registrant);
      }
      for (final String witness in pluginWitnesses) {
        expectNotExists(projectPath, witness);
      }
154
      modulePluginContentWitnesses.forEach((String witness, String content) {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        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);
      });
    }

171 172
    void expectModulePluginInjected(String projectPath) {
      for (final String registrant in modulePluginRegistrants) {
173 174
        expectExists(projectPath, registrant);
      }
175
      for (final String witness in modulePluginWitnesses) {
176 177
        expectExists(projectPath, witness);
      }
178
      modulePluginContentWitnesses.forEach((String witness, String content) {
179 180 181 182
        expectContains(projectPath, witness, content);
      });
    }

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

195
    testUsingContext('get fetches packages', () async {
196 197
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
198 199 200 201 202 203
      removeGeneratedFiles(projectPath);

      await runCommandIn(projectPath, 'get');

      expectDependenciesResolved(projectPath);
      expectZeroPluginsInjected(projectPath);
204
    }, overrides: <Type, Generator>{
205 206 207 208 209 210 211 212
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
213
    });
214

215
    testUsingContext('get --offline fetches packages', () async {
216 217
      final String projectPath = await createProject(tempDir,
        arguments: <String>['--no-pub', '--template=module']);
218 219 220 221 222 223
      removeGeneratedFiles(projectPath);

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

      expectDependenciesResolved(projectPath);
      expectZeroPluginsInjected(projectPath);
224
    }, overrides: <Type, Generator>{
225 226 227 228 229 230 231 232
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
233
    });
234

235
    testUsingContext('set no plugins as usage value', () async {
236 237 238 239 240 241 242
      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;

243 244
      expect(await getCommand.usageValues,
             containsPair(CustomDimensions.commandPackagesNumberPlugins, '0'));
245
    }, overrides: <Type, Generator>{
246 247 248 249 250 251 252 253
      Pub: () => Pub(
        fileSystem: globals.fs,
        logger: globals.logger,
        processManager: globals.processManager,
        usage: globals.flutterUsage,
        botDetector: globals.botDetector,
        platform: globals.platform,
      ),
254
    });
255

256 257 258 259 260 261 262 263 264 265 266
    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;

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

279 280 281 282 283 284 285 286
    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;

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

    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;

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

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

326 327 328 329 330 331 332 333 334
      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);

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

      expect(await getCommand.usageValues,
             containsPair(CustomDimensions.commandPackagesAndroidEmbeddingVersion, 'v1'));
    }, 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 359 360 361
    });

    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;

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

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

      await runCommandIn(projectPath, 'upgrade');

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

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

      await runCommandIn(projectPath, 'get');
399

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

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

      await runCommandIn(projectPath, 'get');

      expectDependenciesResolved(projectPath);

      await runCommandIn(exampleProjectPath, 'get');

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

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

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

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

459
      expect(processManager, hasNoRemainingExpectations);
460
    }, overrides: <Type, Generator>{
461 462 463
      FileSystem: () => MemoryFileSystem.test(),
      Platform: () => FakePlatform(operatingSystem: 'linux', environment: <String, String>{}),
      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 480 481
      Cache.flutterRoot = '';
      globals.fs.file('pubspec.yaml').createSync();
      processManager.addCommand(
        const FakeCommand(command: <String>['/bin/cache/dart-sdk/bin/pub', '--trace', 'run', 'test']),
      );
482
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
483

484
      expect(processManager, hasNoRemainingExpectations);
485
    }, overrides: <Type, Generator>{
486 487 488
      FileSystem: () => MemoryFileSystem.test(),
      Platform: () => FakePlatform(operatingSystem: 'linux', environment: <String, String>{}),
      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 507 508 509 510 511
    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>[
          '/bin/cache/dart-sdk/bin/pub', 'run', '--foo', 'bar'],
          stdin: stdin,
        ),
      );
      await createTestCommandRunner(PackagesCommand()).run(<String>['packages', '--verbose', 'pub', 'run', '--foo', 'bar']);
512

513
      expect(processManager, hasNoRemainingExpectations);
514
    }, overrides: <Type, Generator>{
515 516 517
      FileSystem: () => MemoryFileSystem.test(),
      Platform: () => FakePlatform(operatingSystem: 'linux', environment: <String, String>{}),
      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

    testUsingContext('upgrade does not check for pubspec.yaml if -h/--help is passed', () async {
      Cache.flutterRoot = '';
      processManager.addCommand(
        FakeCommand(command: const <String>[
          '/bin/cache/dart-sdk/bin/pub', 'upgrade', '-h'],
          stdin:  IOSink(StreamController<List<int>>().sink),
        ),
      );
      await createTestCommandRunner(PackagesCommand()).run(<String>['pub', 'upgrade', '-h']);

      expect(processManager, hasNoRemainingExpectations);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem.test(),
      Platform: () => FakePlatform(operatingSystem: 'linux', 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,
      ),
    });
554 555
  });
}