Skip to content

Commit

Permalink
(chocolatey#3000) Move unsupported element rules to rule classes
Browse files Browse the repository at this point in the history
This commit moves the existing rules that was added previously for
unsupported metadata to their own classes that hooks
into the rule service to make the validation.
  • Loading branch information
AdmiringWorm committed Jan 27, 2023
1 parent fbe4c0f commit e9e180e
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 71 deletions.
16 changes: 8 additions & 8 deletions src/chocolatey.tests.integration/scenarios/PackScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,63 +439,63 @@ public void should_throw_exception_on_all_unsupported_elements()
{
AddFile("myPackage.nuspec", NuspecContentWithAllUnsupportedElements);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_serviceable_element()
{
AddFile("myPackage.nuspec", NuspecContentWithServiceableElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_license_element()
{
AddFile("myPackage.nuspec", NuspecContentWithLicenseElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_repository_element()
{
AddFile("myPackage.nuspec", NuspecContentWithRepositoryElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_package_types_element()
{
AddFile("myPackage.nuspec", NuspecContentWithPackageTypesElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_framework_references_element()
{
AddFile("myPackage.nuspec", NuspecContentWithFrameWorkReferencesElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_readme_element()
{
AddFile("myPackage.nuspec", NuspecContentWithReadmeElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}

[Fact]
public void should_throw_exception_on_icon_element()
{
AddFile("myPackage.nuspec", NuspecContentWithIconElement);

ServiceAct.ShouldThrow<System.IO.FileFormatException>();
ServiceAct.ShouldThrow<System.IO.InvalidDataException>();
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props" Condition="Exists('..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -216,6 +216,13 @@
<Compile Include="infrastructure.app\nuget\ChocolateyNuGetSettings.cs" />
<Compile Include="infrastructure.app\nuget\ChocolateySourceCacheContext.cs" />
<Compile Include="infrastructure.app\rules\MetadataRuleBase.cs" />
<Compile Include="infrastructure.app\rules\FrameWorkReferencesMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\IconMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\LicenseMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\PackageTypesMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\ReadmeMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\RepositoryMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\ServicableMetadataRule.cs" />
<Compile Include="infrastructure.app\services\RuleService.cs" />
<Compile Include="infrastructure\cryptography\DefaultEncryptionUtility.cs" />
<Compile Include="infrastructure\adapters\IEncryptionUtility.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class FrameWorkReferencesMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (has_element(reader, "frameworkReferences"))
{
yield return new RuleResult(RuleType.Error, "<frameworkReferences> elements are not supported in Chocolatey CLI.");
}
}
}
}
32 changes: 32 additions & 0 deletions src/chocolatey/infrastructure.app/rules/IconMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class IconMetadataRule : IMetadataRule
{
public IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (!(reader.GetIcon() is null))
{
yield return new RuleResult(RuleType.Error, "<icon> elements are not supported in Chocolatey CLI, use <iconUrl> instead.");
}
}
}
}
33 changes: 33 additions & 0 deletions src/chocolatey/infrastructure.app/rules/LicenseMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class LicenseMetadataRule : IMetadataRule
{
public IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (!(reader.GetLicenseMetadata() is null))
{
yield return new RuleResult(RuleType.Error, "<license> elements are not supported in Chocolatey CLI, use <licenseUrl> instead.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class PackageTypesMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (has_element(reader, "packageTypes"))
{
yield return new RuleResult(RuleType.Error, "<packageTypes> elements are not supported in Chocolatey CLI.");
}
}
}
}
32 changes: 32 additions & 0 deletions src/chocolatey/infrastructure.app/rules/ReadmeMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class ReadmeMetadataRule : IMetadataRule
{
public IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (!(reader.GetReadme() is null))
{
yield return new RuleResult(RuleType.Error, "<readme> elements are not supported in Chocolatey CLI.");
}
}
}
}
36 changes: 36 additions & 0 deletions src/chocolatey/infrastructure.app/rules/RepositoryMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System;
using System.Collections.Generic;
using System.Linq;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class RepositoryMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
var metadataNode = reader.Xml.Root.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, "metadata"));

if (has_element(reader, "repository"))
{
yield return new RuleResult(RuleType.Error, "<repository> elements are not supported in Chocolatey CLI, use <packageSourceUrl> instead.");
}
}
}
}
32 changes: 32 additions & 0 deletions src/chocolatey/infrastructure.app/rules/ServicableMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2023-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class ServicableMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (has_element(reader, "serviceable"))
{
yield return new RuleResult(RuleType.Error, "<serviceable> elements are not supported in Chocolatey CLI.");
}
}
}
}
Loading

0 comments on commit e9e180e

Please sign in to comment.