Adding more tests and clarity around file paths
I noticed we were really undertesting all of the things that handle paths and file names. I gave this some love and a little clean up where we weren't doing the right thing in RazorSourceDocument. Also changed the template engine tests to use the FileSystemRazorProject. These tests are already using the files on disk as inputs. I turned off checksums for these since they now have the full file path, and that would not be portable.
This commit is contained in:
parent
fbb73bccd0
commit
03dea86c4e
|
|
@ -183,13 +183,20 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
// Fall back to the relative path only if necessary.
|
||||
filePath = projectItem.RelativePhysicalPath;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
// Then fall back to the FilePath (yeah it's a bad name) which is like an MVC view engine path
|
||||
// It's much better to have something than nothing.
|
||||
filePath = projectItem.FilePath;
|
||||
}
|
||||
|
||||
using (var stream = projectItem.Read())
|
||||
{
|
||||
// Autodetect the encoding.
|
||||
return new StreamSourceDocument(stream, null, new RazorSourceDocumentProperties(filePath, projectItem.FilePath));
|
||||
return new StreamSourceDocument(stream, null, new RazorSourceDocumentProperties(filePath, projectItem.RelativePhysicalPath));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,7 +76,16 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
var items = fileSystemProject.EnumerateItems("/");
|
||||
|
||||
// Assert
|
||||
Assert.Collection(items.OrderBy(f => f.FilePath),
|
||||
Assert.Collection(
|
||||
items.OrderBy(f => f.FilePath),
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal("_ViewImports.cshtml", item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Home.cshtml", item.FilePath);
|
||||
|
|
@ -84,6 +93,14 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Equal(Path.Combine(TestFolder, "Home.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal("Home.cshtml", item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Views/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine("Views", "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
|
|
@ -93,6 +110,14 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Equal(Path.Combine("Views", "About", "About.cshtml"), item.RelativePhysicalPath);
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Views/Home/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine("Views", "Home", "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Views/Home/Index.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
|
|
@ -111,7 +136,15 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
var items = fileSystemProject.EnumerateItems("/Views");
|
||||
|
||||
// Assert
|
||||
Assert.Collection(items.OrderBy(f => f.FilePath),
|
||||
Assert.Collection(
|
||||
items.OrderBy(f => f.FilePath),
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/Views", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine( "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/About/About.cshtml", item.FilePath);
|
||||
|
|
@ -120,6 +153,13 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Equal(Path.Combine("About", "About.cshtml"), item.RelativePhysicalPath);
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Home/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/Views", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine("Home", "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Home/Index.cshtml", item.FilePath);
|
||||
Assert.Equal("/Views", item.BasePath);
|
||||
|
|
@ -141,6 +181,44 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Empty(items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindHierarchicalItems_FindsItemsWithMatchingNames()
|
||||
{
|
||||
// Arrange
|
||||
var fileSystemProject = new FileSystemRazorProject(TestFolder);
|
||||
|
||||
// Act
|
||||
var items = fileSystemProject.FindHierarchicalItems("/Views/Home/Index.cshtml", "_ViewImports.cshtml");
|
||||
|
||||
// Assert
|
||||
Assert.Collection(
|
||||
items,
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Views/Home/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine("Views", "Home", "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/Views/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "Views", "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal(Path.Combine("Views", "_ViewImports.cshtml"), item.RelativePhysicalPath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal("/_ViewImports.cshtml", item.FilePath);
|
||||
Assert.Equal("/", item.BasePath);
|
||||
Assert.Equal(Path.Combine(TestFolder, "_ViewImports.cshtml"), item.PhysicalPath);
|
||||
Assert.Equal("_ViewImports.cshtml", item.RelativePhysicalPath);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetItem_ReturnsFileFromDisk()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,39 +12,35 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
|
|||
public void GenerateCodeWithDefaults()
|
||||
{
|
||||
// Arrange
|
||||
var filePath = Path.Combine(TestProjectRoot, $"{FileName}.cshtml");
|
||||
var content = File.ReadAllText(filePath);
|
||||
var projectItem = new TestRazorProjectItem($"{FileName}.cshtml", "")
|
||||
var project = new FileSystemRazorProject(TestProjectRoot);
|
||||
var razorEngine = RazorEngine.Create(engine =>
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
var project = new TestRazorProject(new[]{ projectItem });
|
||||
var razorEngine = RazorEngine.Create();
|
||||
engine.Features.Add(new SuppressChecksumOptionsFeature());
|
||||
});
|
||||
var templateEngine = new RazorTemplateEngine(razorEngine, project);
|
||||
|
||||
// Act
|
||||
var resultcSharpDocument = templateEngine.GenerateCode(projectItem.FilePath);
|
||||
var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml");
|
||||
|
||||
// Assert
|
||||
AssertCSharpDocumentMatchesBaseline(resultcSharpDocument);
|
||||
AssertCSharpDocumentMatchesBaseline(cSharpDocument);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenerateCodeWithBaseType()
|
||||
{
|
||||
// Arrange
|
||||
var filePath = Path.Combine(TestProjectRoot, $"{FileName}.cshtml");
|
||||
var content = File.ReadAllText(filePath);
|
||||
var projectItem = new TestRazorProjectItem($"{FileName}.cshtml", "")
|
||||
var project = new FileSystemRazorProject(TestProjectRoot);
|
||||
var razorEngine = RazorEngine.Create(engine =>
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
var project = new TestRazorProject(new[] { projectItem });
|
||||
var razorEngine = RazorEngine.Create(engine => engine.SetBaseType("MyBaseType"));
|
||||
engine.Features.Add(new SuppressChecksumOptionsFeature());
|
||||
|
||||
engine.SetBaseType("MyBaseType");
|
||||
});
|
||||
var templateEngine = new RazorTemplateEngine(razorEngine, project);
|
||||
|
||||
// Act
|
||||
var cSharpDocument = templateEngine.GenerateCode(projectItem.FilePath);
|
||||
var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml");
|
||||
|
||||
// Assert
|
||||
AssertCSharpDocumentMatchesBaseline(cSharpDocument);
|
||||
|
|
@ -54,15 +50,11 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
|
|||
public void GenerateCodeWithConfigureClass()
|
||||
{
|
||||
// Arrange
|
||||
var filePath = Path.Combine(TestProjectRoot, $"{FileName}.cshtml");
|
||||
var content = File.ReadAllText(filePath);
|
||||
var projectItem = new TestRazorProjectItem($"{FileName}.cshtml", "")
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
var project = new TestRazorProject(new[] { projectItem });
|
||||
var project = new FileSystemRazorProject(TestProjectRoot);
|
||||
var razorEngine = RazorEngine.Create(engine =>
|
||||
{
|
||||
engine.Features.Add(new SuppressChecksumOptionsFeature());
|
||||
|
||||
engine.ConfigureClass((document, @class) =>
|
||||
{
|
||||
@class.ClassName = "MyClass";
|
||||
|
|
@ -81,7 +73,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
|
|||
var templateEngine = new RazorTemplateEngine(razorEngine, project);
|
||||
|
||||
// Act
|
||||
var cSharpDocument = templateEngine.GenerateCode(projectItem.FilePath);
|
||||
var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml");
|
||||
|
||||
// Assert
|
||||
AssertCSharpDocumentMatchesBaseline(cSharpDocument);
|
||||
|
|
@ -91,21 +83,17 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
|
|||
public void GenerateCodeWithSetNamespace()
|
||||
{
|
||||
// Arrange
|
||||
var filePath = Path.Combine(TestProjectRoot, $"{FileName}.cshtml");
|
||||
var content = File.ReadAllText(filePath);
|
||||
var projectItem = new TestRazorProjectItem($"{FileName}.cshtml", "")
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
var project = new TestRazorProject(new[] { projectItem });
|
||||
var project = new FileSystemRazorProject(TestProjectRoot);
|
||||
var razorEngine = RazorEngine.Create(engine =>
|
||||
{
|
||||
engine.Features.Add(new SuppressChecksumOptionsFeature());
|
||||
|
||||
engine.SetNamespace("MyApp.Razor.Views");
|
||||
});
|
||||
var templateEngine = new RazorTemplateEngine(razorEngine, project);
|
||||
|
||||
// Act
|
||||
var cSharpDocument = templateEngine.GenerateCode(projectItem.FilePath);
|
||||
var cSharpDocument = templateEngine.GenerateCode($"{FileName}.cshtml");
|
||||
|
||||
// Assert
|
||||
AssertCSharpDocumentMatchesBaseline(cSharpDocument);
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
public void ReadFrom_ProjectItem()
|
||||
{
|
||||
// Arrange
|
||||
var projectItem = new TestRazorProjectItem("filePath.cshtml", "c:\\myapp\\filePath.cshtml", "c:\\myapp\\");
|
||||
var projectItem = new TestRazorProjectItem("filePath.cshtml", "c:\\myapp\\filePath.cshtml", "filePath.cshtml", "c:\\myapp\\");
|
||||
|
||||
// Act
|
||||
var document = RazorSourceDocument.ReadFrom(projectItem);
|
||||
|
|
@ -82,6 +82,51 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Equal(projectItem.Content, ReadContent(document));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadFrom_ProjectItem_NoRelativePath()
|
||||
{
|
||||
// Arrange
|
||||
var projectItem = new TestRazorProjectItem("filePath.cshtml", "c:\\myapp\\filePath.cshtml", basePath: "c:\\myapp\\");
|
||||
|
||||
// Act
|
||||
var document = RazorSourceDocument.ReadFrom(projectItem);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("c:\\myapp\\filePath.cshtml", document.FilePath);
|
||||
Assert.Null(document.RelativePath);
|
||||
Assert.Equal(projectItem.Content, ReadContent(document));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadFrom_ProjectItem_FallbackToRelativePath()
|
||||
{
|
||||
// Arrange
|
||||
var projectItem = new TestRazorProjectItem("filePath.cshtml", relativePhysicalPath: "filePath.cshtml", basePath: "c:\\myapp\\");
|
||||
|
||||
// Act
|
||||
var document = RazorSourceDocument.ReadFrom(projectItem);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("filePath.cshtml", document.FilePath);
|
||||
Assert.Equal("filePath.cshtml", document.RelativePath);
|
||||
Assert.Equal(projectItem.Content, ReadContent(document));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadFrom_ProjectItem_FallbackToFileName()
|
||||
{
|
||||
// Arrange
|
||||
var projectItem = new TestRazorProjectItem("filePath.cshtml", basePath: "c:\\myapp\\");
|
||||
|
||||
// Act
|
||||
var document = RazorSourceDocument.ReadFrom(projectItem);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("filePath.cshtml", document.FilePath);
|
||||
Assert.Null(document.RelativePath);
|
||||
Assert.Equal(projectItem.Content, ReadContent(document));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithoutEncoding()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -310,5 +310,51 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
var paths = imports.Select(i => i.FilePath);
|
||||
Assert.Equal(expected, paths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateCodeDocument_WithFileSystemProject_ReturnsCorrectItems()
|
||||
{
|
||||
// Arrange
|
||||
var testFolder = Path.Combine(
|
||||
TestProject.GetProjectDirectory(typeof(FileSystemRazorProjectTest)),
|
||||
"TestFiles",
|
||||
"FileSystemRazorProject");
|
||||
|
||||
var project = new FileSystemRazorProject(testFolder);
|
||||
var razorEngine = RazorEngine.Create();
|
||||
var templateEngine = new RazorTemplateEngine(razorEngine, project)
|
||||
{
|
||||
Options =
|
||||
{
|
||||
ImportsFileName = "_ViewImports.cshtml"
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var codeDocument = templateEngine.CreateCodeDocument("/Views/Home/Index.cshtml");
|
||||
|
||||
// Assert
|
||||
Assert.Collection(
|
||||
codeDocument.Imports,
|
||||
item =>
|
||||
{
|
||||
Assert.Equal(Path.Combine(testFolder, "_ViewImports.cshtml"), item.FilePath);
|
||||
Assert.Equal("_ViewImports.cshtml", item.RelativePath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal(Path.Combine(testFolder, "Views", "_ViewImports.cshtml"), item.FilePath);
|
||||
Assert.Equal(Path.Combine("Views", "_ViewImports.cshtml"), item.RelativePath);
|
||||
|
||||
},
|
||||
item =>
|
||||
{
|
||||
Assert.Equal(Path.Combine(testFolder, "Views", "Home", "_ViewImports.cshtml"), item.FilePath);
|
||||
Assert.Equal(Path.Combine("Views", "Home", "_ViewImports.cshtml"), item.RelativePath);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
#pragma checksum "TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithBaseType.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "38aa8e26c5d2a85c61d8e93fe69dd326fe82671b"
|
||||
// <auto-generated/>
|
||||
#pragma warning disable 1591
|
||||
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Razor.Template), @"default", @"/TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithBaseType.cshtml")]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
#pragma checksum "TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithConfigureClass.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "38aa8e26c5d2a85c61d8e93fe69dd326fe82671b"
|
||||
// <auto-generated/>
|
||||
#pragma warning disable 1591
|
||||
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Razor.MyClass), @"default", @"/TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithConfigureClass.cshtml")]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
#pragma checksum "TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithDefaults.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "38aa8e26c5d2a85c61d8e93fe69dd326fe82671b"
|
||||
// <auto-generated/>
|
||||
#pragma warning disable 1591
|
||||
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Razor.Template), @"default", @"/TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithDefaults.cshtml")]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
#pragma checksum "TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithSetNamespace.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "38aa8e26c5d2a85c61d8e93fe69dd326fe82671b"
|
||||
// <auto-generated/>
|
||||
#pragma warning disable 1591
|
||||
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(MyApp.Razor.Views.Template), @"default", @"/TestFiles/IntegrationTests/RazorTemplateEngineIntegrationTest/GenerateCodeWithSetNamespace.cshtml")]
|
||||
|
|
|
|||
|
|
@ -11,10 +11,12 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
public TestRazorProjectItem(
|
||||
string filePath,
|
||||
string physicalPath = null,
|
||||
string relativePhysicalPath = null,
|
||||
string basePath = "/")
|
||||
{
|
||||
FilePath = filePath;
|
||||
PhysicalPath = physicalPath;
|
||||
RelativePhysicalPath = relativePhysicalPath;
|
||||
BasePath = basePath;
|
||||
}
|
||||
|
||||
|
|
@ -24,6 +26,8 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
|
||||
public override string PhysicalPath { get; }
|
||||
|
||||
public override string RelativePhysicalPath { get; }
|
||||
|
||||
public override bool Exists => true;
|
||||
|
||||
public string Content { get; set; } = "Default content";
|
||||
|
|
|
|||
Loading…
Reference in New Issue