提交 3b1e9095 编写于 作者: T Tomas Matousek

Remove dependency on mscorlib from portable C# script tests

上级 c257c2df
......@@ -229,7 +229,7 @@ public class TestAnalyzer : DiagnosticAnalyzer
new SyntaxTree[] { CSharp.SyntaxFactory.ParseSyntaxTree(analyzerSource) },
new MetadataReference[]
{
SystemRuntimeNetstandard13FacadeRef.Value,
TestReferences.NetStandard13.SystemRuntime,
MetadataReference.CreateFromFile(immutable.Path),
MetadataReference.CreateFromFile(analyzer.Path)
},
......
......@@ -3,30 +3,37 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.CodeAnalysis.Scripting.Test;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using TestBase = PortableTestUtils::Roslyn.Test.Utilities.TestBase;
using AssertEx = PortableTestUtils::Roslyn.Test.Utilities.AssertEx;
using TestBase = PortableTestUtils::Roslyn.Test.Utilities.TestBase;
using WorkItemAttribute = PortableTestUtils::Roslyn.Test.Utilities.WorkItemAttribute;
namespace Microsoft.CodeAnalysis.CSharp.Scripting.Test
{
using static TestCompilationFactory;
using DiagnosticExtensions = PortableTestUtils::Microsoft.CodeAnalysis.DiagnosticExtensions;
using TestReferences = PortableTestUtils::TestReferences;
public class InteractiveSessionTests : TestBase
{
private static readonly CSharpCompilationOptions s_signedDll =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, cryptoPublicKey: TestResources.TestKeys.PublicKey_ce65828c82a341f2);
private static readonly CSharpCompilationOptions s_signedDll2 =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, cryptoPublicKey: TestResources.TestKeys.PublicKey_ce65828c82a341f2);
[Fact]
public async Task CompilationChain_GlobalImportsRebinding()
{
......@@ -362,8 +369,11 @@ public void References_Versioning_StrongNames2()
[Fact]
public void References_Versioning_WeakNames1()
{
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var result = CSharpScript.EvaluateAsync($@"
#r ""{c1.Path}""
......@@ -378,8 +388,11 @@ public void References_Versioning_WeakNames1()
[Fact]
public void References_Versioning_WeakNames2()
{
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var result = CSharpScript.Create($@"
#r ""{c1.Path}""
......@@ -395,8 +408,11 @@ public void References_Versioning_WeakNames2()
[Fact]
public void References_Versioning_WeakNames3()
{
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", assemblyName: "C").EmitToArray());
var c1 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var c2 = Temp.CreateFile(extension: ".dll").WriteAllBytes(
CreateCSharpCompilation(@"[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")] public class C {}", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, assemblyName: "C").EmitToArray());
var script0 = CSharpScript.Create($@"
#r ""{c1.Path}""
......@@ -545,7 +561,7 @@ public async Task MissingRefrencesAutoResolution()
[Fact]
public void HostObjectInInMemoryAssembly()
{
var lib = CreateCSharpCompilationWithMscorlib("public class C { public int X = 1, Y = 2; }", "HostLib");
var lib = CreateCSharpCompilation("public class C { public int X = 1, Y = 2; }", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, "HostLib");
var libImage = lib.EmitToArray();
var libRef = MetadataImageReference.CreateFromImage(libImage);
......@@ -567,5 +583,541 @@ public void HostObjectInInMemoryAssembly()
Assert.Equal(3, result);
}
}
[Fact]
public async Task SharedLibCopy_Identical_Weak()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib2Name);
var libBaseImage = libBase.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"var l2 = new Lib2();");
var s4 = await s3.ContinueWithAsync($@"l2.libBase.X");
var c4 = s4.Script.GetCompilation();
c4.VerifyAssemblyAliases(
lib2Name,
lib1Name,
"mscorlib",
libBaseName + ": <implicit>,global");
var libBaseRefAndSymbol = c4.GetBoundReferenceManager().GetReferencedAssemblies().ToArray()[3];
Assert.Equal(fileBase1.Path, ((PortableExecutableReference)libBaseRefAndSymbol.Key).FilePath);
}
[Fact]
public async Task SharedLibCopy_Identical_Strong()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib2Name);
var libBaseImage = libBase.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"var l2 = new Lib2();");
var s4 = await s3.ContinueWithAsync($@"l2.libBase.X");
var c4 = s4.Script.GetCompilation();
c4.VerifyAssemblyAliases(
lib2Name,
lib1Name,
"mscorlib",
libBaseName + ": <implicit>,global");
var libBaseRefAndSymbol = c4.GetBoundReferenceManager().GetReferencedAssemblies().ToArray()[3];
Assert.Equal(fileBase1.Path, ((PortableExecutableReference)libBaseRefAndSymbol.Key).FilePath);
}
[Fact]
public async Task SharedLibCopy_SameVersion_Weak_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var libBase2 = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"var l2 = new Lib2();");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_SameVersion_Strong_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_SameVersion_StrongWeak_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_SameVersion_StrongDifferentPKT_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll2);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_DifferentVersion_Weak()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase2.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1().libBase.X;");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"var l2 = new Lib2().libBase.X;");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_DifferentVersion_Strong()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase2.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
Assert.Equal(1, s1.ReturnValue);
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"new Lib2().libBase.X");
Assert.Equal(2, s3.ReturnValue);
}
[Fact, WorkItem(6457, "https://github.com/dotnet/roslyn/issues/6457")]
public async Task MissingReferencesReuse()
{
var source = @"
public class C
{
public System.Diagnostics.Process P;
}
";
var lib = CSharpCompilation.Create(
"Lib",
new[] { SyntaxFactory.ParseSyntaxTree(source) },
new[] { TestReferences.NetFx.v4_0_30319.mscorlib, TestReferences.NetFx.v4_0_30319.System },
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var libFile = Temp.CreateFile("lib").WriteAllBytes(lib.EmitToArray());
var s0 = await CSharpScript.RunAsync("C c;", ScriptOptions.Default.WithReferences(libFile.Path));
var s1 = await s0.ContinueWithAsync("c = new C()");
}
}
}
......@@ -577,16 +577,16 @@ public void ReferenceSearchPaths1()
");
var dir1 = Temp.CreateDirectory();
dir1.CreateFile("1.dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib("public class C1 {}", "1").EmitToArray());
dir1.CreateFile("1.dll").WriteAllBytes(CreateCSharpCompilationWithCorlib("public class C1 {}", "1").EmitToArray());
var dir2 = Temp.CreateDirectory();
dir2.CreateFile("2.dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib("public class C2 {}", "2").EmitToArray());
dir2.CreateFile("2.dll").WriteAllBytes(CreateCSharpCompilationWithCorlib("public class C2 {}", "2").EmitToArray());
var dir3 = Temp.CreateDirectory();
dir3.CreateFile("3.dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib("public class C3 {}", "3").EmitToArray());
dir3.CreateFile("3.dll").WriteAllBytes(CreateCSharpCompilationWithCorlib("public class C3 {}", "3").EmitToArray());
var dir4 = Temp.CreateDirectory();
dir4.CreateFile("4.dll").WriteAllBytes(CreateCSharpCompilationWithMscorlib("public class C4 {}", "4").EmitToArray());
dir4.CreateFile("4.dll").WriteAllBytes(CreateCSharpCompilationWithCorlib("public class C4 {}", "4").EmitToArray());
var runner = CreateRunner(new[] { "/r:4.dll", $"/lib:{dir1.Path}", $"/libpath:{dir2.Path}", $"/libpaths:{dir3.Path};{dir4.Path}", main.Path });
......
......@@ -30,12 +30,6 @@ public class HostModel
public class InteractiveSessionTests : TestBase
{
private static readonly CSharpCompilationOptions s_signedDll =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, cryptoPublicKey: TestResources.TestKeys.PublicKey_ce65828c82a341f2);
private static readonly CSharpCompilationOptions s_signedDll2 =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, cryptoPublicKey: TestResources.TestKeys.PublicKey_ce65828c82a341f2);
internal static readonly Assembly HostAssembly = typeof(InteractiveSessionTests).GetTypeInfo().Assembly;
#region Namespaces, Types
......@@ -1134,579 +1128,69 @@ public void AwaitChain2()
[Fact]
public void ReferenceDirective_FileWithDependencies()
{
string file1 = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
string file2 = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSInterfaces01).Path;
// ICSPropImpl in CSClasses01.dll implements ICSProp in CSInterfces01.dll.
object result = CSharpScript.EvaluateAsync(@"
#r """ + file1 + @"""
#r """ + file2 + @"""
new Metadata.ICSPropImpl()
").Result;
Assert.NotNull(result);
}
[Fact]
public void ReferenceDirective_RelativeToBaseParent()
{
string path = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
string fileName = Path.GetFileName(path);
string dir = Path.Combine(Path.GetDirectoryName(path), "subdir");
var script = CSharpScript.Create($@"#r ""..\{fileName}""",
ScriptOptions.Default.WithFilePath(Path.Combine(dir, "a.csx")));
script.GetCompilation().VerifyDiagnostics();
}
[Fact]
public void ReferenceDirective_RelativeToBaseRoot()
{
string path = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
string root = Path.GetPathRoot(path);
string unrooted = path.Substring(root.Length);
string dir = Path.Combine(root, "foo", "bar", "baz");
var script = CSharpScript.Create($@"#r ""\{unrooted}""",
ScriptOptions.Default.WithFilePath(Path.Combine(dir, "a.csx")));
script.GetCompilation().VerifyDiagnostics();
}
[Fact, WorkItem(6457, "https://github.com/dotnet/roslyn/issues/6457")]
public async Task MissingReferencesReuse()
{
var source = @"
public class C
{
public System.Diagnostics.Process P;
}
";
var lib = CSharpCompilation.Create(
"Lib",
new[] { SyntaxFactory.ParseSyntaxTree(source) },
new[] { TestReferences.NetFx.v4_0_30319.mscorlib, TestReferences.NetFx.v4_0_30319.System },
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var libFile = Temp.CreateFile("lib").WriteAllBytes(lib.EmitToArray());
var s0 = await CSharpScript.RunAsync("C c;", ScriptOptions.Default.WithReferences(libFile.Path));
var s1 = await s0.ContinueWithAsync("c = new C()");
}
[Fact]
public async Task SharedLibCopy_Identical_Weak()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib2Name);
var libBaseImage = libBase.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"var l2 = new Lib2();");
var s4 = await s3.ContinueWithAsync($@"l2.libBase.X");
var c4 = s4.Script.GetCompilation();
c4.VerifyAssemblyAliases(
lib2Name,
lib1Name,
"mscorlib",
libBaseName + ": <implicit>,global");
var libBaseRefAndSymbol = c4.GetBoundReferenceManager().GetReferencedAssemblies().ToArray()[3];
Assert.Equal(fileBase1.Path, ((PortableExecutableReference)libBaseRefAndSymbol.Key).FilePath);
}
[Fact]
public async Task SharedLibCopy_Identical_Strong()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase.ToMetadataReference() }, lib2Name);
var libBaseImage = libBase.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBaseImage);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"var l2 = new Lib2();");
var s4 = await s3.ContinueWithAsync($@"l2.libBase.X");
var c4 = s4.Script.GetCompilation();
c4.VerifyAssemblyAliases(
lib2Name,
lib1Name,
"mscorlib",
libBaseName + ": <implicit>,global");
var libBaseRefAndSymbol = c4.GetBoundReferenceManager().GetReferencedAssemblies().ToArray()[3];
Assert.Equal(fileBase1.Path, ((PortableExecutableReference)libBaseRefAndSymbol.Key).FilePath);
}
[Fact]
public async Task SharedLibCopy_SameVersion_Weak_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var libBase2 = CreateCSharpCompilation(@"
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1();");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"var l2 = new Lib2();");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_SameVersion_Strong_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var file1 = Temp.CreateFile();
var file2 = Temp.CreateFile();
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
var lib1 = CreateCSharpCompilationWithCorlib(@"
public interface I
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
}
[Fact]
public async Task SharedLibCopy_SameVersion_StrongWeak_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
int F();
}");
Assert.True(exceptionSeen);
}
lib1.Emit(file1.Path);
[Fact]
public async Task SharedLibCopy_SameVersion_StrongDifferentPKT_DifferentContent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
var lib2 = CreateCSharpCompilation(@"
public class C : I
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
public int F() => 1;
}", new MetadataReference[] { TestReferences.NetStandard13.SystemRuntime, lib1.ToMetadataReference() });
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll2);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
lib2.Emit(file2.Path);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"new Lib2().libBase.X");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
Assert.True(exceptionSeen);
object result = CSharpScript.EvaluateAsync($@"
#r ""{file1.Path}""
#r ""{file2.Path}""
new C()
").Result;
Assert.NotNull(result);
}
[Fact]
public async Task SharedLibCopy_DifferentVersion_Weak()
public void ReferenceDirective_RelativeToBaseParent()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase2.ToMetadataReference() }, lib2Name);
var file = Temp.CreateFile();
var lib = CreateCSharpCompilationWithCorlib("public class C {}");
lib.Emit(file.Path);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
string dir = Path.Combine(Path.GetDirectoryName(file.Path), "subdir");
string libFileName = Path.GetFileName(file.Path);
string scriptPath = Path.Combine(dir, "a.csx");
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"var l1 = new Lib1().libBase.X;");
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
bool exceptionSeen = false;
try
{
await s2.ContinueWithAsync($@"var l2 = new Lib2().libBase.X;");
}
catch (FileLoadException fileLoadEx) when (fileLoadEx.InnerException is InteractiveAssemblyLoaderException)
{
exceptionSeen = true;
}
var script = CSharpScript.Create(
$@"#r ""..\{libFileName}""",
ScriptOptions.Default.WithFilePath(scriptPath));
Assert.True(exceptionSeen);
script.GetCompilation().VerifyDiagnostics();
}
[Fact]
public async Task SharedLibCopy_DifferentVersion_Strong()
public void ReferenceDirective_RelativeToBaseRoot()
{
string libBaseName = "LibBase_" + Guid.NewGuid();
string lib1Name = "Lib1_" + Guid.NewGuid();
string lib2Name = "Lib2_" + Guid.NewGuid();
var libBase1 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""1.0.0.0"")]
public class LibBase
{
public readonly int X = 1;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
var file = Temp.CreateFile();
var lib = CreateCSharpCompilationWithCorlib("public class C {}");
lib.Emit(file.Path);
var libBase2 = CreateCSharpCompilation(@"
[assembly: System.Reflection.AssemblyVersion(""2.0.0.0"")]
public class LibBase
{
public readonly int X = 2;
}
", new[] { TestReferences.NetFx.v4_0_30319.mscorlib }, libBaseName, s_signedDll);
string root = Path.GetPathRoot(file.Path);
string unrooted = file.Path.Substring(root.Length);
var lib1 = CreateCSharpCompilation(@"
public class Lib1
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase1.ToMetadataReference() }, lib1Name);
var lib2 = CreateCSharpCompilation(@"
public class Lib2
{
public LibBase libBase = new LibBase();
}
", new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libBase2.ToMetadataReference() }, lib2Name);
var libBase1Image = libBase1.EmitToArray();
var libBase2Image = libBase2.EmitToArray();
var lib1Image = lib1.EmitToArray();
var lib2Image = lib2.EmitToArray();
var root = Temp.CreateDirectory();
var dir1 = root.CreateDirectory("1");
var file1 = dir1.CreateFile(lib1Name + ".dll").WriteAllBytes(lib1Image);
var fileBase1 = dir1.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase1Image);
string dir = Path.Combine(root, "foo", "bar", "baz");
string scriptPath = Path.Combine(dir, "a.csx");
var dir2 = root.CreateDirectory("2");
var file2 = dir2.CreateFile(lib2Name + ".dll").WriteAllBytes(lib2Image);
var fileBase2 = dir2.CreateFile(libBaseName + ".dll").WriteAllBytes(libBase2Image);
var script = CSharpScript.Create(
$@"#r ""\{unrooted}""",
ScriptOptions.Default.WithFilePath(scriptPath));
var s0 = await CSharpScript.RunAsync($@"#r ""{file1.Path}""");
var s1 = await s0.ContinueWithAsync($@"new Lib1().libBase.X");
Assert.Equal(1, s1.ReturnValue);
var s2 = await s1.ContinueWithAsync($@"#r ""{file2.Path}""");
var s3 = await s2.ContinueWithAsync($@"new Lib2().libBase.X");
Assert.Equal(2, s3.ReturnValue);
script.GetCompilation().VerifyDiagnostics();
}
[Fact]
......@@ -1715,13 +1199,13 @@ public void ExtensionPriority1()
string mainName = "Main_" + Guid.NewGuid();
string libName = "Lib_" + Guid.NewGuid();
var libExe = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""exe""; }", libName);
var libDll = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""dll""; }", libName);
var libWinmd = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""winmd""; }", libName);
var libExe = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""exe""; }", libName);
var libDll = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""dll""; }", libName);
var libWinmd = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""winmd""; }", libName);
var main = CreateCSharpCompilation(
@"public static class M { public static readonly C X = new C(); }",
new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libExe.ToMetadataReference() },
new MetadataReference[] { TestReferences.NetStandard13.SystemRuntime, libExe.ToMetadataReference() },
mainName);
var exeImage = libExe.EmitToArray();
......@@ -1745,13 +1229,13 @@ public void ExtensionPriority2()
string mainName = "Main_" + Guid.NewGuid();
string libName = "Lib_" + Guid.NewGuid();
var libExe = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""exe""; }", libName);
var libDll = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""dll""; }", libName);
var libWinmd = TestCompilationFactory.CreateCSharpCompilationWithMscorlib(@"public class C { public string F = ""winmd""; }", libName);
var libExe = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""exe""; }", libName);
var libDll = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""dll""; }", libName);
var libWinmd = CreateCSharpCompilationWithCorlib(@"public class C { public string F = ""winmd""; }", libName);
var main = CreateCSharpCompilation(
@"public static class M { public static readonly C X = new C(); }",
new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, libExe.ToMetadataReference() },
new MetadataReference[] { TestReferences.NetStandard13.SystemRuntime, libExe.ToMetadataReference() },
mainName);
var exeImage = libExe.EmitToArray();
......@@ -1779,7 +1263,7 @@ public class D { }
public class E { }
";
var libRef = CreateCSharpCompilationWithMscorlib(source, "lib").EmitToImageReference();
var libRef = CreateCSharpCompilationWithCorlib(source, "lib").EmitToImageReference();
var script = CSharpScript.Create(@"new C()",
ScriptOptions.Default.WithReferences(libRef.WithAliases(new[] { "Hidden" })).WithImports("Hidden::N"));
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.VisualBasic;
......@@ -9,28 +10,28 @@ internal static class TestCompilationFactory
{
// TODO: we need to clean up and refactor CreateCompilationWithMscorlib in compiler tests
// so that it can be used in portable tests.
internal static Compilation CreateCSharpCompilationWithMscorlib(string source, string assemblyName)
internal static Compilation CreateCSharpCompilationWithCorlib(string source, string assemblyName = null)
{
return CSharpCompilation.Create(
assemblyName,
assemblyName ?? Guid.NewGuid().ToString(),
new[] { CSharp.SyntaxFactory.ParseSyntaxTree(source) },
new[] { TestReferences.NetFx.v4_0_30319.mscorlib },
new[] { TestReferences.NetStandard13.SystemRuntime },
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}
internal static Compilation CreateVisualBasicCompilationWithMscorlib(string source, string assemblyName)
internal static Compilation CreateVisualBasicCompilationWithCorlib(string source, string assemblyName = null)
{
return VisualBasicCompilation.Create(
assemblyName,
assemblyName ?? Guid.NewGuid().ToString(),
new[] { VisualBasic.SyntaxFactory.ParseSyntaxTree(source) },
new[] { TestReferences.NetFx.v4_0_30319.mscorlib },
new[] { TestReferences.NetStandard13.SystemRuntime },
new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}
internal static Compilation CreateCSharpCompilation(string source, MetadataReference[] references, string assemblyName, CSharpCompilationOptions options = null)
internal static Compilation CreateCSharpCompilation(string source, MetadataReference[] references, string assemblyName = null, CSharpCompilationOptions options = null)
{
return CSharpCompilation.Create(
assemblyName,
assemblyName ?? Guid.NewGuid().ToString(),
new[] { CSharp.SyntaxFactory.ParseSyntaxTree(source) },
references,
options ?? new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
......
......@@ -80,7 +80,7 @@ Type ""#help"" for more information.
<Fact()>
Public Sub TestReferenceDirective()
Dim file1 = Temp.CreateFile("1.dll").WriteAllBytes(TestCompilationFactory.CreateVisualBasicCompilationWithMscorlib("
Dim file1 = Temp.CreateFile("1.dll").WriteAllBytes(TestCompilationFactory.CreateVisualBasicCompilationWithCorlib("
public Class C1
Public Function Foo() As String
Return ""Bar""
......
......@@ -120,7 +120,7 @@ public class TestAnalyzer : DiagnosticAnalyzer
new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(analyzerSource) },
new MetadataReference[]
{
TestBase.SystemRuntimeNetstandard13FacadeRef.Value,
TestReferences.NetStandard13.SystemRuntime,
MetadataReference.CreateFromFile(immutable.Path),
MetadataReference.CreateFromFile(analyzer.Path)
},
......
......@@ -534,6 +534,23 @@ public static PortableExecutableReference mscorlib
}
}
public static class NetStandard13
{
private static PortableExecutableReference s_systemRuntime;
public static PortableExecutableReference SystemRuntime
{
get
{
if (s_systemRuntime == null)
{
s_systemRuntime = AssemblyMetadata.CreateFromImage(TestResources.NetFX.ReferenceAssemblies_netstandard1_3.System_Runtime).GetReference(display: @"System.Runtime.dll (netstandard13 ref)");
}
return s_systemRuntime;
}
}
}
public static class DiagnosticTests
{
public static class ErrTestLib01
......
......@@ -526,11 +526,6 @@ public static MetadataReference SystemRuntimePP7Ref
}
}
public static Lazy<MetadataReference> SystemRuntimeNetstandard13FacadeRef { get; } =
new Lazy<MetadataReference>(() => AssemblyMetadata.CreateFromImage(
TestResources.NetFX.ReferenceAssemblies_netstandard1_3.System_Runtime)
.GetReference(display: "System.Runtime.dll"));
private static MetadataReference s_FSharpTestLibraryRef;
public static MetadataReference FSharpTestLibraryRef
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册