public static string Run( string scriptName, bool debug, ScenarioDirectory directory) { return(Run(scriptName, debug, Enumerable.Empty <string>(), Enumerable.Empty <string>(), directory)); }
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 InstallingAPackage(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "When I install ScriptCs.Adder" .f(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario))); "Then the ScriptCs.Adder NuGet package is added to the packages folder" .f(() => 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" .f(() => 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" .f(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( @"<package id=""ScriptCs.Adder.Local"" version=""0.1.1"" targetFramework=""net45"" />")); }
private static string Execute( IEnumerable <string> args, IEnumerable <string> scriptArgs, ScenarioDirectory directory) { var commandArgs = new List <string>(args); var scriptArgsArray = scriptArgs.ToArray(); if (scriptArgsArray.Any()) { commandArgs.Add("--"); commandArgs.AddRange(scriptArgsArray); } #if DEBUG var config = "Debug"; #else var config = "Release"; #endif var exe = Path.GetFullPath( Path.Combine("..", "..", "..", "..", "..", "src", "ScriptCs", "bin", config, "net461", "scriptcs.exe")); var info = new ProcessStartInfo { FileName = isMono ? "mono" : exe, Arguments = isMono ? string.Concat(exe, " ", string.Join(" ", commandArgs)) : string.Join(" ", commandArgs), WorkingDirectory = directory.Name, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardErrorEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8 }; var result = info.Run(Path.GetFileName(directory.Name) + ".log"); if (result.Item1 != 0) { var message = string.Format( CultureInfo.InvariantCulture, "scriptcs.exe exited with code {0}. The output was: {1}", result.Item1.ToString(CultureInfo.InvariantCulture), result.Item2); throw new ScriptCsException(message); } return(result.Item2); }
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 access 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 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 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 LocalConfiguration(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!"");")); "And a local config file specfying the log level as debug" .f(() => directory.WriteLine("scriptcs.opts", @"{ logLevel: ""debug"" }")); "When I execute the script without the log level option" .f(() => output = ScriptCsExe.Run("foo.csx", false, directory)); "Then I see debug messages" .f(() => output.ShouldContain("DEBUG:")); }
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 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 SavingAPackage(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); "When I install ScriptCs.Adder manually" .f(() => { ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario)); directory.DeleteFile(ScriptCsExe.PackagesFile); }); "And I save packages" .f(() => ScriptCsExe.Save(directory)); "Then ScriptCs.Adder is added to the packages file" .f(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( @"<package id=""ScriptCs.Adder.Local"" version=""0.1.1"" targetFramework=""net45"" />")); }
public static string Run( string scriptName, bool debug, IEnumerable <string> args, IEnumerable <string> scriptArgs, ScenarioDirectory directory) { var debugArgs = debug && !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-DEBUG") && !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-D") ? new[] { "-debug" } : new string[0]; return(Execute( (scriptName == null ? Enumerable.Empty <string>() : new[] { scriptName }).Concat(debugArgs).Concat(args), scriptArgs, directory)); }
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 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 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"), directory)); "Then the ScriptCs.Calculator instance is created" .f(() => output.ShouldContain("Type:Calculator")); }
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 string Install(string package, ScenarioDirectory directory) { using (var writer = new StreamWriter(directory.Map(NugetFile), false)) { writer.Write( @"<?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>" ); writer.Flush(); } return Execute(new[] { "-install", package }, Enumerable.Empty<string>(), directory); }
public static string Install(string package, ScenarioDirectory directory) { using (var writer = new StreamWriter(directory.Map(NugetFile), false)) { writer.Write( @"<?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>" ); writer.Flush(); } return(Execute(new[] { "-install", package }, Enumerable.Empty <string>(), directory)); }
public static string Run(IEnumerable<string> args, ScenarioDirectory directory) { return Run(null, true, args, Enumerable.Empty<string>(), directory); }
private static string Execute( IEnumerable<string> args, IEnumerable<string> scriptArgs, ScenarioDirectory directory) { var commandArgs = new List<string>(args); var scriptArgsArray = scriptArgs.ToArray(); if (scriptArgsArray.Any()) { commandArgs.Add("--"); commandArgs.AddRange(scriptArgsArray); } #if DEBUG var config = "Debug"; #else var config = "Release"; #endif var exe = Path.GetFullPath( Path.Combine("..", "..", "..", "..", "src", "ScriptCs", "bin", config, "scriptcs.exe")); var info = new ProcessStartInfo { FileName = isMono ? "mono" : exe, Arguments = isMono ? string.Concat(exe, " ", string.Join(" ", commandArgs)) : string.Join(" ", commandArgs), WorkingDirectory = directory.Name, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true }; var result = info.Run(Path.GetFileName(directory.Name) + ".log"); if (result.Item1 != 0) { var message = string.Format( CultureInfo.InvariantCulture, "scriptcs.exe exited with code {0}. The output was: {1}", result.Item1.ToString(CultureInfo.InvariantCulture), result.Item2); throw new ScriptCsException(message); } return result.Item2; }
public static string Run(string scriptName, bool debug, IEnumerable<string> args, ScenarioDirectory directory) { return Run(scriptName, debug, args, Enumerable.Empty<string>(), directory); }
public static string Run(string scriptName, ScenarioDirectory directory) { return Run(scriptName, true, Enumerable.Empty<string>(), Enumerable.Empty<string>(), directory); }
public static string Run( string scriptName, bool debug, IEnumerable<string> args, IEnumerable<string> scriptArgs, ScenarioDirectory directory) { var debugArgs = debug && !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-DEBUG") && !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-D") ? new[] { "-debug" } : new string[0]; return Execute( (scriptName == null ? Enumerable.Empty<string>() : new[] { scriptName }).Concat(debugArgs).Concat(args), scriptArgs, directory); }
public static void UsingAMethodInAScriptLibraryInTheRepl(ScenarioDirectory directory, string output) { var scenario = MethodBase.GetCurrentMethod().GetFullName(); var args = new[] {"-r"}; "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" .f(() => output = ScriptCsExe.Run("foo.csx", false, args, Enumerable.Empty<string>(), directory)); "Then the ScriptCs.Calculator instance is created" .f(() => output.ShouldContain("Type:Calculator")); }
public static string Run(IEnumerable <string> args, ScenarioDirectory directory) { return(Run(null, true, args, Enumerable.Empty <string>(), directory)); }
public static string Save(ScenarioDirectory directory) { return(Execute(new[] { "-save" }, Enumerable.Empty <string>(), directory)); }
public static string Clean(ScenarioDirectory directory) { return(Execute(new[] { "-clean" }, Enumerable.Empty <string>(), directory)); }
public static string Save(ScenarioDirectory directory) { return Execute(new[] { "-save" }, Enumerable.Empty<string>(), directory); }
public static string Clean(ScenarioDirectory directory) { return Execute(new[] { "-clean" }, Enumerable.Empty<string>(), directory); }