Unverified Commit 3da9eee2 authored by Jesús S Guerrero's avatar Jesús S Guerrero Committed by GitHub

[flutter_tools] re-use findProjectRoot on flutter command (#104850)

parent 7df7e8ab
...@@ -58,7 +58,6 @@ class PackagesCommand extends FlutterCommand { ...@@ -58,7 +58,6 @@ class PackagesCommand extends FlutterCommand {
class PackagesGetCommand extends FlutterCommand { class PackagesGetCommand extends FlutterCommand {
PackagesGetCommand(this.name, this.upgrade) { PackagesGetCommand(this.name, this.upgrade) {
requiresPubspecYaml();
argParser.addFlag('offline', argParser.addFlag('offline',
negatable: false, negatable: false,
help: 'Use cached packages instead of accessing the network.', help: 'Use cached packages instead of accessing the network.',
......
...@@ -12,6 +12,7 @@ import '../application_package.dart'; ...@@ -12,6 +12,7 @@ import '../application_package.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../base/context.dart'; import '../base/context.dart';
import '../base/io.dart' as io; import '../base/io.dart' as io;
import '../base/os.dart';
import '../base/user_messages.dart'; import '../base/user_messages.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../build_info.dart'; import '../build_info.dart';
...@@ -1475,16 +1476,12 @@ abstract class FlutterCommand extends Command<void> { ...@@ -1475,16 +1476,12 @@ abstract class FlutterCommand extends Command<void> {
// If there is no pubspec in the current directory, look in the parent // If there is no pubspec in the current directory, look in the parent
// until one can be found. // until one can be found.
bool changedDirectory = false; final String? path = findProjectRoot(globals.fs, globals.fs.currentDirectory.path);
while (!globals.fs.isFileSync('pubspec.yaml')) { if (path == null) {
final Directory nextCurrent = globals.fs.currentDirectory.parent; throwToolExit(userMessages.flutterNoPubspec);
if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
throw ToolExit(userMessages.flutterNoPubspec);
} }
globals.fs.currentDirectory = nextCurrent; if (path != globals.fs.currentDirectory.path) {
changedDirectory = true; globals.fs.currentDirectory = path;
}
if (changedDirectory) {
globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}'); globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}');
} }
} }
......
...@@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/file_system.dart'; ...@@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/packages.dart'; import 'package:flutter_tools/src/commands/packages.dart';
import 'package:flutter_tools/src/dart/pub.dart'; import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:test/fake.dart'; import 'package:test/fake.dart';
...@@ -77,6 +78,24 @@ void main() { ...@@ -77,6 +78,24 @@ void main() {
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
}); });
testUsingContext('pub get on target directory', () async {
fileSystem.currentDirectory.childDirectory('target').createSync();
final Directory targetDirectory = fileSystem.currentDirectory.childDirectory('target');
targetDirectory.childFile('pubspec.yaml').createSync();
final PackagesGetCommand command = PackagesGetCommand('get', false);
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
await commandRunner.run(<String>['get', targetDirectory.path]);
final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory);
expect(rootProject.packageConfigFile.existsSync(), true);
expect(await rootProject.packageConfigFile.readAsString(), '{"configVersion":2,"packages":[]}');
}, overrides: <Type, Generator>{
Pub: () => pub,
ProcessManager: () => FakeProcessManager.any(),
FileSystem: () => fileSystem,
});
testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async { testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async {
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync(); fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true); fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
...@@ -116,7 +135,7 @@ class FakePub extends Fake implements Pub { ...@@ -116,7 +135,7 @@ class FakePub extends Fake implements Pub {
bool shouldSkipThirdPartyGenerator = true, bool shouldSkipThirdPartyGenerator = true,
bool printProgress = true, bool printProgress = true,
}) async { }) async {
fileSystem.currentDirectory fileSystem.directory(directory)
.childDirectory('.dart_tool') .childDirectory('.dart_tool')
.childFile('package_config.json') .childFile('package_config.json')
..createSync(recursive: true) ..createSync(recursive: true)
......
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