[Fixes issue #1528] default reference assemblies made extensible

- Made ReferenceAssemblies and GetCandidateLibraries virtual
- Added relevant tests
This commit is contained in:
Ajay Bhargav Baaskaran 2014-11-14 15:18:31 -08:00
parent 5e067cdb9e
commit 90098411c6
2 changed files with 127 additions and 14 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.Framework.Runtime; using Microsoft.Framework.Runtime;
@ -11,8 +12,11 @@ namespace Microsoft.AspNet.Mvc
{ {
public class DefaultAssemblyProvider : IAssemblyProvider public class DefaultAssemblyProvider : IAssemblyProvider
{ {
// List of Mvc assemblies that we'll use as roots for controller discovery. /// <summary>
private static readonly HashSet<string> _mvcAssemblyList = new HashSet<string>(StringComparer.Ordinal) /// Gets the set of assembly names that are used as root for discovery of
/// MVC controllers, view components and views.
/// </summary>
protected virtual HashSet<string> ReferenceAssemblies { get; } = new HashSet<string>(StringComparer.Ordinal)
{ {
"Microsoft.AspNet.Mvc", "Microsoft.AspNet.Mvc",
"Microsoft.AspNet.Mvc.Core", "Microsoft.AspNet.Mvc.Core",
@ -29,6 +33,7 @@ namespace Microsoft.AspNet.Mvc
_libraryManager = libraryManager; _libraryManager = libraryManager;
} }
/// <inheritdoc />
public IEnumerable<Assembly> CandidateAssemblies public IEnumerable<Assembly> CandidateAssemblies
{ {
get get
@ -37,14 +42,24 @@ namespace Microsoft.AspNet.Mvc
} }
} }
internal IEnumerable<ILibraryInformation> GetCandidateLibraries() /// <summary>
/// Returns a list of libraries that references the assemblies in <see cref="ReferenceAssemblies"/>.
/// By default it returns all assemblies that reference any of the primary MVC assemblies
/// while ignoring MVC assemblies.
/// </summary>
/// <returns>A set of <see cref="ILibraryInformation"/>.</returns>
protected virtual IEnumerable<ILibraryInformation> GetCandidateLibraries()
{ {
if (ReferenceAssemblies == null)
{
return Enumerable.Empty<ILibraryInformation>();
}
// GetReferencingLibraries returns the transitive closure of referencing assemblies // GetReferencingLibraries returns the transitive closure of referencing assemblies
// for a given assembly. In our case, we'll gather all assemblies that reference // for a given assembly.
// any of the primary Mvc assemblies while ignoring Mvc assemblies. return ReferenceAssemblies.SelectMany(_libraryManager.GetReferencingLibraries)
return _mvcAssemblyList.SelectMany(_libraryManager.GetReferencingLibraries) .Distinct()
.Distinct() .Where(IsCandidateLibrary);
.Where(IsCandidateLibrary);
} }
private static Assembly Load(AssemblyName assemblyName) private static Assembly Load(AssemblyName assemblyName)
@ -52,9 +67,10 @@ namespace Microsoft.AspNet.Mvc
return Assembly.Load(assemblyName); return Assembly.Load(assemblyName);
} }
private static bool IsCandidateLibrary(ILibraryInformation library) private bool IsCandidateLibrary(ILibraryInformation library)
{ {
return !_mvcAssemblyList.Contains(library.Name); Debug.Assert(ReferenceAssemblies != null);
return !ReferenceAssemblies.Contains(library.Name);
} }
} }
} }

View File

