Skip to content

Commit

Permalink
Merge pull request #49 from AdmiringWorm/34-rule-cpmr0061
Browse files Browse the repository at this point in the history
(#34) Implement note rule CPMR0061 - Id Contains "."
  • Loading branch information
gep13 committed Jul 8, 2024
2 parents 6f4acd8 + 819f526 commit 3450719
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
HelpUrl: https://ch0.co/rules/cpmr0061,
Id: CPMR0061,
Message: Package ID contains dots (.), that is not part of the accepted suffixes.,
Severity: Note
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
Id: CPMR0029,
Summary: Package ID ends with the reserved suffix `.config`.,
HelpUrl: https://ch0.co/rules/cpmr0029
},
{
Severity: Note,
Id: CPMR0061,
Summary: Package ID contains dots (.), that is not part of the accepted suffixes.,
HelpUrl: https://ch0.co/rules/cpmr0061
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ namespace Chocolatey.Community.Validation.Tests.Rules

public class IdElementRulesTests : RuleTestBase<IdElementRules>
{
[TestCase(null)]
[TestCase("")]
[TestCase("some-id")]
[TestCase("something.portable")]
[TestCase("something.commandline")]
[TestCase("something.powershell")]
[TestCase("something.install")]
[TestCase("something.template")]
[TestCase("something.extension")]
public async Task ShouldNotFlagIdentifier(string id)
{
var testContent = GetTestContent(id);

await VerifyEmptyResults(testContent);
}

[TestCase("testalpha")]
[TestCase("test-ALPha")]
[TestCase("testBETA")]
Expand All @@ -15,29 +31,20 @@ public class IdElementRulesTests : RuleTestBase<IdElementRules>
[TestCase("my prerelease")]
[TestCase("my-package.CONFIG")]
[TestCase("pkg.config")]
[TestCase("something.other")]
[TestCase("something.other.portable")]
[TestCase("different.something.commandline")]
[TestCase("something.other.powershell")]
[TestCase("something.other.install")]
[TestCase("something.other.template")]
[TestCase("something.other.extension")]
public async Task ShouldFlagIdentifier(string id)
{
var testContent = GetTestContent(id);

await VerifyNuspec(testContent);
}

[Test]
public async Task ShouldNotFlagIdentifierWithoutPrereleaseName()
{
var testContent = GetTestContent("some-id");

await VerifyEmptyResults(testContent);
}

[Test]
public async Task ShouldNotFlagAnEmptyIdentifier()
{
var testContent = GetTestContent(string.Empty);

await VerifyEmptyResults(testContent);
}

private static string GetTestContent(string? id)
{
const string format = @"<?xml version=""1.0"" encoding=""utf-8""?>
Expand Down
36 changes: 35 additions & 1 deletion src/Chocolatey.Community.Validation/Rules/IdElementRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace Chocolatey.Community.Validation.Rules

public sealed class IdElementRules : CCRMetadataRuleBase
{
private const string PreReleaseRuleId = "CPMR0024";
private const string ConfigRuleId = "CPMR0029";
private const string DotsInIdentifierRuleId = "CPMR0061";
private const string PreReleaseRuleId = "CPMR0024";

public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecReader reader)
{
Expand All @@ -35,12 +36,45 @@ public override IEnumerable<RuleResult> Validate(global::NuGet.Packaging.NuspecR
{
yield return GetRule(ConfigRuleId);
}

var subId = GetSubIdentifier(id);

if (subId.IndexOf('.') > -1)
{
yield return GetRule(DotsInIdentifierRuleId);
}
}

protected internal override IEnumerable<(RuleType severity, string? id, string summary)> GetRulesInformation()
{
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.");
}

private static string GetSubIdentifier(string id)
{
var possibleSuffixes = new[]
{
".portable",
".commandline",
".install",
".extension",
".template",
".powershell",
// This is not an accepted suffix, but an existing rule aready handle this. So we ignore it.
".config"
};

foreach (var suffix in possibleSuffixes)
{
if (id.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
{
return id.Substring(0, id.Length - suffix.Length);
}
}

return id;
}
}
}

0 comments on commit 3450719

Please sign in to comment.