[flutter_tools] Discover pubspec.yaml in parent directories (#48548)

This commit is contained in:
Jonah Williams 2020-01-27 21:48:01 -08:00 committed by Flutter GitHub Bot
parent 3233252cb1
commit c341d4b78f
2 changed files with 67 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import 'dart:async';
import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:meta/meta.dart';
import 'package:quiver/strings.dart';
@ -707,8 +708,20 @@ abstract class FlutterCommand extends Command<void> {
Future<void> validateCommand() async {
if (_requiresPubspecYaml && !PackageMap.isUsingCustomPackagesPath) {
// Don't expect a pubspec.yaml file if the user passed in an explicit .packages file path.
if (!globals.fs.isFileSync('pubspec.yaml')) {
throw ToolExit(userMessages.flutterNoPubspec);
// If there is no pubspec in the current directory, look in the parent
// until one can be found.
bool changedDirectory = false;
while (!globals.fs.isFileSync('pubspec.yaml')) {
final Directory nextCurrent = globals.fs.currentDirectory.parent;
if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
throw ToolExit(userMessages.flutterNoPubspec);
}
globals.fs.currentDirectory = nextCurrent;
changedDirectory = true;
}
if (changedDirectory) {
globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}');
}
// Validate the current package map only if we will not be running "pub get" later.

View File

@ -125,6 +125,58 @@ void main() {
DeviceManager: () => MockDeviceManager(),
});
testUsingContext('Walks upward looking for a pubspec.yaml and succeeds if found', () async {
globals.fs.file('pubspec.yaml').createSync();
globals.fs.file('.packages')
..createSync()
..writeAsStringSync('Not a valid package');
globals.fs.currentDirectory = globals.fs.directory(globals.fs.path.join('a', 'b', 'c'))
..createSync(recursive: true);
final RunCommand command = RunCommand();
applyMocksToCommand(command);
try {
await createTestCommandRunner(command).run(<String>[
'run',
'--fast-start',
'--no-pub',
]);
fail('Expect exception');
} catch (e) {
expect(e, isInstanceOf<ToolExit>());
}
final BufferLogger bufferLogger = globals.logger as BufferLogger;
expect(bufferLogger.statusText, contains(
'Changing current working directory to:'
));
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(),
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('Walks upward looking for a pubspec.yaml and exits if missing', () async {
globals.fs.currentDirectory = globals.fs.directory(globals.fs.path.join('a', 'b', 'c'))
..createSync(recursive: true);
final RunCommand command = RunCommand();
applyMocksToCommand(command);
try {
await createTestCommandRunner(command).run(<String>[
'run',
'--fast-start',
'--no-pub',
]);
fail('Expect exception');
} catch (e) {
expect(e, isInstanceOf<ToolExit>());
expect(e.toString(), contains('No pubspec.yaml file found'));
}
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(),
ProcessManager: () => FakeProcessManager.any(),
});
group('run app', () {
MemoryFileSystem fs;