public async Task TestPackageRestoredEvent()
        {
            // Arrange
            using (var testSolutionManager = new TestSolutionManager(true))
            {
                var projectA = testSolutionManager.AddNewMSBuildProject();
                var projectB = testSolutionManager.AddNewMSBuildProject();

                var packageIdentity          = Packages[0];
                var testNuGetProjectContext  = new TestNuGetProjectContext();
                var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
                var testSettings             = Configuration.NullSettings.Instance;
                var resolutionContext        = new ResolutionContext();
                var token = CancellationToken.None;

                var deleteOnRestartManager = new TestDeleteOnRestartManager();
                var nuGetPackageManager    = new NuGetPackageManager(
                    sourceRepositoryProvider,
                    testSettings,
                    testSolutionManager,
                    deleteOnRestartManager);

                await nuGetPackageManager.InstallPackageAsync(projectA, packageIdentity,
                                                              resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token);

                await nuGetPackageManager.InstallPackageAsync(projectB, packageIdentity,
                                                              resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token);

                var packageRestoreManager = new PackageRestoreManager(
                    sourceRepositoryProvider,
                    testSettings,
                    testSolutionManager);
                var restoredPackages = new List <PackageIdentity>();
                packageRestoreManager.PackageRestoredEvent += delegate(object sender, PackageRestoredEventArgs args)
                {
                    if (args.Restored)
                    {
                        restoredPackages.Add(args.Package);
                    }
                };

                Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(packageIdentity));

                // Delete packages folder
                TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages"));

                Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity)));

                // Act
                await packageRestoreManager.RestoreMissingPackagesInSolutionAsync(testSolutionManager.SolutionDirectory,
                                                                                  testNuGetProjectContext,
                                                                                  new TestLogger(),
                                                                                  CancellationToken.None);

                Assert.Equal(1, restoredPackages.Count);
                Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity)));
            }
        }
Exemple #2
0
 public Task <PackageRestoreResult> RestoreMissingPackagesInSolutionAsync(
     string solutionDirectory,
     INuGetProjectContext nuGetProjectContext,
     CancellationToken token)
 {
     return(restoreManager.RestoreMissingPackagesInSolutionAsync(
                solutionDirectory,
                nuGetProjectContext,
                token
                ));
 }
        protected static Task <PackageRestoreResult> RestoreNuGetPackages(Solution solution)
        {
            var solutionManager = new MonoDevelopSolutionManager(solution);
            var context         = new FakeNuGetProjectContext {
                LogToConsole = true
            };

            var restoreManager = new PackageRestoreManager(
                solutionManager.CreateSourceRepositoryProvider(),
                solutionManager.Settings,
                solutionManager
                );

            return(restoreManager.RestoreMissingPackagesInSolutionAsync(
                       solutionManager.SolutionDirectory,
                       context,
                       CancellationToken.None));
        }
        public async Task <bool> UIRestorePackagesAsync(CancellationToken token)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            ShowProgressUI();
            OperationId = Guid.NewGuid();

            try
            {
                PackageRestoreManager.PackageRestoreFailedEvent += PackageRestoreFailedEvent;
                var solutionDirectory = SolutionManager.SolutionDirectory;
                await PackageRestoreManager.RestoreMissingPackagesInSolutionAsync(solutionDirectory,
                                                                                  this,
                                                                                  new LoggerAdapter(this),
                                                                                  token);

                if (RestoreException == null)
                {
                    // when the control is first loaded, check for missing packages
                    await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, token);
                }
                else
                {
                    ShowErrorUI(RestoreException.Message);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                ShowErrorUI(ex.Message);
                return(false);
            }
            finally
            {
                PackageRestoreManager.PackageRestoreFailedEvent -= PackageRestoreFailedEvent;
                RestoreException = null;
            }

            NuGetEventTrigger.Instance.TriggerEvent(NuGetEvent.PackageRestoreCompleted);
            return(true);
        }
