/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/InstallPackageWithAvailableItemNameTests.cs

http://github.com/mono/monodevelop · C# · 182 lines · 122 code · 35 blank · 25 comment · 0 complexity · 7feafafc90680b57f668bb687fab4ca3 MD5 · raw file

  1. //
  2. // InstallPackageWithAvailableItemNameTests.cs
  3. //
  4. // Author:
  5. // Matt Ward <matt.ward@microsoft.com>
  6. //
  7. // Copyright (c) 2019 Microsoft
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System.Linq;
  27. using System.Threading.Tasks;
  28. using MonoDevelop.PackageManagement.Tests.Helpers;
  29. using MonoDevelop.Projects;
  30. using NuGet.Packaging.Core;
  31. using NuGet.Versioning;
  32. using NUnit.Framework;
  33. using UnitTests;
  34. namespace MonoDevelop.PackageManagement.Tests
  35. {
  36. [TestFixture]
  37. public class InstallPackageWithAvailableItemNameTests : RestoreTestBase
  38. {
  39. [Test]
  40. public async Task InstallPackage_PackageDefinesCustomAvailableItemNames_BuildActionsIncludeCustomAvailableItemNames ()
  41. {
  42. string solutionFileName = Util.GetSampleProject ("RestoreStylePackageReference", "RestoreStylePackageReference.sln");
  43. using (solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName)) {
  44. CreateNuGetConfigFile (solution.BaseDirectory);
  45. var project = (DotNetProject)solution.FindProjectByName ("RestoreStylePackageReference");
  46. var originalBuildActions = project.GetBuildActions ();
  47. await InstallNuGetPackage (project, "Test.Xam.AvailableItemName", "0.1.0");
  48. var updatedBuildActions = project.GetBuildActions ();
  49. Assert.IsFalse (originalBuildActions.Contains ("TestXamAvailableItem"));
  50. Assert.That (updatedBuildActions, Contains.Item ("TestXamAvailableItem"));
  51. }
  52. }
  53. [Test]
  54. public async Task UpdatePackage_PackageDefinesCustomAvailableItemNames_BuildActionsIncludeUpdateAvailableItemNames ()
  55. {
  56. string solutionFileName = Util.GetSampleProject ("NuGetUpdateAvailableItems", "NuGetUpdateAvailableItems.sln");
  57. using (solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName)) {
  58. CreateNuGetConfigFile (solution.BaseDirectory);
  59. await RestoreNuGetPackages (solution);
  60. var packageReferenceProject = (DotNetProject)solution.FindProjectByName ("PackageReferenceProject");
  61. var sdkStyleProject = (DotNetProject)solution.FindProjectByName ("SdkStyleProject");
  62. var originalBuildActions = packageReferenceProject.GetBuildActions ();
  63. Assert.That (originalBuildActions, Contains.Item ("TestXamAvailableItem"));
  64. Assert.IsFalse (originalBuildActions.Contains ("TestXamAvailableItemUpdated"));
  65. originalBuildActions = sdkStyleProject.GetBuildActions ();
  66. Assert.That (originalBuildActions, Contains.Item ("TestXamAvailableItem"));
  67. Assert.IsFalse (originalBuildActions.Contains ("TestXamAvailableItemUpdated"));
  68. await UpdateNuGetPackage (packageReferenceProject, "Test.Xam.AvailableItemName");
  69. await UpdateNuGetPackage (sdkStyleProject, "Test.Xam.AvailableItemName");
  70. var updatedBuildActions = packageReferenceProject.GetBuildActions ();
  71. Assert.That (updatedBuildActions, Contains.Item ("TestXamAvailableItemUpdated"));
  72. updatedBuildActions = sdkStyleProject.GetBuildActions ();
  73. Assert.That (updatedBuildActions, Contains.Item ("TestXamAvailableItemUpdated"));
  74. }
  75. }
  76. [Test]
  77. public async Task UpdateMultiplePackages_PackageDefinesCustomAvailableItemNames_BuildActionsIncludeUpdateAvailableItemNames ()
  78. {
  79. string solutionFileName = Util.GetSampleProject ("NuGetUpdateAvailableItems", "NuGetUpdateAvailableItems.sln");
  80. using (solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName)) {
  81. CreateNuGetConfigFile (solution.BaseDirectory);
  82. await RestoreNuGetPackages (solution);
  83. var packageReferenceProject = (DotNetProject)solution.FindProjectByName ("PackageReferenceProject");
  84. var sdkStyleProject = (DotNetProject)solution.FindProjectByName ("SdkStyleProject");
  85. var originalBuildActions = packageReferenceProject.GetBuildActions ();
  86. Assert.That (originalBuildActions, Contains.Item ("TestXamAvailableItem"));
  87. Assert.IsFalse (originalBuildActions.Contains ("TestXamAvailableItemUpdated"));
  88. originalBuildActions = sdkStyleProject.GetBuildActions ();
  89. Assert.That (originalBuildActions, Contains.Item ("TestXamAvailableItem"));
  90. Assert.IsFalse (originalBuildActions.Contains ("TestXamAvailableItemUpdated"));
  91. var projects = new [] { packageReferenceProject, sdkStyleProject };
  92. await UpdateNuGetPackage (projects, "Test.Xam.AvailableItemName", "0.2.0");
  93. var updatedBuildActions = packageReferenceProject.GetBuildActions ();
  94. Assert.That (updatedBuildActions, Contains.Item ("TestXamAvailableItemUpdated"));
  95. updatedBuildActions = sdkStyleProject.GetBuildActions ();
  96. Assert.That (updatedBuildActions, Contains.Item ("TestXamAvailableItemUpdated"));
  97. }
  98. }
  99. Task InstallNuGetPackage (DotNetProject project, string packageId, string packageVersion)
  100. {
  101. var solutionManager = new MonoDevelopSolutionManager (project.ParentSolution);
  102. var context = CreateNuGetProjectContext (solutionManager.Settings);
  103. var sources = solutionManager.CreateSourceRepositoryProvider ().GetRepositories ();
  104. var action = new InstallNuGetPackageAction (sources, solutionManager, new DotNetProjectProxy (project), context);
  105. action.LicensesMustBeAccepted = false;
  106. action.OpenReadmeFile = false;
  107. action.PackageId = packageId;
  108. action.Version = NuGetVersion.Parse (packageVersion);
  109. return Task.Run (() => {
  110. action.Execute ();
  111. });
  112. }
  113. Task UpdateNuGetPackage (DotNetProject project, string packageId)
  114. {
  115. var solutionManager = new MonoDevelopSolutionManager (project.ParentSolution);
  116. var context = CreateNuGetProjectContext (solutionManager.Settings);
  117. var packageManager = new MonoDevelopNuGetPackageManager (solutionManager);
  118. var action = new UpdateNuGetPackageAction (
  119. solutionManager,
  120. new DotNetProjectProxy (project),
  121. context,
  122. packageManager,
  123. PackageManagementServices.PackageManagementEvents) {
  124. PackageId = packageId
  125. };
  126. return Task.Run (() => {
  127. action.Execute ();
  128. });
  129. }
  130. Task UpdateNuGetPackage (DotNetProject[] projects, string packageId, string packageVersion)
  131. {
  132. var solutionManager = new MonoDevelopSolutionManager (projects [0].ParentSolution);
  133. var context = CreateNuGetProjectContext (solutionManager.Settings);
  134. var sources = solutionManager.CreateSourceRepositoryProvider ().GetRepositories ();
  135. var action = new UpdateMultipleNuGetPackagesAction (sources, solutionManager, context);
  136. action.LicensesMustBeAccepted = false;
  137. foreach (DotNetProject project in projects) {
  138. action.AddProject (new DotNetProjectProxy (project));
  139. }
  140. var packageIdentity = new PackageIdentity (packageId, NuGetVersion.Parse (packageVersion));
  141. action.AddPackageToUpdate (packageIdentity);
  142. return Task.Run (() => {
  143. action.Execute ();
  144. });
  145. }
  146. }
  147. }