diff options
author | Matt Ward <matt.ward@xamarin.com> | 2017-07-14 14:00:57 +0300 |
---|---|---|
committer | Matt Ward <matt.ward@xamarin.com> | 2017-07-14 14:00:57 +0300 |
commit | 87ba23cf6d42de897bac006ab467e3c6848f3eb3 (patch) | |
tree | e6c20913517e1027ee0a62b77a2e5319a248123c /main/src | |
parent | 41a90e6dffcccb8a23aa2f15543c0d48e3ab8d1b (diff) |
[NuGet] Add more unit tests for DotNetCoreNuGetProject
Added missing tests for the PostProcessAsync method before making
changes to this method.
Diffstat (limited to 'main/src')
8 files changed, 245 insertions, 3 deletions
diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/FakeMonoDevelopBuildIntegratedRestorer.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/FakeMonoDevelopBuildIntegratedRestorer.cs new file mode 100644 index 0000000000..d7cb8c943d --- /dev/null +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/FakeMonoDevelopBuildIntegratedRestorer.cs @@ -0,0 +1,47 @@ +// +// FakeMonoDevelopBuildIntegratedRestorer.cs +// +// Author: +// Matt Ward <matt.ward@xamarin.com> +// +// Copyright (c) 2017 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Threading; +using System.Threading.Tasks; +using NuGet.ProjectManagement.Projects; + +namespace MonoDevelop.PackageManagement.Tests.Helpers +{ + class FakeMonoDevelopBuildIntegratedRestorer : IMonoDevelopBuildIntegratedRestorer + { + public bool LockFileChanged { get; set; } + + public BuildIntegratedNuGetProject ProjectRestored; + + public Task RestorePackages ( + BuildIntegratedNuGetProject project, + CancellationToken cancellationToken) + { + ProjectRestored = project; + return Task.FromResult (0); + } + } +} diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/TestableDotNetCoreNuGetProject.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/TestableDotNetCoreNuGetProject.cs index 6380f38036..3ccbecf181 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/TestableDotNetCoreNuGetProject.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.Helpers/TestableDotNetCoreNuGetProject.cs @@ -34,6 +34,7 @@ namespace MonoDevelop.PackageManagement.Tests.Helpers public TestableDotNetCoreNuGetProject (DotNetProject project) : base (project, new [] { "netcoreapp1.0" }) { + BuildIntegratedRestorer = new FakeMonoDevelopBuildIntegratedRestorer (); } public bool IsSaved { get; set; } @@ -43,5 +44,14 @@ namespace MonoDevelop.PackageManagement.Tests.Helpers IsSaved = true; return Task.FromResult (0); } + + public FakeMonoDevelopBuildIntegratedRestorer BuildIntegratedRestorer; + public Solution SolutionUsedToCreateBuildIntegratedRestorer; + + protected override IMonoDevelopBuildIntegratedRestorer CreateBuildIntegratedRestorer (Solution solution) + { + SolutionUsedToCreateBuildIntegratedRestorer = solution; + return BuildIntegratedRestorer; + } } } diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj index 6d12c04bbb..98dbda9e20 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj @@ -210,6 +210,7 @@ <Compile Include="MonoDevelop.PackageManagement.Tests.Helpers\TestableUninstallNuGetPackagesAction.cs" /> <Compile Include="MonoDevelop.PackageManagement.Tests\PackageReferenceNuGetProjectTests.cs" /> <Compile Include="MonoDevelop.PackageManagement.Tests.Helpers\TestablePackageReferenceNuGetProject.cs" /> + <Compile Include="MonoDevelop.PackageManagement.Tests.Helpers\FakeMonoDevelopBuildIntegratedRestorer.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\..\core\MonoDevelop.Core\MonoDevelop.Core.csproj"> diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/DotNetCoreNuGetProjectTests.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/DotNetCoreNuGetProjectTests.cs index e9148330f5..d6cb64c80c 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/DotNetCoreNuGetProjectTests.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/DotNetCoreNuGetProjectTests.cs @@ -30,6 +30,7 @@ using System.Threading.Tasks; using MonoDevelop.PackageManagement.Tests.Helpers; using MonoDevelop.Projects; using NUnit.Framework; +using NuGet.PackageManagement; using NuGet.Packaging.Core; using NuGet.ProjectManagement; using NuGet.ProjectModel; @@ -44,6 +45,7 @@ namespace MonoDevelop.PackageManagement.Tests TestableDotNetCoreNuGetProject project; FakeNuGetProjectContext context; DependencyGraphCacheContext dependencyGraphCacheContext; + FakeMonoDevelopBuildIntegratedRestorer buildIntegratedRestorer; void CreateNuGetProject (string projectName = "MyProject", string fileName = @"d:\projects\MyProject\MyProject.csproj") { @@ -52,6 +54,7 @@ namespace MonoDevelop.PackageManagement.Tests dotNetProject.Name = projectName; dotNetProject.FileName = fileName.ToNativePath (); project = new TestableDotNetCoreNuGetProject (dotNetProject); + buildIntegratedRestorer = project.BuildIntegratedRestorer; } void AddDotNetProjectPackageReference (string packageId, string version) @@ -86,6 +89,12 @@ namespace MonoDevelop.PackageManagement.Tests return specs.Single (); } + void OnAfterExecuteActions (string packageId, string version, NuGetProjectActionType actionType) + { + var action = new FakeNuGetProjectAction (packageId, version, actionType); + project.OnAfterExecuteActions (new [] { action }); + } + [Test] public async Task GetInstalledPackagesAsync_OnePackageReference_ReturnsOnePackageReference () { @@ -255,5 +264,133 @@ namespace MonoDevelop.PackageManagement.Tests Assert.IsTrue (packageReference.IsFloating ()); Assert.AreEqual ("2.6.0-*", packageReference.AllowedVersions.Float.ToString ()); } + + [Test] + public async Task PostProcessAsync_ProjectAssetsFile_NotifyChangeInAssetsFile () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + dotNetProject.BaseIntermediateOutputPath = @"d:\projects\MyProject\obj".ToNativePath (); + string fileNameChanged = null; + PackageManagementServices.PackageManagementEvents.FileChanged += (sender, e) => { + fileNameChanged = e.Single ().FileName; + }; + + await project.PostProcessAsync (context, CancellationToken.None); + + string expectedFileNameChanged = @"d:\projects\MyProject\obj\project.assets.json".ToNativePath (); + Assert.AreEqual (expectedFileNameChanged, fileNameChanged); + } + + [Test] + public async Task PostProcessAsync_References_NotifyReferencesChangedEventFired () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + string modifiedHint = null; + dotNetProject.Modified += (sender, e) => { + modifiedHint = e.Single ().Hint; + }; + + await project.PostProcessAsync (context, CancellationToken.None); + + Assert.AreEqual ("References", modifiedHint); + } + + [Test] + public async Task PostProcessAsync_RestoreRunLockFileNotChanged_NotifyReferencesChangedEventFired () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + var solution = new Solution (); + solution.RootFolder.AddItem (dotNetProject); + string modifiedHint = null; + dotNetProject.Modified += (sender, e) => { + modifiedHint = e.Single ().Hint; + }; + OnAfterExecuteActions ("NUnit", "2.6.3", NuGetProjectActionType.Install); + + await project.PostProcessAsync (context, CancellationToken.None); + + Assert.AreEqual (solution, project.SolutionUsedToCreateBuildIntegratedRestorer); + Assert.AreEqual (project, buildIntegratedRestorer.ProjectRestored); + Assert.AreEqual ("References", modifiedHint); + } + + [Test] + public async Task PostProcessAsync_RestoreRunLockFileNotChanged_NotifyChangeInAssetsFile () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + dotNetProject.BaseIntermediateOutputPath = @"d:\projects\MyProject\obj".ToNativePath (); + string fileNameChanged = null; + PackageManagementServices.PackageManagementEvents.FileChanged += (sender, e) => { + fileNameChanged = e.Single ().FileName; + }; + OnAfterExecuteActions ("NUnit", "2.6.3", NuGetProjectActionType.Install); + + await project.PostProcessAsync (context, CancellationToken.None); + + string expectedFileNameChanged = @"d:\projects\MyProject\obj\project.assets.json".ToNativePath (); + Assert.AreEqual (expectedFileNameChanged, fileNameChanged); + Assert.AreEqual (project, buildIntegratedRestorer.ProjectRestored); + } + + /// <summary> + /// Build restorer would trigger the notification itself. + /// </summary> + [Test] + public async Task PostProcessAsync_RestoreRunLockFileChanged_NotifyReferencesChangedEventNotFired () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + string modifiedHint = null; + dotNetProject.Modified += (sender, e) => { + modifiedHint = e.Single ().Hint; + }; + buildIntegratedRestorer.LockFileChanged = true; + OnAfterExecuteActions ("NUnit", "2.6.3", NuGetProjectActionType.Install); + + await project.PostProcessAsync (context, CancellationToken.None); + + Assert.AreEqual (project, buildIntegratedRestorer.ProjectRestored); + Assert.IsNull (modifiedHint); + } + + /// <summary> + /// Build restorer would trigger the notification itself. + /// </summary> + [Test] + public async Task PostProcessAsync_RestoreRunLockFileChanged_NotifyChangeInAssetsFileIsNotMade () + { + CreateNuGetProject (); + AddDotNetProjectPackageReference ("NUnit", "2.6.0"); + dotNetProject.BaseIntermediateOutputPath = @"d:\projects\MyProject\obj".ToNativePath (); + string fileNameChanged = null; + PackageManagementServices.PackageManagementEvents.FileChanged += (sender, e) => { + fileNameChanged = e.Single ().FileName; + }; + buildIntegratedRestorer.LockFileChanged = true; + OnAfterExecuteActions ("NUnit", "2.6.3", NuGetProjectActionType.Install); + + await project.PostProcessAsync (context, CancellationToken.None); + + Assert.IsNull (fileNameChanged); + Assert.AreEqual (project, buildIntegratedRestorer.ProjectRestored); + } + + [Test] + public void NotifyProjectReferencesChanged_References_NotifyReferencesChangedEventFired () + { + CreateNuGetProject (); + string modifiedHint = null; + dotNetProject.Modified += (sender, e) => { + modifiedHint = e.Single ().Hint; + }; + + project.NotifyProjectReferencesChanged (); + + Assert.AreEqual ("References", modifiedHint); + } } } diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.csproj b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.csproj index c14285774d..20f387686f 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.csproj +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.csproj @@ -425,6 +425,7 @@ <Compile Include="MonoDevelop.PackageManagement\PendingPackageActionsInformation.cs" /> <Compile Include="MonoDevelop.PackageManagement\PackageActionType.cs" /> <Compile Include="MonoDevelop.PackageManagement\PendingPackageActionsHandler.cs" /> + <Compile Include="MonoDevelop.PackageManagement\IMonoDevelopBuildIntegratedRestorer.cs" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="MonoDevelop.PackageManagement.addin.xml" /> diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/DotNetCoreNuGetProject.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/DotNetCoreNuGetProject.cs index 701ea37597..659798a1b2 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/DotNetCoreNuGetProject.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/DotNetCoreNuGetProject.cs @@ -292,8 +292,7 @@ namespace MonoDevelop.PackageManagement async Task RestorePackages (INuGetProjectContext nuGetProjectContext, CancellationToken token) { var packageRestorer = await Runtime.RunInMainThread (() => { - var solutionManager = PackageManagementServices.Workspace.GetSolutionManager (project.ParentSolution); - return new MonoDevelopBuildIntegratedRestorer (solutionManager); + return CreateBuildIntegratedRestorer (project.ParentSolution); }); var restoreTask = packageRestorer.RestorePackages (this, token); @@ -312,6 +311,12 @@ namespace MonoDevelop.PackageManagement await base.PostProcessAsync (nuGetProjectContext, token); } + protected virtual IMonoDevelopBuildIntegratedRestorer CreateBuildIntegratedRestorer (Solution solution) + { + var solutionManager = PackageManagementServices.Workspace.GetSolutionManager (project.ParentSolution); + return new MonoDevelopBuildIntegratedRestorer (solutionManager); + } + public void OnBeforeUninstall (IEnumerable<NuGetProjectAction> actions) { } diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/IMonoDevelopBuildIntegratedRestorer.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/IMonoDevelopBuildIntegratedRestorer.cs new file mode 100644 index 0000000000..1977713aea --- /dev/null +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/IMonoDevelopBuildIntegratedRestorer.cs @@ -0,0 +1,41 @@ +// +// IMonoDevelopBuildIntegratedRestorer.cs +// +// Author: +// Matt Ward <matt.ward@xamarin.com> +// +// Copyright (c) 2017 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Threading;
+using System.Threading.Tasks; +using NuGet.ProjectManagement.Projects; + +namespace MonoDevelop.PackageManagement +{ + interface IMonoDevelopBuildIntegratedRestorer + { + Task RestorePackages ( + BuildIntegratedNuGetProject project, + CancellationToken cancellationToken); + + bool LockFileChanged { get; } + } +} diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MonoDevelopBuildIntegratedRestorer.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MonoDevelopBuildIntegratedRestorer.cs index f11a4b1082..86aafc4c9a 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MonoDevelopBuildIntegratedRestorer.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/MonoDevelopBuildIntegratedRestorer.cs @@ -44,7 +44,7 @@ using NuGet.Protocol.Core.Types; namespace MonoDevelop.PackageManagement
{
- internal class MonoDevelopBuildIntegratedRestorer
+ internal class MonoDevelopBuildIntegratedRestorer : IMonoDevelopBuildIntegratedRestorer
{
IPackageManagementEvents packageManagementEvents;
List<SourceRepository> sourceRepositories;
|