Merge branch 'rel/vs15.7' into release/2.1
This commit is contained in:
commit
fab70c81ec
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
@ -84,7 +85,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 = GetImportSourceDocuments(importItems);
|
var importSourceDocuments = GetImportSourceDocuments(importItems, suppressExceptions: true);
|
||||||
|
|
||||||
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
|
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
|
||||||
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
|
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
|
||||||
|
|
@ -137,7 +138,9 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal for testing
|
// Internal for testing
|
||||||
internal static IReadOnlyList<RazorSourceDocument> GetImportSourceDocuments(IReadOnlyList<RazorProjectItem> importItems)
|
internal static IReadOnlyList<RazorSourceDocument> GetImportSourceDocuments(
|
||||||
|
IReadOnlyList<RazorProjectItem> importItems,
|
||||||
|
bool suppressExceptions = false)
|
||||||
{
|
{
|
||||||
var imports = new List<RazorSourceDocument>();
|
var imports = new List<RazorSourceDocument>();
|
||||||
for (var i = 0; i < importItems.Count; i++)
|
for (var i = 0; i < importItems.Count; i++)
|
||||||
|
|
@ -146,8 +149,17 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
if (importItem.Exists)
|
if (importItem.Exists)
|
||||||
{
|
{
|
||||||
var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
|
try
|
||||||
imports.Add(sourceDocument);
|
{
|
||||||
|
// Normal import, has file paths, content etc.
|
||||||
|
var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
|
||||||
|
imports.Add(sourceDocument);
|
||||||
|
}
|
||||||
|
catch (IOException) when (suppressExceptions)
|
||||||
|
{
|
||||||
|
// Something happened when trying to read the item from disk.
|
||||||
|
// Catch the exception so we don't crash the editor.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -23,5 +24,39 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
var sourceDocument = Assert.Single(sourceDocuments);
|
var sourceDocument = Assert.Single(sourceDocuments);
|
||||||
Assert.Equal(existingItem.FilePath, sourceDocument.FilePath);
|
Assert.Equal(existingItem.FilePath, sourceDocument.FilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetImportSourceDocuments_UnreadableItem_Throws()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var projectItem = new Mock<RazorProjectItem>(MockBehavior.Strict);
|
||||||
|
projectItem.SetupGet(p => p.Exists).Returns(true);
|
||||||
|
projectItem.SetupGet(p => p.PhysicalPath).Returns("path/to/file.cshtml");
|
||||||
|
projectItem.Setup(p => p.Read()).Throws(new IOException("Couldn't read file."));
|
||||||
|
var items = new[] { projectItem.Object };
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var exception = Assert.Throws<IOException>(() => DefaultRazorProjectEngine.GetImportSourceDocuments(items));
|
||||||
|
Assert.Equal("Couldn't read file.", exception.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetImportSourceDocuments_WithSuppressExceptions_UnreadableItem_DoesNotThrow()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var projectItem = new Mock<RazorProjectItem>(MockBehavior.Strict);
|
||||||
|
projectItem.SetupGet(p => p.Exists).Returns(true);
|
||||||
|
projectItem.SetupGet(p => p.PhysicalPath).Returns("path/to/file.cshtml");
|
||||||
|
projectItem.SetupGet(p => p.FilePath).Returns("path/to/file.cshtml");
|
||||||
|
projectItem.SetupGet(p => p.RelativePhysicalPath).Returns("path/to/file.cshtml");
|
||||||
|
projectItem.Setup(p => p.Read()).Throws(new IOException("Couldn't read file."));
|
||||||
|
var items = new[] { projectItem.Object };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var sourceDocuments = DefaultRazorProjectEngine.GetImportSourceDocuments(items, suppressExceptions: true);
|
||||||
|
|
||||||
|
// Assert - Does not throw
|
||||||
|
Assert.Empty(sourceDocuments);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue