public void EnsureOptions_ConfiguresDefaultCompilationOptions() { // Arrange var hostingEnvironment = Mock.Of <IWebHostEnvironment>(h => h.EnvironmentName == "Development"); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment); // Act & Assert var compilationOptions = compiler.CSharpCompilationOptions; Assert.False(compilationOptions.AllowUnsafe); Assert.Equal(ReportDiagnostic.Default, compilationOptions.GeneralDiagnosticOption); Assert.Equal(OptimizationLevel.Debug, compilationOptions.OptimizationLevel); Assert.Collection(compilationOptions.SpecificDiagnosticOptions.OrderBy(d => d.Key), item => { Assert.Equal("CS1701", item.Key); Assert.Equal(ReportDiagnostic.Suppress, item.Value); }, item => { Assert.Equal("CS1702", item.Key); Assert.Equal(ReportDiagnostic.Suppress, item.Value); }, item => { Assert.Equal("CS1705", item.Key); Assert.Equal(ReportDiagnostic.Suppress, item.Value); }); }
private static TestRazorViewCompiler GetViewCompiler( TestFileProvider fileProvider = null, RazorReferenceManager referenceManager = null, IList <CompiledViewDescriptor> precompiledViews = null, CSharpCompiler csharpCompiler = null) { fileProvider = fileProvider ?? new TestFileProvider(); var options = Options.Create(new MvcRazorRuntimeCompilationOptions { FileProviders = { fileProvider } }); var compilationFileProvider = new RuntimeCompilationFileProvider(options); referenceManager = referenceManager ?? CreateReferenceManager(); precompiledViews = precompiledViews ?? Array.Empty <CompiledViewDescriptor>(); var hostingEnvironment = Mock.Of <IWebHostEnvironment>(e => e.ContentRootPath == "BasePath"); var fileSystem = new FileProviderRazorProjectFileSystem(compilationFileProvider, hostingEnvironment); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder => { RazorExtensions.Register(builder); }); csharpCompiler = csharpCompiler ?? new CSharpCompiler(referenceManager, hostingEnvironment); return(new TestRazorViewCompiler( fileProvider, projectEngine, csharpCompiler, precompiledViews)); }
public void GetCompilationOptions_ReturnsDefaultOptionsIfApplicationDoesNotHaveDependencyContext() { // Arrange var hostingEnvironment = Mock.Of <IWebHostEnvironment>(); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment); // Act var options = compiler.GetDependencyContextCompilationOptions(); // Assert Assert.Same(DependencyContextCompilationOptions.Default, options); }
public void GetCompilationOptions_ReturnsDefaultOptionsIfApplicationNameIsNullOrEmpty(string name) { // Arrange var hostingEnvironment = Mock.Of <IWebHostEnvironment>(e => e.ApplicationName == name); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment); // Act var options = compiler.GetDependencyContextCompilationOptions(); // Assert Assert.Same(DependencyContextCompilationOptions.Default, options); }
public void EnsureOptions_ConfiguresDefaultParseOptions() { // Arrange var hostingEnvironment = Mock.Of <IWebHostEnvironment>(h => h.EnvironmentName == "Development"); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment); // Act & Assert var parseOptions = compiler.ParseOptions; Assert.Equal(LanguageVersion.CSharp7_3, parseOptions.LanguageVersion); Assert.Equal(new[] { "DEBUG" }, parseOptions.PreprocessorSymbolNames); }
public void EnsureOptions_SetsPreprocessorSymbols(string environment, string expectedConfiguration) { // Arrange var options = new RazorViewEngineOptions(); var hostingEnvironment = new Mock <IWebHostEnvironment>(); hostingEnvironment.SetupGet(e => e.EnvironmentName) .Returns(environment); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment.Object); // Act & Assert var parseOptions = compiler.ParseOptions; Assert.Equal(new[] { expectedConfiguration }, parseOptions.PreprocessorSymbolNames); }
public RuntimeViewCompilerProvider( ApplicationPartManager applicationPartManager, RazorProjectEngine razorProjectEngine, RuntimeCompilationFileProvider fileProvider, CSharpCompiler csharpCompiler, ILoggerFactory loggerFactory) { _applicationPartManager = applicationPartManager; _razorProjectEngine = razorProjectEngine; _csharpCompiler = csharpCompiler; _fileProvider = fileProvider; _logger = loggerFactory.CreateLogger <RuntimeViewCompiler>(); _createCompiler = CreateCompiler; }
public void Constructor_SetsOptimizationLevelBasedOnEnvironment( string environment, OptimizationLevel expected) { // Arrange var options = new RazorViewEngineOptions(); var hostingEnvironment = new Mock <IWebHostEnvironment>(); hostingEnvironment.SetupGet(e => e.EnvironmentName) .Returns(environment); var compiler = new CSharpCompiler(ReferenceManager, hostingEnvironment.Object); // Act & Assert var compilationOptions = compiler.CSharpCompilationOptions; Assert.Equal(expected, compilationOptions.OptimizationLevel); }
public TestRazorViewCompiler( TestFileProvider fileProvider, RazorProjectEngine projectEngine, CSharpCompiler csharpCompiler, IList <CompiledViewDescriptor> precompiledViews, Func <string, CompiledViewDescriptor> compile = null) : base(fileProvider, projectEngine, csharpCompiler, precompiledViews, NullLogger.Instance) { Compile = compile; if (Compile == null) { Compile = path => new CompiledViewDescriptor { RelativePath = path, Item = CreateForView(path), }; } }
public RuntimeViewCompiler( IFileProvider fileProvider, RazorProjectEngine projectEngine, CSharpCompiler csharpCompiler, IList <CompiledViewDescriptor> precompiledViews, ILogger logger) { if (fileProvider == null) { throw new ArgumentNullException(nameof(fileProvider)); } if (projectEngine == null) { throw new ArgumentNullException(nameof(projectEngine)); } if (csharpCompiler == null) { throw new ArgumentNullException(nameof(csharpCompiler)); } if (precompiledViews == null) { throw new ArgumentNullException(nameof(precompiledViews)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } _fileProvider = fileProvider; _projectEngine = projectEngine; _csharpCompiler = csharpCompiler; _logger = logger; _normalizedPathCache = new ConcurrentDictionary <string, string>(StringComparer.Ordinal); // This is our L0 cache, and is a durable store. Views migrate into the cache as they are requested // from either the set of known precompiled views, or by being compiled. _cache = new MemoryCache(new MemoryCacheOptions()); // We need to validate that the all of the precompiled views are unique by path (case-insensitive). // We do this because there's no good way to canonicalize paths on windows, and it will create // problems when deploying to linux. Rather than deal with these issues, we just don't support // views that differ only by case. _precompiledViews = new Dictionary <string, CompiledViewDescriptor>( precompiledViews.Count, StringComparer.OrdinalIgnoreCase); foreach (var precompiledView in precompiledViews) { logger.ViewCompilerLocatedCompiledView(precompiledView.RelativePath); if (!_precompiledViews.ContainsKey(precompiledView.RelativePath)) { // View ordering has precedence semantics, a view with a higher precedence was // already added to the list. _precompiledViews.Add(precompiledView.RelativePath, precompiledView); } } if (_precompiledViews.Count == 0) { logger.ViewCompilerNoCompiledViewsFound(); } }