Exemple #5
0
        public async Task TestRestoreMissingPackages()
        {
            // Arrange
            var testSolutionManager = new TestSolutionManager();
            var projectA            = testSolutionManager.AddNewMSBuildProject();
            var projectB            = testSolutionManager.AddNewMSBuildProject();

            var packageIdentity          = Packages[0];
            var testNuGetProjectContext  = new TestNuGetProjectContext();
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var testSettings             = NullSettings.Instance;
            var resolutionContext        = new ResolutionContext();
            var token = CancellationToken.None;

            var nuGetPackageManager = new NuGetPackageManager(sourceRepositoryProvider, testSettings, testSolutionManager);

            await nuGetPackageManager.InstallPackageAsync(projectA, packageIdentity,
                                                          resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token);

            await nuGetPackageManager.InstallPackageAsync(projectB, packageIdentity,
                                                          resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token);

            var packageRestoreManager = new PackageRestoreManager(sourceRepositoryProvider, testSettings, testSolutionManager);

            Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(packageIdentity));

            // Delete packages folder
            Directory.Delete(Path.Combine(testSolutionManager.SolutionDirectory, "packages"), recursive: true);

            Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity)));

            // Act
            await packageRestoreManager.RestoreMissingPackagesInSolutionAsync(CancellationToken.None);

            Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity)));
        }
        public async Task Test_PackageRestoreFailure_WithRaisedEvents()
        {
            // Arrange
            using (var testSolutionManager = new TestSolutionManager(true))
                using (var randomTestPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder())
                {
                    var projectA = testSolutionManager.AddNewMSBuildProject("projectA");
                    var projectB = testSolutionManager.AddNewMSBuildProject("projectB");
                    var projectC = testSolutionManager.AddNewMSBuildProject("projectC");

                    var jQuery144                = Packages[0];
                    var jQueryValidation         = Packages[2];
                    var testNuGetProjectContext  = new TestNuGetProjectContext();
                    var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
                    var testSettings             = Configuration.NullSettings.Instance;
                    var resolutionContext        = new ResolutionContext();
                    var token = CancellationToken.None;

                    var deleteOnRestartManager = new TestDeleteOnRestartManager();
                    var nuGetPackageManager    = new NuGetPackageManager(
                        sourceRepositoryProvider,
                        testSettings,
                        testSolutionManager,
                        deleteOnRestartManager);

                    await nuGetPackageManager.InstallPackageAsync(projectA, jQueryValidation,
                                                                  resolutionContext, testNuGetProjectContext, sourceRepositoryProvider.GetRepositories().First(), null, token);

                    await nuGetPackageManager.InstallPackageAsync(projectB, jQueryValidation,
                                                                  resolutionContext, testNuGetProjectContext, sourceRepositoryProvider.GetRepositories().First(), null, token);

                    var testPackage1 = new PackageIdentity("package1A", new NuGetVersion("1.0.0"));
                    var testPackage2 = new PackageIdentity("package1B", new NuGetVersion("1.0.0"));

                    var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomTestPackageSourcePath,
                                                                                           testPackage1.Id, testPackage1.Version.ToNormalizedString());
                    using (var packageStream = GetDownloadResult(packageFileInfo))
                    {
                        // Act
                        await projectB.InstallPackageAsync(testPackage1, packageStream, testNuGetProjectContext, token);

                        await projectC.InstallPackageAsync(testPackage1, packageStream, testNuGetProjectContext, token);
                    }

                    packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomTestPackageSourcePath,
                                                                                       testPackage2.Id, testPackage2.Version.ToNormalizedString());
                    using (var packageStream = GetDownloadResult(packageFileInfo))
                    {
                        // Act
                        await projectA.InstallPackageAsync(testPackage2, packageStream, testNuGetProjectContext, token);

                        await projectC.InstallPackageAsync(testPackage2, packageStream, testNuGetProjectContext, token);
                    }

                    var packageRestoreManager = new PackageRestoreManager(
                        sourceRepositoryProvider,
                        testSettings,
                        testSolutionManager);
                    var restoredPackages = new List <PackageIdentity>();
                    packageRestoreManager.PackageRestoredEvent += delegate(object sender, PackageRestoredEventArgs args) { restoredPackages.Add(args.Package); };

                    var restoreFailedPackages = new ConcurrentDictionary <Packaging.PackageReference, IEnumerable <string> >(new PackageReferenceComparer());
                    packageRestoreManager.PackageRestoreFailedEvent += delegate(object sender, PackageRestoreFailedEventArgs args)
                    {
                        restoreFailedPackages.AddOrUpdate(args.RestoreFailedPackageReference,
                                                          args.ProjectNames,
                                                          (Packaging.PackageReference packageReference, IEnumerable <string> oldValue) => { return(oldValue); });
                    };

                    Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation));

                    // Delete packages folder
                    TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages"));

                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(jQuery144));
                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation));
                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage1));
                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage2));

                    // Act
                    await packageRestoreManager.RestoreMissingPackagesInSolutionAsync(testSolutionManager.SolutionDirectory,
                                                                                      testNuGetProjectContext,
                                                                                      CancellationToken.None);

                    // Assert
                    Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQuery144));
                    Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation));
                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage1));
                    Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage2));

                    Assert.Equal(4, restoredPackages.Count);
                    // The ordering is not guaranteed and can vary. Do not assert based on that
                    Assert.True(restoredPackages.Contains(jQuery144));
                    Assert.True(restoredPackages.Contains(jQueryValidation));
                    Assert.True(restoredPackages.Contains(testPackage1));
                    Assert.True(restoredPackages.Contains(testPackage2));

                    Assert.Equal(2, restoreFailedPackages.Count);

                    // The ordering is not guaranteed and can vary. Do not assert based on that
                    var restoreFailedPackageKeys = restoreFailedPackages.Keys;
                    var testPackage1Key          = restoreFailedPackageKeys.Where(r => r.PackageIdentity.Equals(testPackage1)).First();
                    var testPackage1ProjectNames = restoreFailedPackages[testPackage1Key].ToList();

                    Assert.Equal(2, testPackage1ProjectNames.Count);
                    Assert.True(testPackage1ProjectNames.Contains("projectB", StringComparer.OrdinalIgnoreCase));
                    Assert.True(testPackage1ProjectNames.Contains("projectC", StringComparer.OrdinalIgnoreCase));

                    var testPackage2Key          = restoreFailedPackageKeys.Where(r => r.PackageIdentity.Equals(testPackage2)).First();
                    var testPackage2ProjectNames = restoreFailedPackages[testPackage2Key].ToList();

                    Assert.Equal(2, testPackage2ProjectNames.Count);
                    Assert.True(testPackage2ProjectNames.Contains("projectA", StringComparer.OrdinalIgnoreCase));
                    Assert.True(testPackage2ProjectNames.Contains("projectC", StringComparer.OrdinalIgnoreCase));
                }
        }