Пример #1
0
        public async Task TestInstallPackageUnsupportedFx()
        {
            // Arrange
            var randomTestFolder       = TestFilesystemUtility.CreateRandomTestFolder();
            var packagesConfigFileName = "packages.config";
            var targetFramework        = NuGetFramework.UnsupportedFramework;
            var metadata = new Dictionary <string, object>()
            {
                { NuGetProjectMetadataKeys.TargetFramework, targetFramework },
            };
            var packagesConfigFullPath     = Path.Combine(randomTestFolder, packagesConfigFileName);
            var packagesConfigNuGetProject = new PackagesConfigNuGetProject(packagesConfigFullPath, metadata);
            var packageIdentity            = new PackageIdentity("A", new NuGetVersion("1.0.0"));
            var token = CancellationToken.None;

            MakeFileReadOnly(packagesConfigFullPath);

            // Act
            await packagesConfigNuGetProject.InstallPackageAsync(packageIdentity, Stream.Null, new TestNuGetProjectContext(), token);

            MakeFileReadOnly(packagesConfigFullPath);

            // Assert
            var installedPackagesList = (await packagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();

            Assert.Equal(1, installedPackagesList.Count);
            Assert.Equal(packageIdentity, installedPackagesList[0].PackageIdentity);
            Assert.True(installedPackagesList[0].TargetFramework.IsUnsupported);
        }
Пример #2
0
        public async Task TestFolderNuGetProjectInstall()
        {
            // Arrange
            var packageIdentity           = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
            var randomTestSourcePath      = TestFilesystemUtility.CreateRandomTestFolder();
            var packageFileInfo           = TestPackages.GetLegacyTestPackage(randomTestSourcePath, packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
            var randomTestDestinationPath = TestFilesystemUtility.CreateRandomTestFolder();
            var folderNuGetProject        = new FolderNuGetProject(randomTestDestinationPath);
            var packagePathResolver       = new PackagePathResolver(randomTestDestinationPath);
            var packageInstallPath        = packagePathResolver.GetInstallPath(packageIdentity);
            var nupkgFilePath             = Path.Combine(packageInstallPath, packagePathResolver.GetPackageFileName(packageIdentity));
            var testNuGetProjectContext   = new TestNuGetProjectContext();
            var token = CancellationToken.None;

            using (var packageStream = packageFileInfo.OpenRead())
            {
                // Act
                await folderNuGetProject.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);
            }

            // Assert
            Assert.True(File.Exists(nupkgFilePath));
            Assert.True(File.Exists(Path.Combine(packageInstallPath, "lib/test.dll")));
            using (var packageStream = File.OpenRead(nupkgFilePath))
            {
                var zipArchive = new ZipArchive(packageStream);
                Assert.Equal(5, zipArchive.Entries.Count);
            }

            // Clean-up
            TestFilesystemUtility.DeleteRandomTestFolders(randomTestSourcePath, randomTestDestinationPath);
        }
        public void CreateConfigurationDefaultsThrowsWhenXmlIsInvalid()
        {
            //Arrange
            var name1 = "Contoso Package Source";
            var name2 = "My Test Package Source";
            var feed1 = "http://contoso.com/packages/";
            var feed2 = "http://wwww.somerandomURL.com/";

            string nugetConfigFileFolder = TestFilesystemUtility.CreateRandomTestFolder();
            var    nugetConfigFile       = "NuGetDefaults.config";
            var    nugetConfigFilePath   = Path.Combine(nugetConfigFileFolder, nugetConfigFile);

            File.Create(nugetConfigFilePath).Close();

            var enabledReplacement = @"<add key='" + name1 + "' value='" + feed1;

            enabledReplacement = enabledReplacement + @"<add key='" + name2 + "' value='" + feed2;
            var disabledReplacement = string.Empty;
            var defaultPushSource   = "<add key='DefaultPushSource' value='" + feed2 + "' />";

            File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement, defaultPushSource));

            //Act & Assert
            try
            {
                ConfigurationDefaults configDefaults = new ConfigurationDefaults(nugetConfigFileFolder, nugetConfigFile);
            }
            catch (Exception e)
            {
                Assert.True("XmlException" == e.GetType().Name);
            }
        }
Пример #4
0
        public async Task TestUninstallPenultimatePackage()
        {
            // Arrange
            var randomTestFolder       = TestFilesystemUtility.CreateRandomTestFolder();
            var packagesConfigFileName = "packages.config";
            var targetFramework        = NuGetFramework.Parse("net45");
            var metadata = new Dictionary <string, object>()
            {
                { NuGetProjectMetadataKeys.TargetFramework, targetFramework },
            };
            var packagesConfigFullPath     = Path.Combine(randomTestFolder, packagesConfigFileName);
            var packagesConfigNuGetProject = new PackagesConfigNuGetProject(packagesConfigFullPath, metadata);

            var packageA = new PackageIdentity("A", new NuGetVersion("1.0.0"));
            var packageB = new PackageIdentity("B", new NuGetVersion("1.0.0"));
            var testNuGetProjectContext = new TestNuGetProjectContext();
            var token = CancellationToken.None;

            MakeFileReadOnly(packagesConfigFullPath);

            // Act
            await packagesConfigNuGetProject.InstallPackageAsync(packageA, Stream.Null, testNuGetProjectContext, token);

            MakeFileReadOnly(packagesConfigFullPath);

            // Assert
            var installedPackagesList = (await packagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();

            Assert.Equal(1, installedPackagesList.Count);
            Assert.Equal(packageA, installedPackagesList[0].PackageIdentity);
            Assert.Equal(targetFramework, installedPackagesList[0].TargetFramework);

            // Act
            await packagesConfigNuGetProject.InstallPackageAsync(packageB, Stream.Null, testNuGetProjectContext, token);

            // Assert
            installedPackagesList = (await packagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();
            Assert.Equal(2, installedPackagesList.Count);
            Assert.Equal(packageA, installedPackagesList[0].PackageIdentity);
            Assert.Equal(packageB, installedPackagesList[1].PackageIdentity);
            Assert.Equal(targetFramework, installedPackagesList[0].TargetFramework);
            Assert.Equal(targetFramework, installedPackagesList[1].TargetFramework);

            // Main Act
            await packagesConfigNuGetProject.UninstallPackageAsync(packageA, testNuGetProjectContext, token);

            // Main Assert
            installedPackagesList = (await packagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();
            Assert.Equal(1, installedPackagesList.Count);
            Assert.Equal(packageB, installedPackagesList[0].PackageIdentity);
            Assert.Equal(targetFramework, installedPackagesList[0].TargetFramework);
        }
Пример #5
0
        public async Task TestCheckForMissingPackages()
        {
            // Arrange
            var testSolutionManager = new TestSolutionManager();
            var projectA            = testSolutionManager.AddNewMSBuildProject();
            var projectB            = testSolutionManager.AddNewMSBuildProject();

            var packageIdentity         = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
            var randomPackageSourcePath = TestFilesystemUtility.CreateRandomTestFolder();
            var packageFileInfo         = TestPackages.GetLegacyTestPackage(randomPackageSourcePath,
                                                                            packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
            var testNuGetProjectContext = new TestNuGetProjectContext();
            var token = CancellationToken.None;

            using (var packageStream = packageFileInfo.OpenRead())
            {
                // Act
                await projectA.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);

                await projectB.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);
            }

            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var testSettings             = NullSettings.Instance;
            var packageRestoreManager    = new PackageRestoreManager(sourceRepositoryProvider, testSettings, testSolutionManager);

            int  packagesMissingEventCount = 0;
            bool packagesMissing           = false;

            packageRestoreManager.PackagesMissingStatusChanged += delegate(object sender, PackagesMissingStatusEventArgs args)
            {
                packagesMissingEventCount++;
                packagesMissing = args.PackagesMissing;
            };

            // Act
            await packageRestoreManager.RaisePackagesMissingEventForSolution(token);

            // Assert
            Assert.Equal(1, packagesMissingEventCount);
            Assert.False(packagesMissing);

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

            // Act
            await packageRestoreManager.RaisePackagesMissingEventForSolution(token);

            // Assert
            Assert.Equal(2, packagesMissingEventCount);
            Assert.True(packagesMissing);
        }
        private ConfigurationDefaults GetConfigurationDefaults(string configurationDefaultsContent)
        {
            var configurationDefaultsPath = "NuGetDefaults.config";
            var mockBaseDirectory         = TestFilesystemUtility.CreateRandomTestFolder();

            Directory.CreateDirectory(mockBaseDirectory);

            using (var file = File.Create(Path.Combine(mockBaseDirectory, configurationDefaultsPath)))
            {
                var info = new UTF8Encoding(true).GetBytes(configurationDefaultsContent);
                file.Write(info, 0, info.Count());
            }
            return(new ConfigurationDefaults(mockBaseDirectory, configurationDefaultsPath));
        }
Пример #7
0
        public async Task TestFolderNuGetProjectDeletePackage()
        {
            // Arrange
            var packageIdentity           = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
            var randomTestSourcePath      = TestFilesystemUtility.CreateRandomTestFolder();
            var packageFileInfo           = TestPackages.GetLegacyContentPackage(randomTestSourcePath, packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
            var randomTestDestinationPath = TestFilesystemUtility.CreateRandomTestFolder();
            var folderNuGetProject        = new FolderNuGetProject(randomTestDestinationPath);
            var packagePathResolver       = new PackagePathResolver(randomTestDestinationPath);
            var packageInstallPath        = packagePathResolver.GetInstallPath(packageIdentity);
            var nupkgFilePath             = Path.Combine(packageInstallPath, packagePathResolver.GetPackageFileName(packageIdentity));
            var testNuGetProjectContext   = new TestNuGetProjectContext();
            var token = CancellationToken.None;

            using (var packageStream = packageFileInfo.OpenRead())
            {
                // Act
                await folderNuGetProject.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);
            }

            var unNormalizedPackageIdentity = new PackageIdentity(packageIdentity.Id,
                                                                  new NuGetVersion(packageIdentity.Version + ".0"));

            // Assert
            Assert.True(File.Exists(nupkgFilePath));
            Assert.True(File.Exists(Path.Combine(packageInstallPath, "Content/Scripts/test1.js")));
            using (var packageStream = File.OpenRead(nupkgFilePath))
            {
                var zipArchive = new ZipArchive(packageStream);
                Assert.Equal(6, zipArchive.Entries.Count);
            }
            Assert.True(folderNuGetProject.PackageExists(packageIdentity));
            var packageDirectoryPath = folderNuGetProject.GetInstalledPath(unNormalizedPackageIdentity);

            Assert.True(!String.IsNullOrEmpty(packageDirectoryPath));
            Assert.True(Directory.Exists(packageDirectoryPath));

            // Main Act
            await folderNuGetProject.DeletePackage(packageIdentity, testNuGetProjectContext, CancellationToken.None);

            // Assert
            Assert.False(folderNuGetProject.PackageExists(unNormalizedPackageIdentity));
            // Check that the package directories are deleted
            Assert.False(Directory.Exists(packageDirectoryPath));

            // Clean-up
            TestFilesystemUtility.DeleteRandomTestFolders(randomTestSourcePath, randomTestDestinationPath);
        }
        public void GetDefaultPushSourceReturnsNull()
        {
            //Arrange
            string nugetConfigFileFolder = TestFilesystemUtility.CreateRandomTestFolder();
            var    nugetConfigFile       = "NuGetDefaults.config";
            var    nugetConfigFilePath   = Path.Combine(nugetConfigFileFolder, nugetConfigFile);

            File.Create(nugetConfigFilePath).Close();

            var configurationDefaultsContent = @"<configuration></configuration>";

            File.WriteAllText(nugetConfigFilePath, configurationDefaultsContent);

            ConfigurationDefaults configDefaults = new ConfigurationDefaults(nugetConfigFileFolder, nugetConfigFile);

            Assert.Null(configDefaults.DefaultPushSource);
        }
Пример #9
0
        public async Task UnzippedPackageInstall_Basic()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var testSolutionManager      = new TestSolutionManager();
            var testSettings             = new NullSettings();
            var nuGetPackageManager      = new NuGetPackageManager(sourceRepositoryProvider, testSettings, testSolutionManager);
            var packagesFolderPath       = PackagesFolderPathUtility.GetPackagesFolderPath(testSolutionManager, testSettings);

            var randomPackagesConfigFolderPath = TestFilesystemUtility.CreateRandomTestFolder();
            var randomPackagesConfigPath       = Path.Combine(randomPackagesConfigFolderPath, "packages.config");
            var token = CancellationToken.None;

            var projectTargetFramework    = NuGetFramework.Parse("net45");
            var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(projectTargetFramework, new TestNuGetProjectContext());
            var msBuildNuGetProject       = new MSBuildNuGetProject(msBuildNuGetProjectSystem, packagesFolderPath, randomPackagesConfigPath);
            var packageIdentity           = NoDependencyLibPackages[0];

            // Pre-Assert
            // Check that the packages.config file does not exist
            Assert.False(File.Exists(randomPackagesConfigPath));
            // Check that there are no packages returned by PackagesConfigProject
            var packagesInPackagesConfig = (await msBuildNuGetProject.PackagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();

            Assert.Equal(0, packagesInPackagesConfig.Count);
            Assert.Equal(0, msBuildNuGetProjectSystem.References.Count);

            // Act
            await nuGetPackageManager.InstallPackageAsync(msBuildNuGetProject, packageIdentity,
                                                          new ResolutionContext(), new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token);

            // Assert
            // Check that the packages.config file exists after the installation
            Assert.True(File.Exists(randomPackagesConfigPath));
            // Check the number of packages and packages returned by PackagesConfigProject after the installation
            packagesInPackagesConfig = (await msBuildNuGetProject.PackagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList();
            Assert.Equal(1, packagesInPackagesConfig.Count);
            Assert.Equal(packageIdentity, packagesInPackagesConfig[0].PackageIdentity);
            Assert.Equal(projectTargetFramework, packagesInPackagesConfig[0].TargetFramework);

            // Clean-up
            TestFilesystemUtility.DeleteRandomTestFolders(testSolutionManager.SolutionDirectory, randomPackagesConfigFolderPath);
        }
Пример #10
0
        public void TestFolderNuGetProjectMetadata()
        {
            // Arrange
            var randomTestFolder   = TestFilesystemUtility.CreateRandomTestFolder();
            var folderNuGetProject = new FolderNuGetProject(randomTestFolder);

            // Act & Assert
            NuGetFramework targetFramework;

            Assert.True(folderNuGetProject.TryGetMetadata <NuGetFramework>(NuGetProjectMetadataKeys.TargetFramework, out targetFramework));
            string name;

            Assert.True(folderNuGetProject.TryGetMetadata <string>(NuGetProjectMetadataKeys.Name, out name));
            Assert.Equal(NuGetFramework.AnyFramework, targetFramework);
            Assert.Equal(randomTestFolder, name);
            Assert.Equal(2, folderNuGetProject.Metadata.Count);

            // Clean-up
            TestFilesystemUtility.DeleteRandomTestFolders(randomTestFolder);
        }
Пример #11
0
        public async Task TestGetMissingPackagesForSolution()
        {
            // Arrange
            var testSolutionManager = new TestSolutionManager();
            var projectA            = testSolutionManager.AddNewMSBuildProject();
            var projectB            = testSolutionManager.AddNewMSBuildProject();

            var packageIdentity         = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
            var randomPackageSourcePath = TestFilesystemUtility.CreateRandomTestFolder();
            var packageFileInfo         = TestPackages.GetLegacyTestPackage(randomPackageSourcePath,
                                                                            packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
            var testNuGetProjectContext = new TestNuGetProjectContext();
            var token = CancellationToken.None;

            using (var packageStream = packageFileInfo.OpenRead())
            {
                // Act
                await projectA.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);

                await projectB.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);
            }

            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var testSettings             = NullSettings.Instance;
            var packageRestoreManager    = new PackageRestoreManager(sourceRepositoryProvider, testSettings, testSolutionManager);

            // Act
            var packageReferencesFromSolution = (await packageRestoreManager.GetPackageReferencesFromSolution(token)).ToList();
            var missingPackagesFromSolution   = (await packageRestoreManager.GetMissingPackagesInSolution(token)).ToList();

            Assert.Equal(2, packageReferencesFromSolution.Count);
            Assert.Equal(0, missingPackagesFromSolution.Count);

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

            packageReferencesFromSolution = (await packageRestoreManager.GetPackageReferencesFromSolution(token)).ToList();
            missingPackagesFromSolution   = (await packageRestoreManager.GetMissingPackagesInSolution(token)).ToList();
            Assert.Equal(2, packageReferencesFromSolution.Count);
            Assert.Equal(1, missingPackagesFromSolution.Count);
        }
Пример #12
0
        public void ConfigDefaultsAreProperlyReadFromConfigDefaultsFile()
        {
            //Arrange
            var name1 = "Contoso Package Source";
            var name2 = "My Test Package Source";
            var feed1 = "http://contoso.com/packages/";
            var feed2 = "http://wwww.somerandomURL.com/";

            string nugetConfigFileFolder = TestFilesystemUtility.CreateRandomTestFolder();
            var    nugetConfigFile       = "NuGetDefaults.config";
            var    nugetConfigFilePath   = Path.Combine(nugetConfigFileFolder, nugetConfigFile);

            File.Create(nugetConfigFilePath).Close();

            var enabledReplacement = @"<add key='" + name1 + "' value='" + feed1 + "' />";

            enabledReplacement = enabledReplacement + @"<add key='" + name2 + "' value='" + feed2 + "' />";
            var disabledReplacement = string.Empty;
            var defaultPushSource   = "<add key='DefaultPushSource' value='" + feed2 + "' />";

            File.WriteAllText(nugetConfigFilePath, CreateNuGetConfigContent(enabledReplacement, disabledReplacement, defaultPushSource));

            //Act
            ConfigurationDefaults       configDefaults = new ConfigurationDefaults(nugetConfigFileFolder, nugetConfigFile);
            IEnumerable <PackageSource> defaultSourcesFromConfigFile = configDefaults.DefaultPackageSources;
            string packageRestore    = configDefaults.DefaultPackageRestoreConsent;
            string packagePushSource = configDefaults.DefaultPushSource;

            //Assert
            VerifyPackageSource(defaultSourcesFromConfigFile, 2, new string[] { name1, name2 }, new string[] { feed1, feed2 });
            Assert.Equal(feed2, packagePushSource);
            Assert.Equal("true", packageRestore.ToLowerInvariant());

            //Clean up
            TestFilesystemUtility.DeleteRandomTestFolders(nugetConfigFileFolder);
        }