Handle unreadable import items in design time
This commit is contained in:
parent
d1bf120c54
commit
79e744f691
|
|
@ -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
|
||||||
|
|
@ -65,7 +66,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 = importItems.Select(i => ConvertToSourceDocument(i));
|
||||||
|
|
||||||
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 +85,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 = importItems.Select(i => ConvertToSourceDocument(i, 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);
|
||||||
|
|
||||||
|
|
||||||
return RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions);
|
return RazorCodeDocument.Create(sourceDocument, importSourceDocuments, parserOptions, codeGenerationOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,12 +138,20 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal for testing
|
// Internal for testing
|
||||||
internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem)
|
internal static RazorSourceDocument ConvertToSourceDocument(RazorProjectItem importItem, bool suppressExceptions = false)
|
||||||
{
|
{
|
||||||
if (importItem.Exists)
|
if (importItem.Exists)
|
||||||
{
|
{
|
||||||
// Normal import, has file paths, content etc.
|
try
|
||||||
return RazorSourceDocument.ReadFrom(importItem);
|
{
|
||||||
|
// 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".
|
// Marker import, doesn't exist, used as an identifier for "there could be something here".
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
@ -33,5 +34,38 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
// Assert
|
// Assert
|
||||||
Assert.NotNull(sourceDocument);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue