Add validity checks to create (#3215)
* Add validity checks to create * Adjust indent
This commit is contained in:
@@ -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<String> _packageDependencies = new Set<String>.from(<String>[
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user