Skip to content

Commit

Permalink
Merge pull request chocolatey-community#51 from AdmiringWorm/cpmr0070
Browse files Browse the repository at this point in the history
(chocolatey-community#39) Add rule for validating underscore in ID
  • Loading branch information
gep13 authored Jul 8, 2024
2 parents 3450719 + b04709a commit e1e65f7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0070,
Id: CPMR0070,
Message: Package ID contains underscores (_). Normally a Package ID is separated by dashes (-).,
Severity: Note
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
Id: CPMR0061,
Summary: Package ID contains dots (.), that is not part of the accepted suffixes.,
HelpUrl: https://ch0.co/rules/cpmr0061
},
{
Severity: Note,
Id: CPMR0070,
Summary: Package ID contains underscores (_). Normally a Package ID is separated by dashes (-).,
HelpUrl: https://ch0.co/rules/cpmr0070
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public async Task ShouldNotFlagIdentifier(string id)
[TestCase("something.other.install")]
[TestCase("something.other.template")]
[TestCase("something.other.extension")]
[TestCase("with_underscores")]
public async Task ShouldFlagIdentifier(string id)
{
var testContent = GetTestContent(id);
Expand Down
7 changes: 7 additions & 0 deletions src/Chocolatey.Community.Validation/Rules/IdElementRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public sealed class IdElementRules : CCRMetadataRuleBase
private const string ConfigRuleId = "CPMR0029";
private const string DotsInIdentifierRuleId = "CPMR0061";
private const string PreReleaseRuleId = "CPMR0024";
private const string UnderscoreRuleId = "CPMR0070";

public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecReader reader)
{
Expand Down Expand Up @@ -37,6 +38,11 @@ public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecR
yield return GetRule(ConfigRuleId);
}

if (id.IndexOf('_') >= 0)
{
yield return GetRule(UnderscoreRuleId);
}

var subId = GetSubIdentifier(id);

if (subId.IndexOf('.') > -1)
Expand All @@ -50,6 +56,7 @@ public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecR
yield return (RuleType.Error, PreReleaseRuleId, "Package ID includes a prerelease version name.");
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 (-).");
}

private static string GetSubIdentifier(string id)
Expand Down

0 comments on commit e1e65f7

Please sign in to comment.