From 8847b86681ad4f9452e75a378ee6c7c86b720134 Mon Sep 17 00:00:00 2001 From: stevemessick Date: Fri, 8 Apr 2016 20:37:50 -0700 Subject: [PATCH] Add validity checks to create (#3215) * Add validity checks to create * Adjust indent --- .../lib/src/commands/create.dart | 29 ++++++++++++++++++- packages/flutter_tools/test/create_test.dart | 26 +++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index d567d4de53..4611e21274 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -88,6 +88,10 @@ class CreateCommand extends Command { String dirPath = path.normalize(projectDir.absolute.path); String projectName = _normalizeProjectName(path.basename(dirPath)); + if (_validateProjectDir(dirPath) != null) { + printError(_validateProjectDir(dirPath)); + return 1; + } if (_validateProjectName(projectName) != null) { printError(_validateProjectName(projectName)); return 1; @@ -217,9 +221,32 @@ final Set _packageDependencies = new Set.from([ String _validateProjectName(String projectName) { if (_packageDependencies.contains(projectName)) { return "Invalid project name: '$projectName' - this will conflict with Flutter " - "package dependencies."; + "package dependencies."; } + return null; +} +/// Return `null` if the project directory is legal. Return a validation message +/// if we should disallow the directory name. +String _validateProjectDir(String projectName) { + FileSystemEntityType type = FileSystemEntity.typeSync(projectName); + if (type != FileSystemEntityType.NOT_FOUND) { + switch(type) { + case FileSystemEntityType.DIRECTORY: + // Do not re-use directory if it is not empty. + if (new Directory(projectName).listSync(followLinks: false).isNotEmpty) { + return "Invalid project name: '$projectName' - refers to a directory " + "that is not empty."; + }; + break; + case FileSystemEntityType.FILE: + // Do not overwrite files. + return "Invalid project name: '$projectName' - file exists."; + case FileSystemEntityType.LINK: + // Do not overwrite links. + return "Invalid project name: '$projectName' - refers to a link."; + } + } return null; } diff --git a/packages/flutter_tools/test/create_test.dart b/packages/flutter_tools/test/create_test.dart index 3347c60869..8f890731ab 100644 --- a/packages/flutter_tools/test/create_test.dart +++ b/packages/flutter_tools/test/create_test.dart @@ -48,5 +48,31 @@ void main() { }, // This test can take a while due to network requests. timeout: new Timeout(new Duration(minutes: 2))); + + // Verify that we fail with an error code when the file exists. + testUsingContext('fails when file exists', () async { + ArtifactStore.flutterRoot = '../..'; + CreateCommand command = new CreateCommand(); + CommandRunner runner = new CommandRunner('test_flutter', '') + ..addCommand(command); + File existingFile = new File("${temp.path.toString()}/bad"); + if (!existingFile.existsSync()) existingFile.createSync(); + await runner.run(['create', existingFile.path]) + .then((int code) => expect(code, equals(1))); + }); + + // Verify that we fail with an error code when the file exists. + testUsingContext('fails with non-empty directory', () async { + ArtifactStore.flutterRoot = '../..'; + CreateCommand command = new CreateCommand(); + CommandRunner runner = new CommandRunner('test_flutter', '') + ..addCommand(command); + File existingFile = new File("${temp.path.toString()}/bad"); + if (!existingFile.existsSync()) existingFile.createSync(); + RandomAccessFile raf = existingFile.openSync(); + raf.close(); + await runner.run(['create', temp.path]) + .then((int code) => expect(code, equals(1))); + }); }); }