public void MinClientVersionUtility_ReadFromNuspecNullDoesNotThrow()
        {
            // Arrange
            var nuspec = GetNuspec();

            // Act & Assert
            MinClientVersionUtility.VerifyMinClientVersion(nuspec);
        }
        public void MinClientVersionUtility_ReadFromNuspecInCompatThrows()
        {
            // Arrange
            var nuspec = GetNuspec("99.0.0");

            // Act & Assert
            Assert.Throws(typeof(MinClientVersionException),
                          () => MinClientVersionUtility.VerifyMinClientVersion(nuspec));
        }
        private static void EnsurePackageCompatibility(
            NuGetProject nuGetProject,
            PackageIdentity packageIdentity,
            NuspecReader nuspecReader)
        {
            // Validate that the current version of NuGet satisfies the minVersion attribute specified in the .nuspec
            MinClientVersionUtility.VerifyMinClientVersion(nuspecReader);

            // Validate the package type. There must be zero package types or exactly one package
            // type that is one of the recognized package types.
            var packageTypes   = nuspecReader.GetPackageTypes();
            var identityString = $"{packageIdentity.Id} {packageIdentity.Version.ToNormalizedString()}";

            if (packageTypes.Count > 1)
            {
                throw new PackagingException(string.Format(
                                                 CultureInfo.CurrentCulture,
                                                 Strings.MultiplePackageTypesNotSupported,
                                                 identityString));
            }
            else if (packageTypes.Count == 1)
            {
                var packageType       = packageTypes[0];
                var packageTypeString = packageType.Name;
                if (packageType.Version != PackageType.EmptyVersion)
                {
                    packageTypeString += " " + packageType.Version;
                }

                var projectName = NuGetProject.GetUniqueNameOrName(nuGetProject);

                if (packageType == PackageType.Legacy ||   // Added for "quirks mode", but not yet fully implemented.
                    packageType == PackageType.Dependency) // A package explicitly stated as a dependency.
                {
                    // These types are always acceptable.
                }
                else if (nuGetProject is ProjectKNuGetProjectBase &&
                         packageType == PackageType.DotnetCliTool)
                {
                    // ProjectKNuGetProjectBase projects are .NET Core (both "dotnet" and "dnx").
                    // .NET CLI tools are support for "dotnet" projects. The projects eventually
                    // call into INuGetPackageManager, which is not implemented by NuGet. This code
                    // will make the decision of how to install the .NET CLI tool package.
                }
                else
                {
                    throw new PackagingException(string.Format(
                                                     CultureInfo.CurrentCulture,
                                                     Strings.UnsupportedPackageType,
                                                     identityString,
                                                     packageTypeString,
                                                     projectName));
                }
            }
        }
        /// <summary>
        /// Read dependency info from a nuspec.
        /// </summary>
        /// <remarks>This also verifies minClientVersion.</remarks>
        protected static FindPackageByIdDependencyInfo GetDependencyInfo(NuspecReader reader)
        {
            // Since this is the first place a package is read after selecting it as the best version
            // check the minClientVersion here to verify we are okay to read this package.
            MinClientVersionUtility.VerifyMinClientVersion(reader);

            // Create dependency info
            return(new FindPackageByIdDependencyInfo(
                       reader.GetDependencyGroups(),
                       reader.GetFrameworkReferenceGroups()));
        }
Esempio n. 5
0
        private static void EnsurePackageCompatibility(
            NuGetProject nuGetProject,
            PackageIdentity packageIdentity,
            NuspecReader nuspecReader)
        {
            // Validate that the current version of NuGet satisfies the minVersion attribute specified in the .nuspec
            MinClientVersionUtility.VerifyMinClientVersion(nuspecReader);

            // Validate the package type. There must be zero package types or exactly one package
            // type that is one of the recognized package types.
            var packageTypes   = nuspecReader.GetPackageTypes();
            var identityString = $"{packageIdentity.Id} {packageIdentity.Version.ToNormalizedString()}";

            if (packageTypes.Count > 1)
            {
                throw new PackagingException(string.Format(
                                                 CultureInfo.CurrentCulture,
                                                 Strings.MultiplePackageTypesNotSupported,
                                                 identityString));
            }
            else if (packageTypes.Count == 1)
            {
                var packageType       = packageTypes[0];
                var packageTypeString = packageType.Name;
                if (packageType.Version != PackageType.EmptyVersion)
                {
                    packageTypeString += " " + packageType.Version;
                }

                var projectName = NuGetProject.GetUniqueNameOrName(nuGetProject);

                if (packageType == PackageType.Legacy ||   // Added for "quirks mode", but not yet fully implemented.
                    packageType == PackageType.Dependency) // A package explicitly stated as a dependency.
                {
                    // These types are always acceptable.
                }
                else
                {
                    throw new PackagingException(string.Format(
                                                     CultureInfo.CurrentCulture,
                                                     Strings.UnsupportedPackageType,
                                                     identityString,
                                                     packageTypeString,
                                                     projectName));
                }
            }
        }
        public void MinClientVersionUtility_ReadFromNuspecInCompatThrowsVerifyLogMessage()
        {
            // Arrange
            var         nuspec     = GetNuspec("99.0.0");
            ILogMessage logMessage = null;

            // Act
            try
            {
                MinClientVersionUtility.VerifyMinClientVersion(nuspec);
            }
            catch (MinClientVersionException ex)
            {
                logMessage = ex.AsLogMessage();
            }

            // Assert
            Assert.Equal(NuGetLogCode.NU1401, logMessage.Code);
            Assert.Contains("requires NuGet client version", logMessage.Message);
            Assert.Equal(LogLevel.Error, logMessage.Level);
        }