flutter/packages/flutter_tools/test/general.shard/update_packages_test.dart
auto-submit[bot] 4372bfbc6c
Reverts "Add workspace (#169451)" (#169468)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#169451
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Broke a number of post-submit tests
(ios_app_extension, packages_autoroller).
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: mosuem
<!-- end_original_pr_author -->

<!-- start_reviewers -->
Reviewed By: {matanlurey}
<!-- end_reviewers -->

<!-- start_revert_body -->
This change reverts the following previous change:
Reland after #169357.

Switch Flutter to use pub workspaces as a preparation to unpin selected
packages.

Assumptions:

1. No packages in this repository are published to pub.dev --> We can
use `any` dependencies in most local pubspecs, as the global constraint
defines the version. An exception are the packages used outside of this
repo with an `sdk` dependency, namely `flutter_localizations`,
`flutter_test`, and `flutter`.
2. The "universes" `{flutter_tools}` and `{flutter,
flutter_localizations, flutter_goldens}` can use different packages
versions, as they are not resolved together. --> We do not need to
upgrade them in sync, we can first do one "universe", then the other.

Based on these assumptions, we use
https://github.com/mosuem/pubspec_merger.dart to merge all packages in
the `flutter` universe into a top-level pub workspace.

The `flutter` and `flutter_tools` workspaces being separate also ensures
that changes to `flutter` will not inadvertently break `flutter_tools`,
with not-so-nice consequences for our users which would be unable to run
`flutter upgrade`.

There is a third "top-level" pubspec besides `./pubspec.yaml` and
`packages/flutter_tools/pubspec.yaml`, namely
`packages/flutter_tools/.../widget_preview_scaffold/pubspec.yaml`. This
is an artifact due to it living under `flutter_tools`, so it can't be
part of the `./pubspec.yaml` workspace. Moving it would be a larger
change, and out of the scope of this PR.

This required a rewrite of the update-packages tool, but the main
functionality stays the same, as well as the argument names, to ensure a
seamless transition.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

<!-- end_revert_body -->

Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
2025-05-26 14:07:27 +00:00

265 lines
8.1 KiB
Dart

// 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';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/update_packages.dart';
import 'package:flutter_tools/src/update_packages_pins.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:
sdk: ^3.7.0-0
dependencies:
# To update these, use "flutter update-packages --force-upgrade".
collection: 1.14.11
meta: 1.1.8
macros: 0.0.1
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: 1437
''';
const String kExtraPubspecYaml = r'''
name: nodeps
description: A dummy pubspec with no dependencies
homepage: http://flutter.dev
environment:
sdk: ^3.7.0-0
''';
const String kInvalidGitPubspec = '''
name: flutter
description: A framework for writing Flutter applications
homepage: http://flutter.dev
environment:
sdk: ^3.7.0-0
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:
''';
const String kVersionJson = '''
{
"frameworkVersion": "1.2.3",
"channel": "[user-branch]",
"repositoryUrl": "git@github.com:flutter/flutter.git",
"frameworkRevision": "1234567812345678123456781234567812345678",
"frameworkCommitDate": "2024-02-06 22:26:52 +0100",
"engineRevision": "abcdef01abcdef01abcdef01abcdef01abcdef01",
"dartSdkVersion": "1.2.3",
"devToolsVersion": "1.2.3",
"flutterVersion": "1.2.3"
}
''';
void main() {
late FileSystem fileSystem;
late Directory flutterSdk;
late Directory flutter;
setUp(() {
fileSystem = MemoryFileSystem.test();
// Setup simplified Flutter SDK.
flutterSdk = fileSystem.directory('flutter')..createSync();
// Create version file
flutterSdk.childFile('version').writeAsStringSync('1.2.3');
// Create version JSON file
flutterSdk.childDirectory('bin').childDirectory('cache').childFile('flutter.version.json')
..createSync(recursive: true)
..writeAsStringSync(kVersionJson);
// Create a pubspec file
flutter = flutterSdk.childDirectory('packages').childDirectory('flutter')
..createSync(recursive: true);
flutter.childFile('pubspec.yaml').writeAsStringSync(kFlutterPubspecYaml);
});
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.',
);
});
testWithoutContext('createTemporaryFlutterSdk creates an unpinned flutter SDK', () {
// A stray extra package should not cause a crash.
final Directory extra = flutterSdk.childDirectory('packages').childDirectory('extra')
..createSync(recursive: true);
extra.childFile('pubspec.yaml').writeAsStringSync(kExtraPubspecYaml);
// Create already parsed pubspecs.
final PubspecYaml flutterPubspec = PubspecYaml(flutter);
final PubspecDependency gitDependency = flutterPubspec.dependencies.firstWhere(
(PubspecDependency dep) => dep.kind == DependencyKind.git,
);
expect(gitDependency.lockLine, '''
git:
url: https://github.com/flutter/gallery.git
ref: d00362e6bdd0f9b30bba337c358b9e4a6e4ca950
''');
final BufferLogger bufferLogger = BufferLogger.test();
final Directory result = createTemporaryFlutterSdk(
bufferLogger,
fileSystem,
flutterSdk,
<PubspecYaml>[flutterPubspec],
fileSystem.systemTempDirectory,
);
expect(result, exists);
// We get a warning about the unexpected package.
expect(
bufferLogger.warningText,
contains("Unexpected package 'extra' found in packages directory"),
);
// The version file exists.
expect(result.childFile('version'), exists);
expect(result.childFile('version').readAsStringSync(), '1.2.3');
expect(
fileSystem.file(fileSystem.path.join(result.path, 'bin', 'cache', 'flutter.version.json')),
exists,
);
// 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');
});
testWithoutContext('Throws a StateError on a malformed git: reference', () {
// Create an invalid pubspec file.
flutter.childFile('pubspec.yaml').writeAsStringSync(kInvalidGitPubspec);
expect(() => PubspecYaml(flutter), throwsStateError);
});
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',
'macros: 0.0.1',
'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',
'macros: 0.0.1',
'typed_data: 1.1.6',
'vector_math: 2.0.8',
'sky_engine: ',
'gallery: ',
'flutter_test: ',
'flutter_goldens: ',
}),
);
expect(
pubspecYaml.dependencies
.map<String>(
(PubspecDependency dependency) => '${dependency.name}: ${dependency.version}',
)
.toSet(),
equals(<String>{
'collection: 1.14.11',
'meta: 1.1.8',
'macros: 0.0.1',
'typed_data: 1.1.6',
'vector_math: 2.0.8',
'sky_engine: ',
'gallery: ',
}),
);
});
testWithoutContext('PubspecYaml apply skips explicitly excluded packages', () async {
final PubspecYaml flutterPubspec = PubspecYaml(flutter);
final PubDependencyTree flutterTree = PubDependencyTree();
final List<String> depsLines = <String>[
// Have to add these first so that flutterTree.fill ignores the one in
// the pubspec.
'- macros 0.0.1 [_macros]',
'- _macros 0.0.1',
for (final PubspecDependency dependency in flutterPubspec.allDependencies)
'- ${dependency.name} ${dependency.version}',
];
final Set<String> dependencies =
flutterPubspec.allDependencies.map<String>((PubspecDependency dep) => dep.name).toSet();
depsLines.add('- flutter 1.0.0 [${dependencies.join(' ')}}]');
depsLines.forEach(flutterTree.fill);
flutterPubspec.apply(flutterTree, <String>{});
final String contents = flutter.childFile('pubspec.yaml').readAsStringSync();
expect(contents, isNot(contains('_macros: 0.0.1')));
});
}