diff --git a/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj b/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj index a31459e..4641f34 100644 --- a/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj +++ b/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj @@ -83,7 +83,7 @@ Properties\SolutionVersion.cs - + diff --git a/src/chocolatey.package.validator.tests/infrastructure.app/CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecs.cs b/src/chocolatey.package.validator.tests/infrastructure.app/CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecs.cs deleted file mode 100644 index 2b3d6f7..0000000 --- a/src/chocolatey.package.validator.tests/infrastructure.app/CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecs.cs +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright © 2015 - Present RealDimensions Software, LLC -// -// 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.package.validator.tests.infrastructure.app -{ - using System.Collections.Generic; - using chocolatey.package.validator.infrastructure.app.rules; - using chocolatey.package.validator.infrastructure.rules; - using Moq; - using NuGet; - using Should; - - public abstract class CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecsBase : TinySpec - { - protected CopyrightAndAuthorFieldsShouldntContainEmailRequirement requirement; - protected Mock package = new Mock(); - protected Mock packageFile = new Mock(); - - public override void Context() - { - requirement = new CopyrightAndAuthorFieldsShouldntContainEmailRequirement(); - } - - public class when_inspecting_package_with_email_in_copyright_field : CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecsBase - { - private PackageValidationOutput result; - - public override void Context() - { - base.Context(); - - package.Setup(p => p.Copyright).Returns("bob@bob.co.uk"); - } - - public override void Because() - { - result = requirement.is_valid(package.Object); - } - - [Fact] - public void should_not_be_valid() - { - result.Validated.ShouldBeFalse(); - } - - [Fact] - public void should_not_override_the_base_message() - { - result.ValidationFailureMessageOverride.ShouldBeNull(); - } - } - - public class when_inspecting_package_with_no_email_in_copyright_field : CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecsBase - { - private PackageValidationOutput result; - - public override void Context() - { - base.Context(); - - package.Setup(p => p.Copyright).Returns("Copyright 2016"); - } - - public override void Because() - { - result = requirement.is_valid(package.Object); - } - - [Fact] - public void should_be_valid() - { - result.Validated.ShouldBeTrue(); - } - - [Fact] - public void should_not_override_the_base_message() - { - result.ValidationFailureMessageOverride.ShouldBeNull(); - } - } - - public class when_inspecting_package_with_email_in_author_field : CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecsBase - { - private PackageValidationOutput result; - - public override void Context() - { - base.Context(); - - package.Setup(p => p.Authors).Returns(new List { "Chocolatey", "bob@bob.co.uk"}); - } - - public override void Because() - { - result = requirement.is_valid(package.Object); - } - - [Fact] - public void should_not_be_valid() - { - result.Validated.ShouldBeFalse(); - } - - [Fact] - public void should_not_override_the_base_message() - { - result.ValidationFailureMessageOverride.ShouldBeNull(); - } - } - - public class when_inspecting_package_with_no_email_in_author_field : CopyrightAndAuthorFieldsShouldntContainEmailRequirementSpecsBase - { - private PackageValidationOutput result; - - public override void Context() - { - base.Context(); - - package.Setup(p => p.Authors).Returns(new List {"Chocolatey", "NuGet"} ); - } - - public override void Because() - { - result = requirement.is_valid(package.Object); - } - - [Fact] - public void should_be_valid() - { - result.Validated.ShouldBeTrue(); - } - - [Fact] - public void should_not_override_the_base_message() - { - result.ValidationFailureMessageOverride.ShouldBeNull(); - } - } - } -} \ No newline at end of file diff --git a/src/chocolatey.package.validator.tests/infrastructure.app/NuspecDoesNotContainEmailRequirementSpecs.cs b/src/chocolatey.package.validator.tests/infrastructure.app/NuspecDoesNotContainEmailRequirementSpecs.cs new file mode 100644 index 0000000..59ad991 --- /dev/null +++ b/src/chocolatey.package.validator.tests/infrastructure.app/NuspecDoesNotContainEmailRequirementSpecs.cs @@ -0,0 +1,328 @@ +// Copyright © 2015 - Present RealDimensions Software, LLC +// +// 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.package.validator.tests.infrastructure.app +{ + using System.Collections.Generic; + using chocolatey.package.validator.infrastructure.app.rules; + using chocolatey.package.validator.infrastructure.rules; + using Moq; + using NuGet; + using Should; + + public abstract class NuspecDoesNotContainEmailRequirementSpecsBase : TinySpec + { + protected NuspecDoesNotContainEmailRequirement requirement; + protected Mock package = new Mock(); + protected Mock packageFile = new Mock(); + + public override void Context() + { + requirement = new NuspecDoesNotContainEmailRequirement(); + } + + public class when_inspecting_package_with_email_in_copyright_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Copyright).Returns("bob@bob.co.uk"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_no_email_in_copyright_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Copyright).Returns("Copyright 2016"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_email_in_author_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Authors).Returns(new List { "Chocolatey", "bob@bob.co.uk"}); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_no_email_in_author_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Authors).Returns(new List {"Chocolatey", "NuGet"} ); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_email_in_description_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Description).Returns("bob@bob.co.uk"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_no_email_in_description_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Description).Returns("Package Description"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_email_in_release_notes_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.ReleaseNotes).Returns("bob@bob.co.uk"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_no_email_in_release_notes_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.ReleaseNotes).Returns("Package Release Notes"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_email_in_tags_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Tags).Returns( + "test bob@bob.co.uk"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_no_email_in_tags_field : NuspecDoesNotContainEmailRequirementSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + package.Setup(p => p.Tags).Returns( + "test admin"); + } + + public override void Because() + { + result = requirement.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + } +} \ No newline at end of file diff --git a/src/chocolatey.package.validator/chocolatey.package.validator.csproj b/src/chocolatey.package.validator/chocolatey.package.validator.csproj index 3490ac5..c7e9156 100644 --- a/src/chocolatey.package.validator/chocolatey.package.validator.csproj +++ b/src/chocolatey.package.validator/chocolatey.package.validator.csproj @@ -132,7 +132,7 @@ - + diff --git a/src/chocolatey.package.validator/infrastructure.app/rules/CopyRightAndAuthorFieldsShouldntContainEmailRequirement.cs b/src/chocolatey.package.validator/infrastructure.app/rules/NuspecDoesNotContainEmailRequirement.cs similarity index 50% rename from src/chocolatey.package.validator/infrastructure.app/rules/CopyRightAndAuthorFieldsShouldntContainEmailRequirement.cs rename to src/chocolatey.package.validator/infrastructure.app/rules/NuspecDoesNotContainEmailRequirement.cs index 72ecaf7..2874044 100644 --- a/src/chocolatey.package.validator/infrastructure.app/rules/CopyRightAndAuthorFieldsShouldntContainEmailRequirement.cs +++ b/src/chocolatey.package.validator/infrastructure.app/rules/NuspecDoesNotContainEmailRequirement.cs @@ -15,12 +15,13 @@ namespace chocolatey.package.validator.infrastructure.app.rules { - using System.Linq; + using System.Collections.Generic; + using System.Reflection; using System.Text.RegularExpressions; - using NuGet; using infrastructure.rules; + using NuGet; - public class CopyrightAndAuthorFieldsShouldntContainEmailRequirement : BasePackageRule + public class NuspecDoesNotContainEmailRequirement : BasePackageRule { // http://www.regular-expressions.info/email.html private const string EmailRegexPattern = @"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}"; @@ -30,17 +31,35 @@ public override string ValidationFailureMessage get { return -@"Email address should not be used in the Author and Copyright fields of the nuspec file. [More...](https://github.com/chocolatey/package-validator/wiki/CopyrightAndAuthorFieldsShouldntContainEmail)"; +@"Email address should not be used in any field of the nuspec file. [More...](https://github.com/chocolatey/package-validator/wiki/NuspecDoesNotContainEmail)"; } } public override PackageValidationOutput is_valid(IPackage package) { - if (string.IsNullOrWhiteSpace(package.Copyright) && package.Authors == null) return true; + var properties = package.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var regex = new Regex(EmailRegexPattern); - return !regex.IsMatch(package.Copyright.to_lower()) && !regex.IsMatch(package.Authors.Join(" ").to_lower()); + foreach (var property in properties) + { + var propertyValue = property.GetValue(package, null).to_string().to_lower(); + + // NOTE: Not overally happy with this! + // Want to be able to each of the values in the IEnumerable, to then check if there is + // an email contained within one of them. + if (property.PropertyType.FullName.Contains("IEnumerable`1[[System.String")) + { + propertyValue = string.Join(",", ((IEnumerable)property.GetValue(package, null)).or_empty_list_if_null()).to_lower(); + } + + if (regex.IsMatch(propertyValue)) + { + return false; + } + } + + return true; } } } \ No newline at end of file