Change final code documents to not contain non-existent imports.
- Existent imports are imports that have content that contribute to the processing of a Razor document. Prior to this we had a legacy expectation that code documents had empty markers in them for all of their import locations. This proved troublesome when cross-referencing files that had file paths and were supposed to be existent but weren't in metadata. Now that we have a project engine with a de-coupled import feature we can rely on the import feature for finding all locations of important files and then strip out any non-existent items.
This commit is contained in:
parent
64c86634a8
commit
b538ceba93
|
|
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
var importFeature = GetRequiredFeature<IImportProjectFeature>();
|
var importFeature = GetRequiredFeature<IImportProjectFeature>();
|
||||||
var importItems = importFeature.GetImports(projectItem);
|
var importItems = importFeature.GetImports(projectItem);
|
||||||
var importSourceDocuments = importItems.Select(ConvertToSourceDocument);
|
var importSourceDocuments = GetImportSourceDocuments(importItems);
|
||||||
|
|
||||||
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureParserOptions);
|
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureParserOptions);
|
||||||
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureCodeGenerationOptions);
|
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureCodeGenerationOptions);
|
||||||
|
|
@ -84,12 +84,11 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
var importFeature = GetRequiredFeature<IImportProjectFeature>();
|
var importFeature = GetRequiredFeature<IImportProjectFeature>();
|
||||||
var importItems = importFeature.GetImports(projectItem);
|
var importItems = importFeature.GetImports(projectItem);
|
||||||
var importSourceDocuments = importItems.Select(ConvertToSourceDocument);
|
var importSourceDocuments = GetImportSourceDocuments(importItems);
|
||||||
|
|
||||||
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
|
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
|
||||||
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
|
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
|
||||||
|
|
||||||
|
|
||||||
return RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions);
|
return RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,17 +137,21 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal for testing
|
// Internal for testing
|
||||||
internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem)
|
internal static IReadOnlyList<RazorSourceDocument> GetImportSourceDocuments(IReadOnlyList<RazorProjectItem> importItems)
|
||||||
{
|
{
|
||||||
if (importItem.Exists)
|
var imports = new List<RazorSourceDocument>();
|
||||||
|
for (var i = 0; i < importItems.Count; i++)
|
||||||
{
|
{
|
||||||
// Normal import, has file paths, content etc.
|
var importItem = importItems[i];
|
||||||
return RazorSourceDocument.ReadFrom(importItem);
|
|
||||||
|
if (importItem.Exists)
|
||||||
|
{
|
||||||
|
var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
|
||||||
|
imports.Add(sourceDocument);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marker import, doesn't exist, used as an identifier for "there could be something here".
|
return imports;
|
||||||
var sourceDocumentProperties = new RazorSourceDocumentProperties(importItem.FilePath, importItem.RelativePhysicalPath);
|
|
||||||
return RazorSourceDocument.Create(string.Empty, sourceDocumentProperties);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
// Arrange
|
// Arrange
|
||||||
var projectItem = new TestRazorProjectItem("Index.cshtml");
|
var projectItem = new TestRazorProjectItem("Index.cshtml");
|
||||||
|
|
||||||
var testImport = Mock.Of<RazorProjectItem>(i => i.Read() == new MemoryStream() && i.FilePath == "testvalue");
|
var testImport = Mock.Of<RazorProjectItem>(i => i.Read() == new MemoryStream() && i.FilePath == "testvalue" && i.Exists == true);
|
||||||
var importFeature = new Mock<IImportProjectFeature>();
|
var importFeature = new Mock<IImportProjectFeature>();
|
||||||
importFeature
|
importFeature
|
||||||
.Setup(feature => feature.GetImports(It.IsAny<RazorProjectItem>()))
|
.Setup(feature => feature.GetImports(It.IsAny<RazorProjectItem>()))
|
||||||
|
|
|
||||||
|
|
@ -9,29 +9,19 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
public class DefaultRazorProjectEngineTest
|
public class DefaultRazorProjectEngineTest
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ConvertToSourceDocument_ConvertsNormalImports()
|
public void GetImportSourceDocuments_DoesNotIncludeNonExistentItems()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var projectItem = new TestRazorProjectItem("Index.cshtml");
|
var existingItem = new TestRazorProjectItem("Index.cshtml");
|
||||||
|
var nonExistentItem = Mock.Of<RazorProjectItem>(item => item.Exists == false);
|
||||||
|
var items = new[] { existingItem, nonExistentItem };
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var sourceDocument = DefaultRazorProjectEngine.ConvertToSourceDocument(projectItem);
|
var sourceDocuments = DefaultRazorProjectEngine.GetImportSourceDocuments(items);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.NotNull(sourceDocument);
|
var sourceDocument = Assert.Single(sourceDocuments);
|
||||||
}
|
Assert.Equal(existingItem.FilePath, sourceDocument.FilePath);
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ConvertToSourceDocument_ConvertsMarkerImports()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var projectItem = Mock.Of<RazorProjectItem>(item => item.FilePath == "Index.cshtml" && item.Exists == false);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var sourceDocument = DefaultRazorProjectEngine.ConvertToSourceDocument(projectItem);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.NotNull(sourceDocument);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue