Move dart pub deps call to <Pub>.deps and use it accordingly (#158524)

Closes https://github.com/flutter/flutter/issues/158396, closes https://github.com/flutter/flutter/pull/158370.
This commit is contained in:
Matan Lurey
2024-11-13 10:03:53 -08:00
committed by GitHub
parent 4de32b8702
commit 2d1a3efc38
16 changed files with 600 additions and 337 deletions

View File

@@ -121,7 +121,7 @@ void testUsingContext(
FileSystem: () => LocalFileSystemBlockingSetCurrentDirectory(),
PlistParser: () => FakePlistParser(),
Signals: () => FakeSignals(),
Pub: () => ThrowingPub(), // prevent accidentally using pub.
Pub: () => const ThrowingPub(), // prevent accidentally using pub.
CrashReporter: () => const NoopCrashReporter(),
TemplateRenderer: () => const MustacheTemplateRenderer(),
BuildTargets: () => const BuildTargetsImpl(),

View File

@@ -0,0 +1,84 @@
// 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:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/project.dart';
/// A fake implementation of [Pub] with a pre-primed [deps] response.
final class FakePubWithPrimedDeps implements Pub {
/// Creates an implementation of [Pub] with a pre-primed [Pub.deps] response.
///
/// It is expected that every [FlutterProject] that is provided to the
/// [Pub.deps] call is represented by [rootPackageName].
///
/// Optionally, provide [devDependencies] (of [rootPackageName]), and non
/// dev-dependencies ([dependencies]) of any package to a set of any other
/// packages. A resulting valid `dart pub deps --json` response is implicitly
/// created.
factory FakePubWithPrimedDeps({
String rootPackageName = 'app_name',
Set<String> devDependencies = const <String>{},
Map<String, Set<String>> dependencies = const <String, Set<String>>{},
}) {
// Start the packages: [ ... ] list with the root package.
final List<Object?> packages = <Object?>[
<String, Object?>{
'name': rootPackageName,
'kind': 'root',
'dependencies': <String>[
...dependencies.keys,
...devDependencies,
]..sort(),
'directDependencies': <String>[
...?dependencies[rootPackageName],
...devDependencies,
]..sort(),
'devDependencies': <String>[
...devDependencies,
],
},
];
// Add all non-dev dependencies.
for (final String packageName in dependencies.keys) {
final bool direct = dependencies[rootPackageName]!.contains(packageName);
packages.add(<String, Object?>{
'name': packageName,
'kind': direct ? 'direct' : 'transitive',
'dependencies': <String>[
...?dependencies[packageName],
],
'directDependencies': <String>[
...?dependencies[packageName],
],
});
}
// Add all dev-dependencies.
for (final String packageName in devDependencies) {
packages.add(<String, Object?>{
'name': packageName,
'kind': 'dev',
'dependencies': <String>[],
'directDependencies': <String>[],
});
}
return FakePubWithPrimedDeps._(<String, Object?>{
'root': rootPackageName,
'packages': packages,
});
}
const FakePubWithPrimedDeps._(this._deps);
final Map<String, Object?> _deps;
@override
Future<Map<String, Object?>> deps(FlutterProject project) async => _deps;
@override
Never noSuchMethod(Invocation invocation) {
throw UnsupportedError('Only <Pub>.deps is expected to be called');
}
}

View File

@@ -44,7 +44,7 @@ final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
Analytics: () => const NoOpAnalytics(),
FlutterVersion: () => FakeFlutterVersion(), // prevent requirement to mock git for test runner.
Signals: () => FakeSignals(), // prevent registering actual signal handlers.
Pub: () => ThrowingPub(), // prevent accidental invocations of pub.
Pub: () => const ThrowingPub(), // prevent accidental invocations of pub.
};
/// Manages interaction with the tool injection and runner system.

View File

@@ -3,47 +3,25 @@
// found in the LICENSE file.
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/project.dart';
class ThrowingPub implements Pub {
@override
Future<void> batch(List<String> arguments, {
PubContext? context,
String? directory,
MessageFilter? filter,
String? failureMessage = 'pub failed',
}) {
throw UnsupportedError('Attempted to invoke pub during test.');
}
final class ThrowingPub implements Pub {
const ThrowingPub();
@override
Future<void> get({
PubContext? context,
required FlutterProject project,
bool upgrade = false,
bool offline = false,
bool checkLastModified = true,
bool skipPubspecYamlCheck = false,
bool generateSyntheticPackage = false,
bool generateSyntheticPackageForExample = false,
String? flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
PubOutputMode outputMode = PubOutputMode.all,
}) {
throw UnsupportedError('Attempted to invoke pub during test.');
}
@override
Future<void> interactively(
List<String> arguments, {
FlutterProject? project,
required PubContext context,
required String command,
bool touchesPackageConfig = false,
bool generateSyntheticPackage = false,
PubOutputMode outputMode = PubOutputMode.all,
}) {
throw UnsupportedError('Attempted to invoke pub during test.');
Never noSuchMethod(Invocation invocation) {
throw UnsupportedError(
'Attempted to invoke pub during test, which otherwise was unexpected. '
'This error may be caused by either changing the implementation details '
'of the Flutter CLI in where the "Pub" class is now being used, or '
'adding a unit test that transitively depends on "Pub".\n'
'\n'
'Possible options for resolution:\n'
' 1. Refactor the code or test to not rely on "Pub".\n'
' 2. Create and use a test-appropriate Fake (grep for "implements Pub") '
' for example code across the test/ repo. It is possible that the '
' file you are editing already has an appropriate Fake.\n'
' 3. Use "FakePubWithPrimedDeps" if your code will transitively call '
' "dart pub deps --json" and you need a realistic output.',
);
}
}