Browse Source

Implement same behaviour as Visual Studio's EnvDTE.ProjectItems by throwing an exception when AddFileFromCopy called and the destination file exists.

pull/15/head
Matt Ward 14 years ago
parent
commit
74a201369f
  1. 1
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 8
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/ProjectItems.cs
  3. 23
      src/AddIns/Misc/PackageManagement/Project/Src/FileExistsException.cs
  4. 1
      src/AddIns/Misc/PackageManagement/Project/Src/IPackageManagementFileService.cs
  5. 6
      src/AddIns/Misc/PackageManagement/Project/Src/PackageManagementFileService.cs
  6. 21
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/ProjectItemsTests.cs
  7. 8
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakeFileService.cs

1
src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj

@ -94,6 +94,7 @@ @@ -94,6 +94,7 @@
<Compile Include="Src\EnvDTE\Reference.cs" />
<Compile Include="Src\EnvDTE\References.cs" />
<Compile Include="Src\EnvDTE\Solution.cs" />
<Compile Include="Src\FileExistsException.cs" />
<Compile Include="Src\IAddPackageReferenceView.cs" />
<Compile Include="Src\IPackageActionRunner.cs" />
<Compile Include="Src\IPackageFromRepository.cs" />

8
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/ProjectItems.cs

@ -27,9 +27,17 @@ namespace ICSharpCode.PackageManagement.EnvDTE @@ -27,9 +27,17 @@ namespace ICSharpCode.PackageManagement.EnvDTE
project.Save();
}
void ThrowExceptionIfFileExists(string filePath)
{
if (fileService.FileExists(filePath)) {
throw new FileExistsException(filePath);
}
}
void CopyFileIntoProject(string oldFileName, string fileName)
{
string newFileName = GetFileNameInProject(fileName);
ThrowExceptionIfFileExists(newFileName);
fileService.CopyFile(oldFileName, newFileName);
}

23
src/AddIns/Misc/PackageManagement/Project/Src/FileExistsException.cs

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
namespace ICSharpCode.PackageManagement
{
public class FileExistsException : Exception
{
public FileExistsException(string fileName)
: base(GetErrorMessage(fileName))
{
}
static string GetErrorMessage(string fileName)
{
fileName = Path.GetFileName(fileName);
string message = "Unable to add '{0}'. A file with that name already exists.";
return String.Format(message, fileName);
}
}
}

1
src/AddIns/Misc/PackageManagement/Project/Src/IPackageManagementFileService.cs

@ -11,5 +11,6 @@ namespace ICSharpCode.PackageManagement @@ -11,5 +11,6 @@ namespace ICSharpCode.PackageManagement
void RemoveDirectory(string path);
void OpenFile(string fileName);
void CopyFile(string oldFileName, string newFileName);
bool FileExists(string fileName);
}
}

6
src/AddIns/Misc/PackageManagement/Project/Src/PackageManagementFileService.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Gui;
@ -48,5 +49,10 @@ namespace ICSharpCode.PackageManagement @@ -48,5 +49,10 @@ namespace ICSharpCode.PackageManagement
FileService.CopyFile(oldFileName, newFileName, isDirectory: false, overwrite: false);
}
}
public bool FileExists(string fileName)
{
return File.Exists(fileName);
}
}
}

21
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/ProjectItemsTests.cs

@ -4,11 +4,13 @@ @@ -4,11 +4,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using ICSharpCode.PackageManagement;
using ICSharpCode.PackageManagement.EnvDTE;
using DTE = ICSharpCode.PackageManagement.EnvDTE;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
using PackageManagement.Tests.Helpers;
using DTE = ICSharpCode.PackageManagement.EnvDTE;
namespace PackageManagement.Tests.EnvDTE
{
@ -306,5 +308,22 @@ namespace PackageManagement.Tests.EnvDTE @@ -306,5 +308,22 @@ namespace PackageManagement.Tests.EnvDTE
Assert.AreEqual("Program.cs", projectItemName);
}
[Test]
public void AddFileFromCopy_FileAlreadyExistsOnFileSystem_ThrowsException()
{
CreateProjectItems();
msbuildProject.FileName = @"d:\projects\myproject\myproject\myproject.csproj";
string fileName = @"d:\projects\myproject\packages\tools\test.cs";
string existingFile = @"d:\projects\myproject\myproject\test.cs";
fakeFileService.ExistingFileNames.Add(existingFile);
Exception ex =
Assert.Throws(typeof(FileExistsException), () => projectItems.AddFromFileCopy(fileName));
bool contains = ex.Message.Contains("'test.cs'");
Assert.IsTrue(contains);
}
}
}

8
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakeFileService.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.PackageManagement;
using ICSharpCode.SharpDevelop.Project;
@ -54,5 +55,12 @@ namespace PackageManagement.Tests.Helpers @@ -54,5 +55,12 @@ namespace PackageManagement.Tests.Helpers
OldFileNamePassedToCopyFile = oldFileName;
NewFileNamePassedToCopyFile = newFileName;
}
public List<string> ExistingFileNames = new List<string>();
public bool FileExists(string fileName)
{
return ExistingFileNames.Contains(fileName);
}
}
}

Loading…
Cancel
Save