Esempio n. 1
0
        public void AddFromRegistry_WithMissingRegistryKey_Fails()
        {
            var srp = Mock.Of <ISourceRepositoryProvider>();

            string errorHandlerMessage = null;
            var    provider            = new PreinstalledRepositoryProvider(
                registryKeyRoot: $"NuGetTest_{Guid.NewGuid()}",
                errorHandler: errorMessage => { errorHandlerMessage = errorMessage; },
                provider: srp);

            // Act
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      provider.AddFromRegistry("InvalidKeyName", isPreUnzipped: true)
                                                                      );

            // VsResources.PreinstalledPackages_RegistryKeyError
            const string ExpectedErrorMessage = "error accessing registry key";

            Assert.Contains(ExpectedErrorMessage, errorHandlerMessage,
                            StringComparison.OrdinalIgnoreCase);
            Assert.Contains(ExpectedErrorMessage, exception.Message,
                            StringComparison.OrdinalIgnoreCase);
        }
        public void AddFromRegistry_WithValidRegistryValue_Succeeds()
        {
            var srp = Mock.Of<ISourceRepositoryProvider>();
            Mock.Get(srp)
                .Setup(x => x.CreateRepository(It.IsAny<PackageSource>(), It.IsAny<FeedType>()))
                .Returns((PackageSource ps, FeedType ft) => new SourceRepository(
                    ps, Enumerable.Empty<INuGetResourceProvider>()));

            var provider = new PreinstalledRepositoryProvider(_ => { }, srp);

            // Act
            using (var tc = new TestContext(_testDirectory.Path))
            {
                provider.AddFromRegistry(tc.RepoKeyName, isPreUnzipped: true);
            }

            Assert.NotNull(provider.GetRepositories().Single());

            Mock.Get(srp)
                .Verify(
                    x => x.CreateRepository(It.IsAny<PackageSource>(), It.IsAny<FeedType>()),
                    Times.Once());
        }
Esempio n. 3
0
        public void AddFromRegistry_WithInvalidRegistryValue_Fails(RegistryHive registryHive)
        {
            var srp = Mock.Of <ISourceRepositoryProvider>();

            string errorHandlerMessage = null;
            var    provider            = new PreinstalledRepositoryProvider(
                errorMessage => { errorHandlerMessage = errorMessage; }, srp);

            // Act
            using (var tc = new TestContext(string.Empty, registryHive))
            {
                var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                          provider.AddFromRegistry(tc.RepoKeyName, isPreUnzipped: true)
                                                                          );

                // VsResources.PreinstalledPackages_InvalidRegistryValue
                const string ExpectedErrorMessage = "Could not find a registry key with name";

                Assert.Contains(ExpectedErrorMessage, errorHandlerMessage,
                                StringComparison.OrdinalIgnoreCase);
                Assert.Contains(ExpectedErrorMessage, exception.Message,
                                StringComparison.OrdinalIgnoreCase);
            }
        }
        public void InstallPackagesFromRegistryRepositoryImpl(string keyName, bool isPreUnzipped, bool skipAssemblyReferences, bool ignoreDependencies, Project project, IDictionary <string, string> packageVersions)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(keyName));
            }

            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (packageVersions == null ||
                !packageVersions.Any())
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(packageVersions));
            }

            try
            {
                PumpingJTF.Run(async() =>
                {
                    // HACK !!! : This is a hack for PCL projects which send isPreUnzipped = true, but their package source
                    // (located at C:\Program Files (x86)\Microsoft SDKs\NuGetPackages) follows the V3
                    // folder version format.
                    if (isPreUnzipped)
                    {
                        var isProjectJsonProject = await EnvDTEProjectUtility.HasBuildIntegratedConfig(project);
                        isPreUnzipped            = isProjectJsonProject ? false : isPreUnzipped;
                    }

                    // create a repository provider with only the registry repository
                    var repoProvider = new PreinstalledRepositoryProvider(ErrorHandler, _sourceRepositoryProvider);
                    repoProvider.AddFromRegistry(keyName, isPreUnzipped);

                    var toInstall = GetIdentitiesFromDict(packageVersions);

                    // Skip assembly references and disable binding redirections should be done together
                    var disableBindingRedirects = skipAssemblyReferences;

                    var projectContext = new VSAPIProjectContext(skipAssemblyReferences, disableBindingRedirects)
                    {
                        PackageExtractionContext = new PackageExtractionContext(
                            PackageSaveMode.Defaultv2,
                            PackageExtractionBehavior.XmlDocFileSaveMode,
                            ClientPolicyContext.GetClientPolicy(_settings, NullLogger.Instance),
                            NullLogger.Instance)
                    };

                    await InstallInternalAsync(
                        project,
                        toInstall,
                        repoProvider,
                        projectContext,
                        includePrerelease: false,
                        ignoreDependencies: ignoreDependencies,
                        token: CancellationToken.None);
                });
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPackageInstaller).FullName);
                throw;
            }
        }