Handle unreadable import items in design time

This commit is contained in:
Ajay Bhargav Baaskaran 2018-03-19 15:04:33 -07:00
parent d1bf120c54
commit 79e744f691
2 changed files with 48 additions and 6 deletions

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var importFeature = GetRequiredFeature<IImportProjectFeature>();
var importItems = importFeature.GetImports(projectItem);
var importSourceDocuments = importItems.Select(ConvertToSourceDocument);
var importSourceDocuments = importItems.Select(i => ConvertToSourceDocument(i));
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureParserOptions);
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureCodeGenerationOptions);
@ -84,12 +85,11 @@ namespace Microsoft.AspNetCore.Razor.Language
var importFeature = GetRequiredFeature<IImportProjectFeature>();
var importItems = importFeature.GetImports(projectItem);
var importSourceDocuments = importItems.Select(ConvertToSourceDocument);
var importSourceDocuments = importItems.Select(i => ConvertToSourceDocument(i, suppressExceptions: true));
var parserOptions = GetRequiredFeature<IRazorParserOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeParserOptions);
var codeGenerationOptions = GetRequiredFeature<IRazorCodeGenerationOptionsFactoryProjectFeature>().Create(ConfigureDesignTimeCodeGenerationOptions);
return RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions);
}
@ -138,12 +138,20 @@ namespace Microsoft.AspNetCore.Razor.Language
}
// Internal for testing
internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem)
internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem, bool suppressExceptions = false)
{
if (importItem.Exists)
{
// Normal import, has file paths, content etc.
return RazorSourceDocument.ReadFrom(importItem);
try
{
// Normal import, has file paths, content etc.
return RazorSourceDocument.ReadFrom(importItem);
}
catch (IOException) when (suppressExceptions)
{
// Something happened when trying to read the item from disk.
// Catch the exception so we don't crash the editor.
}
}
// Marker import, doesn't exist, used as an identifier for "there could be something here".

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;
@ -33,5 +34,38 @@ namespace Microsoft.AspNetCore.Razor.Language
// Assert
Assert.NotNull(sourceDocument);
}
[Fact]
public void ConvertToSourceDocument_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."));
// Act & Assert
var exception = Assert.Throws<IOException>(() => DefaultRazorProjectEngine.ConvertToSourceDocument(projectItem.Object));
Assert.Equal("Couldn't read file.", exception.Message);
}
[Fact]
public void ConvertToSourceDocument_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."));
// Act
var sourceDocument = DefaultRazorProjectEngine.ConvertToSourceDocument(projectItem.Object, suppressExceptions: true);
// Assert - Does not throw
Assert.NotNull(sourceDocument);
Assert.Equal(0, sourceDocument.Length);
}
}
}