Fix breaks in Razor Tests

I removed a reference from a src project which exposed this issue. We
shouldn't be using these types in new Razor.
This commit is contained in:
Ryan Nowak 2017-03-27 14:32:30 -07:00
parent af7798a9be
commit 737a9a58ad
5 changed files with 101 additions and 252 deletions

View File

@ -2,10 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor.Extensions.Internal;
using Microsoft.AspNetCore.Razor.Evolution;
using Microsoft.Extensions.FileProviders;
using Moq;
using Xunit;
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
GetRazorProject(new TestFileProvider()));
new TestRazorProject());
// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
};
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
GetRazorProject(new TestFileProvider()));
new TestRazorProject());
// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
// Arrange
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
GetRazorProject(new TestFileProvider()));
new TestRazorProject());
// Act
var imports = mvcRazorTemplateEngine.Options.DefaultImports;
@ -91,11 +91,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
// Arrange
var path = "/Views/Home/Index.cshtml";
var fileProvider = new TestFileProvider();
fileProvider.AddFile(path, "Hello world");
var item = new TestRazorProjectItem(path)
{
Content = "Hello world",
};
var project = new TestRazorProject(new List<RazorProjectItem>() { item, });
var mvcRazorTemplateEngine = new MvcRazorTemplateEngine(
RazorEngine.Create(),
GetRazorProject(fileProvider));
project);
// Act
var codeDocument = mvcRazorTemplateEngine.CreateCodeDocument(path);
@ -110,17 +114,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
imports.CopyTo(0, contentChars, 0, imports.Length);
return new string(contentChars);
}
private static RazorProject GetRazorProject(IFileProvider fileProvider)
{
var razorProject = new Mock<RazorProject>();
razorProject.Setup(s => s.GetItem(It.IsAny<string>()))
.Returns((string path) => {
var fileInfo = fileProvider.GetFileInfo(path);
return new DefaultRazorProjectItem(fileInfo, null, path);
});
return razorProject.Object;
}
}
}

View File

@ -12,7 +12,6 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.FileProviders;
using Moq;
using Xunit;
@ -132,12 +131,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
});
var inputContent = ResourceFile.ReadResource(_assembly, inputFile, sourceFile: true);
var fileProvider = new TestFileProvider();
fileProvider.AddFile(inputFile, inputContent);
var fileInfo = fileProvider.GetFileInfo(inputFile);
var razorTemplateEngine = new MvcRazorTemplateEngine(engine, GetRazorProject(fileProvider));
var razorProjectItem = new DefaultRazorProjectItem(fileInfo, basePath: null, path: inputFile);
var codeDocument = razorTemplateEngine.CreateCodeDocument(razorProjectItem);
var item = new TestRazorProjectItem(inputFile) { Content = inputContent, };
var project = new TestRazorProject(new List<RazorProjectItem>()
{
item,
});
var razorTemplateEngine = new MvcRazorTemplateEngine(engine, project);
var codeDocument = razorTemplateEngine.CreateCodeDocument(item);
codeDocument.Items["SuppressUniqueIds"] = "test";
codeDocument.Items["NewLineString"] = "\r\n";
@ -173,12 +174,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
});
var inputContent = ResourceFile.ReadResource(_assembly, inputFile, sourceFile: true);
var fileProvider = new TestFileProvider();
fileProvider.AddFile(inputFile, inputContent);
var fileInfo = fileProvider.GetFileInfo(inputFile);
var razorTemplateEngine = new MvcRazorTemplateEngine(engine, GetRazorProject(fileProvider));
var razorProjectItem = new DefaultRazorProjectItem(fileInfo, basePath: null, path: inputFile);
var codeDocument = razorTemplateEngine.CreateCodeDocument(razorProjectItem);
var item = new TestRazorProjectItem(inputFile) { Content = inputContent, };
var project = new TestRazorProject(new List<RazorProjectItem>()
{
item,
});
var razorTemplateEngine = new MvcRazorTemplateEngine(engine, project);
var codeDocument = razorTemplateEngine.CreateCodeDocument(item);
codeDocument.Items["SuppressUniqueIds"] = "test";
codeDocument.Items["NewLineString"] = "\r\n";
@ -208,11 +211,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
.Select(assemblyPath => MetadataReference.CreateFromFile(assemblyPath))
.ToList<MetadataReference>();
var syntaxTree = CreateTagHelperSyntaxTree();
var syntaxTree = CreateTagHelperSyntaxTree();
var compilation = CSharpCompilation.Create("Microsoft.AspNetCore.Mvc.Razor", syntaxTree, references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var stream = new MemoryStream();
var compilationResult = compilation.Emit(stream, options: new EmitOptions() );
var compilationResult = compilation.Emit(stream, options: new EmitOptions());
stream.Position = 0;
Assert.True(compilationResult.Success);
@ -237,37 +240,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
return new SyntaxTree[] { CSharpSyntaxTree.ParseText(text) };
}
private static RazorProject GetRazorProject(IFileProvider fileProvider)
{
var razorProject = new Mock<RazorProject>();
return razorProject.Object;
}
}
public class DefaultRazorProjectItem : RazorProjectItem
{
public DefaultRazorProjectItem(IFileInfo fileInfo, string basePath, string path)
{
FileInfo = fileInfo;
BasePath = basePath;
Path = path;
}
public IFileInfo FileInfo { get; }
public override string BasePath { get; }
public override string Path { get; }
public override string PhysicalPath { get; }
public override bool Exists => FileInfo.Exists;
public override Stream Read()
{
return FileInfo.CreateReadStream();
}
}
}

