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

import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9 10 11 12 13 14 15 16 17 18 19
import 'package:flutter_tools/src/commands/update_packages.dart';

import '../src/common.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:
20
  sdk: '>=3.0.0-0 <4.0.0'
21 22 23 24 25 26 27 28 29 30 31

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

32 33 34 35 36
  gallery:
    git:
      url: https://github.com/flutter/gallery.git
      ref: d00362e6bdd0f9b30bba337c358b9e4a6e4ca950

37 38 39 40 41 42 43 44 45 46 47
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: 1437
''';

48 49 50 51 52 53
const String kExtraPubspecYaml = r'''
name: nodeps
description: A dummy pubspec with no dependencies
homepage: http://flutter.dev

environment:
54
  sdk: '>=3.0.0-0 <4.0.0'
55 56
''';

57 58 59 60 61 62
const String kInvalidGitPubspec = '''
name: flutter
description: A framework for writing Flutter applications
homepage: http://flutter.dev

environment:
63
  sdk: '>=3.0.0-0 <4.0.0'
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

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:
''';

79
void main() {
80 81 82
  late FileSystem fileSystem;
  late Directory flutterSdk;
  late Directory flutter;
83

84 85
  setUp(() {
    fileSystem = MemoryFileSystem.test();
86
    // Setup simplified Flutter SDK.
87
    flutterSdk = fileSystem.directory('flutter')..createSync();
88 89 90
    // Create version file
    flutterSdk.childFile('version').writeAsStringSync('1.2.3');
    // Create a pubspec file
91
    flutter = flutterSdk.childDirectory('packages').childDirectory('flutter')
92
      ..createSync(recursive: true);
93 94
    flutter.childFile('pubspec.yaml').writeAsStringSync(kFlutterPubspecYaml);
  });
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  testWithoutContext('kManuallyPinnedDependencies pins are actually pins', () {
    expect(
      kManuallyPinnedDependencies.values,
      isNot(contains(anyOf('any', startsWith('^'), startsWith('>'), startsWith('<')))),
      reason: 'Version pins in kManuallyPinnedDependencies must be specific pins, not ranges.',
    );
    expect(
      kManuallyPinnedDependencies.keys,
      unorderedEquals(const <String>[
        'flutter_gallery_assets',
        'flutter_template_images',
        'video_player',
        'material_color_utilities',
        'url_launcher_android',
        'archive',
        'path_provider_android',
112
        'flutter_plugin_android_lifecycle',
113 114 115 116
      ]),
    );
  });

117 118
  testWithoutContext(
      'createTemporaryFlutterSdk creates an unpinned flutter SDK', () {
119 120
    // A stray extra package should not cause a crash.
    final Directory extra = flutterSdk
121 122
        .childDirectory('packages')
        .childDirectory('extra')
123
      ..createSync(recursive: true);
124
    extra.childFile('pubspec.yaml').writeAsStringSync(kExtraPubspecYaml);
125

126 127 128
    // Create already parsed pubspecs.
    final PubspecYaml flutterPubspec = PubspecYaml(flutter);

129 130
    final PubspecDependency gitDependency = flutterPubspec.dependencies
        .firstWhere((PubspecDependency dep) => dep.kind == DependencyKind.git);
131 132 133 134 135 136 137 138
    expect(
      gitDependency.lockLine,
      '''
    git:
      url: https://github.com/flutter/gallery.git
      ref: d00362e6bdd0f9b30bba337c358b9e4a6e4ca950
''',
    );
139
    final BufferLogger bufferLogger = BufferLogger.test();
140
    final Directory result = createTemporaryFlutterSdk(
141
      bufferLogger,
142 143 144
      fileSystem,
      flutterSdk,
      <PubspecYaml>[flutterPubspec],
145
      fileSystem.systemTempDirectory,
146 147 148 149
    );

    expect(result, exists);

150 151
    // We get a warning about the unexpected package.
    expect(
152
      bufferLogger.warningText,
153 154 155
      contains("Unexpected package 'extra' found in packages directory"),
    );

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    // The version file exists.
    expect(result.childFile('version'), exists);
    expect(result.childFile('version').readAsStringSync(), '1.2.3');

    // The sky_engine package exists
    expect(fileSystem.directory('${result.path}/bin/cache/pkg/sky_engine'), exists);

    // The flutter pubspec exists
    final File pubspecFile = fileSystem.file('${result.path}/packages/flutter/pubspec.yaml');
    expect(pubspecFile, exists);

    // The flutter pubspec contains `any` dependencies.
    final PubspecYaml outputPubspec = PubspecYaml(pubspecFile.parent);
    expect(outputPubspec.name, 'flutter');
    expect(outputPubspec.dependencies.first.name, 'collection');
    expect(outputPubspec.dependencies.first.version, 'any');
  });
173 174

  testWithoutContext('Throws a StateError on a malformed git: reference', () {
175 176
    // Create an invalid pubspec file.
    flutter.childFile('pubspec.yaml').writeAsStringSync(kInvalidGitPubspec);
177 178 179 180 181 182

    expect(
      () => PubspecYaml(flutter),
      throwsStateError,
    );
  });
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

  testWithoutContext('PubspecYaml Loads dependencies', () async {
    final PubspecYaml pubspecYaml = PubspecYaml(flutter);
    expect(
        pubspecYaml.allDependencies
            .map<String>((PubspecDependency dependency) => '${dependency.name}: ${dependency.version}')
            .toSet(),
        equals(<String>{
          'collection: 1.14.11',
          'meta: 1.1.8',
          'typed_data: 1.1.6',
          'vector_math: 2.0.8',
          'sky_engine: ',
          'gallery: ',
          'flutter_test: ',
          'flutter_goldens: ',
          'archive: 2.0.11',
        }));
    expect(
        pubspecYaml.allExplicitDependencies
            .map<String>((PubspecDependency dependency) => '${dependency.name}: ${dependency.version}')
            .toSet(),
        equals(<String>{
          'collection: 1.14.11',
          'meta: 1.1.8',
          'typed_data: 1.1.6',
          'vector_math: 2.0.8',
          'sky_engine: ',
          'gallery: ',
          'flutter_test: ',
213
          'flutter_goldens: ',
214 215 216 217 218 219 220 221 222 223 224
        }));
    expect(
        pubspecYaml.dependencies
            .map<String>((PubspecDependency dependency) => '${dependency.name}: ${dependency.version}')
            .toSet(),
        equals(<String>{
          'collection: 1.14.11',
          'meta: 1.1.8',
          'typed_data: 1.1.6',
          'vector_math: 2.0.8',
          'sky_engine: ',
225
          'gallery: ',
226 227
        }));
  });
228
}