Unverified Commit 1995da2c authored by Remi Rousselet's avatar Remi Rousselet Committed by GitHub

Disable the automatic "pub get" if the project is using a third-party tool for...

Disable the automatic "pub get" if the project is using a third-party tool for linking dependencies. (#86177)
parent e084e9d9
......@@ -139,6 +139,7 @@ class PackagesGetCommand extends FlutterCommand {
context: PubContext.pubGet,
directory: directory,
upgrade: upgrade,
shouldSkipThirdPartyGenerator: false,
offline: boolArg('offline'),
generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
);
......
......@@ -86,6 +86,10 @@ abstract class Pub {
///
/// [context] provides extra information to package server requests to
/// understand usage.
///
/// If [shouldSkipThirdPartyGenerator] is true, the overall pub get will be
/// skipped if the package config file has a "generator" other than "pub".
/// Defaults to true.
Future<void> get({
required PubContext context,
String directory,
......@@ -95,6 +99,7 @@ abstract class Pub {
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
});
/// Runs pub in 'batch' mode.
......@@ -169,6 +174,7 @@ class _DefaultPub implements Pub {
bool generateSyntheticPackage = false,
String? flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) async {
directory ??= _fileSystem.currentDirectory.path;
final File packageConfigFile = _fileSystem.file(
......@@ -185,6 +191,26 @@ class _DefaultPub implements Pub {
_fileSystem.path.join(directory, 'pubspec.lock')
);
if (shouldSkipThirdPartyGenerator && packageConfigFile.existsSync()) {
Map<String, Object?> packageConfigMap;
try {
packageConfigMap = jsonDecode(
packageConfigFile.readAsStringSync(),
) as Map<String, Object?>;
} on FormatException {
packageConfigMap = <String, Object?>{};
}
final bool isPackageConfigGeneratedByThirdParty =
packageConfigMap.containsKey('generator') &&
packageConfigMap['generator'] != 'pub';
if (isPackageConfigGeneratedByThirdParty) {
_logger.printTrace('Skipping pub get: generated by third-party.');
return;
}
}
// If the pubspec.yaml is older than the package config file and the last
// flutter version used is the same as the current version skip pub get.
// This will incorrectly skip pub on the master branch if dependencies
......
......@@ -118,5 +118,6 @@ class FakePub extends Fake implements Pub {
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) async { }
}
......@@ -113,6 +113,7 @@ class FakePub extends Fake implements Pub {
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) async {
fileSystem.currentDirectory
.childDirectory('.dart_tool')
......
......@@ -1045,7 +1045,17 @@ class FakePub extends Fake implements Pub {
int calledGet = 0;
@override
Future<void> get({PubContext context, String directory, bool skipIfAbsent = false, bool upgrade = false, bool offline = false, bool generateSyntheticPackage = false, String flutterRootOverride, bool checkUpToDate = false}) async {
Future<void> get({
PubContext context,
String directory,
bool skipIfAbsent = false,
bool upgrade = false,
bool offline = false,
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) async {
calledGet += 1;
}
}
......
......@@ -47,6 +47,186 @@ void main() {
), throwsToolExit(message: 'Your Flutter SDK download may be corrupt or missing permissions to run'));
});
group('shouldSkipThirdPartyGenerator', () {
testWithoutContext('does not skip pub get the parameter is false', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/pub',
'--verbosity=warning',
'get',
'--no-precompile',
])
]);
final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync();
fileSystem.file('version').writeAsStringSync('b');
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('''
{
"configVersion": 2,
"packages": [],
"generated": "2021-07-08T10:02:49.155589Z",
"generator": "third-party",
"generatorVersion": "2.14.0-276.0.dev"
}
''');
final Pub pub = Pub(
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
usage: TestUsage(),
platform: FakePlatform(
environment: const <String, String>{},
),
botDetector: const BotDetectorAlwaysNo(),
);
await pub.get(
context: PubContext.pubGet,
checkUpToDate: true,
shouldSkipThirdPartyGenerator: false,
);
expect(processManager, hasNoRemainingExpectations);
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
});
testWithoutContext('does not skip pub get if package_config.json has "generator": "pub"', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/pub',
'--verbosity=warning',
'get',
'--no-precompile',
])
]);
final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('''
{
"configVersion": 2,
"packages": [],
"generated": "2021-07-08T10:02:49.155589Z",
"generator": "pub",
"generatorVersion": "2.14.0-276.0.dev"
}
''');
fileSystem.file('.dart_tool/version').writeAsStringSync('a');
fileSystem.file('version').writeAsStringSync('b');
final Pub pub = Pub(
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
usage: TestUsage(),
platform: FakePlatform(
environment: const <String, String>{},
),
botDetector: const BotDetectorAlwaysNo(),
);
await pub.get(
context: PubContext.pubGet,
checkUpToDate: true,
);
expect(processManager, hasNoRemainingExpectations);
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
});
testWithoutContext('does not skip pub get if package_config.json has "generator": "pub"', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/pub',
'--verbosity=warning',
'get',
'--no-precompile',
])
]);
final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('''
{
"configVersion": 2,
"packages": [],
"generated": "2021-07-08T10:02:49.155589Z",
"generator": "pub",
"generatorVersion": "2.14.0-276.0.dev"
}
''');
fileSystem.file('.dart_tool/version').writeAsStringSync('a');
fileSystem.file('version').writeAsStringSync('b');
final Pub pub = Pub(
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
usage: TestUsage(),
platform: FakePlatform(
environment: const <String, String>{},
),
botDetector: const BotDetectorAlwaysNo(),
);
await pub.get(
context: PubContext.pubGet,
checkUpToDate: true,
);
expect(processManager, hasNoRemainingExpectations);
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
});
testWithoutContext('skips pub get if the package config "generator" is '
'different than "pub"', () async {
final FakeProcessManager processManager = FakeProcessManager.empty();
final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"generator": "third-party"}');
final Pub pub = Pub(
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
usage: TestUsage(),
platform: FakePlatform(
environment: const <String, String>{},
),
botDetector: const BotDetectorAlwaysNo(),
);
await pub.get(
context: PubContext.pubGet,
checkUpToDate: true,
);
expect(
logger.traceText,
contains('Skipping pub get: generated by third-party.'),
);
});
});
testWithoutContext('checkUpToDate skips pub get if the package config is newer than the pubspec '
'and the current framework version is the same as the last version', () async {
final FakeProcessManager processManager = FakeProcessManager.empty();
......
......@@ -722,5 +722,6 @@ class FakePub extends Fake implements Pub {
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) async { }
}
......@@ -30,6 +30,7 @@ class ThrowingPub implements Pub {
bool generateSyntheticPackage = false,
String? flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
}) {
throw UnsupportedError('Attempted to invoke pub during test.');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment