forked from aspnet-contrib/AspNet.Security.OAuth.Providers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticAnalysisTests.cs
106 lines (84 loc) · 3.48 KB
/
StaticAnalysisTests.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
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/
using System.Reflection;
using System.Runtime.Loader;
namespace AspNet.Security.OAuth;
public static class StaticAnalysisTests
{
#if JETBRAINS_ANNOTATIONS
[Fact]
#endif
public static void Publicly_Visible_Parameters_Have_Null_Annotations()
{
// Arrange
var assemblyNames = GetProviderAssemblyNames();
var types = GetPublicTypes(assemblyNames);
var nullabilityContext = new NullabilityInfoContext();
var testedMethods = 0;
// Act
foreach (var type in types)
{
var methods = GetPublicOrProtectedConstructorsOrMethods(type);
foreach (var method in methods)
{
var parameters = method.GetParameters();
foreach (var parameter in parameters)
{
var hasNullabilityAnnotation = parameter
.GetCustomAttributes()
.Any((p) => p.GetType().FullName == "JetBrains.Annotations.CanBeNullAttribute" ||
p.GetType().FullName == "JetBrains.Annotations.NotNullAttribute");
var nullabilityInfo = nullabilityContext.Create(parameter);
if (nullabilityInfo.WriteState == NullabilityState.Nullable)
{
continue;
}
// Assert
hasNullabilityAnnotation.ShouldBeTrue(
$"The {parameter.Name} parameter of {type.Name}.{method.Name} does not have a [NotNull] or [CanBeNull] annotation.");
testedMethods++;
}
}
}
testedMethods.ShouldBeGreaterThan(0);
}
private static AssemblyName[] GetProviderAssemblyNames()
{
var thisType = typeof(StaticAnalysisTests);
var assemblies = thisType.Assembly
.GetReferencedAssemblies()
.Where((p) => p.FullName.StartsWith(thisType.Namespace + ".", StringComparison.Ordinal))
.ToArray();
assemblies.ShouldNotBeEmpty();
return assemblies;
}
private static Type[] GetPublicTypes(IEnumerable<AssemblyName> assemblyNames)
{
var types = assemblyNames
.Select((p) => AssemblyLoadContext.Default.LoadFromAssemblyName(p))
.SelectMany((p) => p.GetTypes())
.Where((p) => p.IsPublic || p.IsNestedPublic)
.ToArray();
types.ShouldNotBeEmpty();
return types;
}
private static MethodBase[] GetPublicOrProtectedConstructorsOrMethods(Type type)
{
var constructors = type
.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic)
.Select((p) => (MethodBase)p)
.ToArray();
var methods = type
.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
.ToArray();
return methods
.Concat(constructors)
.Where((p) => p.IsPublic || p.IsFamily) // public or protected
.Where((p) => !p.IsAbstract)
.Where((p) => !p.IsSpecialName) // No property get/set or event add/remove methods
.ToArray();
}
}