private static void VerifyParsedPackages(XDocument document, IEnumerable<VsTemplateWizardPackageInfo> expectedPackages)
        {
            // Arrange
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate");

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(expectedPackages.Count(), results.Single().Packages.Count);
            foreach (var pair in expectedPackages.Zip(results.Single().Packages,
                (expectedPackage, resultPackage) => new { expectedPackage, resultPackage }))
            {
                Assert.Equal(pair.expectedPackage.Id, pair.resultPackage.Id);
                Assert.Equal(pair.expectedPackage.Version, pair.resultPackage.Version);
            }
        }
        public void GetConfigurationFromXmlDocument_WorksWithMissingWizardDataElement()
        {
            // Arrange
            var document = new XDocument(new XElement(VSTemplateNamespace + "VSTemplate"));
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate");

            // Assert
            Assert.Empty(results);
        }
        public void GetConfigurationFromXmlDocument_WorksWithRegistryRepository()
        {
            // Arrange
            var registryPath = @"SOFTWARE\NuGet\Repository";
            var registryKey = "AspNetMvc4";
            var registryValue = @"C:\AspNetMvc4\Packages";

            var document = BuildDocumentWithPackage("registry", new XAttribute("keyName", registryKey));
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            var hkcu_repository = new Mock<IRegistryKey>();
            var hkcu = new Mock<IRegistryKey>();
            hkcu_repository.Setup(k => k.GetValue(registryKey)).Returns(registryValue);
            hkcu.Setup(r => r.OpenSubKey(registryPath)).Returns(hkcu_repository.Object);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate", registryKeys: new[] { hkcu.Object });

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(registryValue, results.Single().RepositoryPath);
        }
        public void GetConfigurationFromXmlDocument_FallsBackWhenHKCURegistryValueIsEmpty()
        {
            // Arrange
            var registryPath = @"SOFTWARE\NuGet\Repository";
            var registryKeyName = "AspNetMvc4";
            var registryValue = @"C:\AspNetMvc4\Packages";

            var document = BuildDocumentWithPackage("registry", new XAttribute("keyName", registryKeyName));
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            // HKCU key exists, but the value does not
            var hkcu_repository = new Mock<IRegistryKey>();
            hkcu_repository.Setup(r => r.GetValue(registryKeyName)).Returns(String.Empty);

            var hkcu = new Mock<IRegistryKey>();
            hkcu.Setup(r => r.OpenSubKey(registryPath)).Returns(hkcu_repository.Object);

            // HKLM key is configured
            var hklm_repository = new Mock<IRegistryKey>();
            var hklm = new Mock<IRegistryKey>();
            hklm_repository.Setup(k => k.GetValue(registryKeyName)).Returns(registryValue);
            hklm.Setup(r => r.OpenSubKey(registryPath)).Returns(hklm_repository.Object);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate", registryKeys: new[] { hkcu.Object, hklm.Object });

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(registryValue, results.Single().RepositoryPath);
        }
        public void GetConfigurationFromXmlDocument_IsPreunzippedCanBeSetOnMultipleRepositories()
        {
            // Arrange
            var firstRepository = new VsTemplatePackagesNode("", new XObject[] { new XAttribute("isPreunzipped", "true") });
            var secondRepository = new VsTemplatePackagesNode("", new XObject[] { new XAttribute("isPreunzipped", "false") });
            var thirdRepository = new VsTemplatePackagesNode("", new XObject[] { new XAttribute("isPreunzipped", "true") });


            var document = BuildDocument(new[] { firstRepository, secondRepository, thirdRepository });
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);
            var extensionManagerMock = new Mock<IVsExtensionManager>();
            var extensionMock = new Mock<IInstalledExtension>();
            var extension = extensionMock.Object;
            extensionManagerMock.Setup(em => em.TryGetInstalledExtension("myExtensionId", out extension)).Returns(true);

            var hkcu = new Mock<IRegistryKey>();

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate", vsExtensionManager: extensionManagerMock.Object, registryKeys: new[] { hkcu.Object });

            // Assert
            Assert.Equal(3, results.Count());
            Assert.True(results.First().IsPreunzipped);
            Assert.False(results.Skip(1).First().IsPreunzipped);
            Assert.True(results.Last().IsPreunzipped);
        }
        public void GetConfigurationFromXmlDocument_WorksWithMultipleRepositories()
        {
            // Arrange
            var registryPath = @"SOFTWARE\NuGet\Repository";
            var registryKey = "AspNetMvc4";
            var registryValue = @"C:\AspNetMvc4\Packages";

            var extensionRepository = new VsTemplatePackagesNode("extension", new XObject[] { new XAttribute("repositoryId", "myExtensionId"), BuildPackageElement("packageFromExtension", "1.0") });
            var registryRepository = new VsTemplatePackagesNode("registry", new XObject[] { new XAttribute("keyName", registryKey), BuildPackageElement("packageFromRegistry", "2.0") });

            var document = BuildDocument(new[] { extensionRepository, registryRepository });
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);
            var extensionManagerMock = new Mock<IVsExtensionManager>();
            var extensionMock = new Mock<IInstalledExtension>();
            extensionMock.Setup(e => e.InstallPath).Returns(@"C:\Extension\Dir");
            var extension = extensionMock.Object;
            extensionManagerMock.Setup(em => em.TryGetInstalledExtension("myExtensionId", out extension)).Returns(true);

            var hkcu_repository = new Mock<IRegistryKey>();
            var hkcu = new Mock<IRegistryKey>();
            hkcu_repository.Setup(k => k.GetValue(registryKey)).Returns(registryValue);
            hkcu.Setup(r => r.OpenSubKey(registryPath)).Returns(hkcu_repository.Object);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate", vsExtensionManager: extensionManagerMock.Object, registryKeys: new[] { hkcu.Object });

            // Assert
            Assert.Equal(2, results.Count());
            var extensionResult = results.First();
            var registryResult = results.Last();

            Assert.Equal(1, extensionResult.Packages.Count);
            Assert.Equal(@"C:\Extension\Dir\Packages", extensionResult.RepositoryPath);
            Assert.Equal("packageFromExtension", extensionResult.Packages.Single().Id);
            Assert.Equal(new SemanticVersion("1.0"), extensionResult.Packages.Single().Version);

            Assert.Equal(1, registryResult.Packages.Count);
            Assert.Equal(registryValue, registryResult.RepositoryPath);
            Assert.Equal("packageFromRegistry", registryResult.Packages.Single().Id);
            Assert.Equal(new SemanticVersion("2.0"), registryResult.Packages.Single().Version);
        }
        public void GetConfigurationFromXmlDocument_WorksWithExtensionRepository()
        {
            // Arrange
            var document = BuildDocumentWithPackage("extension", new XAttribute("repositoryId", "myExtensionId"));
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);
            var extensionManagerMock = new Mock<IVsExtensionManager>();
            var extensionMock = new Mock<IInstalledExtension>();
            extensionMock.Setup(e => e.InstallPath).Returns(@"C:\Extension\Dir");
            var extension = extensionMock.Object;
            extensionManagerMock.Setup(em => em.TryGetInstalledExtension("myExtensionId", out extension)).Returns(true);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate", vsExtensionManager: extensionManagerMock.Object);

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(1, results.Single().Packages.Count);
            Assert.Equal(@"C:\Extension\Dir\Packages", results.Single().RepositoryPath);
        }
        public void GetConfigurationFromXmlDocument_WorksWithTemplateRepository()
        {
            // Arrange
            var document = BuildDocumentWithPackage("template");
            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"C:\Some\file.vstemplate");

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(1, results.Single().Packages.Count);
            Assert.Equal(@"C:\Some", results.Single().RepositoryPath);
        }
        public void GetConfigurationRecognizeIsPreunzippedAttribute(string preunzippedValue, bool expectedResult)
        {
            // Arrange
            var document = BuildDocument();
            document.Element("VSTemplate")
                    .Element(VSTemplateNamespace + "WizardData")
                    .Element(VSTemplateNamespace + "packages")
                    .Add(new XAttribute("isPreunzipped", preunzippedValue));

            var wizard = new VsTemplateWizard(null, null, null, null, null, null);

            // Act
            var results = wizard.GetConfigurationsFromXmlDocument(document, @"c:\some\file.vstemplate");

            // Assert
            Assert.Equal(1, results.Count());
            Assert.Equal(expectedResult, results.First().IsPreunzipped);
        }