Exemplo n.º 1
0
        public object LoadFromFile(ISimpleConfig config, string path)
        {
            var fileSystem = new FileSystem { CurrentDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase };
            log.InfoFormat(CultureInfo.InvariantCulture, "Executing '{0}'", fileSystem.GetFullPath(path));
            log.DebugFormat(CultureInfo.InvariantCulture, "The current directory is {0}", fileSystem.CurrentDirectory);

            var scriptCsLog = LogManager.GetLogger("ScriptCs");
            var lineProcessors = new ILineProcessor[]
            {
                new LoadLineProcessor(fileSystem),
                new ReferenceLineProcessor(fileSystem),
                new UsingLineProcessor(),
            };

            var filePreProcessor = new FilePreProcessor(fileSystem, scriptCsLog, lineProcessors);
            var engine = new RoslynScriptInMemoryEngine(new ConfigRScriptHostFactory(config), scriptCsLog);
            var executor = new ConfigRScriptExecutor(fileSystem, filePreProcessor, engine, scriptCsLog);
            executor.AddReferenceAndImportNamespaces(new[] { typeof(Config), typeof(IScriptHost) });

            ScriptResult result;
            executor.Initialize(new string[0], new IScriptPack[0]);
            try
            {
                result = executor.Execute(path);
            }
            finally
            {
                executor.Terminate();
            }

            RethrowExceptionIfAny(result, path);
            return result.ReturnValue;
        }
            public void ShouldExposeExceptionThrownByScriptWhenErrorOccurs()
            {
                var scriptEngine = new RoslynScriptInMemoryEngine(new ScriptHostFactory(), new TestLogProvider());
                // Arrange
                var lines = new List<string>
                {
                    "using System;",
                    @"throw new InvalidOperationException(""InvalidOperationExceptionMessage."");"
                };

                var code = string.Join(Environment.NewLine, lines);
                var session = new ScriptPackSession(Enumerable.Empty<IScriptPack>(), new string[0]);

                // Act
                var result = scriptEngine.Execute(code, new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                        session);

                // Assert
                var exception = Assert.Throws<InvalidOperationException>(() => result.ExecuteExceptionInfo.Throw());
                exception.StackTrace.ShouldContain("Submission#0");
                exception.Message.ShouldContain("InvalidOperationExceptionMessage");
            }
            public void ShouldExposeExceptionThrownByCompilation()
            {
                var scriptEngine = new RoslynScriptInMemoryEngine(new ScriptHostFactory(), new TestLogProvider());

                // Arrange
                var lines = new List<string>
                {
                    "using Sysasdasdasdtem;"
                };

                var code = string.Join(Environment.NewLine, lines);
                var session = new ScriptPackSession(Enumerable.Empty<IScriptPack>(), new string[0]);

                // Act
                var result = scriptEngine.Execute(code, new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                        session);

                // Assert
                var exception = Assert.Throws<ScriptCompilationException>(() => result.CompileExceptionInfo.Throw());
                exception.InnerException.ShouldBeType<CompilationErrorException>();
                exception.Message.ShouldContain("The type or namespace name 'Sysasdasdasdtem' could not be found");
            }
Exemplo n.º 4
0
        public object LoadFromFile(ISimpleConfig config, string path)
        {
            var fileSystem = new FileSystem { CurrentDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase };
            log.InfoFormat("Executing '{0}'", fileSystem.GetFullPath(path));
            log.DebugFormat("The current directory is {0}", fileSystem.CurrentDirectory);

            var scriptCsLog = new LogProviderAdapter();
            var lineProcessors = new ILineProcessor[]
            {
                new LoadLineProcessor(fileSystem),
                new ReferenceLineProcessor(fileSystem),
                new UsingLineProcessor(),
            };

            var filePreProcessor = new FilePreProcessor(fileSystem, scriptCsLog, lineProcessors);
            var engine = new RoslynScriptInMemoryEngine(new ConfigRScriptHostFactory(config), scriptCsLog);
            var executor = new ScriptExecutor(fileSystem, filePreProcessor, engine, scriptCsLog);
            executor.AddReferenceAndImportNamespaces(new[] { typeof(Config), typeof(IScriptHost) });
            executor.AddReferences(this.references);

            ScriptResult result;
            executor.Initialize(new string[0], new IScriptPack[0]);

            // HACK (adamralph): BaseDirectory is set to bin subfolder in Initialize()!
            executor.ScriptEngine.BaseDirectory = executor.FileSystem.CurrentDirectory;
            try
            {
                result = executor.Execute(path);
            }
            finally
            {
                executor.Terminate();
            }

            RethrowExceptionIfAny(result, path);
            return result.ReturnValue;
        }