PageRenderTime 40ms CodeModel.GetById 17ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/src/AddIns/Misc/PackageManagement/Test/Src/PackageReferencesForProjectTests.cs

https://github.com/ajadex/SharpDevelop
C# | 149 lines | 106 code | 26 blank | 17 comment | 0 complexity | dbcb36b9f5c7c4d25d38f00f0f1687d6 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Collections.Generic;
 21using ICSharpCode.Core;
 22using ICSharpCode.PackageManagement;
 23using ICSharpCode.SharpDevelop.Project;
 24using NuGet;
 25using NUnit.Framework;
 26using PackageManagement.Tests.Helpers;
 27
 28namespace PackageManagement.Tests
 29{
 30	[TestFixture]
 31	public class PackageReferencesForProjectTests
 32	{
 33		PackageReferencesForProject packageReferencesForProject;
 34		FakePackageReferenceFileFactory fakePackageReferenceFileFactory;
 35		FakePackageReferenceFile fakePackageReferenceFile;
 36		FakePackageReferenceInstaller fakePackageReferenceInstaller;
 37		
 38		TestableProject CreateProject()
 39		{
 40			return ProjectHelper.CreateTestProject();
 41		}
 42		
 43		void CreatePackageReferencesForProject(MSBuildBasedProject project)
 44		{
 45			fakePackageReferenceFileFactory = new FakePackageReferenceFileFactory();
 46			fakePackageReferenceFile = fakePackageReferenceFileFactory.FakePackageReferenceFile;
 47			fakePackageReferenceInstaller = new FakePackageReferenceInstaller();
 48			packageReferencesForProject = 
 49				new PackageReferencesForProject(
 50					project,
 51					fakePackageReferenceInstaller,
 52					fakePackageReferenceFileFactory);
 53		}
 54		
 55		void AddPackageReference(string packageId, string version)
 56		{
 57			fakePackageReferenceFile.AddFakePackageReference(packageId, version);
 58		}
 59		
 60		void PackageReferenceCollectionAssertAreEqual(IEnumerable<PackageReference> expected, IEnumerable<PackageReference> actual)
 61		{
 62			IEnumerable<string> expectedAsStrings = ConvertToStrings(expected);
 63			IEnumerable<string> actualAsStrings = ConvertToStrings(actual);
 64			
 65			CollectionAssert.AreEqual(expectedAsStrings, actualAsStrings);
 66		}
 67		
 68		List<string> ConvertToStrings(IEnumerable<PackageReference> packageReferences)
 69		{
 70			var converted = new List<string>();
 71			foreach (PackageReference packageReference in packageReferences) {
 72				string convertedPackageReference = ConvertToString(packageReference);
 73				converted.Add(convertedPackageReference);
 74			}
 75			return converted;
 76		}
 77		
 78		string ConvertToString(PackageReference packageReference)
 79		{
 80			return String.Format(
 81				"Id: {0}, Version: {1}",
 82				packageReference.Id,
 83				packageReference.Version);
 84		}
 85		
 86		[Test]
 87		public void InstallPackages_OnePackageReferenceInPackageConfigFile_OpensPackageConfigFileInProjectDirectory()
 88		{
 89			TestableProject project = CreateProject();
 90			project.FileName = new FileName(@"d:\projects\myproject\myproject.csproj");
 91			CreatePackageReferencesForProject(project);
 92			AddPackageReference("PackageId", "1.0.3.2");
 93			
 94			packageReferencesForProject.InstallPackages();
 95			
 96			string fileName = fakePackageReferenceFileFactory.FileNamePassedToCreatePackageReferenceFile;
 97			string expectedFileName = @"d:\projects\myproject\packages.config";
 98			
 99			Assert.AreEqual(expectedFileName, fileName);
100		}
101		
102		[Test]
103		public void InstallPackages_OnePackageReferenceInPackageConfigFile_OnePackageInstalled()
104		{
105			TestableProject project = CreateProject();
106			CreatePackageReferencesForProject(project);
107			AddPackageReference("PackageId", "1.0.3.2");
108			
109			packageReferencesForProject.InstallPackages();
110			
111			IEnumerable<PackageReference> packageReferencesInstalled = 
112				fakePackageReferenceInstaller.PackageReferencesPassedToInstallPackages;
113			
114			List<PackageReference> expectedPackageReferences = 
115				fakePackageReferenceFile.FakePackageReferences;
116			
117			CollectionAssert.AreEqual(expectedPackageReferences, packageReferencesInstalled);
118		}
119		
120		[Test]
121		public void InstallPackages_OnePackageReferenceInPackageConfigFile_ProjectPassedToInstaller()
122		{
123			TestableProject expectedProject = CreateProject();
124			CreatePackageReferencesForProject(expectedProject);
125			AddPackageReference("PackageId", "1.0.3.2");
126			
127			packageReferencesForProject.InstallPackages();
128			
129			MSBuildBasedProject project = fakePackageReferenceInstaller.ProjectPassedToInstallPackages;
130			
131			Assert.AreEqual(expectedProject, project);
132		}
133		
134		[Test]
135		public void RemovePackageReferences_TwoPackageReferencesInPackageConfigFile_PackageReferenceFileIsDeleted()
136		{			
137			TestableProject project = CreateProject();
138			CreatePackageReferencesForProject(project);
139			AddPackageReference("One", "1.0.3.2");
140			AddPackageReference("Two", "2.0.44");
141			
142			packageReferencesForProject.RemovePackageReferences();
143			
144			bool deleted = fakePackageReferenceFile.IsDeleteCalled;
145			
146			Assert.IsTrue(deleted);
147		}
148	}
149}