Skip to content

Commit

Permalink
Fix linter rule: Max variables limit increased from 256 -> 512 (#15464)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford authored Nov 4, 2024
1 parent dc8ff0c commit b31dd99
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 8,602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bicep.Core.Diagnostics;
using FluentAssertions;
using FluentAssertions.Collections;
using System.Linq;

namespace Bicep.Core.UnitTests.Assertions
{
Expand Down Expand Up @@ -55,39 +56,56 @@ public AndConstraint<IDiagnosticCollectionAssertions> NotHaveAnyDiagnostics(stri
return new AndConstraint<IDiagnosticCollectionAssertions>(this);
}

public AndConstraint<IDiagnosticCollectionAssertions> HaveDiagnostics(IEnumerable<(string code, DiagnosticLevel level, string message)> diagnostics, string because = "", params object[] becauseArgs)
public AndConstraint<IDiagnosticCollectionAssertions> HaveDiagnostics(IEnumerable<(string code, DiagnosticLevel level, string message)> expectedDiagnostics, string because = "", params object[] becauseArgs)
{
var actions = new List<Action<IDiagnostic>>();
foreach (var (code, level, message) in diagnostics)
var expectedArray = expectedDiagnostics.ToArray();
if (expectedArray.Any())
{
actions.Add(x =>
AssertionExtensions.Should(Subject).HaveCount(expectedArray.Count(), $"{because} expected {expectedArray.Count()} diagnostics");
if (Subject.Count() == expectedArray.Count())
{
x.Should().HaveCodeAndSeverity(code, level).And.HaveMessage(message);
});
var actions = expectedArray.Select(diagnostic =>
new Action<IDiagnostic>(x =>
{
x.Should().HaveCodeAndSeverity(diagnostic.code, diagnostic.level).And.HaveMessage(diagnostic.message);
})).ToList();

AssertionExtensions.Should(Subject).SatisfyRespectively(actions, because, becauseArgs);
}
}
else
{
Subject.Should().NotHaveAnyDiagnostics(because, becauseArgs);
}


AssertionExtensions.Should(Subject).SatisfyRespectively(actions, because, becauseArgs);

return new AndConstraint<IDiagnosticCollectionAssertions>(this);
}

public AndConstraint<IDiagnosticCollectionAssertions> HaveFixableDiagnostics(IEnumerable<(string code, DiagnosticLevel level, string message, string fixDescription, string fixReplacementText)> diagnostics, string because = "", params object[] becauseArgs)
public AndConstraint<IDiagnosticCollectionAssertions> HaveFixableDiagnostics(IEnumerable<(string code, DiagnosticLevel level, string message, string fixDescription, string fixReplacementText)> expectedDiagnostics, string because = "", params object[] becauseArgs)
{
var actions = new List<Action<IDiagnostic>>();
foreach (var (code, level, message, fixDescription, fixReplacementText) in diagnostics)
var expectedArray = expectedDiagnostics.ToArray();
if (expectedArray.Any())
{
actions.Add(x =>
AssertionExtensions.Should(Subject).HaveCount(expectedArray.Count(), $"{because} expected {expectedArray.Count()} fixable diagnostics");

var actions = new List<Action<IDiagnostic>>();
foreach (var (code, level, message, fixDescription, fixReplacementText) in expectedDiagnostics)
{
x.Should()
.HaveCodeAndSeverity(code, level)
.And.HaveMessage(message)
.And.HaveCodeFix(fixDescription, fixReplacementText);
});
actions.Add(x =>
{
x.Should()
.HaveCodeAndSeverity(code, level)
.And.HaveMessage(message)
.And.HaveCodeFix(fixDescription, fixReplacementText);
});
}

AssertionExtensions.Should(Subject).SatisfyRespectively(actions, because, becauseArgs);
}
else
{
Subject.Should().NotHaveAnyDiagnostics(because, becauseArgs);
}


AssertionExtensions.Should(Subject).SatisfyRespectively(actions, because, becauseArgs);

return new AndConstraint<IDiagnosticCollectionAssertions>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,25 @@
namespace Bicep.Core.UnitTests.Diagnostics.LinterRuleTests
{
[TestClass]
public class MaxNumberAssertsRuleTests : LinterRuleTestsBase
public class MaxNumberAssertsRuleTests : MaxNumberTestsBase
{
[TestMethod]
public void ParameterNameInFormattedMessage()
public void LimitShouldBeInFormattedMessage()
{
var ruleToTest = new MaxNumberAssertsRule();
ruleToTest.GetMessage(1).Should().Be("Too many predeployment conditions. Number of 'assert' statements is limited to 1.");
}

private void CompileAndTest(string text, bool tooManyAsserts)
{
AssertLinterRuleDiagnostics(MaxNumberAssertsRule.Code, text, diags =>
{
if (tooManyAsserts)
{
var rule = new MaxNumberAssertsRule();
diags.Should().ContainSingleDiagnostic("max-asserts", DiagnosticLevel.Error, rule.GetMessage(MaxNumberAssertsRule.MaxNumber));
}
else
{
diags.Should().BeEmpty();
}
},
new Options(OnCompileErrors.Ignore));
}

[DataRow(@"
assert a1 = true
assert a2 = true
assert a3 = true
assert a4 = true
assert a5 = true
assert a6 = true
assert a7 = true
assert a8 = true
assert a9 = true
assert a10 = true
assert a11 = true
assert a12 = true
assert a13 = true
assert a14 = true
assert a15 = true
assert a16 = true
assert a17 = true
assert a18 = true
assert a19 = true
assert a20 = true
assert a21 = true
assert a22 = true
assert a23 = true
assert a24 = true
assert a25 = true
assert a26 = true
assert a27 = true
assert a28 = true
assert a29 = true
assert a30 = true
assert a31 = true
assert a32 = true",
false
)]

[DataRow(@"
assert a1 = true
assert a2 = true
assert a3 = true
assert a4 = true
assert a5 = true
assert a6 = true
assert a7 = true
assert a8 = true
assert a9 = true
assert a10 = true
assert a11 = true
assert a12 = true
assert a13 = true
assert a14 = true
assert a15 = true
assert a16 = true
assert a17 = true
assert a18 = true
assert a19 = true
assert a20 = true
assert a21 = true
assert a22 = true
assert a23 = true
assert a24 = true
assert a25 = true
assert a26 = true
assert a27 = true
assert a28 = true
assert a29 = true
assert a30 = true
assert a31 = true
assert a32 = true
assert a33 = true",
true
)]

[DataRow(
1, 32, "assert a% = true",
new string[] { })]
[DataRow(
1, 33, "assert a% = true",
new string[] { "Too many predeployment conditions. Number of 'assert' statements is limited to 32." })]
[DataTestMethod]
public void TestRule(string text, bool tooManyAsserts)
public void TooManyAsserts(int i, int j, string pattern, string[] expectedMessages)
{
CompileAndTest(text, tooManyAsserts);
CompileAndTest(GenerateText(i, j, pattern), MaxNumberAssertsRule.Code, Core.Diagnostics.DiagnosticLevel.Error, expectedMessages, new Options() { OnCompileErrors = OnCompileErrors.Ignore });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,174 +9,28 @@
namespace Bicep.Core.UnitTests.Diagnostics.LinterRuleTests
{
[TestClass]
public class MaxNumberOutputsRuleTests : LinterRuleTestsBase
public class MaxNumberOutputsRuleTests : MaxNumberTestsBase
{
[TestMethod]
public void MaxNumberOfParamsInFormattedMessage()
public void LimitShouldBeInFormattedMessage()
{
var ruleToTest = new MaxNumberOutputsRule();
ruleToTest.GetMessage(123).Should().Be("Too many outputs. Number of outputs is limited to 123.");
}

private void CompileAndTest(string text, params string[] unusedParams)
{
AssertLinterRuleDiagnostics(
MaxNumberOutputsRule.Code, text, diags =>
{
if (unusedParams.Any())
{
diags.Should().HaveCount(unusedParams.Count());
var rule = new MaxNumberOutputsRule();
string[] expectedMessages = unusedParams.Select(p => rule.GetMessage(MaxNumberOutputsRule.MaxNumber)).ToArray();
diags.Select(e => e.Message).Should().ContainInOrder(expectedMessages);
}
else
{
diags.Should().BeEmpty();
}
},
new Options(OnCompileErrors: OnCompileErrors.Ignore));
}

[DataRow(@"
output o1 string
output o2 string
output o3 string
output o4 string
output o5 string
output o6 string
output o7 string
output o8 string
output o9 string
output o10 string
output o11 string
output o12 string
output o13 string
output o14 string
output o15 string
output o16 string
output o17 string
output o18 string
output o19 string
output o20 string
output o21 string
output o22 string
output o23 string
output o24 string
output o25 string
output o26 string
output o27 string
output o28 string
output o29 string
output o30 string
output o31 string
output o32 string
output o33 string
output o34 string
output o35 string
output o36 string
output o37 string
output o38 string
output o39 string
output o40 string
output o41 string
output o42 string
output o43 string
output o44 string
output o45 string
output o46 string
output o47 string
output o48 string
output o49 string
output o50 string
output o51 string
output o52 string
output o53 string
output o54 string
output o55 string
output o56 string
output o57 string
output o58 string
output o59 string
output o60 string
output o61 string
output o62 string
output o63 string
output o64 string
")]
[DataRow(@"
output o1 string
output o2 string
output o3 string
output o4 string
output o5 string
output o6 string
output o7 string
output o8 string
output o9 string
output o10 string
output o11 string
output o12 string
output o13 string
output o14 string
output o15 string
output o16 string
output o17 string
output o18 string
output o19 string
output o20 string
output o21 string
output o22 string
output o23 string
output o24 string
output o25 string
output o26 string
output o27 string
output o28 string
output o29 string
output o30 string
output o31 string
output o32 string
output o33 string
output o34 string
output o35 string
output o36 string
output o37 string
output o38 string
output o39 string
output o40 string
output o41 string
output o42 string
output o43 string
output o44 string
output o45 string
output o46 string
output o47 string
output o48 string
output o49 string
output o50 string
output o51 string
output o52 string
output o53 string
output o54 string
output o55 string
output o56 string
output o57 string
output o58 string
output o59 string
output o60 string
output o61 string
output o62 string
output o63 string
output o64 string
output o65 string
",
"o1")]
[DataRow(
1, 64, "output o% string = 'o%'",
new string[] { })]
[DataRow(
2, 65, "output o% string = 'o%'",
new string[] {})]
[DataRow(
1, 65, "output o% string = 'o%'",
new string[] { "Too many outputs. Number of outputs is limited to 64." })]
[DataTestMethod]
public void TestRule(string text, params string[] unusedParams)
public void TooManyOutputs(int i, int j, string pattern, string[] expectedMessages)
{
CompileAndTest(text, unusedParams);
CompileAndTest(GenerateText(i, j, pattern), MaxNumberOutputsRule.Code, Core.Diagnostics.DiagnosticLevel.Error, expectedMessages);
}
}
}
Loading

0 comments on commit b31dd99

Please sign in to comment.