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