Skip to content

Commit

Permalink
Fix 8318 by removing reflection code (Azure#8327)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Weatherford <Stephen.Weatherford.com>
  • Loading branch information
StephenWeatherford authored Sep 11, 2022
1 parent 467dcad commit eba45c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/Bicep.Core.UnitTests/Diagnostics/LinterAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Bicep.Core.Analyzers;
using Bicep.Core.Analyzers.Interfaces;
using Bicep.Core.Analyzers.Linter;
using Bicep.Core.Analyzers.Linter.Rules;
using Bicep.Core.Configuration;
Expand All @@ -30,7 +33,7 @@ public void HasBuiltInRules()
linter.GetRuleSet().Should().NotBeEmpty();
}

// No need to add new rules here
// No need to add new rules here, just checking a few known ones
[DataTestMethod]
[DataRow(AdminUsernameShouldNotBeLiteralRule.Code)]
[DataRow(ExplicitValuesForLocationParamsRule.Code)]
Expand All @@ -43,6 +46,25 @@ public void BuiltInRulesExistSanityCheck(string ruleCode)
linter.GetRuleSet().Should().Contain(r => r.Code == ruleCode);
}

[TestMethod]
public void AllDefinedRulesAreListInLinterRulesProvider()
{
var linter = new LinterAnalyzer(configuration);
var ruleTypes = linter.GetRuleSet().Select(r => r.GetType()).ToArray();

var expectedRuleTypes = typeof(LinterAnalyzer).Assembly
.GetTypes()
.Where(t => typeof(IBicepAnalyzerRule).IsAssignableFrom(t)
&& t.IsClass
&& t.IsPublic
&& t.GetConstructor(Type.EmptyTypes) != null);

var actualTypeNames = ruleTypes.Select(t => t.FullName ?? throw new ArgumentNullException("bad type"));
var expectedTypeNames = expectedRuleTypes.Select(t => t.FullName ?? throw new ArgumentNullException("bad type"));

actualTypeNames.Should().BeEquivalentTo(expectedTypeNames, "LinterRuleProvider.GetRuleTypes() needs to be updated to list all defined rules in the core assembly");
}

[TestMethod]
public void AllRulesHaveUniqueDetails()
{
Expand Down
38 changes: 32 additions & 6 deletions src/Bicep.Core/Analyzers/Linter/LinterRulesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Bicep.Core.Analyzers.Interfaces;
using Bicep.Core.Analyzers.Linter.Rules;

namespace Bicep.Core.Analyzers.Linter
{
Expand Down Expand Up @@ -39,12 +41,36 @@ private Dictionary<string, string> GetLinterRulesInternal()

public IEnumerable<Type> GetRuleTypes()
{
return Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => typeof(IBicepAnalyzerRule).IsAssignableFrom(t)
&& t.IsClass
&& t.IsPublic
&& t.GetConstructor(Type.EmptyTypes) != null);
// Can't use reflection to get this list because the output dotnet executable is trimmed,
// and dependencies that can't be determined at compile time get removed.
return new[] {
typeof(AdminUsernameShouldNotBeLiteralRule),
typeof(ArtifactsParametersRule),
typeof(ExplicitValuesForLocationParamsRule),
typeof(MaxNumberOutputsRule),
typeof(MaxNumberParametersRule),
typeof(MaxNumberResourcesRule),
typeof(MaxNumberVariablesRule),
typeof(NoHardcodedEnvironmentUrlsRule),
typeof(NoHardcodedLocationRule),
typeof(NoLocationExprOutsideParamsRule),
typeof(NoUnnecessaryDependsOnRule),
typeof(NoUnusedExistingResourcesRule),
typeof(NoUnusedParametersRule),
typeof(NoUnusedVariablesRule),
typeof(OutputsShouldNotContainSecretsRule),
typeof(PreferInterpolationRule),
typeof(PreferUnquotedPropertyNamesRule),
typeof(SecretsInParamsMustBeSecureRule),
typeof(SecureParameterDefaultRule),
typeof(SecureParamsInNestedDeploymentsRule),
typeof(SimplifyInterpolationRule),
typeof(ProtectCommandToExecuteSecretsRule),
typeof(UseRecentApiVersionRule),
typeof(UseResourceIdFunctionsRule),
typeof(UseStableResourceIdentifiersRule),
typeof(UseStableVMImageRule),
};
}

public ImmutableDictionary<string, string> GetLinterRules() => linterRulesLazy.Value;
Expand Down

0 comments on commit eba45c5

Please sign in to comment.