Commit c341d4b7 authored by Jonah Williams's avatar Jonah Williams Committed by Flutter GitHub Bot

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

parent 3233252c
......@@ -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.
......
......@@ -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;
......
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