From c176bdbab0061d6bf821df8e8d4f18e98ec7e3a7 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 25 Apr 2017 11:04:09 -0700 Subject: [PATCH 1/3] Use Bundled NETStandard.Library \ NETCoreApp versions instead of explicitly specifying one --- build/common.props | 2 +- build/dependencies.props | 4 +--- .../Microsoft.AspNetCore.Razor.Performance.csproj | 9 ++------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/build/common.props b/build/common.props index b289043f85..51974f7014 100644 --- a/build/common.props +++ b/build/common.props @@ -18,7 +18,7 @@ - + diff --git a/build/dependencies.props b/build/dependencies.props index 86e4670399..211b66eb3c 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -7,11 +7,9 @@ 2.0.0-* 10.0.1 4.7.1 - 1.6.1 1.3.0 2.0.0-rc5-61501-05 - 2.0.0-* 15.0.0 2.2.0 - \ No newline at end of file + diff --git a/test/Microsoft.AspNetCore.Razor.Performance/Microsoft.AspNetCore.Razor.Performance.csproj b/test/Microsoft.AspNetCore.Razor.Performance/Microsoft.AspNetCore.Razor.Performance.csproj index 268a7edd83..ecb9fd2484 100644 --- a/test/Microsoft.AspNetCore.Razor.Performance/Microsoft.AspNetCore.Razor.Performance.csproj +++ b/test/Microsoft.AspNetCore.Razor.Performance/Microsoft.AspNetCore.Razor.Performance.csproj @@ -1,4 +1,4 @@ - + @@ -8,11 +8,6 @@ true true false - - 2.0.0-* @@ -23,4 +18,4 @@ - \ No newline at end of file + From 207e0f0b59921efc2c47d2d58e1b0056c33dc3ce Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Fri, 28 Apr 2017 15:31:26 -0700 Subject: [PATCH 2/3] Change `GetImports` to allow querying of information on non-existent files. - Added tests to validate imports can still be found on non-existent files. #1267 --- .../RazorTemplateEngine.cs | 6 --- .../RazorTemplateEngineTest.cs | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorTemplateEngine.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorTemplateEngine.cs index 62bee58376..5717db117d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/RazorTemplateEngine.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorTemplateEngine.cs @@ -182,12 +182,6 @@ namespace Microsoft.AspNetCore.Razor.Language { throw new ArgumentNullException(nameof(projectItem)); } - - if (!projectItem.Exists) - { - throw new InvalidOperationException(Resources.FormatRazorTemplateEngine_ItemCouldNotBeFound(projectItem.Path)); - } - var result = new List(); diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorTemplateEngineTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorTemplateEngineTest.cs index 75ca3e4acf..442d0b4b25 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorTemplateEngineTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorTemplateEngineTest.cs @@ -11,6 +11,53 @@ namespace Microsoft.AspNetCore.Razor.Language { public class RazorTemplateEngineTest { + [Fact] + public void GetImports_CanQueryInformationOnNonExistentFileWithoutImports() + { + // Arrange + var project = new TestRazorProject(); + var razorEngine = RazorEngine.Create(); + var templateEngine = new RazorTemplateEngine(razorEngine, project) + { + Options = + { + ImportsFileName = "MyImport.cshtml" + } + }; + var projectItemToQuery = project.GetItem("/Views/Home/Index.cshtml"); + + // Act + var imports = templateEngine.GetImports(projectItemToQuery); + + // Assert + Assert.Empty(imports); + } + + [Fact] + public void GetImports_CanQueryInformationOnNonExistentFileWithImports() + { + // Arrange + var path = "/Views/Home/MyImport.cshtml"; + var projectItem = new TestRazorProjectItem(path); + var project = new TestRazorProject(new[] { projectItem }); + var razorEngine = RazorEngine.Create(); + var templateEngine = new RazorTemplateEngine(razorEngine, project) + { + Options = + { + ImportsFileName = "MyImport.cshtml" + } + }; + var projectItemToQuery = project.GetItem("/Views/Home/Index.cshtml"); + + // Act + var imports = templateEngine.GetImports(projectItemToQuery); + + // Assert + var import = Assert.Single(imports); + Assert.Equal(projectItem.Path, import.FileName); + } + [Fact] public void GenerateCode_ThrowsIfItemCannotBeFound() { From 86beea4269fc53de0543263452186c1331b6719c Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Fri, 28 Apr 2017 16:56:36 -0700 Subject: [PATCH 3/3] Fix @namespace directive to handle incomplete directives. - This involved not using the `First()` method when reading the directives tokens. - Added two tests to validate an empty directive token and a missed directive token for the `@namespace` directive. #1268 --- .../NamespaceDirective.cs | 9 +++- .../NamespaceDirectiveTest.cs | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs index 188b60f6ca..946788eb77 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/NamespaceDirective.cs @@ -85,7 +85,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { var directiveSource = NormalizeDirectory(directive.Source?.FilePath); - var baseNamespace = directive.Tokens.First().Content; + var baseNamespace = directive.Tokens.FirstOrDefault()?.Content; + if (string.IsNullOrEmpty(baseNamespace)) + { + // The namespace directive was incomplete. + @namespace = string.Empty; + return false; + } + if (string.IsNullOrEmpty(source) || directiveSource == null) { // No sources, can't compute a suffix. diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/NamespaceDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/NamespaceDirectiveTest.cs index 292afbd945..688517e8cb 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/NamespaceDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/NamespaceDirectiveTest.cs @@ -9,6 +9,48 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { public class NamespaceDirectiveTest { + [Fact] + public void TryComputeNamespace_IncompleteDirective_UsesEmptyNamespace() + { + // Arrange + var source = "c:\\foo\\bar\\bleh.cshtml"; + var imports = "c:\\foo\\baz\\bleh.cshtml"; + var node = new DirectiveIRNode() + { + Descriptor = NamespaceDirective.Directive, + Source = new SourceSpan(imports, 0, 0, 0, 0), + }; + + // Act + var computed = NamespaceDirective.TryComputeNamespace(source, node, out var @namespace); + + // Assert + Assert.False(computed); + Assert.Equal(string.Empty, @namespace); + } + + [Fact] + public void TryComputeNamespace_EmptyDirective_UsesEmptyNamespace() + { + // Arrange + var source = "c:\\foo\\bar\\bleh.cshtml"; + var imports = "c:\\foo\\baz\\bleh.cshtml"; + var node = new DirectiveIRNode() + { + Descriptor = NamespaceDirective.Directive, + Source = new SourceSpan(imports, 0, 0, 0, 0), + }; + node.Children.Add(new DirectiveTokenIRNode() { Content = string.Empty }); + node.Children[0].Parent = node; + + // Act + var computed = NamespaceDirective.TryComputeNamespace(source, node, out var @namespace); + + // Assert + Assert.False(computed); + Assert.Equal(string.Empty, @namespace); + } + // When we don't have a relationship between the source file and the imports file // we will just use the namespace on the node directly. [Theory]