Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Sep 15, 2023
1 parent 5e9cae4 commit 6192a96
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 5 deletions.
32 changes: 28 additions & 4 deletions Src/FluentAssertions/Common/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Reflection;
using FluentAssertions.Execution;
using FluentAssertions.Extensibility;
using JetBrains.Annotations;

namespace FluentAssertions.Common;
Expand Down Expand Up @@ -50,18 +51,41 @@ internal static void EnsureInitialized()
{
if (!isInitialized)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
InitializeExtensionAssemblies();

Reflector = new FullFrameworkReflector();
#if NETFRAMEWORK || NETCOREAPP
#if NETFRAMEWORK || NETCOREAPP
ConfigurationStore = new ConfigurationStoreExceptionInterceptor(new AppSettingsConfigurationStore());
#else
#else
ConfigurationStore = new NullConfigurationStore();
#endif
#endif
ThrowException = new TestFrameworkProvider(Configuration).Throw;

isInitialized = true;
}
}
}

private static void InitializeExtensionAssemblies()
{
var currentAssembly = Assembly.GetExecutingAssembly().GetName();

var testAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.GetReferencedAssemblies().Any(r => r.FullName == currentAssembly.FullName) && !a.IsDynamic && !IsFramework(a.GetName()));

foreach (Assembly testAssembly in testAssemblies)
{
foreach (var attribute in testAssembly.GetCustomAttributes<ExtensionAssemblyAttribute>())
{
attribute.Initialize();
}
}
}

private static bool IsFramework(AssemblyName assembly)
{
#pragma warning disable CA1310
return assembly.FullName.StartsWith("Microsoft") || assembly.FullName.StartsWith("System");
#pragma warning restore CA1310
}
}
28 changes: 28 additions & 0 deletions Src/FluentAssertions/Extensibility/ExtensionAssemblyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Reflection;

namespace FluentAssertions.Extensibility;

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ExtensionAssemblyAttribute : Attribute
{
private readonly string methodName;
private readonly Type type;

public ExtensionAssemblyAttribute()
{
}

#pragma warning disable CA1019
public ExtensionAssemblyAttribute(Type type, string methodName)
#pragma warning restore CA1019
{
this.type = type;
this.methodName = methodName;
}

public void Initialize()
{
type?.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static)?.Invoke(obj: null, parameters: null);
}
}
3 changes: 3 additions & 0 deletions Src/FluentAssertions/FluentAssertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<Version>7.0.0</Version>
</PropertyGroup>

<PropertyGroup Label="Package info">
Expand Down Expand Up @@ -53,8 +54,10 @@
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="PolySharp" Version="1.13.2" PrivateAssets="all" />
<PackageReference Include="xunit.extensibility.core" Version="2.5.0" />
</ItemGroup>


<!-- Target framework dependent configuration -->
<Choose>
<When Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ItemGroup>
<PackageReference Include="Chill" Version="4.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="Xunit.StaFact" Version="1.1.11" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace FluentAssertions.Specs.Extensibility;

public class ExtensionAssemblyAttributeSpecs
{
[Fact]
public void Extension_assembly_initialization_code_will_be_called_only_once()
{
for (int i = 0; i < 10; i++)
{
FileContainingAssemblyLevelAttributes.ShouldBeCalledOnlyOnce.Should().Be(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FluentAssertions.Extensibility;
using FluentAssertions.Specs.Extensibility;

// With specific initialization code to invoke before the first assertion happens
[assembly: ExtensionAssembly(
typeof(FileContainingAssemblyLevelAttributes),
nameof(FileContainingAssemblyLevelAttributes.InitializeBeforeFirstAssertion))]

// Multiple attributes are allowed and no initialization code is required
[assembly: ExtensionAssembly]

namespace FluentAssertions.Specs.Extensibility;

public static class FileContainingAssemblyLevelAttributes
{
public static int ShouldBeCalledOnlyOnce { get; private set; }

public static void InitializeBeforeFirstAssertion()
{
++ShouldBeCalledOnlyOnce;
}
}
3 changes: 2 additions & 1 deletion Tests/FluentAssertions.Specs/FluentAssertions.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@
<ProjectReference Include="..\AssemblyB\AssemblyB.csproj" />
</ItemGroup>

</Project>

</Project>
1 change: 1 addition & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="Local" value="C:\Workspaces\fluentassertions.datasets\Artifacts\" />
</packageSources>
</configuration>

0 comments on commit 6192a96

Please sign in to comment.