Skip to content

Commit

Permalink
Merge pull request #95 from AdmiringWorm/additional-requirements
Browse files Browse the repository at this point in the history
(GH-70) Validation for description element in nuspec file
  • Loading branch information
gep13 committed Feb 13, 2019
2 parents 6dc31ec + 54ae5df commit 0b71c68
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Chocolatey.Language.Server/DiagnosticsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void PublishDiagnostics(Uri uri, Buffer buffer)
var diagnostics = new List<Diagnostic>();

diagnostics.AddRange(NuspecDoesNotContainTemplatedValuesRequirement(syntaxTree, textPositions));
diagnostics.AddRange(NuspecDescriptionLengthValidation(syntaxTree, textPositions));

_router.Document.PublishDiagnostics(new PublishDiagnosticsParams
{
Expand All @@ -64,5 +65,59 @@ private IEnumerable<Diagnostic> NuspecDoesNotContainTemplatedValuesRequirement(X
};
}
}

/// <summary>
/// Handler to validate the length of description in the package metadata.
/// </summary>
/// <seealso href="https://github.com/chocolatey/package-validator/blob/master/src/chocolatey.package.validator/infrastructure.app/rules/DescriptionRequirement.cs">Package validator requirement for description.</seealso>
/// <seealso href="https://github.com/chocolatey/package-validator/blob/master/src/chocolatey.package.validator/infrastructure.app/rules/DescriptionWordCountMaximum4000Requirement.cs">Package validator maximum length requirement for description.</seealso>
/// <seealso href="https://github.com/chocolatey/package-validator/blob/master/src/chocolatey.package.validator/infrastructure.app/rules/DescriptionWordCountMinimum30Guideline.cs">Package validator minimum length guideline for description.</seealso>
private IEnumerable<Diagnostic> NuspecDescriptionLengthValidation(XmlDocumentSyntax syntaxTree, TextPositions textPositions)
{
var descriptionElement = syntaxTree.DescendantNodes().OfType<XmlElementSyntax>().FirstOrDefault(x => string.Equals(x.Name, "description", StringComparison.OrdinalIgnoreCase));

if (descriptionElement == null)
{
yield return new Diagnostic {
Message = "Description is required. See https://github.com/chocolatey/package-validator/wiki/DescriptionNotEmpty",
Severity = DiagnosticSeverity.Error,
Range = textPositions.GetRange(0, syntaxTree.End)
};
yield break;
}

var descriptionLength = descriptionElement.GetContentValue().Trim().Length;

if (descriptionLength == 0)
{
var range = textPositions.GetRange(descriptionElement.StartTag.End, descriptionElement.EndTag.Start);

yield return new Diagnostic {
Message = "Description is required. See https://github.com/chocolatey/package-validator/wiki/DescriptionNotEmpty",
Severity = DiagnosticSeverity.Error,
Range = range
};
}
else if (descriptionLength <= 30)
{
var range = textPositions.GetRange(descriptionElement.StartTag.End, descriptionElement.EndTag.Start);

yield return new Diagnostic {
Message = "Description should be sufficient to explain the software. See https://github.com/chocolatey/package-validator/wiki/DescriptionCharacterCountMinimum",
Severity = DiagnosticSeverity.Warning,
Range = range
};
}
else if (descriptionLength > 4000)
{
var range = textPositions.GetRange(descriptionElement.StartTag.End, descriptionElement.EndTag.Start);

yield return new Diagnostic {
Message = "Description should not exceed 4000 characters. See https://github.com/chocolatey/package-validator/wiki/DescriptionCharacterCountMaximum",
Severity = DiagnosticSeverity.Error,
Range = range
};
}
}
}
}

0 comments on commit 0b71c68

Please sign in to comment.