@ -1,10 +1,8 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using Microsoft.Framework.Runtime; using Microsoft.Framework.Runtime;
using Moq; using Moq;
using Xunit; using Xunit;
@ -27,7 +25,7 @@ namespace Microsoft.AspNet.Mvc.Core
CreateLibraryInfo("SomeRandomAssembly"), CreateLibraryInfo("SomeRandomAssembly"),
}) })
.Verifiable(); .Verifiable();
var provider = new DefaultAssemblyProvider(manager.Object); var provider = new TestAssemblyProvider(manager.Object);
// Act // Act
var candidates = provider.GetCandidateLibraries(); var candidates = provider.GetCandidateLibraries();
@ -49,7 +47,7 @@ namespace Microsoft.AspNet.Mvc.Core
.Returns(new[] { CreateLibraryInfo("Bar") }); .Returns(new[] { CreateLibraryInfo("Bar") });
manager.Setup(f => f.GetReferencingLibraries("Microsoft.AspNet.Mvc")) manager.Setup(f => f.GetReferencingLibraries("Microsoft.AspNet.Mvc"))
.Returns(new[] { CreateLibraryInfo("Baz") }); .Returns(new[] { CreateLibraryInfo("Baz") });
var provider = new DefaultAssemblyProvider(manager.Object); var provider = new TestAssemblyProvider(manager.Object);
// Act // Act
var candidates = provider.GetCandidateLibraries(); var candidates = provider.GetCandidateLibraries();
@ -58,11 +56,110 @@ namespace Microsoft.AspNet.Mvc.Core
Assert.Equal(new[] { "Baz", "Foo", "Bar" }, candidates.Select(a => a.Name)); Assert.Equal(new[] { "Baz", "Foo", "Bar" }, candidates.Select(a => a.Name));
} }
[Fact]
public void CandidateAssemblies_ReturnsLibrariesReferencingDefaultAssemblies()
{
// Arrange
var defaultProvider = new TestAssemblyProvider(CreateLibraryManager());
// Act
var defaultProviderCandidates = defaultProvider.GetCandidateLibraries();
// Assert
Assert.Equal(new[] { "Baz" }, defaultProviderCandidates.Select(a => a.Name));
}
[Fact]
public void CandidateAssemblies_ReturnsLibrariesReferencingOverriddenAssemblies()
{
// Arrange
var overriddenProvider = new OverriddenAssemblyProvider(CreateLibraryManager());
// Act
var overriddenProviderCandidates = overriddenProvider.GetCandidateLibraries();
// Assert
Assert.Equal(new[] { "Foo", "Bar" }, overriddenProviderCandidates.Select(a => a.Name));
}
[Fact]
public void CandidateAssemblies_ReturnsEmptySequenceWhenReferenceAssembliesIsNull()
{
// Arrange
var nullProvider = new NullAssemblyProvider(CreateLibraryManager());
// Act
var nullProviderCandidates = nullProvider.GetCandidateLibraries();
// Assert
Assert.Empty(nullProviderCandidates.Select(a => a.Name));
}
private static ILibraryInformation CreateLibraryInfo(string name) private static ILibraryInformation CreateLibraryInfo(string name)
{ {
var info = new Mock<ILibraryInformation>(); var info = new Mock<ILibraryInformation>();
info.SetupGet(b => b.Name).Returns(name); info.SetupGet(b => b.Name).Returns(name);
return info.Object; return info.Object;
} }
private static ILibraryManager CreateLibraryManager()
{
var manager = new Mock<ILibraryManager>();
manager.Setup(f => f.GetReferencingLibraries(It.IsAny<string>()))
.Returns(Enumerable.Empty<ILibraryInformation>());
manager.Setup(f => f.GetReferencingLibraries("Microsoft.AspNet.Mvc.Core"))
.Returns(new[] { CreateLibraryInfo("Baz") });
manager.Setup(f => f.GetReferencingLibraries("MyAssembly"))
.Returns(new[] { CreateLibraryInfo("Foo") });
manager.Setup(f => f.GetReferencingLibraries("AnotherAssembly"))
.Returns(new[] { CreateLibraryInfo("Bar") });
return manager.Object;
}
private class TestAssemblyProvider : DefaultAssemblyProvider
{
public new IEnumerable<ILibraryInformation> GetCandidateLibraries()
{
return base.GetCandidateLibraries();
}
public TestAssemblyProvider(ILibraryManager libraryManager) : base(libraryManager)
{
}
}
private class OverriddenAssemblyProvider : TestAssemblyProvider
{
protected override HashSet<string> ReferenceAssemblies
{
get
{
return new HashSet<string>
{
"MyAssembly",
"AnotherAssembly"
};
}
}
public OverriddenAssemblyProvider(ILibraryManager libraryManager) : base(libraryManager)
{
}
}
private class NullAssemblyProvider : TestAssemblyProvider
{
protected override HashSet<string> ReferenceAssemblies
{
get
{
return null;
}
}
public NullAssemblyProvider(ILibraryManager libraryManager) : base(libraryManager)
{
}
}
} }
} }