Exemplo n.º 1
0
        private static void GetPropertyNamesAndRuleSet(StringBuilder sb, RequirePackageMetadataState state)
        {
            // Determine more the .NET property names... which are more readable.
            var properties = typeof(RequirePackageMetadataState)
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .OrderBy(x => x.Name);
            var firstHeading    = "Readable .NET Name";
            var maxPropertyName = Math.Max(properties.Max(x => x.Name.Length), firstHeading.Length);

            sb.AppendLine($"{firstHeading.PadRight(maxPropertyName)} | JSON Name");
            sb.AppendLine($"{new string('-', maxPropertyName)} | ----------");
            foreach (var property in properties)
            {
                var jsonProperty = property.GetCustomAttribute <JsonPropertyAttribute>();
                if (jsonProperty != null)
                {
                    sb.AppendLine($"{property.Name.PadRight(maxPropertyName)} | {jsonProperty.PropertyName}");
                }
                else
                {
                    sb.AppendLine($"{property.Name.PadRight(maxPropertyName)} | {property.Name}");
                }
            }

            // Display the JSON ruleset
            sb.AppendLine();
            sb.AppendLine($"If question marks ('?') or weird characters appear below, consider using {WriteDefaultRuleSetOptionTemplate}.");
            sb.AppendLine();
            sb.AppendLine(SerializeState(state));
        }
Exemplo n.º 2
0
        public void UsesCustomRuleSetForInvalidPackage()
        {
            var state = new RequirePackageMetadataState
            {
                AllowedCopyrightNotices = new[] { "My copyright" },
                AllowedAuthors          = new[] { "My authors" },
            };
            var ruleSetPath = Path.Combine(_directory, "rules.json");

            File.WriteAllText(ruleSetPath, JsonConvert.SerializeObject(state));
            var args = new[]
            {
                Path.Combine(_directory, "inner", "testA.nupkg"),
                "--rule-set",
                ruleSetPath
            };

            CreatePackage(Path.Combine("inner", "testA.nupkg"));

            var exitCode = Program.Run(args, _console);

            Assert.Equal(1, exitCode);
            AssertCounts(valid: 0, invalid: 1);
            Assert.Contains("  - The package metadata defines 'Microsoft' as one of the authors which is not allowed by policy.", _console.Messages);
            Assert.Contains("  - The package metadata contains a non-compliant copyright element.", _console.Messages);
        }
Exemplo n.º 3
0
        public void UsesCustomRuleSetForValidPackage()
        {
            var state = new RequirePackageMetadataState
            {
                AllowedCopyrightNotices = new[] { "My copyright" },
                AllowedAuthors          = new[] { "My authors" },
                IsLicenseUrlRequired    = false,
                IsProjectUrlRequired    = false,
            };
            var ruleSetPath = Path.Combine(_directory, "rules.json");

            File.WriteAllText(ruleSetPath, JsonConvert.SerializeObject(state));
            var args = new[]
            {
                Path.Combine(_directory, "inner", "testA.nupkg"),
                "--rule-set",
                ruleSetPath
            };

            CreatePackage(
                Path.Combine("inner", "testA.nupkg"),
                copyright: state.AllowedCopyrightNotices[0],
                authors: state.AllowedAuthors[0],
                licenseUrl: null,
                projectUrl: null);

            var exitCode = Program.Run(args, _console);

            Assert.Equal(0, exitCode);
            AssertCounts(valid: 1, invalid: 0);
        }
Exemplo n.º 4
0
 private static string SerializeState(RequirePackageMetadataState state)
 {
     return(JsonConvert.SerializeObject(state, Formatting.Indented));
 }
Exemplo n.º 5
0
        private async Task <bool> IsValidAsync(
            IPackageService packageService,
            RequirePackageMetadataState state,
            string packagePath)
        {
            if (!File.Exists(packagePath) && !Directory.Exists(packagePath))
            {
                OutputColor(
                    ConsoleColor.Red,
                    () =>
                {
                    _console.WriteLine("INVALID.");
                    _console.WriteLine(packagePath);
                    _console.WriteLine("The path does not exist.");
                });
                return(false);
            }

            if (File.GetAttributes(packagePath).HasFlag(FileAttributes.Directory))
            {
                OutputColor(
                    ConsoleColor.Red,
                    () =>
                {
                    _console.WriteLine("INVALID.");
                    _console.WriteLine(packagePath);
                    _console.WriteLine("The path is a directory, not a file.");
                });
                return(false);
            }

            Package package;

            using (var packageStream = File.OpenRead(packagePath))
            {
                var packageArchiveReader = new PackageArchiveReader(packageStream);

                var packageStreamMetadata = new PackageStreamMetadata
                {
                    HashAlgorithm = CoreConstants.Sha512HashAlgorithmId,
                    Hash          = CryptographyService.GenerateHash(
                        packageStream.AsSeekableStream(),
                        CoreConstants.Sha512HashAlgorithmId),
                    Size = packageStream.Length
                };

                var owner       = new User();
                var currentUser = owner;
                var isVerified  = true;

                package = await packageService.CreatePackageAsync(
                    packageArchiveReader,
                    packageStreamMetadata,
                    owner,
                    currentUser,
                    isVerified);
            }

            var isCompliant = RequirePackageMetadataComplianceUtility.IsPackageMetadataCompliant(
                package,
                state,
                out var complianceFailures);

            if (isCompliant)
            {
                OutputColor(
                    ConsoleColor.Green,
                    () =>
                {
                    _console.WriteLine("VALID.");
                    _console.WriteLine(packagePath);
                    _console.WriteLine($"The package {package.Id} {package.Version} is compliant.");
                });
                return(true);
            }
            else
            {
                OutputColor(
                    ConsoleColor.Red,
                    () =>
                {
                    var single = complianceFailures.Count == 1;
                    _console.WriteLine("INVALID.");
                    _console.WriteLine(packagePath);
                    _console.WriteLine($"The package {package.Id} {package.Version} is not compliant.");
                    _console.WriteLine($"There {(single ? "is" : "are")} {complianceFailures.Count} problem{(single ? string.Empty : "s")}.");
                    foreach (var failure in complianceFailures)
                    {
                        _console.WriteLine($"  - {failure}");
                    }
                });
                return(false);
            }
        }