update_packages_test.dart 13.2 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter 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 7
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/cache.dart';
10
import 'package:flutter_tools/src/commands/update_packages.dart';
11
import 'package:flutter_tools/src/dart/pub.dart';
12
import 'package:flutter_tools/src/project.dart';
13
import 'package:test/fake.dart';
14
import 'package:yaml/yaml.dart';
15

16 17 18 19 20 21 22 23 24 25
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';

// An example pubspec.yaml from flutter, not necessary for it to be up to date.
const String kFlutterPubspecYaml = r'''
name: flutter
description: A framework for writing Flutter applications
homepage: http://flutter.dev

environment:
26
  sdk: '>=3.0.0-0 <4.0.0'
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

dependencies:
  # To update these, use "flutter update-packages --force-upgrade".
  collection: 1.14.11
  meta: 1.1.8
  typed_data: 1.1.6
  vector_math: 2.0.8

  sky_engine:
    sdk: flutter

  gallery:
    git:
      url: https://github.com/flutter/gallery.git
      ref: d00362e6bdd0f9b30bba337c358b9e4a6e4ca950

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_goldens:
    sdk: flutter

  archive: 2.0.11 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"

# PUBSPEC CHECKSUM: 1234
''';

// An example pubspec.yaml, not necessary for it to be up to date.
const String kExamplesPubspecYaml = r'''
name: examples
description: Examples for flutter
homepage: http://flutter.dev

version: 1.0.0

environment:
63
  sdk: '>=3.0.0-0 <4.0.0'
64 65 66 67 68 69 70 71 72 73 74
  flutter: ">=2.5.0-6.0.pre.30 <3.0.0"

dependencies:
  cupertino_icons: 1.0.4
  flutter:
    sdk: flutter

  archive: 2.0.11 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"

# PUBSPEC CHECKSUM: 6543
''';
75 76

void main() {
77
  group('update-packages', () {
78 79 80 81
    late FileSystem fileSystem;
    late Directory flutterSdk;
    late Directory flutter;
    late FakePub pub;
82
    late FakeProcessManager processManager;
83
    late BufferLogger logger;
84 85 86

    setUpAll(() {
      Cache.disableLocking();
87
      logger = BufferLogger.test();
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    });

    setUp(() {
      fileSystem = MemoryFileSystem.test();
      flutterSdk = fileSystem.directory('flutter')..createSync();
      flutterSdk.childFile('version').writeAsStringSync('1.2.3');
      flutter = flutterSdk.childDirectory('packages').childDirectory('flutter')
        ..createSync(recursive: true);
      flutterSdk.childDirectory('dev').createSync(recursive: true);
      flutterSdk.childDirectory('examples').childFile('pubspec.yaml')
        ..createSync(recursive: true)
        ..writeAsStringSync(kExamplesPubspecYaml);
      flutter.childFile('pubspec.yaml').writeAsStringSync(kFlutterPubspecYaml);
      Cache.flutterRoot = flutterSdk.absolute.path;
      pub = FakePub(fileSystem);
103
      processManager = FakeProcessManager.empty();
104 105 106 107 108 109
    });

    testUsingContext('updates packages', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>['update-packages']);
      expect(pub.pubGetDirectories, equals(<String>[
110
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
111 112 113 114 115 116 117
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, isEmpty);
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
118
      ProcessManager: () => processManager,
119
      Cache: () => Cache.test(
120
        processManager: processManager,
121 122 123 124 125 126 127 128 129 130
      ),
    });

    testUsingContext('force updates packages', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--force-upgrade',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
131
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
132 133 134 135
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
136
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
137 138 139 140
      ]));
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
141
      ProcessManager: () => processManager,
142
      Cache: () => Cache.test(
143
        processManager: processManager,
144 145 146 147 148 149 150 151 152 153 154
      ),
    });

    testUsingContext('force updates packages --jobs=1', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--force-upgrade',
        '--jobs=1',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
155
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
156 157 158 159
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
160
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
161 162 163 164
      ]));
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
165
      ProcessManager: () => processManager,
