diff --git a/source/TestUtils/PeanutButter.RandomGenerators/BuilderFinderExtensions.cs b/source/TestUtils/PeanutButter.RandomGenerators/BuilderFinderExtensions.cs index c14cc66b7f..1bfcf73e8f 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/BuilderFinderExtensions.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/BuilderFinderExtensions.cs @@ -28,7 +28,10 @@ private static Type TryFindBuilderTypeInClassHeirachyFor(Type potentialBuilder, { var current = TryFindGenericBuilderInClassHeirachy(potentialBuilder); if (current == null) + { return null; + } + var typeParameters = current.GetGenericArguments(); return typeParameters.Length > 1 && typeParameters[1] == buildType ? current @@ -43,14 +46,19 @@ internal static Type TryFindGenericBuilderInClassHeirachy(this Type current) { var genericBase = current.GetGenericTypeDefinition(); if (genericBase == GenericBuilderBaseType) + { break; + } } current = current.BaseType(); } if (current == ObjectType || current == null) + { return null; + } + return current; } } \ No newline at end of file diff --git a/source/TestUtils/PeanutButter.RandomGenerators/EnumerableExtensions.cs b/source/TestUtils/PeanutButter.RandomGenerators/EnumerableExtensions.cs index ffb42d460e..7471f8b25c 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/EnumerableExtensions.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/EnumerableExtensions.cs @@ -21,7 +21,10 @@ public static class EnumerableExtensions public static IEnumerable Randomize(this IEnumerable input) { if (input == null) + { return null; + } + var src = new List(input); var result = new List(); while (src.Count > 0) diff --git a/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderBase.cs b/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderBase.cs index b465db0695..cfaba8908d 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderBase.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderBase.cs @@ -105,11 +105,17 @@ private static ModuleBuilder CreateOrReuseDynamicModule() internal static Type ReuseOrGenerateDynamicBuilderFor(Type type) { if (type == null) + { return null; + } + lock (DynamicBuilders) { if (DynamicBuilders.TryGetValue(type, out var dynamicBuilderType)) + { return dynamicBuilderType; + } + var t = typeof(GenericBuilder<,>); var modBuilder = CreateOrReuseDynamicModule(); @@ -336,14 +342,18 @@ bool retrying private static void AttemptToLoadAssemblyAlongside(string fileName) { - var codeBase = new Uri(typeof(T).Assembly.CodeBase).LocalPath; + var codeBase = new Uri(typeof(T).Assembly.Location).LocalPath; if (!File.Exists(codeBase)) + { return; + } var folder = Path.GetDirectoryName(codeBase); var search = Path.Combine(folder ?? "", fileName); if (!File.Exists(search)) + { return; + } try { diff --git a/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderLocator.cs b/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderLocator.cs index 5da1a4935f..bdaba82aa5 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderLocator.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/GenericBuilderLocator.cs @@ -59,11 +59,16 @@ public static Type GetBuilderFor(Type type) public static Type TryFindExistingBuilderFor(Type type) { if (type == null) + { return null; + } + lock (BuilderTypeCache) { if (BuilderTypeCache.TryGetValue(type, out var result)) + { return result; + } } return TryFindExistingBuilderAndCacheFor(type); @@ -94,7 +99,10 @@ private static Type TryFindExistingBuilderAndCacheFor(Type type) private static void CacheBuilderType(Type type, Type builderType) { if (type == null) + { return; + } + lock (BuilderTypeCache) { BuilderTypeCache[type] = builderType; @@ -107,7 +115,10 @@ private static void TryCacheBuilderType(Type builderType) { var genericBuilder = builderType.TryFindGenericBuilderInClassHeirachy(); if (genericBuilder.GenericTypeArguments.Length != 2) + { return; + } + var builtType = genericBuilder.GenericTypeArguments[1]; // Naive, but let's run with it CacheBuilderType(builderType, builtType); } @@ -160,7 +171,10 @@ internal static Type TryFindBuilderInAnyOtherAssemblyInAppDomainFor(Type propert var types = allBuilders.Where(t => t.IsBuilderFor(propertyType)) .ToArray(); if (!types.Any()) + { return null; + } + return types.Length == 1 ? types.First() : FindClosestNamespaceMatchFor(propertyType, types); @@ -183,7 +197,10 @@ private static void LoadImmediateAssembliesIfRequired() lock (ReferenceLoadLock) { if (_haveLoadedImmediateAssemblies) + { return; + } + _haveLoadedImmediateAssemblies = true; AppDomain.CurrentDomain.GetAssemblies().ForEach(LoadReferencedAssemblies); } @@ -247,14 +264,20 @@ private static Type TryFindBuilderInCurrentAssemblyFor(Type propType) private static Type FindClosestNamespaceMatchFor(Type propertyType, IEnumerable types) { if (propertyType?.Namespace == null) // R# is convinced this might happen :/ + { return null; + } + var seekNamespace = propertyType.Namespace.Split('.'); return types.Aggregate( (Type)null, (acc, cur) => { if (acc?.Namespace == null || cur.Namespace == null) + { return cur; + } + var accParts = acc.Namespace.Split('.'); var curParts = cur.Namespace.Split('.'); var accMatchIndex = seekNamespace.MatchIndexFor(accParts); diff --git a/source/TestUtils/PeanutButter.RandomGenerators/PeanutButter.RandomGenerators.csproj b/source/TestUtils/PeanutButter.RandomGenerators/PeanutButter.RandomGenerators.csproj index ce0185159c..513defb96f 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/PeanutButter.RandomGenerators.csproj +++ b/source/TestUtils/PeanutButter.RandomGenerators/PeanutButter.RandomGenerators.csproj @@ -1,13 +1,13 @@  - net462;netstandard2.0 + net462;netstandard2.0;net8.0;net7.0;net6.0 Debug;Release;Debug-X true AnyCPU latest - + NETSTANDARD diff --git a/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs b/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs index db2cef0d18..68041f2509 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/RequireUnique.cs @@ -52,7 +52,10 @@ public override void Init(Type entityType) var generatorKey = new Tuple(entityType, PropertyNames.Single()); if (!Generators.ContainsKey(generatorKey)) + { Generators[generatorKey] = UniqueRandomValueGenerator.For(PropertyType); + } + _generator = Generators[generatorKey]; } diff --git a/source/TestUtils/PeanutButter.RandomGenerators/StringArrayExtensionsForMatchingNamespaces.cs b/source/TestUtils/PeanutButter.RandomGenerators/StringArrayExtensionsForMatchingNamespaces.cs index d102633eb5..bbf4faa95b 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/StringArrayExtensionsForMatchingNamespaces.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/StringArrayExtensionsForMatchingNamespaces.cs @@ -25,7 +25,10 @@ public static int MatchIndexFor(this string[] src, string[] other) private static int StringCompare(string first, string second) { if (first == second) + { return 0; + } + var ordered = new[] { first, diff --git a/source/TestUtils/PeanutButter.RandomGenerators/UniqueRandomValueGenerator.cs b/source/TestUtils/PeanutButter.RandomGenerators/UniqueRandomValueGenerator.cs index fdf80419e2..633c1e6ddc 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/UniqueRandomValueGenerator.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/UniqueRandomValueGenerator.cs @@ -94,7 +94,10 @@ private int GenerateInt() { var max = _used.Count * 2; if (max < 10) + { max = 10; + } + return GetRandomInt(1, max); } } \ No newline at end of file