Example #1
0
        public void PopulateFeature_PopulatesRazorCompiledItemsFromTypeAssembly()
        {
            // Arrange
            var item1 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item1" && i.Type == typeof(TestView));
            var item2 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item2" && i.Type == typeof(TestPage));

            var assembly = new TestAssembly(new[]
            {
                new RazorCompiledItemAttribute(typeof(TestView), "mvc.1.0.razor-page", "Item1"),
                new RazorCompiledItemAttribute(typeof(TestView), "mvc.1.0.razor-view", "Item1"),
            });

            var part1 = new AssemblyPart(assembly);
            var part2 = new Mock <ApplicationPart>();

            part2
            .As <IRazorCompiledItemProvider>()
            .Setup(p => p.CompiledItems)
            .Returns(new[] { item1, item2, });
            var featureProvider = new RazorCompiledItemFeatureProvider();
            var feature         = new ViewsFeature();

            // Act
            featureProvider.PopulateFeature(new[] { part1, part2.Object }, feature);

            // Assert
            Assert.Equal(new[] { item1, item2 }, feature.ViewDescriptors.Select(d => d.Item));
        }
Example #2
0
        public void AssemblyPart_Assembly_ReturnsAssembly()
        {
            // Arrange
            var assembly = typeof(AssemblyPartTest).GetTypeInfo().Assembly;
            var part = new AssemblyPart(assembly);

            // Act & Assert
            Assert.Equal(part.Assembly, assembly);
        }
Example #3
0
        public void AssemblyPart_Assembly_ReturnsAssembly()
        {
            // Arrange
            var assembly = typeof(AssemblyPartTest).Assembly;
            var part     = new AssemblyPart(assembly);

            // Act & Assert
            Assert.Equal(part.Assembly, assembly);
        }
Example #4
0
        public void AssemblyPart_Name_ReturnsAssemblyName()
        {
            // Arrange
            var part = new AssemblyPart(typeof(AssemblyPartTest).GetTypeInfo().Assembly);

            // Act
            var name = part.Name;

            // Assert
            Assert.Equal("Microsoft.AspNetCore.Mvc.Core.Test", name);
        }
Example #5
0
        public void AssemblyPart_Name_ReturnsAssemblyName()
        {
            // Arrange
            var part = new AssemblyPart(typeof(AssemblyPartTest).Assembly);

            // Act
            var name = part.Name;

            // Assert
            Assert.Equal("Microsoft.AspNetCore.Mvc.Core.Test", name);
        }
Example #6
0
        public void AssemblyPart_Types_ReturnsDefinedTypes()
        {
            // Arrange
            var assembly = typeof(AssemblyPartTest).GetTypeInfo().Assembly;
            var part = new AssemblyPart(assembly);

            // Act
            var types = part.Types;

            // Assert
            Assert.Equal(assembly.DefinedTypes, types);
            Assert.NotSame(assembly.DefinedTypes, types);
        }
Example #7
0
        public void AssemblyPart_Types_ReturnsDefinedTypes()
        {
            // Arrange
            var assembly = typeof(AssemblyPartTest).Assembly;
            var part     = new AssemblyPart(assembly);

            // Act
            var types = part.Types;

            // Assert
            Assert.Equal(assembly.DefinedTypes, types);
            Assert.NotSame(assembly.DefinedTypes, types);
        }
Example #8
0
        public void GetReferencePaths_ReturnsEmptySequenceForDynamicAssembly()
        {
            // Arrange
            var name     = new AssemblyName($"DynamicAssembly-{Guid.NewGuid()}");
            var assembly = AssemblyBuilder.DefineDynamicAssembly(name,
                                                                 AssemblyBuilderAccess.RunAndCollect);

            var part = new AssemblyPart(assembly);

            // Act
            var references = part.GetReferencePaths().ToList();

            // Assert
            Assert.Empty(references);
        }
