PageRenderTime 42ms CodeModel.GetById 15ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://github.com/ajadex/SharpDevelop
C# | 342 lines | 262 code | 63 blank | 17 comment | 0 complexity | e8db8a6b80a44cac8a8f2d848e3e31a3 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.PackageManagement;
 22using ICSharpCode.PackageManagement.Scripting;
 23using NuGet;
 24using NUnit.Framework;
 25using PackageManagement.Tests.Helpers;
 26using Rhino.Mocks;
 27
 28namespace PackageManagement.Tests
 29{
 30	[TestFixture]
 31	public class UpdateSolutionPackagesActionTests
 32	{
 33		TestableUpdateSolutionPackagesAction action;
 34		IPackageManagementSolution solution;
 35		List<IPackageManagementProject> projects;
 36		IPackageRepository sourceRepository;
 37		IPackageManagementEvents packageEvents;
 38		
 39		void CreateAction()
 40		{
 41			CreateSolution();
 42			packageEvents = MockRepository.GenerateStub<IPackageManagementEvents>();
 43			action = new TestableUpdateSolutionPackagesAction(solution, packageEvents);
 44		}
 45		
 46		void CreateActionWithOperations(params PackageOperation[] operations)
 47		{
 48			CreateAction();
 49			action.AddOperations(operations);
 50		}
 51		
 52		void CreateSolution()
 53		{
 54			projects = new List<IPackageManagementProject>();
 55			sourceRepository = MockRepository.GenerateStub<IPackageRepository>();
 56			
 57			solution = MockRepository.GenerateStub<IPackageManagementSolution>();
 58			solution
 59				.Stub(s => s.GetProjects(sourceRepository))
 60				.Return(projects);
 61		}
 62		
 63		IPackageFromRepository CreatePackage(string packageId, string version)
 64		{
 65			var package = MockRepository.GenerateStub<IPackageFromRepository>();
 66			package.Stub(p => p.Id).Return(packageId);
 67			package.Stub(p => p.Version).Return(new SemanticVersion(version));
 68			package.Stub(p => p.Repository).Return(sourceRepository);
 69			return package;
 70		}
 71		
 72		void ProjectHasOlderVersionOfPackage(IPackageManagementProject project, IPackageFromRepository package)
 73		{
 74			project.Stub(p => p.HasOlderPackageInstalled(package)).Return(true);
 75		}
 76		
 77		PackageOperation CreateInstallOperationWithFile(string fileName)
 78		{
 79			return PackageOperationHelper.CreateInstallOperationWithFile(fileName);
 80		}
 81		
 82		IPackageScriptRunner CreatePackageScriptRunner()
 83		{
 84			return MockRepository.GenerateStub<IPackageScriptRunner>();
 85		}
 86		
 87		IPackageManagementProject AddProjectToSolution()
 88		{
 89			IPackageManagementProject project = MockRepository.GenerateStub<IPackageManagementProject>();
 90			projects.Add(project);
 91			return project;
 92		}
 93		
 94		IPackageFromRepository AddPackageToAction(string packageId, string version)
 95		{
 96			IPackageFromRepository package = CreatePackage(packageId, version);
 97			action.AddPackages(new IPackageFromRepository[] { package });
 98			return package;
 99		}
100		
101		[Test]
102		public void UpdateDependencies_DefaultValue_IsTrue()
103		{
104			CreateActionWithOperations();
105			
106			bool result = action.UpdateDependencies;
107			
108			Assert.IsTrue(result);
109		}
110		
111		[Test]
112		public void HasPackageScriptsToRun_PackageHasInitPowerShellScript_ReturnsTrue()
113		{
114			PackageOperation operation = CreateInstallOperationWithFile(@"tools\install.ps1");
115			CreateActionWithOperations(operation);
116			
117			bool result = action.HasPackageScriptsToRun();
118			
119			Assert.IsTrue(result);
120		}
121		
122		[Test]
123		public void HasPackageScriptsToRun_PackageHasOneTextFile_ReturnsFalse()
124		{
125			PackageOperation operation = CreateInstallOperationWithFile(@"tools\readme.txt");
126			CreateActionWithOperations(operation);
127			
128			bool result = action.HasPackageScriptsToRun();
129			
130			Assert.IsFalse(result);
131		}
132		
133		[Test]
134		public void Execute_OneProjectThatHasOlderVersionOfPackageBeingUpdated_PackageOperationsAreRun()
135		{
136			PackageOperation operation = CreateInstallOperationWithFile(@"tools\readme.txt");
137			CreateActionWithOperations(operation);
138			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
139			IPackageManagementProject project = AddProjectToSolution();
140			var expectedOperations = new PackageOperation[] { operation };
141			
142			action.Execute();
143			
144			project.AssertWasCalled(p => p.RunPackageOperations(expectedOperations));
145		}
146		
147		[Test]
148		public void Execute_OneProjectThatHasOlderVersionOfPackageBeingUpdated_PackageReferenceUpdatedInProject()
149		{
150			CreateAction();
151			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
152			IPackageManagementProject project = AddProjectToSolution();
153			ProjectHasOlderVersionOfPackage(project, package);
154			
155			action.Execute();
156			
157			project.AssertWasCalled(p => p.UpdatePackageReference(package, action));
158		}
159		
160		[Test]
161		public void Execute_OneProjectThatHasOlderVersionsOfTwoPackagesBeingUpdated_PackageReferenceUpdatedInProjectForBothPackages()
162		{
163			CreateAction();
164			IPackageFromRepository package1 = AddPackageToAction("A", "1.0");
165			IPackageFromRepository package2 = AddPackageToAction("B", "1.0");
166			IPackageManagementProject project = AddProjectToSolution();
167			ProjectHasOlderVersionOfPackage(project, package1);
168			ProjectHasOlderVersionOfPackage(project, package2);
169			
170			action.Execute();
171			
172			project.AssertWasCalled(p => p.UpdatePackageReference(package1, action));
173			project.AssertWasCalled(p => p.UpdatePackageReference(package2, action));
174		}
175		
176		[Test]
177		public void Execute_TwoProjectsThatHaveOlderVersionsOfPackageBeingUpdated_PackageReferenceUpdatedInBothProjects()
178		{
179			CreateAction();
180			IPackageFromRepository package = AddPackageToAction("A", "1.0");
181			IPackageManagementProject project1 = AddProjectToSolution();
182			IPackageManagementProject project2 = AddProjectToSolution();
183			ProjectHasOlderVersionOfPackage(project1, package);
184			ProjectHasOlderVersionOfPackage(project2, package);
185			
186			action.Execute();
187			
188			project1.AssertWasCalled(p => p.UpdatePackageReference(package, action));
189			project2.AssertWasCalled(p => p.UpdatePackageReference(package, action));
190		}
191		
192		[Test]
193		public void Execute_TwoProjectsAndOnlyOneHasOlderVersionsOfPackageBeingUpdated_PackageReferenceUpdatedInOnlyOneProject()
194		{
195			CreateAction();
196			IPackageFromRepository package = AddPackageToAction("A", "1.0");
197			IPackageManagementProject project1 = AddProjectToSolution();
198			IPackageManagementProject project2 = AddProjectToSolution();
199			ProjectHasOlderVersionOfPackage(project2, package);
200			
201			action.Execute();
202			
203			project1.AssertWasNotCalled(p => p.UpdatePackageReference(package, action));
204			project2.AssertWasCalled(p => p.UpdatePackageReference(package, action));
205		}
206		
207		[Test]
208		public void Execute_PackageScriptRunnerSet_RunPackageScriptsActionCreatedUsingPackageScriptRunner()
209		{
210			CreateAction();
211			IPackageScriptRunner expectedRunner = CreatePackageScriptRunner();
212			action.PackageScriptRunner = expectedRunner;
213			AddPackageToAction("Test", "1.0");
214			AddProjectToSolution();
215			
216			action.Execute();
217			
218			IPackageScriptRunner actualRunner = action.ScriptRunnerPassedToCreateRunPackageScriptsAction;
219			Assert.AreEqual(expectedRunner, actualRunner);
220		}
221		
222		[Test]
223		public void Execute_PackageScriptRunnerSet_RunPackageScriptsActionCreatedUsingProjects()
224		{
225			CreateAction();
226			AddPackageToAction("Test", "1.0");
227			IPackageManagementProject project1 = AddProjectToSolution();
228			IPackageManagementProject project2 = AddProjectToSolution();
229			var expectedProjects = new IPackageManagementProject[] {
230				project1,
231				project2
232			};
233			action.PackageScriptRunner = CreatePackageScriptRunner();
234			
235			action.Execute();
236			
237			Assert.AreEqual(expectedProjects, action.ProjectsPassedToCreateRunPackageScriptsAction);
238		}
239		
240		[Test]
241		public void Execute_PackageScriptRunnerSet_RunPackageScriptsActionIsDisposed()
242		{
243			CreateAction();
244			AddPackageToAction("Test", "1.0");
245			AddProjectToSolution();
246			action.PackageScriptRunner = CreatePackageScriptRunner();
247			
248			action.Execute();
249			
250			Assert.IsTrue(action.IsRunPackageScriptsActionDisposed);
251		}
252		
253		[Test]
254		public void Execute_NullPackageScriptRunner_RunPackageScriptsActionIsNotCreated()
255		{
256			CreateAction();
257			AddPackageToAction("Test", "1.0");
258			AddProjectToSolution();
259			action.PackageScriptRunner = null;
260			
261			action.Execute();
262			
263			Assert.IsFalse(action.IsRunPackageScriptsActionCreated);
264		}
265		
266		[Test]
267		public void Execute_PackageScriptRunnerSet_PackageReferenceUpdatedInProject()
268		{
269			CreateAction();
270			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
271			IPackageManagementProject project = AddProjectToSolution();
272			ProjectHasOlderVersionOfPackage(project, package);
273			action.PackageScriptRunner = CreatePackageScriptRunner();
274			
275			action.Execute();
276			
277			project.AssertWasCalled(p => p.UpdatePackageReference(package, action));
278		}
279		
280		[Test]
281		public void Execute_PackageScriptRunnerSet_ProjectsFromSolutionReadOnlyOnce()
282		{
283			CreateAction();
284			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
285			IPackageManagementProject project = AddProjectToSolution();
286			ProjectHasOlderVersionOfPackage(project, package);
287			action.PackageScriptRunner = CreatePackageScriptRunner();
288			
289			action.Execute();
290			
291			solution.AssertWasCalled(
292				s => s.GetProjects(sourceRepository),
293				setupConstraint => setupConstraint.Repeat.Once());
294		}
295		
296		[Test]
297		public void Execute_NullPackageScriptRunner_ProjectsFromSolutionReadOnlyOnce()
298		{
299			CreateAction();
300			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
301			IPackageManagementProject project = AddProjectToSolution();
302			ProjectHasOlderVersionOfPackage(project, package);
303			action.PackageScriptRunner = null;
304			
305			action.Execute();
306			
307			solution.AssertWasCalled(
308				s => s.GetProjects(sourceRepository),
309				setupConstraint => setupConstraint.Repeat.Once());
310		}
311		
312		[Test]
313		public void Execute_TwoProjectsBeingUpdated_LoggerSetOnBothProjects()
314		{
315			CreateAction();
316			ILogger logger = MockRepository.GenerateStub<ILogger>();
317			action.Logger = logger;
318			IPackageFromRepository package = AddPackageToAction("A", "1.0");
319			IPackageManagementProject project1 = AddProjectToSolution();
320			IPackageManagementProject project2 = AddProjectToSolution();
321			
322			action.Execute();
323			
324			Assert.AreEqual(logger, project1.Logger);
325			Assert.AreEqual(logger, project2.Logger);
326		}
327		
328		[Test]
329		public void Execute_OneProjectThatHasOlderVersionOfPackageBeingUpdated_PackagesUpdatedEventIsFired()
330		{
331			PackageOperation operation = CreateInstallOperationWithFile(@"tools\readme.txt");
332			CreateActionWithOperations(operation);
333			IPackageFromRepository package = AddPackageToAction("Test", "1.0");
334			IPackageManagementProject project = AddProjectToSolution();
335			var expectedOperations = new PackageOperation[] { operation };
336			
337			action.Execute();
338			
339			packageEvents.AssertWasCalled(events => events.OnParentPackagesUpdated(action.Packages));
340		}
341	}
342}