Merge pull request #8979 from abpframework/EngincanV/validate-project-name

CLI: Validate project name for the template creation
pull/9029/head
Alper Ebicoglu 4 years ago committed by GitHub
commit bc20714d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,6 +61,11 @@ namespace Volo.Abp.Cli.Commands
);
}
if (!ProjectNameValidator.IsValid(projectName))
{
throw new CliUsageException("The project name is invalid! Please specify a different name.");
}
Logger.LogInformation("Creating your project...");
Logger.LogInformation("Project name: " + projectName);
@ -554,4 +559,4 @@ namespace Volo.Abp.Cli.Commands
}
}
}
}
}

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Volo.Abp.Cli.Utils
{
public static class ProjectNameValidator
{
private static readonly string[] IllegalProjectNames = new[]
{
"MyCompanyName.MyProjectName",
"MyProjectName",
"CON", //Windows doesn't accept these names as file name
"AUX",
"PRN",
"COM1",
"LPT2"
};
private static readonly char[] IllegalChars = new[]
{
'/',
'?',
':',
'&',
'\\',
'*',
'\'',
'<',
'>',
'|',
'#',
'%',
};
private static bool HasParentDirectoryString(string projectName)
{
return projectName.Contains("..");
}
private static bool HasIllegalChar(string projectName)
{
foreach (var illegalWord in IllegalChars)
{
if (projectName.Contains(illegalWord))
{
return false;
}
}
return true;
}
private static bool HasSurrogateOrControlChar(string projectName)
{
return projectName.Any(chr => char.IsControl(chr) || char.IsSurrogate(chr));
}
private static bool IsIllegalProjectName(string projectName)
{
foreach (var illegalProjectName in IllegalProjectNames)
{
if (projectName.Equals(illegalProjectName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return true;
}
public static bool IsValid(string projectName)
{
if (projectName == null)
{
throw new CliUsageException("Project name cannot be empty!");
}
if (HasIllegalChar(projectName))
{
return false;
}
if (HasSurrogateOrControlChar(projectName))
{
return false;
}
if (HasParentDirectoryString(projectName))
{
return false;
}
if (IsIllegalProjectName(projectName))
{
return false;
}
return true;
}
}
}

@ -0,0 +1,75 @@
using System.IO;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.ProjectModification;
using Xunit;
namespace Volo.Abp.Cli
{
public class ProjectNameValidation_Tests : AbpCliTestBase
{
private readonly NewCommand _newCommand;
public ProjectNameValidation_Tests()
{
_newCommand = GetRequiredService<NewCommand>();
}
[Fact]
public async Task IllegalProjectName_Test()
{
var illegalProjectNames = new[]
{
"MyCompanyName.MyProjectName",
"MyProjectName",
"CON", //Windows doesn't accept these names as file name
"AUX",
"PRN",
"COM1",
"LPT2"
};
foreach (var illegalProjectName in illegalProjectNames)
{
var args = new CommandLineArgs("new", illegalProjectName);
await _newCommand.ExecuteAsync(args).ShouldThrowAsync<CliUsageException>();
}
}
[Fact]
public async Task ContainsIllegalChar_Test()
{
var illegalChars = new[]
{
'/',
'?',
':',
'&',
'\\',
'*',
'\'',
'<',
'>',
'|',
'#',
'%',
};
foreach (var illegalChar in illegalChars)
{
var args = new CommandLineArgs("new", "Test" + illegalChar);
await _newCommand.ExecuteAsync(args).ShouldThrowAsync<CliUsageException>();
}
}
[Fact]
public async Task ParentDirectoryContain_Test()
{
var args = new CommandLineArgs("new", "Test..Test");
await _newCommand.ExecuteAsync(args).ShouldThrowAsync<CliUsageException>();
}
}
}
Loading…
Cancel
Save