Provides helpers functions around NuGet.exe
Inheritance: NuGetGallery.FunctionalTests.HelperBase
        public async Task UploadNewPackage(string packageId, string version = "1.0.0", string minClientVersion = null,
                                           string title        = null, string tags   = null, string description = null, string licenseUrl = null,
                                           string dependencies = null, string apiKey = null, bool success       = true)
        {
            WriteLine("Uploading new package '{0}', version '{1}'", packageId, version);

            var packageCreationHelper = new PackageCreationHelper(TestOutputHelper);
            var packageFullPath       = await packageCreationHelper.CreatePackage(packageId, version, minClientVersion, title, tags, description, licenseUrl, dependencies);

            try
            {
                var commandlineHelper = new CommandlineHelper(TestOutputHelper);
                var processResult     = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl, apiKey);

                if (success)
                {
                    Assert.True(processResult.ExitCode == 0,
                                "The package upload via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                                processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
                }
                else
                {
                    Assert.False(processResult.ExitCode == 0,
                                 "The package upload via Nuget.exe succeeded but was expected to fail. Check the logs to see the process error and output stream.  Exit Code: " +
                                 processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
                }
            }
            finally
            {
                // Delete package from local disk once it gets uploaded
                CleanCreatedPackage(packageFullPath);
            }
        }
        public async Task UnlistPackage(string packageId, string version = "1.0.0", string apiKey = null, bool success = true)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException($"{nameof(packageId)} cannot be null or empty!");
            }

            WriteLine("Unlisting package '{0}', version '{1}'", packageId, version);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            var processResult     = await commandlineHelper.DeletePackageAsync(packageId, version, UrlHelper.V2FeedPushSourceUrl, apiKey);

            if (success)
            {
                Assert.True(processResult.ExitCode == 0,
                            "The package unlist via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                            processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
            }
            else
            {
                Assert.False(processResult.ExitCode == 0,
                             "The package unlist via Nuget.exe succeeded but was expected to fail. Check the logs to see the process error and output stream.  Exit Code: " +
                             processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
            }
        }
        public async Task CheckIfBaseTestPackageExistsAsync()
        {
            // Check if the BaseTestPackage exists in current source and if not upload it.
            // This will be used by the download related tests.
            try
            {
                var clientSdkHelper = new ClientSdkHelper(ConsoleTestOutputHelper.New);
                if (!clientSdkHelper.CheckIfPackageExistsInSource(Constants.TestPackageId, UrlHelper.V2FeedRootUrl))
                {
                    var testOutputHelper      = ConsoleTestOutputHelper.New;
                    var commandlineHelper     = new CommandlineHelper(testOutputHelper);
                    var packageCreationHelper = new PackageCreationHelper(testOutputHelper);
                    var packageFullPath       = await packageCreationHelper.CreatePackage(Constants.TestPackageId, "1.0.0");

                    var processResult = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl);

                    Assert.True(processResult.ExitCode == 0, Constants.UploadFailureMessage);
                }
            }
            catch (Exception exception)
            {
                var message = string.Format(
                    "The initialization method to pre-upload test package has failed. Hence failing all the tests. Make sure that a package by name {0} exists @ {1} before running tests. Check test run error for details",
                    Constants.TestPackageId, UrlHelper.BaseUrl);
                throw new InvalidOperationException(message, exception);
            }
        }
Exemple #4
0
        /// <summary>
        /// Creates a package with the specified Id and Version and uploads it and checks if the upload has suceeded.
        /// This will be used by test classes which tests scenarios on top of upload.
        /// </summary>
        public async Task UploadNewPackageAndVerify(string packageId, string version = "1.0.0", string minClientVersion = null, string title = null, string tags = null, string description = null, string licenseUrl = null, string dependencies = null)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                packageId = DateTime.Now.Ticks.ToString();
            }

            WriteLine("Uploading new package '{0}', version '{1}'", packageId, version);

            var packageCreationHelper = new PackageCreationHelper(TestOutputHelper);
            var packageFullPath       = await packageCreationHelper.CreatePackage(packageId, version, minClientVersion, title, tags, description, licenseUrl, dependencies);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            var processResult     = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl);

            Assert.True(processResult.ExitCode == 0, "The package upload via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " + processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");

            var packageExistsInSource = CheckIfPackageVersionExistsInSource(packageId, version, UrlHelper.V2FeedRootUrl);
            var userMessage           = string.Format("Package {0} with version {1} is not found in the site {2} after uploading.", packageId, version, UrlHelper.V2FeedRootUrl);

            Assert.True(packageExistsInSource, userMessage);

            // Delete package from local disk so once it gets uploaded
            if (File.Exists(packageFullPath))
            {
                File.Delete(packageFullPath);
                Directory.Delete(Path.GetFullPath(Path.GetDirectoryName(packageFullPath)), true);
            }
        }
