Example #1
0
        public void DefaultUsings()
        {
            var source =
                @"
dynamic d = new ExpandoObject();
Process p = new Process();
Expression<Func<int>> e = () => 1;
var squares = from x in new[] { 1, 2, 3 } select x * x;
var sb = new StringBuilder();
var list = new List<int>();
var stream = new MemoryStream();
await Task.Delay(10);

Console.Write(""OK"");
";

            var cwd = Temp.CreateDirectory();

            cwd.CreateFile("a.csx").WriteAllText(source);

            var result = ProcessUtilities.Run(CsiPath, "a.csx", workingDirectory: cwd.Path);

            AssertEx.AssertEqualToleratingWhitespaceDifferences("OK", result.Output);
            Assert.False(result.ContainsErrors);
        }
        public async Task HostObjectBinding_DuplicateReferences()
        {
            var options = ScriptOptions.Default.
                          AddReferences(typeof(C).Assembly, typeof(C).Assembly);

            var s0 = await CSharpScript.RunAsync <int>("x", options, new C());

            var c0 = s0.Script.GetCompilation();

            // includes corlib, host type assembly by default:
            AssertEx.Equal(new[]
            {
                typeof(object).GetTypeInfo().Assembly.Location,
                typeof(C).Assembly.Location,
                typeof(C).Assembly.Location,
                typeof(C).Assembly.Location,
            }, c0.ExternalReferences.SelectAsArray(m => m.Display));

            Assert.Equal(1, s0.ReturnValue);

            var s1 = await s0.ContinueWithAsync($@"
#r ""{typeof(C).Assembly.Location}""
#r ""{typeof(C).Assembly.Location}""
x            
");

            Assert.Equal(1, s1.ReturnValue);
        }
Example #3
0
        public void CurrentWorkingDirectory_Change()
        {
            var dir = Temp.CreateDirectory();

            dir.CreateFile("a.csx").WriteAllText(@"int X = 1;");
            dir.CreateFile("C.dll").WriteAllBytes(TestResources.General.C1);

            var result = ProcessUtilities.Run(CsiPath, "", stdInput:
                                              $@"#load ""a.csx""
#r ""C.dll""
Directory.SetCurrentDirectory(@""{dir.Path}"")
#load ""a.csx""
#r ""C.dll""
X
new C()
Environment.Exit(0)
");

            AssertEx.AssertEqualToleratingWhitespaceDifferences($@"
Microsoft (R) Visual C# Interactive Compiler version {s_compilerVersion}
Copyright (C) Microsoft Corporation. All rights reserved.

Type ""#help"" for more information.
> > > > > > 1
> C {{ }}
> 
", result.Output);

            AssertEx.AssertEqualToleratingWhitespaceDifferences($@"
(1,7): error CS1504: Source file 'a.csx' could not be opened -- Could not find file.
(1,1): error CS0006: Metadata file 'C.dll' could not be found
", result.Errors);

            Assert.Equal(0, result.ExitCode);
        }
Example #4
0
        public void ReferenceSearchPaths_Sdk()
        {
            var cwd = Temp.CreateDirectory();

            cwd.CreateFile("a.csx").WriteAllText(@"Console.Write(typeof(DataSet).Name);");

            var result = ProcessUtilities.Run(CsiPath, "/r:System.Data.dll /u:System.Data;System a.csx", workingDirectory: cwd.Path);

            AssertEx.AssertEqualToleratingWhitespaceDifferences("DataSet", result.Output);
            Assert.False(result.ContainsErrors);
        }
Example #5
0
        public void CurrentWorkingDirectory1()
        {
            var dir = Temp.CreateDirectory();

            dir.CreateFile("a.csx").WriteAllText(@"Console.Write(Environment.CurrentDirectory + ';' + typeof(C).Name);");
            dir.CreateFile("C.dll").WriteAllBytes(TestResources.General.C1);

            var result = ProcessUtilities.Run(CsiPath, "/r:C.dll a.csx", workingDirectory: dir.Path);

            AssertEx.AssertEqualToleratingWhitespaceDifferences(dir.Path + ";C", result.Output);
            Assert.False(result.ContainsErrors);
        }
Example #6
0
        public void CurrentWorkingDirectory_Change()
        {
            var dir = Temp.CreateDirectory();

            dir.CreateFile("a.csx").WriteAllText(@"int X = 1;");
            dir.CreateFile("C.dll").WriteAllBytes(TestResources.General.C1);

            var result = ProcessUtilities.Run(
                CsiPath,
                "",
                stdInput: $@"#load ""a.csx""
#r ""C.dll""
Directory.SetCurrentDirectory(@""{dir.Path}"")
#load ""a.csx""
#r ""C.dll""
X
new C()
Environment.Exit(0)
"
                );

            var expected =
                $@"
{string.Format(CSharpScriptingResources.LogoLine1, s_compilerVersion)}
{CSharpScriptingResources.LogoLine2}

{ScriptingResources.HelpPrompt}
> > > > > > 1
> C {{ }}
> 
";

            // The German translation (and possibly others) contains an en dash (0x2013),
            // but csi.exe outputs it as a hyphen-minus (0x002d). We need to fix up the
            // expected string before we can compare it to the actual output.
            expected = expected.Replace((char)0x2013, (char)0x002d); // EN DASH -> HYPHEN-MINUS
            AssertEx.AssertEqualToleratingWhitespaceDifferences(expected, result.Output);

            AssertEx.AssertEqualToleratingWhitespaceDifferences(
                $@"
(1,7): error CS1504: {string.Format(CSharpResources.ERR_NoSourceFile, "a.csx", CSharpResources.CouldNotFindFile)}
(1,1): error CS0006: {string.Format(CSharpResources.ERR_NoMetadataFile, "C.dll")}
",
                result.Errors
                );

            Assert.Equal(0, result.ExitCode);
        }
Example #7
0
        public void LineNumber_Information_On_Exception()
        {
            var source = @"Console.WriteLine(""OK"");
throw new Exception(""Error!"");
";

            var cwd = Temp.CreateDirectory();

            cwd.CreateFile("a.csx").WriteAllText(source);

            var result = ProcessUtilities.Run(CsiPath, "a.csx", workingDirectory: cwd.Path);

            Assert.True(result.ContainsErrors);
            AssertEx.AssertEqualToleratingWhitespaceDifferences("OK", result.Output);
            AssertEx.AssertEqualToleratingWhitespaceDifferences($@"
System.Exception: Error!
   + <Initialize>.MoveNext(){string.Format(ScriptingResources.AtFileLine, $"{cwd}{Path.DirectorySeparatorChar}a.csx", "2")}
", result.Errors);
        }
Example #8
0
        public void LineNumber_Information_On_Exception()
        {
            var source = @"Console.WriteLine(""OK"");
throw new Exception(""Error!"");
";

            var cwd = Temp.CreateDirectory();

            cwd.CreateFile("a.csx").WriteAllText(source);

            var result = ProcessUtilities.Run(CsiPath, "a.csx", workingDirectory: cwd.Path);

            Assert.True(result.ContainsErrors);
            AssertEx.AssertEqualToleratingWhitespaceDifferences("OK", result.Output);
            AssertEx.AssertStartsWithToleratingWhitespaceDifferences($@"
System.Exception: Error!
   at Submission#0.<<Initialize>>d__0.MoveNext() in {cwd}{Path.DirectorySeparatorChar}a.csx:line 2
", result.Errors);
        }
        public async Task SearchPaths_BaseDirectory()
        {
            var options = ScriptOptions.Default.
                          WithMetadataResolver(new TestMetadataReferenceResolver(
                                                   pathResolver: new VirtualizedRelativePathResolver(existingFullPaths: new[] { @"C:\dir\x.dll" }, baseDirectory: @"C:\goo\bar"),
                                                   files: new Dictionary <string, PortableExecutableReference> {
                { @"C:\dir\x.dll", (PortableExecutableReference)SystemCoreRef }
            }));

            var script = CSharpScript.Create(@"
#r ""x.dll""
using System.Linq;

var x = from a in new[] { 1, 2 ,3 } select a + 1;
", options.WithFilePath(@"C:\dir\a.csx"));

            var state = await script.RunAsync().ContinueWith <IEnumerable <int> >("x", options.WithFilePath(null));

            AssertEx.Equal(new[] { 2, 3, 4 }, state.ReturnValue);
        }
        public async Task MissingRefrencesAutoResolution()
        {
            var portableLib = CSharpCompilation.Create(
                "PortableLib",
                new[] { SyntaxFactory.ParseSyntaxTree("public class C {}") },
                new[] { SystemRuntimePP7Ref },
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            var portableLibRef = portableLib.ToMetadataReference();

            var loader = new InteractiveAssemblyLoader();

            loader.RegisterDependency(Assembly.Load(portableLib.EmitToArray().ToArray()));

            var s0 = await CSharpScript.Create("new C()", options : ScriptOptions.Default.AddReferences(portableLibRef), assemblyLoader : loader).RunAsync();

            var c0 = s0.Script.GetCompilation();

            // includes corlib, host type assembly by default:
            AssertEx.Equal(new[]
            {
                typeof(object).GetTypeInfo().Assembly.Location,
                "PortableLib"
            }, c0.ExternalReferences.SelectAsArray(m => m.Display));

            // System.Runtime, 4.0.0.0 depends on all the assemblies below:
            AssertEx.Equal(new[]
            {
                "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "PortableLib, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
                "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Data.SqlXml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            }, c0.GetBoundReferenceManager().GetReferencedAssemblies().Select(a => a.Value.Identity.GetDisplayName()));
        }