Ejemplo n.º 1
0
        public void Compile_ReturnsGeneratedCodePath_IfLinePragmaIsNotAvailable()
        {
            // Arrange
            var fileContent = "file content";
            var content     = @"this should fail";

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                Content = fileContent
            },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType <CompilationResult>(result);
            Assert.Null(result.CompiledType);

            var compilationFailure = Assert.Single(result.CompilationFailures);

            Assert.Equal("Generated Code", compilationFailure.SourceFilePath);
            Assert.Equal(content, compilationFailure.SourceFileContent);
        }
Ejemplo n.º 2
0
        public void Compile_DoesNotThrow_IfFileCannotBeRead()
        {
            // Arrange
            var path    = "some-relative-path";
            var content = $@"
#line 1 ""{path}""
this should fail";

            var mockFileInfo = new Mock <IFileInfo>();

            mockFileInfo.Setup(f => f.CreateReadStream())
            .Throws(new Exception());
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(path, mockFileInfo.Object);

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(mockFileInfo.Object, path);

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType <CompilationResult>(result);
            Assert.Null(result.CompiledType);
            var compilationFailure = Assert.Single(result.CompilationFailures);

            Assert.Equal(path, compilationFailure.SourceFilePath);
            Assert.Null(compilationFailure.SourceFileContent);
        }
        public void Compile_ReturnsCompilationFailureWithPathsFromLinePragmas()
        {
            // Arrange
            var viewPath = "some-relative-path";
            var fileContent = "test file content";
            var content = $@"
#line 1 ""{viewPath}""
this should fail";
            var fileProvider = new TestFileProvider();
            var fileInfo = fileProvider.AddFile(viewPath, fileContent);

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(fileProvider),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(fileInfo, "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType<CompilationResult>(result);
            Assert.Null(result.CompiledType);
            var compilationFailure = Assert.Single(result.CompilationFailures);
            Assert.Equal(relativeFileInfo.RelativePath, compilationFailure.SourceFilePath);
            Assert.Equal(fileContent, compilationFailure.SourceFileContent);
        }
Ejemplo n.º 4
0
        public void Compile_UsesApplicationsCompilationSettings_ForParsingAndCompilation()
        {
            // Arrange
            var content = @"
#if MY_CUSTOM_DEFINE
public class MyCustomDefinedClass {}
#else
public class MyNonCustomDefinedClass {}
#endif
";

            var options = GetOptions();

            options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                options,
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.NotNull(result.CompiledType);
            Assert.Equal("MyCustomDefinedClass", result.CompiledType.Name);
        }
Ejemplo n.º 5
0
        public void Compile_SucceedsIfReferencesAreAddedInCallback()
        {
            // Arrange
            var options = GetOptions(context =>
            {
                var assemblyLocation = typeof(object).GetTypeInfo().Assembly.Location;

                context.Compilation = context
                                      .Compilation
                                      .AddReferences(MetadataReference.CreateFromFile(assemblyLocation));
            });
            var content            = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: null,
                viewEngineOptions: options,
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path.cshtml");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Null(result.CompilationFailures);
            Assert.NotNull(result.CompiledType);
        }
Ejemplo n.º 6
0
        public void Compile_ReturnsCompilationFailureWithPathsFromLinePragmas()
        {
            // Arrange
            var viewPath     = "some-relative-path";
            var fileContent  = "test file content";
            var content      = $@"
#line 1 ""{viewPath}""
this should fail";
            var fileProvider = new TestFileProvider();
            var fileInfo     = fileProvider.AddFile(viewPath, fileContent);

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(fileProvider),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(fileInfo, "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType <CompilationResult>(result);
            Assert.Null(result.CompiledType);
            var compilationFailure = Assert.Single(result.CompilationFailures);

            Assert.Equal(relativeFileInfo.RelativePath, compilationFailure.SourceFilePath);
            Assert.Equal(fileContent, compilationFailure.SourceFileContent);
        }
Ejemplo n.º 7
0
        public void Compile_DoesNotThrowIfReferencesWereClearedInCallback()
        {
            // Arrange
            var options = GetOptions(context =>
            {
                context.Compilation = context.Compilation.RemoveAllReferences();
            });
            var content            = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: GetDependencyContext(),
                viewEngineOptions: options,
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path.cshtml");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Single(result.CompilationFailures);
        }
Ejemplo n.º 8
0
        public void Compile_ThrowsIfDependencyContextReturnsNoReferencesAndTheApplicationFailsToCompile()
        {
            // Arrange
            var content           = "public class MyTestType  {}";
            var dependencyContext = new DependencyContext(
                new TargetInfo("framework", "runtime", "signature", isPortable: true),
                Extensions.DependencyModel.CompilationOptions.Default,
                new CompilationLibrary[0],
                new RuntimeLibrary[0],
                Enumerable.Empty <RuntimeFallbacks>());
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: dependencyContext,
                viewEngineOptions: GetOptions(),
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path.cshtml");

            var expected = "The Razor page 'some-relative-path.cshtml' failed to compile. Ensure that your "
                           + "application's project.json sets the 'preserveCompilationContext' compilation property.";

            // Act and Assert
            var ex = Assert.Throws <InvalidOperationException>(() =>
                                                               compilationService.Compile(relativeFileInfo, content));

            Assert.Equal(expected, ex.Message);
        }
Ejemplo n.º 9
0
        public void Compile_ThrowsIfDependencyContextIsNullAndTheApplicationFailsToCompileWithNoReferences()
        {
            // Arrange
            var content            = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: null,
                viewEngineOptions: GetOptions(),
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path.cshtml");

            var expected = "The Razor page 'some-relative-path.cshtml' failed to compile. Ensure that your "
                           + "application's project.json sets the 'preserveCompilationContext' compilation property.";

            // Act and Assert
            var ex = Assert.Throws <InvalidOperationException>(() =>
                                                               compilationService.Compile(relativeFileInfo, content));

            Assert.Equal(expected, ex.Message);
        }
        public void Compile_ReturnsCompilationResult()
        {
            // Arrange
            var content = @"
public class MyTestType  {}";

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Equal("MyTestType", result.CompiledType.Name);
        }
Ejemplo n.º 11
0
        public void Compile_ReturnsCompilationResult()
        {
            // Arrange
            var content = @"
public class MyTestType  {}";

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Equal("MyTestType", result.CompiledType.Name);
        }
Ejemplo n.º 12
0
        public void Compile_RunsCallback()
        {
            // Arrange
            var content = "public class MyTestType  {}";
            RoslynCompilationContext usedCompilation = null;

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(callback: c => usedCompilation = c),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo {
                PhysicalPath = "SomePath"
            },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            Assert.NotNull(usedCompilation);
            Assert.Single(usedCompilation.Compilation.SyntaxTrees);
        }
        public void Compile_SucceedsIfReferencesAreAddedInCallback()
        {
            // Arrange
            var options = GetOptions(context =>
            {
                var assemblyLocation = typeof(object).GetTypeInfo().Assembly.Location;

                context.Compilation = context
                    .Compilation
                    .AddReferences(MetadataReference.CreateFromFile(assemblyLocation));
            });
            var content = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: null,
                viewEngineOptions: options,
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path.cshtml");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Null(result.CompilationFailures);
            Assert.NotNull(result.CompiledType);
        }
        public void Compile_DoesNotThrowIfReferencesWereClearedInCallback()
        {
            // Arrange
            var options = GetOptions(context =>
            {
                context.Compilation = context.Compilation.RemoveAllReferences();
            });
            var content = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: GetDependencyContext(),
                viewEngineOptions: options,
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path.cshtml");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.Single(result.CompilationFailures);
        }
        public void Compile_ThrowsIfDependencyContextReturnsNoReferencesAndTheApplicationFailsToCompile()
        {
            // Arrange
            var content = "public class MyTestType  {}";
            var dependencyContext = new DependencyContext(
                new TargetInfo("framework", "runtime", "signature", isPortable: true),
                Extensions.DependencyModel.CompilationOptions.Default,
                new CompilationLibrary[0],
                new RuntimeLibrary[0],
                Enumerable.Empty<RuntimeFallbacks>());
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: dependencyContext,
                viewEngineOptions: GetOptions(),
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path.cshtml");

            var expected = "The Razor page 'some-relative-path.cshtml' failed to compile. Ensure that your "
                 + "application's project.json sets the 'preserveCompilationContext' compilation property.";

            // Act and Assert
            var ex = Assert.Throws<InvalidOperationException>(() =>
                compilationService.Compile(relativeFileInfo, content));
            Assert.Equal(expected, ex.Message);
        }
        public void Compile_ThrowsIfDependencyContextIsNullAndTheApplicationFailsToCompileWithNoReferences()
        {
            // Arrange
            var content = "public class MyTestType  {}";
            var compilationService = new DefaultRoslynCompilationService(
                dependencyContext: null,
                viewEngineOptions: GetOptions(),
                fileProviderAccessor: GetFileProviderAccessor(),
                loggerFactory: NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path.cshtml");

            var expected = "The Razor page 'some-relative-path.cshtml' failed to compile. Ensure that your "
                 + "application's project.json sets the 'preserveCompilationContext' compilation property.";

            // Act and Assert
            var ex = Assert.Throws<InvalidOperationException>(() =>
                compilationService.Compile(relativeFileInfo, content));
            Assert.Equal(expected, ex.Message);
        }
        public void Compile_RunsCallback()
        {
            // Arrange
            var content = "public class MyTestType  {}";
            RoslynCompilationContext usedCompilation = null;

            var compilationService = new DefaultRoslynCompilationService(
                 GetDependencyContext(),
                 GetOptions(callback: c => usedCompilation = c),
                 GetFileProviderAccessor(),
                 NullLoggerFactory.Instance);

            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            Assert.NotNull(usedCompilation);
            Assert.Single(usedCompilation.Compilation.SyntaxTrees);
        }
        public void Compile_UsesApplicationsCompilationSettings_ForParsingAndCompilation()
        {
            // Arrange
            var content = @"
#if MY_CUSTOM_DEFINE
public class MyCustomDefinedClass {}
#else
public class MyNonCustomDefinedClass {}
#endif
";

            var options = GetOptions();
            options.ParseOptions = options.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");

            var compilationService = new DefaultRoslynCompilationService(
                 GetDependencyContext(),
                 options,
                 GetFileProviderAccessor(),
                 NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { PhysicalPath = "SomePath" },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.NotNull(result.CompiledType);
            Assert.Equal("MyCustomDefinedClass", result.CompiledType.Name);
        }
        public void Compile_ReturnsGeneratedCodePath_IfLinePragmaIsNotAvailable()
        {
            // Arrange
            var fileContent = "file content";
            var content = @"this should fail";

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(
                new TestFileInfo { Content = fileContent },
                "some-relative-path");

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType<CompilationResult>(result);
            Assert.Null(result.CompiledType);

            var compilationFailure = Assert.Single(result.CompilationFailures);
            Assert.Equal("Generated Code", compilationFailure.SourceFilePath);
            Assert.Equal(content, compilationFailure.SourceFileContent);
        }
        public void Compile_DoesNotThrow_IfFileCannotBeRead()
        {
            // Arrange
            var path = "some-relative-path";
            var content = $@"
#line 1 ""{path}""
this should fail";

            var mockFileInfo = new Mock<IFileInfo>();
            mockFileInfo.Setup(f => f.CreateReadStream())
                .Throws(new Exception());
            var fileProvider = new TestFileProvider();
            fileProvider.AddFile(path, mockFileInfo.Object);

            var compilationService = new DefaultRoslynCompilationService(
                GetDependencyContext(),
                GetOptions(),
                GetFileProviderAccessor(),
                NullLoggerFactory.Instance);
            var relativeFileInfo = new RelativeFileInfo(mockFileInfo.Object, path);

            // Act
            var result = compilationService.Compile(relativeFileInfo, content);

            // Assert
            Assert.IsType<CompilationResult>(result);
            Assert.Null(result.CompiledType);
            var compilationFailure = Assert.Single(result.CompilationFailures);
            Assert.Equal(path, compilationFailure.SourceFilePath);
            Assert.Null(compilationFailure.SourceFileContent);
        }