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

Use directory exists instead of path.dirname (#112219)

parent 78e94155
......@@ -590,15 +590,15 @@ class _WindowsUtils extends OperatingSystemUtils {
String? findProjectRoot(FileSystem fileSystem, [ String? directory ]) {
const String kProjectRootSentinel = 'pubspec.yaml';
directory ??= fileSystem.currentDirectory.path;
Directory currentDirectory = fileSystem.directory(directory).absolute;
while (true) {
if (fileSystem.isFileSync(fileSystem.path.join(directory!, kProjectRootSentinel))) {
return directory;
if (currentDirectory.childFile(kProjectRootSentinel).existsSync()) {
return currentDirectory.path;
}
final String parent = fileSystem.path.dirname(directory);
if (directory == parent) {
if (!currentDirectory.existsSync() || currentDirectory.parent.path == currentDirectory.path) {
return null;
}
directory = parent;
currentDirectory = currentDirectory.parent;
}
}
......
......@@ -155,6 +155,21 @@ class BuildAarCommand extends BuildSubCommand {
if (remainingArguments.isEmpty) {
return FlutterProject.current();
}
return FlutterProject.fromDirectory(globals.fs.directory(findProjectRoot(globals.fs, remainingArguments.first)));
final File mainFile = globals.fs.file(remainingArguments.first);
final String path;
if (!mainFile.existsSync()) {
final Directory pathProject = globals.fs.directory(remainingArguments.first);
if (!pathProject.existsSync()) {
throwToolExit('${remainingArguments.first} does not exist');
}
path = pathProject.path;
} else {
path = mainFile.parent.path;
}
final String? projectRoot = findProjectRoot(globals.fs, path);
if (projectRoot == null) {
throwToolExit('${mainFile.parent.path} is not a valid flutter project');
}
return FlutterProject.fromDirectory(globals.fs.directory(projectRoot));
}
}
......@@ -114,6 +114,22 @@ void main() {
FileSystem: () => fileSystem,
});
testUsingContext('pub get throws error on missing directory', () async {
final PackagesGetCommand command = PackagesGetCommand('get', false);
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
try {
await commandRunner.run(<String>['get', 'missing_dir']);
fail('expected an exception');
} on Exception catch (e) {
expect(e.toString(), contains('Expected to find project root in missing_dir'));
}
}, overrides: <Type, Generator>{
Pub: () => pub,
ProcessManager: () => FakeProcessManager.any(),
FileSystem: () => fileSystem,
});
testUsingContext('pub get triggers localizations generation when generate: true', () async {
final File pubspecFile = fileSystem.currentDirectory.childFile('pubspec.yaml')
..createSync();
......
......@@ -230,6 +230,30 @@ void main() {
});
});
group('throws ToolExit', () {
testUsingContext('main.dart not found', () async {
await expectLater(() async {
await runBuildAarCommand(
'missing_project',
arguments: <String>['--no-pub'],
);
}, throwsToolExit(
message: 'main.dart does not exist',
));
});
testUsingContext('flutter project not valid', () async {
await expectLater(() async {
await runCommandIn(
tempDir.path,
arguments: <String>['--no-pub'],
);
}, throwsToolExit(
message: 'is not a valid flutter project',
));
});
});
testUsingContext('support ExtraDartFlagOptions', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
......
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