public async Task PackagePreFetcher_NoInstallActionsInput() { using (var packagesFolderDir = TestDirectory.Create()) using (var globalPackagesFolder = TestDirectory.Create()) { // Arrange var actions = new List <NuGetProjectAction>(); var packagesFolder = new FolderNuGetProject(packagesFolderDir); var testSettings = new Configuration.NullSettings(); var logger = new TestLogger(); var target = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")); var target2 = new PackageIdentity("packageB", NuGetVersion.Parse("1.0.0")); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(target, packagesFolder)); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(target2, packagesFolder)); // Act using (var cacheContext = new SourceCacheContext()) { var result = await PackagePreFetcher.GetPackagesAsync( actions, packagesFolder, new PackageDownloadContext(cacheContext), globalPackagesFolder, logger, CancellationToken.None); // Assert Assert.Equal(0, result.Count); } } }
public async ValueTask UninstallPackagesAsync( string projectId, IReadOnlyList <PackageIdentity> packageIdentities, CancellationToken cancellationToken) { Assumes.NotNullOrEmpty(projectId); Assumes.NotNullOrEmpty(packageIdentities); cancellationToken.ThrowIfCancellationRequested(); MSBuildNuGetProject project = await GetMsBuildNuGetProjectAsync(projectId, cancellationToken); IEnumerable <NuGetProjectAction>?actions = packageIdentities .Select(packageIdentity => NuGetProjectAction.CreateUninstallProjectAction(packageIdentity, project)); NuGetPackageManager packageManager = await _state.GetPackageManagerAsync(cancellationToken); Assumes.NotNull(packageManager); INuGetProjectContext projectContext = await ServiceLocator.GetComponentModelServiceAsync <INuGetProjectContext>(); Assumes.NotNull(projectContext); await packageManager.ExecuteNuGetProjectActionsAsync( project, actions, projectContext, NullSourceCacheContext.Instance, CancellationToken.None); }
public async Task PackagePreFetcher_NoInstallActionsInput() { using (var packagesFolderDir = TestFileSystemUtility.CreateRandomTestFolder()) { // Arrange var actions = new List <NuGetProjectAction>(); var packagesFolder = new FolderNuGetProject(packagesFolderDir); var testSettings = new Configuration.NullSettings(); var logger = new TestLogger(); var target = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")); var target2 = new PackageIdentity("packageB", NuGetVersion.Parse("1.0.0")); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(target)); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(target2)); // Act var result = await PackagePreFetcher.GetPackagesAsync( actions, packagesFolder, testSettings, logger, CancellationToken.None); // Assert Assert.Equal(0, result.Count); } }
public void ApplyChanges_RemoveUninstalledPackage() { // Arrange var lockFile = new PackagesLockFile { Targets = new List <PackagesLockFileTarget> { new PackagesLockFileTarget { Dependencies = new List <LockFileDependency> { new LockFileDependency { Id = "packageA", ResolvedVersion = NuGetVersion.Parse("1.0.0") } } } } }; var actionList = new List <NuGetProjectAction> { NuGetProjectAction.CreateUninstallProjectAction(new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")), null) }; var contentHashUtility = new Mock <IPackagesConfigContentHashProvider>(); // Act PackagesConfigLockFileUtility.ApplyChanges(lockFile, actionList, contentHashUtility.Object, CancellationToken.None); // Assert Assert.Equal(0, lockFile.Targets[0].Dependencies.Count); }
public async Task PackagePreFetcher_PackageAlreadyExistsReinstall() { using (var sourceDir = TestDirectory.Create()) using (var packagesFolderDir = TestDirectory.Create()) using (var globalPackagesFolder = TestDirectory.Create()) { // Arrange var actions = new List <NuGetProjectAction>(); var packagesFolder = new FolderNuGetProject(packagesFolderDir); var testSettings = NullSettings.Instance; var logger = new TestLogger(); var target = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")); var source = Repository.Factory.GetVisualStudio(new Configuration.PackageSource(sourceDir.Path)); // Add package AddToPackagesFolder(target, packagesFolderDir); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(target, packagesFolder)); actions.Add(NuGetProjectAction.CreateInstallProjectAction(target, source, packagesFolder)); AddToSource(target, sourceDir); // Act using (var cacheContext = new SourceCacheContext()) { var result = await PackagePreFetcher.GetPackagesAsync( actions, packagesFolder, new PackageDownloadContext(cacheContext), globalPackagesFolder, logger, CancellationToken.None); using (var downloadResult = await result[target].GetResultAsync()) { // Assert Assert.Equal(1, result.Count); Assert.False(result[target].InPackagesFolder); Assert.Equal(source.PackageSource, result[target].Source); Assert.Equal(target, result[target].Package); Assert.True(result[target].IsComplete); Assert.Equal(target, downloadResult.PackageReader.GetIdentity()); Assert.NotNull(downloadResult.PackageStream); Assert.Equal(DownloadResourceResultStatus.Available, downloadResult.Status); } } } }
public async Task EnsurePackageCompatibility_WithValidProjectActions_Succeeds() { // Arrange using (var userPackageFolder = TestDirectory.Create()) { var tc = new TestContext(userPackageFolder); await SimpleTestPackageUtility.CreateFolderFeedV3Async( userPackageFolder, PackageSaveMode.Defaultv3, new[] { new SimpleTestPackageContext(tc.PackageIdentityA) { PackageTypes = { PackageType.DotnetCliTool } }, new SimpleTestPackageContext(tc.PackageIdentityB) // Not inspected, because this package is // being uninstalled, not installed. { PackageTypes = { tc.InvalidPackageType } }, new SimpleTestPackageContext(tc.PackageIdentityC) { PackageTypes = { PackageType.Dependency } }, }); // Act & Assert tc.Target.EnsurePackageCompatibility( tc.ProjectKProject, tc.NuGetPathContext.Object, new NuGetProjectAction[] { NuGetProjectAction.CreateInstallProjectAction(tc.PackageIdentityA, tc.SourceRepository, tc.ProjectKProject), NuGetProjectAction.CreateUninstallProjectAction(tc.PackageIdentityB, tc.ProjectKProject), NuGetProjectAction.CreateInstallProjectAction(tc.PackageIdentityC, tc.SourceRepository, tc.ProjectKProject) }, tc.GetRestoreResult(new[] { tc.PackageIdentityA, tc.PackageIdentityB, tc.PackageIdentityC })); } }
public void OnAfterExecuteActions_PackageUninstallAction_PackageUninstalledEventFired() { CreateNuGetProject(); var packageIdentity = new PackageIdentity("Test", NuGetVersion.Parse("1.2")); var actions = new List <NuGetProjectAction> (); var action = NuGetProjectAction.CreateUninstallProjectAction(packageIdentity, project); actions.Add(action); PackageManagementEventArgs eventArgs = null; project.PackageManagementEvents.PackageUninstalled += (sender, e) => { eventArgs = e; }; project.OnAfterExecuteActions(actions); Assert.AreEqual("Test", eventArgs.Id); Assert.AreEqual("1.2", eventArgs.Version.ToString()); Assert.AreEqual(packageIdentity, eventArgs.Package); }
NuGetProjectAction CreateUninstallProjectAction(string packageId) { var package = new PackageIdentity(packageId, null); return(NuGetProjectAction.CreateUninstallProjectAction(package, project)); }
internal static async Task <string> DoUpgradeAsync( INuGetUIContext context, INuGetUI uiService, NuGetProject nuGetProject, IEnumerable <NuGetProjectUpgradeDependencyItem> upgradeDependencyItems, IEnumerable <PackageIdentity> notFoundPackages, IProgress <ProgressDialogData> progress, CancellationToken token) { var startTime = DateTimeOffset.Now; var packagesCount = 0; var status = NuGetOperationStatus.Succeeded; using (var telemetry = new TelemetryActivity(Guid.Empty)) { try { // 0. Fail if any package was not found if (notFoundPackages.Any()) { status = NuGetOperationStatus.Failed; var notFoundPackageIds = string.Join(",", notFoundPackages.Select(t => t.Id)); uiService.ProjectContext.Log(MessageLevel.Error, string.Format(CultureInfo.CurrentCulture, Resources.Migrator_PackageNotFound, notFoundPackageIds)); return(null); } // 1. Backup files (csproj and packages.config) that will change var solutionManager = context.SolutionManager; var msBuildNuGetProject = (MSBuildNuGetProject)nuGetProject; var msBuildNuGetProjectSystem = msBuildNuGetProject.ProjectSystem; var backupPath = string.Empty; try { backupPath = CreateBackup(msBuildNuGetProject, solutionManager.SolutionDirectory); } catch (Exception ex) { status = NuGetOperationStatus.Failed; // log error message uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrader_BackupFailed)); return(null); } // 2. Uninstall all packages currently in packages.config var progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Uninstalling); progress.Report(progressData); // Don't uninstall packages we couldn't find - that will just fail var actions = upgradeDependencyItems.Select(d => d.Package) .Where(p => !notFoundPackages.Contains(p)) .Select(t => NuGetProjectAction.CreateUninstallProjectAction(t, nuGetProject)); try { await context.PackageManager.ExecuteNuGetProjectActionsAsync(nuGetProject, actions, uiService.ProjectContext, NullSourceCacheContext.Instance, CancellationToken.None); } catch (Exception ex) { status = NuGetOperationStatus.Failed; // log error message uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_UninstallFailed)); return(null); } // Reload the project, and get a reference to the reloaded project var uniqueName = msBuildNuGetProjectSystem.ProjectUniqueName; await msBuildNuGetProject.SaveAsync(token); nuGetProject = await solutionManager.GetNuGetProjectAsync(uniqueName); nuGetProject = await solutionManager.UpgradeProjectToPackageReferenceAsync(nuGetProject); // Ensure we use the updated project for installing, and don't display preview or license acceptance windows. context.Projects = new[] { nuGetProject }; var nuGetUI = (NuGetUI)uiService; nuGetUI.Projects = new[] { nuGetProject }; nuGetUI.DisplayPreviewWindow = false; // 4. Install the requested packages var ideExecutionContext = uiService.ProjectContext.ExecutionContext as IDEExecutionContext; if (ideExecutionContext != null) { await ideExecutionContext.SaveExpandedNodeStates(solutionManager); } progressData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage, Resources.NuGetUpgrade_Progress_Installing); progress.Report(progressData); var activeSources = new List <SourceRepository>(); PackageSourceMoniker .PopulateList(context.SourceProvider) .ForEach(s => activeSources.AddRange(s.SourceRepositories)); var packagesToInstall = GetPackagesToInstall(upgradeDependencyItems).ToList(); packagesCount = packagesToInstall.Count; // create low level NuGet actions based on number of packages being installed var lowLevelActions = new List <NuGetProjectAction>(); foreach (var packageIdentity in packagesToInstall) { lowLevelActions.Add(NuGetProjectAction.CreateInstallProjectAction(packageIdentity, activeSources.FirstOrDefault(), nuGetProject)); } try { var buildIntegratedProject = nuGetProject as BuildIntegratedNuGetProject; await context.PackageManager.ExecuteBuildIntegratedProjectActionsAsync( buildIntegratedProject, lowLevelActions, uiService.ProjectContext, token); if (ideExecutionContext != null) { await ideExecutionContext.CollapseAllNodes(solutionManager); } return(backupPath); } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_InstallFailed, backupPath)); uiService.ProjectContext.Log(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Upgrade_RevertSteps, "https://aka.ms/nugetupgraderevertv1")); return(null); } } catch (Exception ex) { status = NuGetOperationStatus.Failed; uiService.ShowError(ex); return(null); } finally { telemetry.TelemetryEvent = VSTelemetryServiceUtility.GetUpgradeTelemetryEvent( uiService.Projects, status, packagesCount); } } }
public async Task PackagePreFetcher_UpdateMultiplePackages() { using (var sourceDir = TestFileSystemUtility.CreateRandomTestFolder()) using (var packagesFolderDir = TestFileSystemUtility.CreateRandomTestFolder()) { // Arrange var actions = new List <NuGetProjectAction>(); var packagesFolder = new FolderNuGetProject(packagesFolderDir); var testSettings = new Configuration.NullSettings(); var logger = new TestLogger(); var targetA1 = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")); var targetA2 = new PackageIdentity("packageA", NuGetVersion.Parse("2.0.0")); var targetB1 = new PackageIdentity("packageB", NuGetVersion.Parse("1.0.0")); var targetB2 = new PackageIdentity("packageB", NuGetVersion.Parse("2.0.0")); var targetC2 = new PackageIdentity("packageC", NuGetVersion.Parse("2.0.0")); var source = Repository.Factory.GetVisualStudio(new Configuration.PackageSource(sourceDir.Path)); // Add packages AddToPackagesFolder(targetA1, packagesFolderDir); AddToPackagesFolder(targetB1, packagesFolderDir); AddToPackagesFolder(targetA2, packagesFolderDir); // Update A and B, install C, A already exists actions.Add(NuGetProjectAction.CreateUninstallProjectAction(targetA1)); actions.Add(NuGetProjectAction.CreateUninstallProjectAction(targetB1)); actions.Add(NuGetProjectAction.CreateInstallProjectAction(targetC2, source)); actions.Add(NuGetProjectAction.CreateInstallProjectAction(targetB2, source)); actions.Add(NuGetProjectAction.CreateInstallProjectAction(targetA2, source)); AddToSource(targetA2, sourceDir); AddToSource(targetB2, sourceDir); AddToSource(targetC2, sourceDir); // Act var result = await PackagePreFetcher.GetPackagesAsync( actions, packagesFolder, testSettings, logger, CancellationToken.None); var resultA2 = await result[targetA2].GetResultAsync(); var resultB2 = await result[targetB2].GetResultAsync(); var resultC2 = await result[targetC2].GetResultAsync(); // Assert Assert.Equal(3, result.Count); Assert.True(result[targetA2].InPackagesFolder); Assert.Null(result[targetA2].Source); Assert.Equal(targetA2, result[targetA2].Package); Assert.True(result[targetA2].IsComplete); Assert.Equal(targetA2, resultA2.PackageReader.GetIdentity()); Assert.NotNull(resultA2.PackageStream); Assert.Equal(DownloadResourceResultStatus.Available, resultA2.Status); Assert.False(result[targetB2].InPackagesFolder); Assert.Equal(source.PackageSource, result[targetB2].Source); Assert.Equal(targetB2, result[targetB2].Package); Assert.True(result[targetB2].IsComplete); Assert.Equal(targetB2, resultB2.PackageReader.GetIdentity()); Assert.NotNull(resultB2.PackageStream); Assert.Equal(DownloadResourceResultStatus.Available, resultB2.Status); Assert.False(result[targetC2].InPackagesFolder); Assert.Equal(source.PackageSource, result[targetC2].Source); Assert.Equal(targetC2, result[targetC2].Package); Assert.True(result[targetC2].IsComplete); Assert.Equal(targetC2, resultC2.PackageReader.GetIdentity()); Assert.NotNull(resultC2.PackageStream); Assert.Equal(DownloadResourceResultStatus.Available, resultC2.Status); } }