Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port linter rule secure-params-in-nested-deploy #7741

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using FluentAssertions.Primitives;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bicep.Core.Parsing;
using System.Collections.Generic;
using System;

namespace Bicep.Core.UnitTests.Assertions
{
Expand Down Expand Up @@ -93,5 +95,20 @@ public static AndConstraint<StringAssertions> HaveLengthLessThanOrEqualTo(this S

return new AndConstraint<StringAssertions>(instance);
}

// Adds StringComparison to StringAssertions.NotContainAny
public static AndConstraint<StringAssertions> NotContainAny(this StringAssertions instance, IEnumerable<string> values, StringComparison stringComparison, string because = "", params object[] becauseArgs)
{
IEnumerable<string> enumerable = values.Where((string v) => Contains(instance.Subject, v, stringComparison));
Execute.Assertion.ForCondition(!enumerable.Any()).BecauseOf(because, becauseArgs).FailWith("Did not expect {context:string} {0} to contain any of the strings: {1}{reason}.",
instance.Subject, enumerable);
return new AndConstraint<StringAssertions>(instance);
}

private static bool Contains(string actual, string expected, StringComparison comparison)
{
return (actual ?? string.Empty).Contains(expected ?? string.Empty, comparison);
}

StephenWeatherford marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.UnitTests.Assertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;

namespace Bicep.Core.UnitTests.AssertionsTests
{
[TestClass]
public class StringAssertionsExtensionsTests
{
[DataRow(
"hello there",
new[] { "Hello", "Dolly" },
StringComparison.InvariantCulture,
null)]
[DataRow(
"hello there",
new[] { "Hello", "Dolly" },
StringComparison.InvariantCultureIgnoreCase,
"Did not expect string \"hello there\" to contain any of the strings: {\"Hello\"} because I said so.")]
[DataTestMethod]
public void NotContainAny_WithStringComparison(string text, IEnumerable<string> values, StringComparison stringComparison, string? expectedFailureMessage)
{
string? actualMessage;
try
{
text.Should().NotContainAny(values, stringComparison, "because I said so");
actualMessage = null;
}
catch (Exception ex)
{
actualMessage = ex.Message;
}

actualMessage.Should().Be(expectedFailureMessage);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -118,7 +119,7 @@ public void RuleConfigs_RuleDescriptionsShouldBeConsistent()
string description = new Regex("^(.+) See https://.+").Match(descriptionWithLink)?.Groups[1].Value ?? "<couldn't find rule description>";
description.Should().MatchRegex("^[A-Z]", "all rule descriptions should start with a capital letter");
description.Should().EndWith(".", "all rule descriptions should end with a period");
description.Should().NotContainAny("Don't", "don't", "Do not", "do not"); // Use "Should" type of language generally (less impolite)
description.Should().NotContainAny(new[] { "don't", "do not" }, StringComparison.InvariantCultureIgnoreCase, "Use \"Should\" type of language generally (less impolite)");
description.Should().NotContain("\"", "use single quotes instead of double quotes in rule descriptions");
}
}
Expand Down
Loading