Commit 8847b866 authored by stevemessick's avatar stevemessick

Add validity checks to create (#3215)

* Add validity checks to create

* Adjust indent
parent d9e0c32d
...@@ -88,6 +88,10 @@ class CreateCommand extends Command { ...@@ -88,6 +88,10 @@ class CreateCommand extends Command {
String dirPath = path.normalize(projectDir.absolute.path); String dirPath = path.normalize(projectDir.absolute.path);
String projectName = _normalizeProjectName(path.basename(dirPath)); String projectName = _normalizeProjectName(path.basename(dirPath));
if (_validateProjectDir(dirPath) != null) {
printError(_validateProjectDir(dirPath));
return 1;
}
if (_validateProjectName(projectName) != null) { if (_validateProjectName(projectName) != null) {
printError(_validateProjectName(projectName)); printError(_validateProjectName(projectName));
return 1; return 1;
...@@ -219,7 +223,30 @@ String _validateProjectName(String projectName) { ...@@ -219,7 +223,30 @@ String _validateProjectName(String projectName) {
return "Invalid project name: '$projectName' - this will conflict with Flutter " 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; return null;
} }
......
...@@ -48,5 +48,31 @@ void main() { ...@@ -48,5 +48,31 @@ void main() {
}, },
// This test can take a while due to network requests. // This test can take a while due to network requests.
timeout: new Timeout(new Duration(minutes: 2))); 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)));
});
}); });
} }
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