CLITestHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.IO;
  3. using JetBrains.Annotations;
  4. using winsw;
  5. namespace winswTests.Util
  6. {
  7. /// <summary>
  8. /// Helper for WinSW CLI testing
  9. /// </summary>
  10. public static class CLITestHelper
  11. {
  12. private const string SeedXml = "<service>"
  13. + "<id>service.exe</id>"
  14. + "<name>Service</name>"
  15. + "<description>The service.</description>"
  16. + "<executable>node.exe</executable>"
  17. + "<arguments>My Arguments</arguments>"
  18. + "<logmode>rotate</logmode>"
  19. + "<workingdirectory>"
  20. + @"C:\winsw\workdir"
  21. + "</workingdirectory>"
  22. + @"<logpath>C:\winsw\logs</logpath>"
  23. + "</service>";
  24. private static readonly ServiceDescriptor DefaultServiceDescriptor = ServiceDescriptor.FromXML(SeedXml);
  25. /// <summary>
  26. /// Runs a simle test, which returns the output CLI
  27. /// </summary>
  28. /// <param name="args">CLI arguments to be passed</param>
  29. /// <param name="descriptor">Optional Service descriptor (will be used for initializationpurposes)</param>
  30. /// <returns>STDOUT if there's no exceptions</returns>
  31. /// <exception cref="Exception">Command failure</exception>
  32. [NotNull]
  33. public static string CLITest(String[] args, ServiceDescriptor descriptor = null)
  34. {
  35. using (StringWriter sw = new StringWriter())
  36. {
  37. TextWriter tmp = Console.Out;
  38. Console.SetOut(sw);
  39. WrapperService.Run(args, descriptor ?? DefaultServiceDescriptor);
  40. Console.SetOut(tmp);
  41. Console.Write(sw.ToString());
  42. return sw.ToString();
  43. }
  44. }
  45. /// <summary>
  46. /// Runs a simle test, which returns the output CLI
  47. /// </summary>
  48. /// <param name="args">CLI arguments to be passed</param>
  49. /// <param name="descriptor">Optional Service descriptor (will be used for initializationpurposes)</param>
  50. /// <returns>Test results</returns>
  51. [NotNull]
  52. public static CLITestResult CLIErrorTest(String[] args, ServiceDescriptor descriptor = null)
  53. {
  54. StringWriter swOut, swErr;
  55. Exception testEx = null;
  56. TextWriter tmpOut = Console.Out;
  57. TextWriter tmpErr = Console.Error;
  58. using (swOut = new StringWriter())
  59. using (swErr = new StringWriter())
  60. try
  61. {
  62. Console.SetOut(swOut);
  63. Console.SetError(swErr);
  64. WrapperService.Run(args, descriptor ?? DefaultServiceDescriptor);
  65. }
  66. catch (Exception ex)
  67. {
  68. testEx = ex;
  69. }
  70. finally
  71. {
  72. Console.SetOut(tmpOut);
  73. Console.SetError(tmpErr);
  74. Console.WriteLine("\n>>> Output: ");
  75. Console.Write(swOut.ToString());
  76. Console.WriteLine("\n>>> Error: ");
  77. Console.Write(swErr.ToString());
  78. if (testEx != null)
  79. {
  80. Console.WriteLine("\n>>> Exception: ");
  81. Console.WriteLine(testEx);
  82. }
  83. }
  84. return new CLITestResult(swOut.ToString(), swErr.ToString(), testEx);
  85. }
  86. }
  87. /// <summary>
  88. /// Aggregated test report
  89. /// </summary>
  90. public class CLITestResult
  91. {
  92. [NotNull]
  93. public String Out { get; private set; }
  94. [NotNull]
  95. public String Err { get; private set; }
  96. [CanBeNull]
  97. public Exception Exception { get; private set; }
  98. public bool HasException { get { return Exception != null; } }
  99. public CLITestResult(String output, String err, Exception exception = null)
  100. {
  101. Out = output;
  102. Err = err;
  103. Exception = exception;
  104. }
  105. }
  106. }