Skip to content

Commit

Permalink
Merge pull request #53 from AdmiringWorm/issue38
Browse files Browse the repository at this point in the history
(#38) Add rule for validating ID length
  • Loading branch information
gep13 authored Jul 29, 2024
2 parents e1e65f7 + 7d89632 commit aa3e029
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0024,
Id: CPMR0024,
Message: Package ID includes a prerelease version name.,
Severity: Error
},
{
HelpUrl: https://ch0.co/rules/cpmr0029,
Id: CPMR0029,
Message: Package ID ends with the reserved suffix `.config`.,
Severity: Error
},
{
HelpUrl: https://ch0.co/rules/cpmr0070,
Id: CPMR0070,
Message: Package ID contains underscores (_). Normally a Package ID is separated by dashes (-).,
Severity: Note
},
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
},
{
HelpUrl: https://ch0.co/rules/cpmr0069,
Id: CPMR0069,
Message: Package ID contains 50 characters without being separated by a dash (-).,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0069,
Id: CPMR0069,
Message: Package ID contains 37 characters with one or more parts being more than 20 characters.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0069,
Id: CPMR0069,
Message: Package ID contains 48 characters without being separated by a dash (-).,
Severity: Note
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
Id: CPMR0070,
Summary: Package ID contains underscores (_). Normally a Package ID is separated by dashes (-).,
HelpUrl: https://ch0.co/rules/cpmr0070
},
{
Severity: Note,
Id: CPMR0069,
Summary: Package ID contains too many characters without being separated by a dash (-).,
HelpUrl: https://ch0.co/rules/cpmr0069
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class IdElementRulesTests : RuleTestBase<IdElementRules>
[TestCase("something.install")]
[TestCase("something.template")]
[TestCase("something.extension")]
[TestCase("iamalongidentifier.extension")]
[TestCase("i-am-a-long-identifier-separated-by-dashes")]
public async Task ShouldNotFlagIdentifier(string id)
{
var testContent = GetTestContent(id);
Expand All @@ -39,6 +41,9 @@ public async Task ShouldNotFlagIdentifier(string id)
[TestCase("something.other.template")]
[TestCase("something.other.extension")]
[TestCase("with_underscores")]
[TestCase("iamaverylongidentifierthatismorethan20characters")]
[TestCase("i-haveasectionlongerthan20-characters.install")]
[TestCase("IAmALongeAlpha.CharacterThatMatches_Multiple_Rules.config")]
public async Task ShouldFlagIdentifier(string id)
{
var testContent = GetTestContent(id);
Expand Down
18 changes: 18 additions & 0 deletions src/Chocolatey.Community.Validation/Rules/IdElementRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class IdElementRules : CCRMetadataRuleBase
private const string DotsInIdentifierRuleId = "CPMR0061";
private const string PreReleaseRuleId = "CPMR0024";
private const string UnderscoreRuleId = "CPMR0070";
private const string TooLongRuleId = "CPMR0069";

public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecReader reader)
{
Expand Down Expand Up @@ -49,6 +50,22 @@ public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecR
{
yield return GetRule(DotsInIdentifierRuleId);
}

if (subId.Length >= 20 && subId.IndexOf('-') >= 0)
{
foreach (var partId in subId.Split('-'))
{
if (partId.Length >= 20)
{
yield return GetRule(TooLongRuleId, "Package ID contains {0} characters with one or more parts being more than 20 characters.".FormatWith(subId.Length));
break;
}
}
}
else if (subId.Length >= 20)
{
yield return GetRule(TooLongRuleId, "Package ID contains {0} characters without being separated by a dash (-).".FormatWith(subId.Length));
}
}

protected internal override IEnumerable<(RuleType severity, string? id, string summary)> GetRulesInformation()
Expand All @@ -57,6 +74,7 @@ public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecR
yield return (RuleType.Error, ConfigRuleId, "Package ID ends with the reserved suffix `.config`.");
yield return (RuleType.Note, DotsInIdentifierRuleId, "Package ID contains dots (.), that is not part of the accepted suffixes.");
yield return (RuleType.Note, UnderscoreRuleId, "Package ID contains underscores (_). Normally a Package ID is separated by dashes (-).");
yield return (RuleType.Note, TooLongRuleId, "Package ID contains too many characters without being separated by a dash (-).");
}

private static string GetSubIdentifier(string id)
Expand Down

0 comments on commit aa3e029

Please sign in to comment.