Inheritance: IPackageSourceProvider
        public void AddSourceThrowsIfSourceIsNull()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            ExceptionAssert.ThrowsArgNull(() => provider.AddPackageSource(null), "source");
        }
        public void CtorIfFirstRunningAddsDefaultSource()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            var sources = provider.LoadPackageSources().ToList();


            // Assert
            Assert.Equal(1, sources.Count);
            Assert.Equal("https://go.microsoft.com/fwlink/?LinkID=230477", sources[0].Source);
        }
        private RecentPackageRepository CreateRecentPackageRepository(
            IEnumerable <IPackage> packagesList = null,
            IEnumerable <IPersistencePackageMetadata> settingsMetadata = null,
            IPackageRepository cacheRepository = null)
        {
            if (packagesList == null)
            {
                var packageA = PackageUtility.CreatePackage("A", "1.0");
                var packageC = PackageUtility.CreatePackage("C", "2.0");

                packagesList = new[] { packageA, packageC };
            }

            var mockRepository = new Mock <IPackageRepository>();

            mockRepository.Setup(p => p.GetPackages()).Returns(packagesList.AsQueryable());

            var mockRepositoryFactory = new Mock <IPackageRepositoryFactory>();

            mockRepositoryFactory.Setup(f => f.CreateRepository(It.IsAny <string>())).Returns(mockRepository.Object);

            var mockSettingsManager = new MockSettingsManager();

            if (settingsMetadata == null)
            {
                var A = new PersistencePackageMetadata("A", "1.0", new DateTime(2010, 8, 12));
                var B = new PersistencePackageMetadata("B", "2.0", new DateTime(2011, 3, 2));
                settingsMetadata = new[] { A, B };
            }

            mockSettingsManager.SavePackageMetadata(settingsMetadata);

            var mockPackageSourceProvider = new MockPackageSourceProvider();

            mockPackageSourceProvider.SavePackageSources(new[] { new PackageSource("source") });

            if (cacheRepository == null)
            {
                cacheRepository = new MockPackageRepository();
            }
            return(new RecentPackageRepository(
                       /* dte */ null,
                       mockRepositoryFactory.Object,
                       mockPackageSourceProvider,
                       mockSettingsManager,
                       cacheRepository));
        }
        private RecentPackageRepository CreateRecentPackageRepository(
            IEnumerable<IPackage> packagesList = null,
            IEnumerable<IPersistencePackageMetadata> settingsMetadata = null,
            IPackageRepository cacheRepository = null)
        {
            if (packagesList == null)
            {
                var packageA = PackageUtility.CreatePackage("A", "1.0");
                var packageC = PackageUtility.CreatePackage("C", "2.0");

                packagesList = new[] { packageA, packageC };
            }

            var mockRepository = new Mock<IPackageRepository>();
            mockRepository.Setup(p => p.GetPackages()).Returns(packagesList.AsQueryable());

            var mockRepositoryFactory = new Mock<IPackageRepositoryFactory>();
            mockRepositoryFactory.Setup(f => f.CreateRepository(It.IsAny<string>())).Returns(mockRepository.Object);

            var mockSettingsManager = new MockSettingsManager();

            if (settingsMetadata == null)
            {
                var A = new PersistencePackageMetadata("A", "1.0", new DateTime(2010, 8, 12));
                var B = new PersistencePackageMetadata("B", "2.0", new DateTime(2011, 3, 2));
                settingsMetadata = new[] { A, B };
            }

            mockSettingsManager.SavePackageMetadata(settingsMetadata);

            var mockPackageSourceProvider = new MockPackageSourceProvider();
            mockPackageSourceProvider.SavePackageSources(new[] { new PackageSource("source") });

            if (cacheRepository == null)
            {
                cacheRepository = new MockPackageRepository();
            }
            return new RecentPackageRepository(
                /* dte */ null,
                mockRepositoryFactory.Object,
                mockPackageSourceProvider,
                mockSettingsManager,
                cacheRepository);
        }
        public void SettingsWithMoreThanOneAggregateSourceAreModifiedToNotHaveOne()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            var sources = provider.LoadPackageSources().ToList();

            // Assert
            Assert.Equal(1, sources.Count);
            Assert.Equal("NuGet official package source", sources[0].Name);
        }
        public void SettingActivePackageSourceToNonExistantSourceThrows()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            ExceptionAssert.ThrowsArgumentException(() => provider.ActivePackageSource = new PackageSource("a", "a"), "value", "The package source does not belong to the collection of available sources.");
        }
        public void RemovingActivePackageSourceSetsActivePackageSourceToNull()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);
            var packageSource = new PackageSource("a", "a");
            provider.SavePackageSources(new[] { packageSource });
            provider.ActivePackageSource = packageSource;

            // Act
            bool result = provider.RemovePackageSource(packageSource);

            // Assert
            Assert.True(result);
            Assert.Null(provider.ActivePackageSource);

            // values should be null because we don't persist aggregate source into user settings file
            var values = userSettingsManager.GetValues(PackageSourceProvider.PackageSourcesSectionName);
            Assert.Null(values);
        }
        public void RemovingUnknownPackageSourceReturnsFalse()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            bool result = provider.RemovePackageSource(new PackageSource("a", "a"));

            // Assert
            Assert.False(result);
        }
        public void SetActivePackageSourcePersistsItToSettingsManager()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            packageSourceProvider.SavePackageSources(new[] { new PackageSource("source", "name"), new PackageSource("source1", "name1") });
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            provider.ActivePackageSource = new PackageSource("source", "name");

            // Assert
            var activeValue = userSettingsManager.GetValue(VsPackageSourceProvider.ActivePackageSourceSectionName, "name");
            Assert.Equal("source", activeValue);

            var invalidActiveValue = userSettingsManager.GetValue(VsPackageSourceProvider.ActivePackageSourceSectionName, "invalidName");
            Assert.Null(invalidActiveValue);
        }
        public void AddSourceSetsPersistsSourcesToSettingsManager()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            for (int i = 0; i < 10; i++)
            {
                provider.AddPackageSource(new PackageSource("source" + i, "name" + i));
            }

            // Assert
            var values = packageSourceProvider.LoadPackageSources().ToList();

            // 11 = 10 package sources that we added + NuGet official source
            Assert.Equal(11, values.Count);
            Assert.Equal(Resources.VsResources.OfficialSourceName, values[0].Name);
            for (int i = 0; i < 10; i++)
            {
                AssertPackageSource(values[i + 1], "name" + i, "source" + i);
            }
        }
        public void CtorAddsAggregrateIfNothingWasPersistedIntoSettingsManager()
        {
            // Arrange
            var userSettingsManager = new MockUserSettingsManager();
            var packageSourceProvider = new MockPackageSourceProvider();
            var provider = new VsPackageSourceProvider(userSettingsManager, packageSourceProvider);

            // Act
            var sources = provider.LoadPackageSources().ToList();

            // Assert
            Assert.Equal(1, sources.Count);
            Assert.Equal("NuGet official package source", sources[0].Name);
        }