public static void InstallingAPackage(ScenarioDirectory directory) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "When I install ScriptCs.Adder" .x(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario))); "Then the ScriptCs.Adder NuGet package is added to the packages folder" .x(() => File.Exists( Path.Combine( directory.Map(ScriptCsExe.PackagesFolder), "ScriptCs.Adder.Local.0.1.1/ScriptCs.Adder.Local.0.1.1.nupkg")) .ShouldBeTrue()); "And the ScriptCs.Adder assembly is extracted" .x(() => File.Exists( Path.Combine( directory.Map(ScriptCsExe.PackagesFolder), "ScriptCs.Adder.Local.0.1.1/lib/net45/ScriptCs.Adder.dll")) .ShouldBeTrue() ); "And ScriptCs.Adder is added to the packages file" .x(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( @"<package id=""ScriptCs.Adder.Local"" version=""0.1.1"" targetFramework=""net45"" />")); }
public static void ScriptThrowsAnException(bool debug, ScenarioDirectory directory, Exception exception, string[] args, string script) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which throws an exception" .x(() => { directory = ScenarioDirectory.Create(scenario); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { script = @"""throw new Exception(""""""BOOM!"""""");"""; } else { script = @"'throw new Exception(""BOOM!"");'"; } args = new[] { "-e", script }; }); "When I execute the script with debug set to {0}" .x(() => exception = Record.Exception(() => ScriptCsExe.Run(args, debug, directory))); "Then scriptcs fails" .x(() => exception.ShouldBeType <ScriptCsException>()); "And I see the exception message" .x(() => { exception.Message.ShouldContain("BOOM!"); }); }
public static void Migrating(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script directory with a full population of legacy artifacts including a hello world script" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("bin/foo.txt", null) .WriteLine(".cache/foo.txt", null) .WriteLine("packages/foo.txt", null) .WriteLine("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?><packages></packages>") .WriteLine("nuget.config", @"<?xml version=""1.0"" encoding=""utf-8""?><configuration></configuration>") .WriteLine("hello.csx", @"Console.WriteLine(""Hello, World!"");")); "When I execute the script" .f(() => ScriptCsExe.Run("hello.csx", directory)); "Then the artifacts are migrated" .f(() => { File.Exists(directory.Map("bin/foo.txt")).ShouldBeTrue("bin/ is unchanged"); File.Exists(directory.Map(".cache/foo.txt")).ShouldBeFalse(".cache/ is renamed to .scriptcs_cache/"); File.Exists(directory.Map("packages/foo.txt")).ShouldBeTrue("packages/ is unchanged"); File.Exists(directory.Map("packages.config")).ShouldBeTrue("packages.config is unchanged"); File.Exists(directory.Map("nuget.config")).ShouldBeTrue("nuget.config is unchanged"); File.Exists(directory.Map("scriptcs_bin/foo.txt")).ShouldBeTrue("bin/ is copied to scriptcs_bin/"); File.Exists(directory.Map(".scriptcs_cache/foo.txt")).ShouldBeTrue(".scriptcs_cache/ is renamed from .cache/"); File.Exists(directory.Map("scriptcs_packages/foo.txt")).ShouldBeTrue("packages/ is copied to scriptcs_packages/"); File.Exists(directory.Map("scriptcs_packages.config")).ShouldBeTrue("packages.config is copied to scriptcs_packages.config"); File.Exists(directory.Map("scriptcs_nuget.config")).ShouldBeTrue("nuget.config is copied to scriptcs_nuget.config"); }); }
public static void HelloWorld(bool debug, ScenarioDirectory directory, string output, string[] args, string script) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a hello world script" .x(() => { directory = ScenarioDirectory.Create(scenario); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { script = @"Console.WriteLine(""""""Hello World!"""""");"; } else { script = @"'Console.WriteLine(""Hello World!"");'"; } args = new[] { "-e", script }; }); "When I execute the script with debug set to {0}" .x(() => output = ScriptCsExe.Run(args, debug, directory)); "Then I see 'Hello World!'" .x(() => output.ShouldContain("Hello World!")); }
public static void ScriptCanWorkWithUsingStatic(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which defined a static import" .x(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "using static System.Console;" + Environment.NewLine + @"WriteLine(""Hello world!"");")); "When I execute the script" .x(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then I see 'Hello world!'" .x(() => output.ShouldContain("Hello world!")); }
public static void ScriptingEngineShouldSupportCSharp71(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses C# 7.1 language feature - named tuples" .x(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"var x = 1; var y = 2; var tuple = (x,y); Console.WriteLine(""Sum="" + (tuple.x + tuple.y))")); "When I execute the script" .x(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then I see the output from the named tuples." .x(() => output.ShouldContain("Sum=3")); }
public static void HelloWorld(bool debug, ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a hello world script" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(""Hello world!"");")); "When I execute the script with debug set to {0}" .f(() => output = ScriptCsExe.Run("foo.csx", debug, directory)); "Then I see 'Hello world!'" .f(() => output.ShouldContain("Hello world!")); }
public static void ScriptCanAccessEnv(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which accesses Env" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "Console.WriteLine(Env)")); "When I execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then the Env object is displayed" .f(() => output.ShouldContain("ScriptCs.ScriptEnvironment")); }
public static void ScriptCanUseDynamic(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses dynamic" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"dynamic obj = new ExpandoObject(); obj.foo = ""bar""; Console.WriteLine(obj.foo); ;")); "When I execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then the dynamic value is properly returned " .f(() => output.ShouldContain("bar")); }
public static void ScriptAssemblyIsSet(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which accesses Env.ScriptAssembly" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "Console.WriteLine(Env.ScriptAssembly)")); "When I execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then the Assembly is displayed" .f(() => output.ShouldContain("Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")); }
public static void ScriptPathIsSet(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which accesses Env.ScriptPath" .x(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "Console.WriteLine(Env.ScriptPath)")); "When I execute the script" .x(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then the ScriptPath is displayed" .x(() => output.ShouldContain("foo.csx")); }
public static void UsingALoadedMethodInAScriptLibrary(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses ScriptCs.Calculator to print the product of 7 and 6" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(new Calculator().Multiply(7, 6));")); "And ScriptCs.Calculator is installed" .f(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); "When executing the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then I see 42" .f(() => output.ShouldContain("42")); }
public static void UsingAMethodInAScriptLibraryInTheRepl(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses ScriptCs.Calculator" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(""Type:"" + new Calculator().GetType().Name);" + Environment.NewLine + "Environment.Exit(0);")); "And ScriptCs.Calculator is installed" .f(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); "When executing the script into REPL" .f(() => output = ScriptCsExe.Run("foo.csx", false, new[] { "-r" }, directory)); "Then the ScriptCs.Calculator instance is created" .f(() => output.ShouldContain("Type:Calculator")); }
public static void LocalConfiguration(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a hello world script" .x(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(""Hello world!"");")); "And a local config file specfying the log level as debug" .x(() => directory.WriteLine("scriptcs.opts", @"{ logLevel: ""debug"" }")); "When I execute the script without the log level option" .x(() => output = ScriptCsExe.Run("foo.csx", false, directory)); "Then I see debug messages" .x(() => output.ShouldContain("DEBUG:")); }
public static void ScriptCanAccessEnv(ScenarioDirectory directory, string output, string[] args, string script) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which access Env" .x(() => { directory = ScenarioDirectory.Create(scenario); script = "Console.WriteLine(Env)"; args = new[] { "-e", script }; }); "When I execute the script" .x(() => output = ScriptCsExe.Run(args, directory)); "Then the Env object is displayed" .x(() => output.ShouldContain("ScriptCs.ScriptEnvironment")); }
public static void PackageContainsAFrameworkAssemblyReference(ScenarioDirectory directory, Exception exception) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a simple script" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "Console.WriteLine();")); "When I install a package which contains a framework assembly reference" .f(() => ScriptCsExe.Install("FrameworkAssemblyReferencer", directory)); "And I execute the script" .f(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); "Then there should be no errors" .f(() => exception.ShouldBeNull()); }
public static void UsingAScriptPack(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses ScriptCs.Adder to print the sum of 1234 and 5678" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(Require<Adder>().Add(1234, 5678));")); "And ScriptCs.Adder is installed" .f(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory)); "When execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then I see 6912" .f(() => output.ShouldContain("6912")); }
public static void ScriptThrowsAnException(bool debug, ScenarioDirectory directory, Exception exception) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which throws an exception" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"throw new Exception(""BOOM!"");")); "When I execute the script with debug set to {0}" .f(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", debug, directory))); "Then scriptcs fails" .f(() => exception.ShouldBeType <ScriptCsException>()); "And I see the exception message" .f(() => exception.Message.ShouldContain("BOOM!")); }
public static void SavingAPackage(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "When I install ScriptCs.Adder manually" .x(() => { ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario)); directory.DeleteFile(ScriptCsExe.PackagesFile); }); "And I save packages" .x(() => ScriptCsExe.Save(directory)); "Then ScriptCs.Adder is added to the packages file" .x(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( @"<package id=""ScriptCs.Adder.Local"" version=""0.1.1"" targetFramework=""net45"" />")); }
public static void PackagesWithDuplicateAssemblies(ScenarioDirectory directory, Exception exception) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a simple script" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", "Console.WriteLine();")); "When I install a package" .f(() => ScriptCsExe.Install("Duplicate.A", directory)); "And I install another package containing the same assembly" .f(() => ScriptCsExe.Install("Duplicate.B", directory)); "And I execute the script" .f(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); "Then there should be no errors" .f(() => exception.ShouldBeNull()); }
public static void UnexpectedOption(Exception exception) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "When I execute scriptcs with an unknown option" .f(() => exception = Record.Exception(() => ScriptCsExe.Run( new[] { "-unknownoption" }, ScenarioDirectory.Create(scenario)))); "Then scriptcs errors" .f(() => exception.ShouldBeType <ScriptCsException>()); "And I see an error message regarding the unknown option" .f(() => exception.Message.ShouldContain("unknownoption")); "And I see scriptcs usage details" .f(() => exception.Message.ShouldContain("Usage:")); }
public static void LoadingFromANonRootedScript(ScenarioDirectory directory, ScenarioDirectory scriptDirectory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses ScriptCs.Calculator and it is non-rooted" .f(() => { directory = ScenarioDirectory.Create(scenario); scriptDirectory = ScenarioDirectory.Create(Path.Combine(scenario, "script")) .WriteLine("foo.csx", @"Console.WriteLine(""Type:"" + new Calculator().GetType().Name);"); }); "And ScriptCs.Calculator is installed" .f(() => ScriptCsExe.Install("ScriptCs.Calculator", scriptDirectory)); "When executing the script" .f(() => output = ScriptCsExe.Run(Path.Combine("script", "foo.csx"), false, directory)); "Then the ScriptCs.Calculator instance is created" .f(() => output.ShouldContain("Type:Calculator")); }
public static void UsingAMethodInAScriptLibrary(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); var args = new string[] { "-loglevel", "info" }; "Given a script which uses ScriptCs.Calculator to print the sum of 40 and 2" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(new Calculator().Add(40, 2));")); "And ScriptCs.Calculator is installed" .f(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); "When executing the script" .f(() => output = ScriptCsExe.Run("foo.csx", false, Enumerable.Empty <string>(), args, directory)); "Then I see 42" .f(() => output.ShouldContain("42")); "Then I see INFO outputted from the required Logger script pack" .f(() => output.ShouldContain("INFO")); }
public static void LegacyFileSystem(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses ScriptCs.Adder to print the sum of 1234 and 5678" .f(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"Console.WriteLine(Require<Adder>().Add(1234, 5678));")); "And a legacy packages file declaring the ScriptCs.Adder dependency" .f(() => { var nugetConfig = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <clear /> <add key=""Local"" value=""" + Path.GetFullPath(Path.Combine("Support", "Gallery")) + @""" /> </packageSources> <activePackageSource> <add key=""All"" value=""(Aggregate source)"" /> </activePackageSource> </configuration> "; var packagesConfig = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""ScriptCs.Adder.Local"" version=""0.1.1"" targetFramework=""net45"" /> </packages> "; directory.WriteLine("nuget.config", nugetConfig); directory.WriteLine("packages.config", packagesConfig); }); "When execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then I see 6912" .f(() => output.ShouldContain("6912")); }
public static void CleaningADirectory(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a directory" .f(() => directory = ScenarioDirectory.Create(scenario)); "And the directory has an installed package" .f(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory)); "And the directory has an assembly cache" .f(() => directory.WriteLine(Path.Combine(directory.Map(ScriptCsExe.DllCacheFolder), "foo.txt"), null)); "When I clean the directory" .f(() => ScriptCsExe.Clean(directory)); "Then the packages folder is removed" .f(() => Directory.Exists(directory.Map(ScriptCsExe.PackagesFolder)).ShouldBeFalse()); "And the assembly cache folder is removed" .f(() => Directory.Exists(directory.Map(ScriptCsExe.DllCacheFolder)).ShouldBeFalse()); }
public static void UsingAnUnavailableScriptPack(ScenarioDirectory directory, string output, Exception exception) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which uses a non-existing script pack 'Foo'" .x(() => directory = ScenarioDirectory.Create(scenario) .WriteLine("foo.csx", @"var fooContext = Require<Foo>(); class Foo : ScriptCs.Contracts.IScriptPackContext{} Console.WriteLine(""hi"");")); "When I execute the script" .x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); "Then scriptcs fails" .x(() => exception.ShouldBeType <ScriptCsException>()); "With a script pack exception" .x(() => { exception.Message.ShouldContain("Tried to resolve a script pack 'Submission#0+Foo', but such script pack is not available in the current execution context."); }); }
public static void LoadedScriptsIsSet(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "Given a script which loads another script and accesses Env.LoadedScripts" .f(() => { directory = ScenarioDirectory.Create(scenario) .WriteLine( "foo.csx", "#load bar.csx;" + Environment.NewLine + "Console.WriteLine(Env.LoadedScripts.First());" ); directory.WriteLine("bar.csx", ""); }); "When I execute the script" .f(() => output = ScriptCsExe.Run("foo.csx", directory)); "Then the loaded script path is displayed" .f(() => output.ShouldContain("bar.csx")); }