Skip to content

Commit

Permalink
Added conventon based assembly loading
Browse files Browse the repository at this point in the history
The ConfigurableBootstrapper will now attempt to locate the
name of the test assembly. Once it has a hold of the name it
will see if it can strip "test", "tests", "unittests", "specs"
or "specifications" from the name.

It will assume that the resulting name is the name of the
assembly that is under test and will identify all the referenced
assemblies and load them into the application domain.

The list of suffixes can be modified by changing the static
field TestAssemblySuffixes on ConfigurableBoostrapper.

Fixes issue NancyFx#421
  • Loading branch information
thecodejunkie committed Aug 16, 2012
1 parent 556230a commit 6657d9f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Nancy.Testing.Experiments.Model/Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
7 changes: 4 additions & 3 deletions src/Nancy.Testing.Experiments.Tests/HomeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class HomeFixture
[Fact]
public void Should_be_able_to_test_route_that_renders_view()
{
AppDomainAssemblyTypeScanner.LoadAssemblies("Models.dll");
//AppDomainAssemblyTypeScanner.LoadAssemblies("Models.dll");

var x =
typeof(HomeFixture).Assembly.GetReferencedAssemblies();
//var x =
// typeof(HomeFixture).Assembly.GetReferencedAssemblies();

//AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
//{
Expand All @@ -36,6 +36,7 @@ public void Should_be_able_to_test_route_that_renders_view()

var browser = new Browser(new ConfigurableBootstrapper(with => {
with.EnableAutoRegistration();
//with.Assembly("Models.dll");
}));

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="HomeFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nancy.Testing.Experiments.Model\Models.csproj">
<Project>{A72EABE4-EA73-4EB0-A79F-92711D6875A9}</Project>
<Name>Models</Name>
</ProjectReference>
<ProjectReference Include="..\Nancy.Testing.Experiments\Nancy.Testing.Experiments.csproj">
<Project>{EF098CBF-1DCE-4528-8CE7-16A56C8558A3}</Project>
<Name>Nancy.Testing.Experiments</Name>
Expand All @@ -77,6 +72,9 @@
<None Include="packages.config" />
<None Include="web.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Home.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
Expand Down Expand Up @@ -93,6 +92,9 @@
<Name>Nancy</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<ProjectExtensions>
Expand Down
53 changes: 51 additions & 2 deletions src/Nancy.Testing/ConfigurableBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Nancy.Testing
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -29,6 +30,13 @@ public class ConfigurableBootstrapper : NancyBootstrapperWithRequestContainerBas
private bool enableAutoRegistration;
private DiagnosticsConfiguration diagnosticConfiguration;

/// <summary>
/// Test project name suffixes that will be stripped from the test name project
/// in order to try and resolve the name of the assembly that is under test so
/// that all of its references can be loaded into the application domain.
/// </summary>
public static IList<string> TestAssemblySuffixes = new[] { "test", "tests", "unittests", "specs", "specifications" };

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableBootstrapper"/> class.
/// </summary>
Expand All @@ -50,11 +58,18 @@ public ConfigurableBootstrapper(Action<ConfigurableBoostrapperConfigurator> conf

if (configuration != null)
{
var testAssembly =
Assembly.GetCallingAssembly();

var testAssemblyName =
testAssembly.GetName().Name;

LoadReferencesForAssemblyUnderTest(testAssemblyName);

var configurator =
new ConfigurableBoostrapperConfigurator(this);

configurator.ErrorHandler<PassThroughErrorHandler>();

configuration.Invoke(configurator);
}
}
Expand Down Expand Up @@ -98,6 +113,34 @@ private IEnumerable<CollectionTypeRegistration> GetCollectionTypeRegistrations()
return this.registeredTypes.Where(x => x.GetType().Equals(typeof(CollectionTypeRegistration))).Cast<CollectionTypeRegistration>();
}

private static void LoadReferencesForAssemblyUnderTest(string testAssemblyName)
{
if (!TestAssemblySuffixes.Any(x => GetSafePathExtension(testAssemblyName).Equals("." + x, StringComparison.OrdinalIgnoreCase)))
{
return;
}

var testAssemblyNameWithoutExtension =
Path.GetFileNameWithoutExtension(testAssemblyName);

var assemblyUnderTest = AppDomain.CurrentDomain
.GetAssemblies()
.SingleOrDefault(x => x.GetName().Name.Equals(testAssemblyNameWithoutExtension, StringComparison.OrdinalIgnoreCase));

if (assemblyUnderTest != null)
{
foreach (var referencedAssembly in assemblyUnderTest.GetReferencedAssemblies())
{
AppDomainAssemblyTypeScanner.LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory, string.Concat(referencedAssembly.Name, ".dll"));
}
}
}

private static string GetSafePathExtension(string name)
{
return Path.GetExtension(name) ?? String.Empty;
}

private IEnumerable<Type> Resolve<T>()
{
var types = this.GetTypeRegistrations()
Expand Down Expand Up @@ -451,6 +494,7 @@ public ErrorPipeline OnError
public class ConfigurableBoostrapperConfigurator
{
private readonly ConfigurableBootstrapper bootstrapper;
private readonly IEnumerable<string> namingConventions = new[] {"test", "tests"};

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurableBoostrapperConfigurator"/> class.
Expand All @@ -459,7 +503,6 @@ public class ConfigurableBoostrapperConfigurator
public ConfigurableBoostrapperConfigurator(ConfigurableBootstrapper bootstrapper)
{
this.bootstrapper = bootstrapper;

this.Diagnostics<DisabledDiagnostics>();
}

Expand All @@ -471,6 +514,12 @@ public ConfigurableBoostrapperConfigurator Binder(IBinder binder)
return this;
}

public ConfigurableBoostrapperConfigurator Assembly(string pattern)
{
AppDomainAssemblyTypeScanner.LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory, pattern);
return this;
}

/// <summary>
/// Configures the bootstrapper to create an <see cref="IBinder"/> instance of the specified type.
/// </summary>
Expand Down

0 comments on commit 6657d9f

Please sign in to comment.