PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ajadex/SharpDevelop
C# | 189 lines | 137 code | 35 blank | 17 comment | 0 complexity | e7bccac6128c04798ef6dc118b3e9e42 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.Design;
 23using NuGet;
 24using NUnit.Framework;
 25using PackageManagement.Tests.Helpers;
 26
 27namespace PackageManagement.Tests
 28{
 29	[TestFixture]
 30	public class PackageActionRunnerTests
 31	{
 32		FakePackageActionRunner fakeConsoleActionRunner;
 33		PackageActionRunner runner;
 34		FakeInstallPackageAction fakeAction;
 35		FakePowerShellDetection powerShellDetection;
 36		FakePackageManagementEvents fakeEvents;
 37		List<FakeInstallPackageAction> fakeActions;
 38		
 39		void CreateRunner()
 40		{
 41			fakeConsoleActionRunner = new FakePackageActionRunner();
 42			powerShellDetection = new FakePowerShellDetection();
 43			fakeEvents = new FakePackageManagementEvents();
 44			fakeActions = new List<FakeInstallPackageAction>();
 45			runner = new PackageActionRunner(fakeConsoleActionRunner, fakeEvents, powerShellDetection);
 46		}
 47		
 48		void CreateInstallActionWithNoPowerShellScripts()
 49		{
 50			var fakeProject = new FakePackageManagementProject();
 51			fakeAction = new FakeInstallPackageAction(fakeProject);
 52			fakeAction.Operations = new PackageOperation[0];
 53			fakeActions.Add(fakeAction);
 54		}
 55		
 56		void CreateInstallActionWithOnePowerShellScript()
 57		{
 58			CreateInstallActionWithNoPowerShellScripts();
 59			
 60			fakeAction.Operations =
 61				PackageOperationHelper.CreateListWithOneInstallOperationWithFile(@"tools\init.ps1");
 62			fakeActions.Add(fakeAction);
 63		}
 64		
 65		void Run()
 66		{
 67			runner.Run(fakeAction);
 68		}
 69		
 70		void RunMultipleActions()
 71		{
 72			runner.Run(fakeActions);
 73		}
 74		
 75		[Test]
 76		public void Run_InstallActionHasNoPowerShellScripts_ActionIsExecutedDirectly()
 77		{
 78			CreateRunner();
 79			CreateInstallActionWithNoPowerShellScripts();
 80			Run();
 81			
 82			bool executed = fakeAction.IsExecuteCalled;
 83			
 84			Assert.IsTrue(executed);
 85		}
 86		
 87		[Test]
 88		public void Run_InstallActionHasOnePowerShellScript_ActionIsPassedToConsoleToRun()
 89		{
 90			CreateRunner();
 91			CreateInstallActionWithOnePowerShellScript();
 92			powerShellDetection.IsPowerShell2InstalledReturnValue = true;
 93			Run();
 94			
 95			IPackageAction action = fakeConsoleActionRunner.ActionPassedToRun;
 96			
 97			Assert.AreEqual(fakeAction, action);
 98		}
 99		
100		[Test]
101		public void Run_InstallActionHasOnePowerShellScript_ActionIsNotExecutedDirectly()
102		{
103			CreateRunner();
104			CreateInstallActionWithOnePowerShellScript();
105			powerShellDetection.IsPowerShell2InstalledReturnValue = true;
106			Run();
107			
108			bool executed = fakeAction.IsExecuteCalled;
109			
110			Assert.IsFalse(executed);
111		}
112		
113		[Test]
114		public void Run_InstallActionHasOnePowerShellScriptAndPowerShellIsNotInstalled_ActionIsExecutedDirectly()
115		{
116			CreateRunner();
117			CreateInstallActionWithOnePowerShellScript();
118			powerShellDetection.IsPowerShell2InstalledReturnValue = false;
119			
120			Run();
121			
122			bool executed = fakeAction.IsExecuteCalled;
123			
124			Assert.IsTrue(executed);
125		}
126		
127		[Test]
128		public void Run_InstallActionHasOnePowerShellScriptAndPowerShellIsNotInstalled_MessageIsReportedThatPowerShellScriptsCannotBeRun()
129		{
130			CreateRunner();
131			CreateInstallActionWithOnePowerShellScript();
132			powerShellDetection.IsPowerShell2InstalledReturnValue = false;
133			
134			Run();
135			
136			string message = fakeEvents.FormattedStringPassedToOnPackageOperationMessageLogged;
137			string expectedMessage = 
138				"PowerShell is not installed. PowerShell scripts will not be run for the package.";
139			
140			Assert.AreEqual(expectedMessage, message);
141		}
142		
143		[Test]
144		public void Run_TwoInstallActionsWithoutPowerShellScripts_ActionsAreExecutedDirectly()
145		{
146			CreateRunner();
147			CreateInstallActionWithNoPowerShellScripts();
148			CreateInstallActionWithNoPowerShellScripts();
149			RunMultipleActions();
150			
151			bool firstActionIsExecuted = fakeActions[0].IsExecuteCalled;
152			bool secondActionIsExecuted = fakeActions[1].IsExecuteCalled;
153			
154			Assert.IsTrue(firstActionIsExecuted);
155			Assert.IsTrue(secondActionIsExecuted);
156		}
157		
158		[Test]
159		public void Run_TwoInstallActionsAndSecondHasOnePowerShellScript_AllActionsPassedToConsoleToRun()
160		{
161			CreateRunner();
162			CreateInstallActionWithNoPowerShellScripts();
163			CreateInstallActionWithOnePowerShellScript();
164			powerShellDetection.IsPowerShell2InstalledReturnValue = true;
165			RunMultipleActions();
166			
167			IEnumerable<IPackageAction> actions = fakeConsoleActionRunner.ActionsRunInOneCall;
168			
169			CollectionAssert.AreEqual(fakeActions, actions);
170		}
171		
172		[Test]
173		public void Run_TwoInstallActionsBothWithOnePowerShellScriptsAndPowerShellIsNotInstalled_ActionsAreExecutedDirectly()
174		{
175			CreateRunner();
176			CreateInstallActionWithOnePowerShellScript();
177			CreateInstallActionWithOnePowerShellScript();
178			powerShellDetection.IsPowerShell2InstalledReturnValue = false;
179			
180			RunMultipleActions();
181			
182			bool firstActionIsExecuted = fakeActions[0].IsExecuteCalled;
183			bool secondActionIsExecuted = fakeActions[1].IsExecuteCalled;
184			
185			Assert.IsTrue(firstActionIsExecuted);
186			Assert.IsTrue(secondActionIsExecuted);
187		}
188	}
189}