提交 dafc24c2 编写于 作者: M Matt Warren

add shared metadata tests for msbuild workspace

上级 6f7d7e63
......@@ -298,6 +298,9 @@
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TestFiles\CSharpProject_CSharpProject_ExternAlias2.csproj" />
</ItemGroup>
<ImportGroup Label="Targets">
<Import Project="..\..\Tools\Microsoft.CodeAnalysis.Toolset.Open\Targets\VSL.Imports.targets" />
<Import Project="..\..\..\build\VSL.Imports.Closed.targets" />
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{74404B73-C865-49CF-A586-C34759F2C95B}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>bug13338</RootNamespace>
<AssemblyName>bug13338</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System">
<Aliases>Sys1, Sys3</Aliases>
</Reference>
<Reference Include="System.Xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
......@@ -77,6 +77,99 @@ public void TestOpenSolution_MultiProjectSolution()
Assert.Equal(5, compReferences.Count);
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void Test_SharedMetadataReferences()
{
CreateFiles(GetMultiProjectSolutionFiles());
var sol = MSBuildWorkspace.Create().OpenSolutionAsync(GetSolutionFileName("TestSolution.sln")).Result;
var p0 = sol.Projects.ElementAt(0);
var p1 = sol.Projects.ElementAt(1);
Assert.NotSame(p0, p1);
var p0mscorlib = GetMetadataReference(p0, "mscorlib");
var p1mscorlib = GetMetadataReference(p1, "mscorlib");
Assert.NotNull(p0mscorlib);
Assert.NotNull(p1mscorlib);
// metadata references to mscorlib in both projects are the same
Assert.Same(p0mscorlib, p1mscorlib);
}
private static MetadataReference GetMetadataReference(Project project, string name)
{
return project.MetadataReferences.OfType<PortableExecutableReference>().SingleOrDefault(mr => mr.FilePath.Contains(name));
}
private static MetadataReference GetMetadataReferenceByAlias(Project project, string aliasName)
{
return project.MetadataReferences.OfType<PortableExecutableReference>().SingleOrDefault(mr =>
!mr.Properties.Aliases.IsDefault && mr.Properties.Aliases.Contains(aliasName));
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace), WorkItem(546171, "DevDiv")]
public void Test_SharedMetadataReferencesWithAliases()
{
var projPath1 = @"CSharpProject\CSharpProject_ExternAlias.csproj";
var projPath2 = @"CSharpProject\CSharpProject_ExternAlias2.csproj";
var files = new FileSet(new Dictionary<string, object>
{
{ projPath1, GetResourceText("CSharpProject_CSharpProject_ExternAlias.csproj") },
{ projPath2, GetResourceText("CSharpProject_CSharpProject_ExternAlias2.csproj") },
{ @"CSharpProject\CSharpExternAlias.cs", GetResourceText("CSharpProject_CSharpExternAlias.cs") },
});
CreateFiles(files);
var fullPath1 = Path.Combine(this.SolutionDirectory.Path, projPath1);
var fullPath2 = Path.Combine(this.SolutionDirectory.Path, projPath2);
using (var ws = MSBuildWorkspace.Create())
{
var proj1 = ws.OpenProjectAsync(fullPath1).Result;
var proj2 = ws.OpenProjectAsync(fullPath2).Result;
var p1Sys1 = GetMetadataReferenceByAlias(proj1, "Sys1");
var p1Sys2 = GetMetadataReferenceByAlias(proj1, "Sys2");
var p2Sys1 = GetMetadataReferenceByAlias(proj2, "Sys1");
var p2Sys3 = GetMetadataReferenceByAlias(proj2, "Sys3");
Assert.NotNull(p1Sys1);
Assert.NotNull(p1Sys2);
Assert.NotNull(p2Sys1);
Assert.NotNull(p2Sys3);
// same filepath but different alias so they are not the same instance
Assert.NotSame(p1Sys1, p1Sys2);
Assert.NotSame(p2Sys1, p2Sys3);
// same filepath and alias so they are the same instance
Assert.Same(p1Sys1, p2Sys1);
var mdp1Sys1 = GetMetadata(p1Sys1);
var mdp1Sys2 = GetMetadata(p1Sys2);
var mdp2Sys1 = GetMetadata(p2Sys1);
var mdp2Sys3 = GetMetadata(p2Sys1);
Assert.NotNull(mdp1Sys1);
Assert.NotNull(mdp1Sys2);
Assert.NotNull(mdp2Sys1);
Assert.NotNull(mdp2Sys3);
// all references to System.dll share the same metadata bytes
Assert.Same(mdp1Sys1, mdp1Sys2);
Assert.Same(mdp1Sys1, mdp2Sys1);
Assert.Same(mdp1Sys1, mdp2Sys3);
}
}
private Metadata GetMetadata(MetadataReference mref)
{
var fnGetMetadata = mref.GetType().GetMethod("GetMetadata", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
return fnGetMetadata?.Invoke(mref, null) as Metadata;
}
[WorkItem(552981, "DevDiv")]
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void TestOpenSolution_DuplicateProjectGuids()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册