Remove IDisposable contract from IMvcRazorHost

Fixes #4709
This commit is contained in:
Pranav K 2016-05-24 11:09:22 -07:00
parent 3d0f436a06
commit 9acd0f578c
4 changed files with 368 additions and 392 deletions

View File

@ -1,7 +1,6 @@
// 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;
using System.IO;
using Microsoft.AspNetCore.Razor.CodeGenerators;
@ -10,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <summary>
/// Specifies the contracts for a Razor host that parses Razor files and generates C# code.
/// </summary>
public interface IMvcRazorHost : IDisposable
public interface IMvcRazorHost
{
/// <summary>
/// Parses and generates the contents of a Razor file represented by <paramref name="inputStream"/>.

View File

@ -295,7 +295,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor
}
var inheritedChunkTrees = GetInheritedChunkTrees(sourceFileName);
return new MvcRazorParser(razorParser, inheritedChunkTrees, DefaultInheritedChunks, ModelExpressionType);
}
@ -345,11 +344,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor
});
}
public void Dispose()
{
_chunkTreeCache.Dispose();
}
private IReadOnlyList<ChunkTree> GetInheritedChunkTrees(string sourceFileName)
{
var inheritedChunkTrees = GetInheritedChunkTreeResults(sourceFileName)

View File

@ -32,8 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
new UsingChunk { Namespace = "AppNamespace.Model" },
};
var cache = new DefaultChunkTreeCache(fileProvider);
using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)))
{
var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false));
var utility = new ChunkInheritanceUtility(host, cache, defaultChunks);
// Act
@ -109,7 +108,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
Assert.Equal(viewImportsPath, chunkTreeResult.FilePath);
});
}
}
[Fact]
public void GetInheritedChunks_ReturnsEmptySequenceIfNoGlobalsArePresent()
@ -120,8 +118,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
fileProvider.AddFile(@"/Views/_Layout.cshtml", string.Empty);
fileProvider.AddFile(@"/Views/home/_not-viewimports.cshtml", string.Empty);
var cache = new DefaultChunkTreeCache(fileProvider);
using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)))
{
var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false));
var defaultChunks = new Chunk[]
{
new InjectChunk("MyTestHtmlHelper", "Html"),
@ -135,7 +132,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
// Assert
Assert.Empty(chunkTrees);
}
}
[Fact]
public void MergeInheritedChunks_MergesDefaultInheritedChunks()
@ -145,8 +141,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
fileProvider.AddFile(@"/Views/_ViewImports.cshtml",
"@inject DifferentHelper<TModel> Html");
var cache = new DefaultChunkTreeCache(fileProvider);
using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)))
{
var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false));
var defaultChunks = new Chunk[]
{
new InjectChunk("MyTestHtmlHelper", "Html"),
@ -185,4 +180,3 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives
}
}
}
}

View File

@ -56,10 +56,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor
var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/";
var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml";
var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider());
using (var host = new MvcRazorHost(
var host = new MvcRazorHost(
chunkTreeCache,
pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)))
{
pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath));
var parser = new RazorParser(
host.CodeLanguage.CreateCodeParser(),
host.CreateMarkupParser(),
@ -73,7 +73,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Assert
Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal);
}
}
[Theory]
[MemberData(nameof(NormalizeChunkInheritanceUtilityPaths_Data))]
@ -84,10 +83,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor
var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/";
var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml";
var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider());
using (var host = new MvcRazorHost(
var host = new MvcRazorHost(
chunkTreeCache,
pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)))
{
pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath));
var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache);
var codeGeneratorContext = new CodeGeneratorContext(
new ChunkGeneratorContext(
@ -106,33 +105,33 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Assert
Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal);
}
}
[Fact]
public void MvcRazorHost_EnablesInstrumentationByDefault()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider), new TagHelperDescriptorResolver(designTime: false)))
{
var host = new MvcRazorHost(
new DefaultChunkTreeCache(fileProvider),
new TagHelperDescriptorResolver(designTime: false));
// Act
var instrumented = host.EnableInstrumentation;
// Assert
Assert.True(instrumented);
}
}
[Fact]
public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
var expectedLineMappings = new[]
{
BuildLineMapping(
@ -175,7 +174,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor
testName: "ModelExpressionTagHelper",
expectedLineMappings: expectedLineMappings);
}
}
[Theory]
[InlineData("Basic")]
@ -189,23 +187,22 @@ namespace Microsoft.AspNetCore.Mvc.Razor
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new TestMvcRazorHost(new DefaultChunkTreeCache(fileProvider)))
{
var host = new TestMvcRazorHost(new DefaultChunkTreeCache(fileProvider));
// Act and Assert
RunRuntimeTest(host, scenarioName);
}
}
[Fact]
public void BasicVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -231,18 +228,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "Basic", expectedLineMappings);
}
}
[Fact]
public void MvcRazorHost_GeneratesCorrectLineMappingsAndUsingStatementsForViewImports()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -259,18 +255,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "_ViewImports", expectedLineMappings);
}
}
[Fact]
public void InjectVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -295,18 +290,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "Inject", expectedLineMappings);
}
}
[Fact]
public void InjectVisitorWithModel_GeneratesCorrectLineMappings()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -339,18 +333,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings);
}
}
[Fact]
public void InjectVisitorWithSemicolon_GeneratesCorrectLineMappings()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -399,18 +392,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings);
}
}
[Fact]
public void ModelVisitor_GeneratesCorrectLineMappings()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var expectedLineMappings = new[]
{
@ -427,18 +418,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// Act and Assert
RunDesignTimeTest(host, "Model", expectedLineMappings);
}
}
[Fact]
public void ModelVisitor_GeneratesLineMappingsForLastModel_WhenMultipleModelsArePresent()
{
// Arrange
var fileProvider = new TestFileProvider();
using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
{
DesignTimeMode = true
})
{
};
host.NamespaceImports.Clear();
var inputFile = "TestFiles/Input/MultipleModels.cshtml";
var outputFile = "TestFiles/Output/DesignTime/MultipleModels.cs";
@ -461,7 +451,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor
Assert.Equal(expectedCode, results.GeneratedCode, ignoreLineEndingDifferences: true);
#endif
}
}
private static void RunRuntimeTest(
MvcRazorHost host,