Exemple #5
0
        public async Task UploadExistingPackage(string packageFullPath, string apiKey = null)
        {
            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            var processResult     = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl, apiKey);

            Assert.True(processResult.ExitCode == 0,
                        "The package upload via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                        processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
        }
Exemple #6
0
        private async Task <string> CreatePackageWithTargetFrameworkInternal(string nuspecFileFullPath, string frameworkVersion)
        {
            string nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);

            AddContent(nuspecDir, frameworkVersion);
            AddLib(nuspecDir, frameworkVersion);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.PackPackageAsync(nuspecFileFullPath, nuspecDir);

            string[] nupkgFiles = Directory.GetFiles(nuspecDir, "*.nupkg").ToArray();
            return(nupkgFiles.Length == 0 ? null : nupkgFiles[0]);
        }
        private async Task <string> CreatePackageWithTargetFrameworkInternal(string nuspecFileFullPath, string frameworkVersion)
        {
            string nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);

            AddContent(nuspecDir, frameworkVersion);
            AddLib(nuspecDir, frameworkVersion);
            var arguments = string.Join(string.Empty, CommandlineHelper.PackCommandString, @"""" + nuspecFileFullPath + @"""", CommandlineHelper.OutputDirectorySwitchString, @"""" + nuspecDir + @"""");

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.InvokeNugetProcess(arguments, Path.GetFullPath(Path.GetDirectoryName(nuspecFileFullPath)));

            string[] nupkgFiles = Directory.GetFiles(nuspecDir, "*.nupkg").ToArray();
            return(nupkgFiles.Length == 0 ? null : nupkgFiles[0]);
        }
 private static async Task EnsureNuGetExeExistsAsync()
 {
     if (!File.Exists("nuget.exe"))
     {
         var nugetExeAddress = "https://nuget.org/nuget.exe";
         using (var webClient = new WebClient())
         {
             webClient.DownloadFileAsync(new Uri(nugetExeAddress), "nuget.exe");
         }
     }
     else
     {
         var commandlineHelper = new CommandlineHelper(ConsoleTestOutputHelper.New);
         await commandlineHelper.UpdateNugetExeAsync();
     }
 }
 private static async Task EnsureNuGetExeExistsAsync()
 {
     if (!File.Exists("nuget.exe"))
     {
         var nugetExeAddress = "https://nuget.org/nuget.exe";
         using (var webClient = new WebClient())
         {
             webClient.DownloadFileAsync(new Uri(nugetExeAddress), "nuget.exe");
         }
     }
     else
     {
         var commandlineHelper = new CommandlineHelper(ConsoleTestOutputHelper.New);
         await commandlineHelper.UpdateNugetExeAsync();
     }
 }
        /// <summary>
        /// Creates a Nuspec file given the Package Name.
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="version"></param>
        /// <param name="minClientVersion"></param>
        /// <param name="title"></param>
        /// <param name="tags"></param>
        /// <param name="description"></param>
        /// <param name="licenseUrl"></param>
        /// <param name="dependencies"></param>
        /// <returns></returns>
        public async Task <string> CreateDefaultNuspecFile(string packageName, string version = "1.0.0", string minClientVersion = null, string title = null, string tags = null, string description = null, string licenseUrl = null, string dependencies = null)
        {
            string packageDir = Path.Combine(Environment.CurrentDirectory, packageName);

            if (Directory.Exists(packageDir))
            {
                Directory.Delete(packageDir, true);
            }

            Directory.CreateDirectory(packageDir);

            var arguments         = string.Join(string.Empty, CommandlineHelper.SpecCommandString, packageName);
            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.InvokeNugetProcess(arguments, packageDir);

            string filePath = Path.Combine(packageDir, packageName + ".nuspec");

            RemoveSampleNuspecValues(filePath);
            UpdateNuspecFile(filePath, "1.0.0", version);
            UpdateNuspecFile(filePath, "Package description", "This is a test package created by the NuGet team.");
            // Apply the minClientVersion to the spec only if it's defined.
            if (minClientVersion != null)
            {
                UpdateNuspecFile(filePath, "<metadata>", String.Format("<metadata minClientVersion=\"{0}\">", minClientVersion));
            }
            if (title != null)
            {
                UpdateNuspecFile(filePath, "</metadata>", String.Format("<title>{0}</title></metadata>", title));
            }
            if (tags != null)
            {
                UpdateNuspecFile(filePath, "Tag1 Tag2", tags);
            }
            if (description != null)
            {
                UpdateNuspecFile(filePath, "This is a test package created by the NuGet team.", description);
            }
            if (licenseUrl != null)
            {
                UpdateNuspecFile(filePath, "</metadata>", String.Format("<licenseUrl>{0}</licenseUrl></metadata>", licenseUrl));
            }
            if (dependencies != null)
            {
                UpdateNuspecFile(filePath, "</dependencies>", String.Format("{0}</dependencies>", dependencies));
            }
            return(filePath);
        }
        private async Task UploadPackage(string packageId, string version, string apiKey = null, bool success = true)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException($"{nameof(packageId)} cannot be null or empty!");
            }

            if (string.IsNullOrEmpty(version))
            {
                throw new ArgumentException($"{nameof(version)} cannot be null or empty!");
            }

            await Task.Yield();

            WriteLine("Uploading new package '{0}', version '{1}'", packageId, version);

            var packageCreationHelper = new PackageCreationHelper(TestOutputHelper);
            var packageFullPath       = await packageCreationHelper.CreatePackage(packageId, version);

            try
            {
                var commandlineHelper = new CommandlineHelper(TestOutputHelper);
                var processResult     = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl, apiKey);

                if (success)
                {
                    Assert.True(processResult.ExitCode == 0,
                                "The package upload via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                                processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");

                    await VerifyPackageExistsInV2Async(packageId, version);
                }
                else
                {
                    Assert.False(processResult.ExitCode == 0,
                                 "The package upload via Nuget.exe succeeded but was expected to fail. Check the logs to see the process error and output stream.  Exit Code: " +
                                 processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
                }
            }
            finally
            {
                // Delete package from local disk once it gets uploaded
                CleanCreatedPackage(packageFullPath);
            }
        }
        /// <summary>
        /// Creates a Nuspec file given the Package Name.
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="version"></param>
        /// <param name="minClientVersion"></param>
        /// <param name="title"></param>
        /// <param name="tags"></param>
        /// <param name="description"></param>
        /// <param name="licenseUrl"></param>
        /// <param name="dependencies"></param>
        /// <returns></returns>
        public async Task<string> CreateDefaultNuspecFile(string packageName, string version = "1.0.0", string minClientVersion = null, string title = null, string tags = null, string description = null, string licenseUrl = null, string dependencies = null)
        {
            string packageDir = Path.Combine(Environment.CurrentDirectory, packageName);
            if (Directory.Exists(packageDir))
            {
                Directory.Delete(packageDir, true);
            }

            Directory.CreateDirectory(packageDir);

            var arguments = string.Join(string.Empty, CommandlineHelper.SpecCommandString, packageName);
            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.InvokeNugetProcess(arguments, packageDir);

            string filePath = Path.Combine(packageDir, packageName + ".nuspec");
            RemoveSampleNuspecValues(filePath);
            UpdateNuspecFile(filePath, "1.0.0", version);
            UpdateNuspecFile(filePath, "Package description", "This is a test package created by the NuGet team.");
            // Apply the minClientVersion to the spec only if it's defined.
            if (minClientVersion != null)
            {
                UpdateNuspecFile(filePath, "<metadata>", String.Format("<metadata minClientVersion=\"{0}\">", minClientVersion));
            }
            if (title != null)
            {
                UpdateNuspecFile(filePath, "</metadata>", String.Format("<title>{0}</title></metadata>", title));
            }
            if (tags != null)
            {
                UpdateNuspecFile(filePath, "Tag1 Tag2", tags);
            }
            if (description != null)
            {
                UpdateNuspecFile(filePath, "This is a test package created by the NuGet team.", description);
            }
            if (licenseUrl != null)
            {
                UpdateNuspecFile(filePath, "</metadata>", String.Format("<licenseUrl>{0}</licenseUrl></metadata>", licenseUrl));
            }
            if (dependencies != null)
            {
                UpdateNuspecFile(filePath, "</dependencies>", String.Format("{0}</dependencies>", dependencies));
            }
            return filePath;
        }
        public async Task UnlistPackage(string packageId, string version = "1.0.0")
        {
            if (string.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException($"{nameof(packageId)} cannot be null or empty!");
            }

            WriteLine("Unlisting package '{0}', version '{1}'", packageId, version);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            var processResult = await commandlineHelper.DeletePackageAsync(packageId, version, UrlHelper.V2FeedPushSourceUrl);

            Assert.True(processResult.ExitCode == 0,
                "The package unlist via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");
        }
        public async Task UploadNewPackage(string packageId, string version = "1.0.0", string minClientVersion = null,
            string title = null, string tags = null, string description = null, string licenseUrl = null,
            string dependencies = null)
        {
            if (string.IsNullOrEmpty(packageId))
            {
                packageId = DateTime.Now.Ticks.ToString();
            }

            WriteLine("Uploading new package '{0}', version '{1}'", packageId, version);

            var packageCreationHelper = new PackageCreationHelper(TestOutputHelper);
            var packageFullPath = await packageCreationHelper.CreatePackage(packageId, version, minClientVersion, title, tags, description, licenseUrl, dependencies);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            var processResult = await commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl);

            Assert.True(processResult.ExitCode == 0,
                "The package upload via Nuget.exe did not succeed properly. Check the logs to see the process error and output stream.  Exit Code: " +
                processResult.ExitCode + ". Error message: \"" + processResult.StandardError + "\"");

            // Delete package from local disk once it gets uploaded
            if (File.Exists(packageFullPath))
            {
                File.Delete(packageFullPath);
                Directory.Delete(Path.GetFullPath(Path.GetDirectoryName(packageFullPath)), true);
            }
        }
        private async Task<string> CreatePackageWithTargetFrameworkInternal(string nuspecFileFullPath, string frameworkVersion)
        {
            string nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);
            AddContent(nuspecDir, frameworkVersion);
            AddLib(nuspecDir, frameworkVersion);
            var arguments = string.Join(string.Empty, CommandlineHelper.PackCommandString, @"""" + nuspecFileFullPath + @"""", CommandlineHelper.OutputDirectorySwitchString, @"""" + nuspecDir + @"""");

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.InvokeNugetProcess(arguments, Path.GetFullPath(Path.GetDirectoryName(nuspecFileFullPath)));

            string[] nupkgFiles = Directory.GetFiles(nuspecDir, "*.nupkg").ToArray();
            return nupkgFiles.Length == 0 ? null : nupkgFiles[0];
        }
        private async Task<string> CreatePackageWithTargetFrameworkInternal(string nuspecFileFullPath, string frameworkVersion)
        {
            string nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);
            AddContent(nuspecDir, frameworkVersion);
            AddLib(nuspecDir, frameworkVersion);

            var commandlineHelper = new CommandlineHelper(TestOutputHelper);
            await commandlineHelper.PackPackageAsync(nuspecFileFullPath, nuspecDir);

            string[] nupkgFiles = Directory.GetFiles(nuspecDir, "*.nupkg").ToArray();
            return nupkgFiles.Length == 0 ? null : nupkgFiles[0];
        }