using System; using System.IO; using JetBrains.Annotations; using winsw; namespace winswTests.Util { /// /// Helper for WinSW CLI testing /// public static class CLITestHelper { private const string SeedXml = "" + "service.exe" + "Service" + "The service." + "node.exe" + "My Arguments" + "rotate" + "" + @"C:\winsw\workdir" + "" + @"C:\winsw\logs" + ""; private static readonly ServiceDescriptor DefaultServiceDescriptor = ServiceDescriptor.FromXML(SeedXml); /// /// Runs a simle test, which returns the output CLI /// /// CLI arguments to be passed /// Optional Service descriptor (will be used for initializationpurposes) /// STDOUT if there's no exceptions /// Command failure [NotNull] public static string CLITest(String[] args, ServiceDescriptor descriptor = null) { using (StringWriter sw = new StringWriter()) { TextWriter tmp = Console.Out; Console.SetOut(sw); WrapperService.Run(args, descriptor ?? DefaultServiceDescriptor); Console.SetOut(tmp); Console.Write(sw.ToString()); return sw.ToString(); } } /// /// Runs a simle test, which returns the output CLI /// /// CLI arguments to be passed /// Optional Service descriptor (will be used for initializationpurposes) /// Test results [NotNull] public static CLITestResult CLIErrorTest(String[] args, ServiceDescriptor descriptor = null) { StringWriter swOut, swErr; Exception testEx = null; TextWriter tmpOut = Console.Out; TextWriter tmpErr = Console.Error; using (swOut = new StringWriter()) using (swErr = new StringWriter()) try { Console.SetOut(swOut); Console.SetError(swErr); WrapperService.Run(args, descriptor ?? DefaultServiceDescriptor); } catch (Exception ex) { testEx = ex; } finally { Console.SetOut(tmpOut); Console.SetError(tmpErr); Console.WriteLine("\n>>> Output: "); Console.Write(swOut.ToString()); Console.WriteLine("\n>>> Error: "); Console.Write(swErr.ToString()); if (testEx != null) { Console.WriteLine("\n>>> Exception: "); Console.WriteLine(testEx); } } return new CLITestResult(swOut.ToString(), swErr.ToString(), testEx); } } /// /// Aggregated test report /// public class CLITestResult { [NotNull] public String Out { get; private set; } [NotNull] public String Err { get; private set; } [CanBeNull] public Exception Exception { get; private set; } public bool HasException { get { return Exception != null; } } public CLITestResult(String output, String err, Exception exception = null) { Out = output; Err = err; Exception = exception; } } }