Merge branch 'release/2.1' into dev

This commit is contained in:
Ajay Bhargav Baaskaran 2018-03-19 16:15:43 -07:00
commit 37c398bcb1
2 changed files with 51 additions and 4 deletions

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
@ -84,7 +85,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var importFeature = GetRequiredFeature<IImportProjectFeature>();
var importItems = importFeature.GetImports(projectItem);
var importSourceDocuments = GetImportSourceDocuments(importItems);
var importSourceDocuments = GetImportSourceDocuments(importItems, suppressExceptions: true);
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
@ -137,7 +138,9 @@ namespace Microsoft.AspNetCore.Razor.Language
}
// 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>();
for (var i = 0; i < importItems.Count; i++)
@ -146,8 +149,17 @@ namespace Microsoft.AspNetCore.Razor.Language
if (importItem.Exists)
{
var sourceDocument = RazorSourceDocument.ReadFrom(importItem);
imports.Add(sourceDocument);
try
{
// 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.
}
}
}

View File

@ -1,6 +1,7 @@
// 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.
using System.IO;
using Moq;
using Xunit;
@ -23,5 +24,39 @@ namespace Microsoft.AspNetCore.Razor.Language
var sourceDocument = Assert.Single(sourceDocuments);
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);
}
}
}