166
      Cache: () => Cache.test(
167 168 169 170
        processManager: processManager,
      ),
    });

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
    testUsingContext('--transitive-closure --consumer-only', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--transitive-closure',
        '--consumer-only',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
      ]));
      // Expecting a line like:
      //   'flutter -> {collection, meta, typed_data, vector_math}'
      expect(
        logger.statusText,
        contains(RegExp(r'flutter -> {([a-z_]+, )*([a-z_]+)+}')),
      );
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
      ProcessManager: () => processManager,
      Cache: () => Cache.test(
        processManager: processManager,
      ),
      Logger: () => logger,
    });

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 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 258 259 260 261 262 263 264 265 266
    testUsingContext('--cherry-pick-package', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--cherry-pick-package=vector_math',
        '--cherry-pick-version=2.0.9',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
      ]));
      expect(pub.pubspecYamls, hasLength(3));
      final String output = pub.pubspecYamls.first;
      expect(output, isNotNull);
      expect(output, contains('collection: 1.14.11\n'));
      expect(output, contains('meta: 1.1.8\n'));
      expect(output, contains('typed_data: 1.1.6\n'));
      expect(output, contains('vector_math: 2.0.9\n'));
      expect(output, isNot(contains('vector_math: 2.0.8')));
      expect(output, isNot(contains('vector_math: ">= 2.0.8"')));
      expect(output, isNot(contains("vector_math: '>= 2.0.8'")));
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
      ProcessManager: () => processManager,
      Cache: () => Cache.test(
        processManager: processManager,
      ),
      Logger: () => logger,
    });

    testUsingContext('--force-upgrade', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--force-upgrade',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
        '/.tmp_rand0/flutter_update_packages.rand0/synthetic_package',
      ]));
      expect(pub.pubspecYamls, hasLength(3));
      final String output = pub.pubspecYamls.first;
      expect(output, isNotNull);
      expect(output, contains("collection: '>= 1.14.11'\n"));
      expect(output, contains("meta: '>= 1.1.8'\n"));
      expect(output, contains("typed_data: '>= 1.1.6'\n"));
      expect(output, contains("vector_math: '>= 2.0.8'\n"));
      expect(output, isNot(contains('vector_math: 2.0.8')));
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
      ProcessManager: () => processManager,
      Cache: () => Cache.test(
        processManager: processManager,
      ),
      Logger: () => logger,
    });

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    testUsingContext('force updates packages --synthetic-package-path', () async {
      final UpdatePackagesCommand command = UpdatePackagesCommand();
      const String dir = '/path/to/synthetic/package';
      await createTestCommandRunner(command).run(<String>[
        'update-packages',
        '--force-upgrade',
        '--synthetic-package-path=$dir',
      ]);
      expect(pub.pubGetDirectories, equals(<String>[
        '$dir/synthetic_package',
        '/flutter/examples',
        '/flutter/packages/flutter',
      ]));
      expect(pub.pubBatchDirectories, equals(<String>[
        '$dir/synthetic_package',
      ]));
    }, overrides: <Type, Generator>{
      Pub: () => pub,
      FileSystem: () => fileSystem,
      ProcessManager: () => processManager,
      Cache: () => Cache.test(
        processManager: processManager,
289 290 291
      ),
    });
  });
292 293 294 295 296 297 298 299 300 301 302 303 304 305

  group('generateFakePubspec', () {
    const String prevVersion = '1.2.0';
    testUsingContext('constrains package versions to >= previous version if doUpgrade: true', () {
      final String pubspecSource = generateFakePubspec(
        <PubspecDependency>[
          PubspecDependency(
            '  foo: $prevVersion',
            'foo',
            '',
            version: prevVersion,
            sourcePath: '/path/to/pubspec.yaml',
            kind: DependencyKind.normal,
            isTransitive: false,
306
            isDevDependency: false,
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
          ),
        ],
        doUpgrade: true,
      );
      final YamlMap pubspec = loadYaml(pubspecSource) as YamlMap;
      expect((pubspec['dependencies'] as YamlMap)['foo'], '>= $prevVersion');
    });

    testUsingContext('uses previous package versions doUpgrade: false', () {
      final String pubspecSource = generateFakePubspec(
        <PubspecDependency>[
          PubspecDependency(
            '  foo: $prevVersion',
            'foo',
            '',
            version: prevVersion,
            sourcePath: '/path/to/pubspec.yaml',
            kind: DependencyKind.normal,
            isTransitive: false,
326
            isDevDependency: false,
327 328 329 330 331 332 333
          ),
        ],
      );
      final YamlMap pubspec = loadYaml(pubspecSource) as YamlMap;
      expect((pubspec['dependencies'] as YamlMap)['foo'], prevVersion);
    });
  });
334 335 336 337 338 339 340 341
}

class FakePub extends Fake implements Pub {
  FakePub(this.fileSystem);

  final FileSystem fileSystem;
  final List<String> pubGetDirectories = <String>[];
  final List<String> pubBatchDirectories = <String>[];
342
  final List<String> pubspecYamls = <String>[];
343 344 345

  @override
  Future<void> get({
346
    required PubContext context,
347
    required FlutterProject project,
348 349 350
    bool upgrade = false,
    bool offline = false,
    bool generateSyntheticPackage = false,
351
    bool generateSyntheticPackageForExample = false,
352
    String? flutterRootOverride,
353 354
    bool checkUpToDate = false,
    bool shouldSkipThirdPartyGenerator = true,
355
    PubOutputMode outputMode = PubOutputMode.all,
356
  }) async {
357
    pubGetDirectories.add(project.directory.path);
358
    pubspecYamls.add(project.directory.childFile('pubspec.yaml').readAsStringSync());
359
    project.directory.childFile('pubspec.lock')
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
      ..createSync(recursive: true)
      ..writeAsStringSync('''
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  async:
    dependency: "direct dev"
    description:
      name: async
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.8.2"
sdks:
  dart: ">=2.14.0 <3.0.0"
''');
    fileSystem.currentDirectory
        .childDirectory('.dart_tool')
        .childFile('package_config.json')
      ..createSync(recursive: true)
      ..writeAsStringSync('{"configVersion":2,"packages":[]}');
  }

  @override
  Future<void> batch(
      List<String> arguments, {
385 386 387
        required PubContext context,
        String? directory,
        MessageFilter? filter,
388 389
        String failureMessage = 'pub failed',
      }) async {
390 391 392
    if (directory != null) {
      pubBatchDirectories.add(directory);
    }
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

'''
Dart SDK 2.16.0-144.0.dev
Flutter SDK 2.9.0-1.0.pre.263
flutter_api_samples 1.0.0

dependencies:
- cupertino_icons 1.0.4
- collection 1.15.0
- meta 1.7.0
- typed_data 1.3.0 [collection]
- vector_math 2.1.1

dev dependencies:

transitive dependencies:
- platform 3.1.0
- process 4.2.4 [file path platform]
411
'''.split('\n').forEach(filter!);
412
  }
413
}