View File

@ -1,186 +0,0 @@
// 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.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
public class TestFileChangeToken : IChangeToken
{
public bool HasChanged => throw new NotImplementedException();
public bool ActiveChangeCallbacks => throw new NotImplementedException();
public IDisposable RegisterChangeCallback(Action<object> callback, object state)
{
throw new NotImplementedException();
}
}
public class TestFileInfo : IFileInfo
{
private string _content;
public bool IsDirectory { get; } = false;
public DateTimeOffset LastModified { get; set; }
public long Length { get; set; }
public string Name { get; set; }
public string PhysicalPath { get; set; }
public string Content
{
get { return _content; }
set
{
_content = value;
Length = Encoding.UTF8.GetByteCount(Content);
}
}
public bool Exists
{
get { return true; }
}
public Stream CreateReadStream()
{
var bytes = Encoding.UTF8.GetBytes(Content);
return new MemoryStream(bytes);
}
}
public class TestFileProvider : IFileProvider
{
private readonly Dictionary<string, IFileInfo> _lookup =
new Dictionary<string, IFileInfo>(StringComparer.Ordinal);
private readonly Dictionary<string, IDirectoryContents> _directoryContentsLookup =
new Dictionary<string, IDirectoryContents>();
private readonly Dictionary<string, TestFileChangeToken> _fileTriggers =
new Dictionary<string, TestFileChangeToken>(StringComparer.Ordinal);
public virtual IDirectoryContents GetDirectoryContents(string subpath)
{
if (_directoryContentsLookup.TryGetValue(subpath, out var value))
{
return value;
}
return new NotFoundDirectoryContents();
}
public TestFileInfo AddFile(string path, string contents)
{
var fileInfo = new TestFileInfo
{
Content = contents,
PhysicalPath = path,
Name = Path.GetFileName(path),
LastModified = DateTime.UtcNow,
};
AddFile(path, fileInfo);
return fileInfo;
}
public void AddFile(string path, IFileInfo contents)
{
_lookup[path] = contents;
}
public void DeleteFile(string path)
{
_lookup.Remove(path);
}
public virtual IFileInfo GetFileInfo(string subpath)
{
if (_lookup.ContainsKey(subpath))
{
return _lookup[subpath];
}
else
{
return new NotFoundFileInfo();
}
}
public virtual IChangeToken Watch(string filter)
{
TestFileChangeToken changeToken;
if (!_fileTriggers.TryGetValue(filter, out changeToken) || changeToken.HasChanged)
{
changeToken = new TestFileChangeToken();
_fileTriggers[filter] = changeToken;
}
return changeToken;
}
private class NotFoundFileInfo : IFileInfo
{
public bool Exists
{
get
{
return false;
}
}
public bool IsDirectory
{
get
{
throw new NotImplementedException();
}
}
public DateTimeOffset LastModified
{
get
{
throw new NotImplementedException();
}
}
public long Length
{
get
{
throw new NotImplementedException();
}
}
public string Name
{
get
{
throw new NotImplementedException();
}
}
public string PhysicalPath
{
get
{
throw new NotImplementedException();
}
}
public Stream CreateReadStream()
{
throw new NotImplementedException();
}
}
}
}

View File

@ -0,0 +1,39 @@
// 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.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Evolution
{
public class TestRazorProject : RazorProject
{
private readonly Dictionary<string, RazorProjectItem> _lookup;
public TestRazorProject()
: this(new RazorProjectItem[0])
{
}
public TestRazorProject(IList<RazorProjectItem> items)
{
_lookup = items.ToDictionary(item => item.Path);
}
public override IEnumerable<RazorProjectItem> EnumerateItems(string basePath)
{
throw new NotImplementedException();
}
public override RazorProjectItem GetItem(string path)
{
if (!_lookup.TryGetValue(path, out var value))
{
value = new NotFoundProjectItem("", path);
}
return value;
}
}
}

View File

@ -0,0 +1,33 @@
// 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 System.Text;
namespace Microsoft.AspNetCore.Razor.Evolution
{
public class TestRazorProjectItem : RazorProjectItem
{
public TestRazorProjectItem(
string path,
string physicalPath = null,
string basePath = "/")
{
Path = path;
PhysicalPath = physicalPath;
BasePath = basePath;
}
public override string BasePath { get; }
public override string Path { get; }
public override string PhysicalPath { get; }
public override bool Exists => true;
public string Content { get; set; } = "Default content";
public override Stream Read() => new MemoryStream(Encoding.UTF8.GetBytes(Content));
}
}