Example #9
0
        public void GetReferencePaths_ReturnsAssemblyLocation_IfPreserveCompilationContextIsNotSet()
        {
            // Arrange
            // src projects do not have preserveCompilationContext specified.
            var assembly = typeof(AssemblyPart).Assembly;
            var part     = new AssemblyPart(assembly);

            // Act
            var references = part.GetReferencePaths().ToList();

            // Assert
            var actual = Assert.Single(references);

            Assert.Equal(assembly.Location, actual);
        }
Example #10
0
        public void GetReferencePaths_ReturnsReferencesFromDependencyContext_IfPreserveCompilationContextIsSet()
        {
            // Arrange
            var assembly = GetType().Assembly;
            var part     = new AssemblyPart(assembly);

            // Act
            var references = part.GetReferencePaths().ToList();

            // Assert
            Assert.Contains(assembly.Location, references);
            Assert.Contains(
                typeof(AssemblyPart).Assembly.GetName().Name,
                references.Select(Path.GetFileNameWithoutExtension));
        }
Example #11
0
        public void PopulateFeature_AddsItemsFromProviderTypes()
        {
            // Arrange
            var item1 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item1" && i.Type == typeof(TestView));
            var item2 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item2" && i.Type == typeof(TestPage));
            var part1 = new AssemblyPart(typeof(RazorCompiledItemFeatureProviderTest).Assembly);
            var part2 = new Mock <ApplicationPart>();

            part2
            .As <IRazorCompiledItemProvider>()
            .Setup(p => p.CompiledItems).Returns(new[] { item1, item2, });
            var featureProvider = new RazorCompiledItemFeatureProvider();
            var feature         = new ViewsFeature();

            // Act
            featureProvider.PopulateFeature(new[] { part1, part2.Object }, feature);

            // Assert
            Assert.Equal(new[] { item1, item2 }, feature.ViewDescriptors.Select(d => d.Item));
        }
Example #12
0
        public void PopulateFeature_ThrowsIfTwoItemsFromSamePart_OnlyDifferInCase()
        {
            // Arrange
            var item1    = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item");
            var item2    = Mock.Of <RazorCompiledItem>(i => i.Identifier == "item");
            var expected = string.Join(
                Environment.NewLine,
                "The following precompiled view paths differ only in case, which is not supported:",
                "Item",
                "item");
            var part1 = new AssemblyPart(typeof(RazorCompiledItemFeatureProviderTest).Assembly);
            var part2 = new Mock <ApplicationPart>();

            part2
            .As <IRazorCompiledItemProvider>()
            .Setup(p => p.CompiledItems).Returns(new[] { item1, item2, });
            var featureProvider = new RazorCompiledItemFeatureProvider();
            var feature         = new ViewsFeature();

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => featureProvider.PopulateFeature(new[] { part1, part2.Object }, feature));

            Assert.Equal(expected, ex.Message);
        }
Example #13
0
        /// <inheritdoc />
        public static IEnumerable <string> GetReferencePaths(this AssemblyPart assemblyPart)
        {
            var assembly = assemblyPart?.Assembly ?? throw new ArgumentNullException(nameof(assemblyPart));

            if (assembly.IsDynamic)
            {
                // Skip loading process for dynamic assemblies. This prevents DependencyContextLoader from reading the
                // .deps.json file from either manifest resources or the assembly location, which will fail.
                return(Enumerable.Empty <string>());
            }

            var dependencyContext = DependencyContext.Load(assembly);

            if (dependencyContext != null)
            {
                return(dependencyContext.CompileLibraries.SelectMany(library => library.ResolveReferencePaths()));
            }

            // If an application has been compiled without preserveCompilationContext, return the path to the assembly
            // as a reference. For runtime compilation, this will allow the compilation to succeed as long as it least
            // one application part has been compiled with preserveCompilationContext and contains a super set of types
            // required for the compilation to succeed.
            return(new[] { assembly.Location });
        }