This commit is contained in:
Prafull Bhosale 2016-10-04 13:16:41 -07:00
Родитель 8230ebb97b
Коммит 9220235972
15 изменённых файлов: 314 добавлений и 57 удалений

Просмотреть файл

@ -3,7 +3,7 @@
<packageSources>
<add key="AspNetCore" value="https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
<add key="BuildTools" value="https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json" />
<add key="BuildTools" value="https://dotnet.myget.org/F/msbuild/api/v3/index.json" />
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="cli-deps" value="https://dotnet.myget.org/F/cli-deps/api/v3/index.json" />
<add key="dotnet-cli" value="https://dotnet.myget.org/F/dotnet-cli/api/v3/index.json" />

Просмотреть файл

@ -14,7 +14,7 @@ namespace E2E_Test
{
}
[Fact]
[Fact (Skip = "Disabling E2E test")]
public void TestAreaGenerator()
{
var args = new string[]

Просмотреть файл

@ -0,0 +1,49 @@
// 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 Microsoft.VisualStudio.Web.CodeGeneration.DotNet;
using Microsoft.VisualStudio.Web.CodeGeneration.MsBuild;
using Microsoft.VisualStudio.Web.CodeGeneration.ProjectInfo;
using NuGet.Frameworks;
using Xunit;
namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
{
public class EFTestFixture
{
NuGetFramework framework = FrameworkConstants.CommonFrameworks.NetStandard16;
protected string _projectPath;
public EFTestFixture()
{
_projectPath = Path.GetFullPath(@"../MsBuildTestApps/MsBuildTestAppSolution/ModelTypesTestLibrary/ModelTypesTestLibrary.csproj");
RunBuild();
}
public void RunBuild()
{
ScaffoldingBuildProcessor processor = new ScaffoldingBuildProcessor();
MsBuilder<ScaffoldingBuildProcessor> builder = new MsBuilder<ScaffoldingBuildProcessor>(_projectPath, processor);
builder.RunMsBuild(FrameworkConstants.CommonFrameworks.NetCoreApp10);
ProjectInfo = new CodeGeneration.ProjectInfo.ProjectInfoContainer()
{
ProjectContext = processor.CreateMsBuildProjectContext(),
ProjectDependencyProvider = processor.CreateDependencyProvider()
};
Workspace = new RoslynWorkspace(ProjectInfo.ProjectContext, ProjectInfo.ProjectDependencyProvider);
}
public ProjectInfoContainer ProjectInfo { get; private set; }
public RoslynWorkspace Workspace { get; private set; }
}
[CollectionDefinition("CodeGeneration.EF")]
public class CodeGenerationSourcesCollection : ICollectionFixture<EFTestFixture>
{
}
}

Просмотреть файл

@ -14,6 +14,7 @@ using Xunit;
namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
{
[Collection("CodeGeneration.EF")]
public class EntityFrameworkServicesTests
{
private IApplicationInfo _appInfo;
@ -25,9 +26,11 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
private CodeAnalysis.Workspace _workspace;
private ILogger _logger;
private IProjectDependencyProvider _projectDependencyProvider;
private EFTestFixture _testFixture;
public EntityFrameworkServicesTests()
public EntityFrameworkServicesTests(EFTestFixture testFixture)
{
_testFixture = testFixture;
}
private EntityFrameworkServices GetEfServices(string path, string applicationName)
@ -37,8 +40,8 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
_packageInstaller = new Mock<IPackageInstaller>();
_serviceProvider = new Mock<IServiceProvider>();
_workspace = null;
_projectDependencyProvider = null;
_workspace = _testFixture.Workspace;
_projectDependencyProvider = _testFixture.ProjectInfo.ProjectDependencyProvider;
_loader = new TestAssemblyLoadContext(_projectDependencyProvider);
_modelTypesLocator = new ModelTypesLocator(_workspace);
var dbContextMock = new Mock<IDbContextEditorServices>();
@ -70,10 +73,10 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
}
[Fact(Skip ="Disable tests that need projectInfo")]
[Fact]
public async void TestGetModelMetadata_WithoutDbContext()
{
var appName = "ModelTypesLocatorTestClassLibrary";
var appName = "ModelTypesTestLibrary";
var path = Path.Combine(Directory.GetCurrentDirectory(), "..", "TestApps", appName);
var efServices = GetEfServices(path, appName);
@ -83,7 +86,7 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
Assert.Null(metadata.ModelMetadata.Navigations);
Assert.False(metadata.ModelMetadata.Properties.Any());
modelType = _modelTypesLocator.GetType("ModelTypesLocatorTestClassLibrary.Car").First();
modelType = _modelTypesLocator.GetType("ModelTypesTestLibrary.Car").First();
metadata = await efServices.GetModelMetadata(modelType);
Assert.Equal(ContextProcessingStatus.ContextAvailable, metadata.ContextProcessingStatus);
Assert.Null(metadata.ModelMetadata.Navigations);
@ -91,10 +94,10 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test
Assert.Equal(3, metadata.ModelMetadata.Properties.Length);
}
[Fact(Skip ="Disable tests that need projectInfo")]
[Fact(Skip ="Disable test because of https://github.com/dotnet/sdk/issues/200")]
public async void TestGetModelMetadata_WithDbContext()
{
var appName = "ModelTypesLocatorTestWebApp";
var appName = "ModelTypesTestLibrary";
var path = Path.Combine(Directory.GetCurrentDirectory(), "..", "TestApps", appName);
var efServices = GetEfServices(path, appName);

Просмотреть файл

@ -13,6 +13,7 @@
},
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",
"Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore": "1.0.0-*",
"Microsoft.VisualStudio.Web.CodeGeneration.MsBuild": "1.0.0-*",
"Microsoft.VisualStudio.Web.CodeGeneration.Test.Sources": {
"version": "1.0.0-*",
"type": "build"

Просмотреть файл

@ -16,13 +16,13 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test
//This is more of an integration test.
public class RazorTemplatingTests
{
[Fact(Skip = "Disable Test that requires dependencyProvider")]
[Fact]
public async void RunTemplateAsync_Generates_Text_For_Template_With_A_Model()
{
//Arrange
var templateContent = @"Hello @Model.Name";
var model = new SimpleModel() { Name = "World" };
var compilationService = GetCompilationService();
var compilationService = new TestCompilationService();
var templatingService = new RazorTemplating(compilationService);
//Act
@ -33,12 +33,12 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test
Assert.Equal("Hello World", result.GeneratedText);
}
[Fact(Skip = "Disable Test that requires dependencyProvider")]
[Fact]
public async void RunTemplateAsync_Returns_Error_For_Invalid_Template()
{
//Arrange
var templateContent = "@Invalid";
var compilationService = GetCompilationService();
var compilationService = new TestCompilationService();
var templatingService = new RazorTemplating(compilationService);
//Act
@ -52,44 +52,6 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test
result.ProcessingException.Message);
}
private ICompilationService GetCompilationService()
{
ProjectContext context = CreateProjectContext(null);
var applicationInfo = new ApplicationInfo("", context.ProjectDirectory);
ICodeGenAssemblyLoadContext loader = new DefaultAssemblyLoadContext();
IApplicationInfo _applicationInfo;
#if RELEASE
_applicationInfo = new ApplicationInfo("ModelTypesLocatorTestClassLibrary", Directory.GetCurrentDirectory(), "Release");
#else
_applicationInfo = new ApplicationInfo("ModelTypesLocatorTestClassLibrary", Directory.GetCurrentDirectory(), "Debug");
#endif
//ILibraryExporter libExporter = new LibraryExporter(context, _applicationInfo);
IProjectDependencyProvider projectDependencyProvider = null;
return new RoslynCompilationService(applicationInfo, loader, projectDependencyProvider);
}
private static ProjectContext CreateProjectContext(string projectPath)
{
#if NET451
projectPath = projectPath ?? Path.Combine("..", "..", "..", "..");
var framework = NuGet.Frameworks.FrameworkConstants.CommonFrameworks.Net451.GetShortFolderName();
#else
projectPath = projectPath ?? Directory.GetCurrentDirectory();
var framework = NuGet.Frameworks.FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName();
#endif
if (!projectPath.EndsWith(Microsoft.DotNet.ProjectModel.Project.FileName))
{
projectPath = Path.Combine(projectPath, Microsoft.DotNet.ProjectModel.Project.FileName);
}
if (!File.Exists(projectPath))
{
throw new InvalidOperationException($"{projectPath} does not exist.");
}
return ProjectContext.CreateContextForEachFramework(projectPath).FirstOrDefault(c => c.TargetFramework.GetShortFolderName() == framework);
}
}
//If i make this a private class inside the above class,

Просмотреть файл

@ -0,0 +1,106 @@
// 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.Linq;
using System.Reflection.PortableExecutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.VisualStudio.Web.CodeGeneration.DotNet;
using Microsoft.VisualStudio.Web.CodeGeneration.Templating.Compilation;
namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test
{
public class TestCompilationService : ICompilationService
{
public Compilation.CompilationResult Compile(string content)
{
var loader = new DefaultAssemblyLoadContext();
var projectContext = CreateProjectContext(null);
var libraryExporter = projectContext.CreateExporter("Debug");
var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(content) };
var exports = libraryExporter.GetAllExports();
var references = GetMetadataReferences(exports);
var assemblyName = Path.GetRandomFileName();
var compilation = CSharpCompilation.Create(assemblyName,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: syntaxTrees,
references: references);
var result = CommonUtilities.GetAssemblyFromCompilation(loader, compilation);
if (result.Success)
{
var type = result.Assembly.GetExportedTypes()
.First();
return Templating.Compilation.CompilationResult.Successful(string.Empty, type);
}
else
{
return Templating.Compilation.CompilationResult.Failed(content, result.ErrorMessages);
}
}
private List<MetadataReference> GetMetadataReferences(IEnumerable<LibraryExport> exports)
{
var references = new List<MetadataReference>();
foreach (var exp in exports)
{
foreach (var lib in exp.CompilationAssemblies)
{
references.Add(GetMetadataReference(lib.ResolvedPath));
}
}
return references;
}
private MetadataReference GetMetadataReference(string path)
{
try
{
using (var stream = File.OpenRead(path))
{
var moduleMetadata = ModuleMetadata.CreateFromStream(stream, PEStreamOptions.PrefetchMetadata);
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);
return assemblyMetadata.GetReference();
}
}
catch
{
return null;
}
}
private static ProjectContext CreateProjectContext(string projectPath)
{
#if NET451
projectPath = projectPath ?? Path.Combine("..", "..", "..", "..");
var framework = NuGet.Frameworks.FrameworkConstants.CommonFrameworks.Net451.GetShortFolderName();
#else
projectPath = projectPath ?? Directory.GetCurrentDirectory();
var framework = NuGet.Frameworks.FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName();
#endif
if (!projectPath.EndsWith(Microsoft.DotNet.ProjectModel.Project.FileName))
{
projectPath = Path.Combine(projectPath, Microsoft.DotNet.ProjectModel.Project.FileName);
}
if (!File.Exists(projectPath))
{
throw new InvalidOperationException($"{projectPath} does not exist.");
}
return ProjectContext.CreateContextForEachFramework(projectPath).FirstOrDefault(c => c.TargetFramework.GetShortFolderName() == framework);
}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
class Class1
{
public static void Main(string[] args) {
}
}
}

Просмотреть файл

@ -0,0 +1,27 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160928-1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!--<ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" />-->
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Просмотреть файл

@ -0,0 +1,24 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160928-1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Просмотреть файл

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary2
{
class Class1
{
public static void Main(string[] args) {
}
}
}

Просмотреть файл

@ -1,17 +1,46 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.25702.0
VisualStudioVersion = 15.0.25729.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelTypesTestLibrary", "ModelTypesTestLibrary\ModelTypesTestLibrary.csproj", "{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModelTypesTestLibrary", "ModelTypesTestLibrary\ModelTypesTestLibrary.csproj", "{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{B45587C0-D866-4B53-A241-E5F1B30B2DBC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibrary2", "ClassLibrary2\ClassLibrary2.csproj", "{679723D0-FCF3-49E6-9628-CAC64C138782}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|x86.ActiveCfg = Debug|x86
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|x86.Build.0 = Debug|x86
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|x86.ActiveCfg = Debug|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Debug|x86.Build.0 = Debug|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Release|Any CPU.Build.0 = Release|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Release|x86.ActiveCfg = Release|Any CPU
{7C316A17-A23C-4B2E-AE75-CBFC6D3EBDAD}.Release|x86.Build.0 = Release|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Debug|x86.ActiveCfg = Debug|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Debug|x86.Build.0 = Debug|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Release|Any CPU.Build.0 = Release|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Release|x86.ActiveCfg = Release|Any CPU
{B45587C0-D866-4B53-A241-E5F1B30B2DBC}.Release|x86.Build.0 = Release|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Debug|Any CPU.Build.0 = Debug|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Debug|x86.ActiveCfg = Debug|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Debug|x86.Build.0 = Debug|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Release|Any CPU.ActiveCfg = Release|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Release|Any CPU.Build.0 = Release|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Release|x86.ActiveCfg = Release|Any CPU
{679723D0-FCF3-49E6-9628-CAC64C138782}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace ModelTypesTestLibrary
{
public class Car
{
public int ID { get; set; }
public string Name { get; set; }
public int ManufacturerID { get; set; }
public Manufacturer Manufacturer { get; set; }
}
public class Manufacturer
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
}

Просмотреть файл

@ -18,8 +18,11 @@
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160929-1</Version>
<Version>1.0.0-alpha-20160928-1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Просмотреть файл

@ -0,0 +1,8 @@
using System;
namespace ModelTypesTestLibrary
{
public class ModelWithMatchingShortName {
}
}