Check if project.json exists before creating new project in the same dir (#3543)

* Check if project.json exists before creating new project in the same dir.

* Change error message.
This commit is contained in:
Lakshmi Priya 2016-06-14 13:20:29 -07:00 committed by Piotr Puszkiewicz
parent f4a8df3a56
commit 8dd2479965
2 changed files with 42 additions and 2 deletions

View file

@ -26,6 +26,13 @@ namespace Microsoft.DotNet.Tools.New
public int CreateEmptyProject(string languageName, string templateDir)
{
// Check if project.json exists in the folder
if (File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "project.json")))
{
Reporter.Error.WriteLine($"Creating new {languageName} project failed, project already exists.");
return 1;
}
var thisAssembly = typeof(NewCommand).GetTypeInfo().Assembly;
var resources = from resourceName in thisAssembly.GetManifestResourceNames()
where resourceName.Contains(templateDir)
@ -43,6 +50,17 @@ namespace Microsoft.DotNet.Tools.New
try
{
// Check if other files from the template exists already, before extraction
IEnumerable<string> fileNames = archive.Entries.Select(e => e.FullName);
foreach (var entry in fileNames)
{
if (File.Exists(Path.Combine(Directory.GetCurrentDirectory(), entry)))
{
Reporter.Error.WriteLine($"Creating new {languageName} project failed, directory already contains {entry}");
return 1;
}
}
archive.ExtractToDirectory(Directory.GetCurrentDirectory());
File.Move(
@ -82,11 +100,12 @@ namespace Microsoft.DotNet.Tools.New
var type = app.Option("-t|--type <TYPE>", "Type of project", CommandOptionType.SingleValue);
var dotnetNew = new NewCommand();
app.OnExecute(() => {
app.OnExecute(() =>
{
var csharp = new { Name = "C#", Alias = new[] { "c#", "cs", "csharp" }, TemplatePrefix = "CSharp", Templates = new[] { "Console", "Web", "Lib", "xunittest" } };
var fsharp = new { Name = "F#", Alias = new[] { "f#", "fs", "fsharp" }, TemplatePrefix = "FSharp", Templates = new[] { "Console" } };
string languageValue = lang.Value() ?? csharp.Name;
var language = new[] { csharp, fsharp }

View file

@ -52,6 +52,27 @@ namespace Microsoft.DotNet.Tests
buildResult.Should().Pass();
buildResult.Should().NotHaveStdErr();
}
[Fact]
public void When_dotnet_new_is_invoked_mupliple_times_it_should_fail()
{
var rootPath = Temp.CreateDirectory().Path;
new TestCommand("dotnet") { WorkingDirectory = rootPath }
.Execute("new");
DateTime expectedState = Directory.GetLastWriteTime(rootPath);
var result = new TestCommand("dotnet") { WorkingDirectory = rootPath }
.ExecuteWithCapturedOutput("new");
DateTime actualState = Directory.GetLastWriteTime(rootPath);
Assert.Equal(expectedState, actualState);
result.Should().Fail();
result.Should().HaveStdErr();
}
private static void AddProjectJsonDependency(string projectJsonPath, string dependencyId, string dependencyVersion)
{