forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinterAnalyzerTests.cs
130 lines (112 loc) · 5.01 KB
/
LinterAnalyzerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
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;
using Bicep.Core.Diagnostics;
using Bicep.Core.Parsing;
using Bicep.Core.Semantics;
using Bicep.Core.TypeSystem;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bicep.Core.UnitTests.Diagnostics
{
[TestClass]
public class LinterAnalyzerTests
{
private readonly RootConfiguration configuration = BicepTestConstants.BuiltInConfiguration;
[TestMethod]
public void HasBuiltInRules()
{
var linter = new LinterAnalyzer(configuration);
linter.GetRuleSet().Should().NotBeEmpty();
}
// No need to add new rules here, just checking a few known ones
[DataTestMethod]
[DataRow(AdminUsernameShouldNotBeLiteralRule.Code)]
[DataRow(ExplicitValuesForLocationParamsRule.Code)]
[DataRow(NoHardcodedEnvironmentUrlsRule.Code)]
[DataRow(NoHardcodedLocationRule.Code)]
public void BuiltInRulesExistSanityCheck(string ruleCode)
{
var linter = new LinterAnalyzer(configuration);
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, "Please verify that the {nameof(LinterRuleTypeGenerator)} source generator is working correctly");
}
[TestMethod]
public void AllRulesHaveUniqueDetails()
{
var analyzer = new LinterAnalyzer(configuration);
var ruleSet = analyzer.GetRuleSet();
var codeSet = ruleSet.Select(r => r.Code).ToHashSet();
codeSet.Should().HaveSameCount(ruleSet);
var descSet = ruleSet.Select(r => r.Description).ToHashSet();
descSet.Should().HaveSameCount(ruleSet);
}
[TestMethod]
public void MostRulesEnabledByDefault()
{
var analyzer = new LinterAnalyzer(configuration);
var ruleSet = analyzer.GetRuleSet();
var numberEnabled = ruleSet.Where(r => r.IsEnabled()).Count();
numberEnabled.Should().BeGreaterThan(ruleSet.Count() / 2, "most rules should probably be enabled by default");
}
[TestMethod]
public void AllRulesHaveDescription()
{
var analyzer = new LinterAnalyzer(configuration);
var ruleSet = analyzer.GetRuleSet();
ruleSet.Should().OnlyContain(r => r.Description.Length > 0);
}
public class LinterThrowsTestRule : LinterRuleBase
{
public LinterThrowsTestRule() : base("ThrowsRule", "Throws an exception when used", null, DiagnosticLevel.Warning) { }
public override IEnumerable<IDiagnostic> AnalyzeInternal(SemanticModel model)
{
// Have a yield return to force this method to return an iterator like the real rules
yield return new AnalyzerDiagnostic(this.AnalyzerName,
new TextSpan(0, 0),
DiagnosticLevel.Warning,
"fakeRule",
"Fake Rule",
null);
// Now throw an exception
throw new System.ArgumentOutOfRangeException();
}
}
[TestMethod]
public void TestRuleThrowingException()
{
var text = @"
@secure()
param param1 string = 'val'";
var compilationResult = CompilationHelper.Compile(text);
var semanticModel = compilationResult.Compilation.GetSemanticModel(compilationResult.BicepFile);
var throwRule = new LinterThrowsTestRule();
var test = () => throwRule.Analyze(semanticModel).ToArray();
test.Should().Throw<ArgumentOutOfRangeException>();
}
}
}