2
0

Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #nullable enable
  2. // Example Runner - Demonstrates discovering and running all examples using the example infrastructure
  3. using Microsoft.Extensions.Logging;
  4. using Serilog;
  5. using Serilog.Core;
  6. using Serilog.Events;
  7. using Terminal.Gui.App;
  8. using Terminal.Gui.Configuration;
  9. using Terminal.Gui.Examples;
  10. using ILogger = Microsoft.Extensions.Logging.ILogger;
  11. // Configure Serilog to write to Debug output and Console
  12. Log.Logger = new LoggerConfiguration ()
  13. .MinimumLevel.Is (LogEventLevel.Verbose)
  14. .WriteTo.Debug ()
  15. .CreateLogger ();
  16. ILogger logger = LoggerFactory.Create (builder =>
  17. {
  18. builder
  19. .AddSerilog (dispose: true) // Integrate Serilog with ILogger
  20. .SetMinimumLevel (LogLevel.Trace); // Set minimum log level
  21. }).CreateLogger ("ExampleRunner Logging");
  22. Logging.Logger = logger;
  23. Logging.Debug ("Logging enabled - writing to Debug output\n");
  24. // Parse command line arguments
  25. bool useFakeDriver = args.Contains ("--fake-driver") || args.Contains ("-f");
  26. int timeout = 30000; // Default timeout in milliseconds
  27. for (var i = 0; i < args.Length; i++)
  28. {
  29. if ((args [i] == "--timeout" || args [i] == "-t") && i + 1 < args.Length)
  30. {
  31. if (int.TryParse (args [i + 1], out int parsedTimeout))
  32. {
  33. timeout = parsedTimeout;
  34. }
  35. }
  36. }
  37. // Configure ForceDriver via ConfigurationManager if requested
  38. if (useFakeDriver)
  39. {
  40. Console.WriteLine ("Using FakeDriver (forced via ConfigurationManager)\n");
  41. ConfigurationManager.RuntimeConfig = """{ "ForceDriver": "FakeDriver" }""";
  42. ConfigurationManager.Enable (ConfigLocations.All);
  43. }
  44. // Discover examples from the Examples directory
  45. string? assemblyDir = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location);
  46. if (assemblyDir is null)
  47. {
  48. Console.WriteLine ("Error: Could not determine assembly directory");
  49. return 1;
  50. }
  51. // Go up to find the Examples directory - from bin/Debug/net8.0 to Examples
  52. string examplesDir = Path.GetFullPath (Path.Combine (assemblyDir, "..", "..", "..", ".."));
  53. if (!Directory.Exists (examplesDir))
  54. {
  55. Console.WriteLine ($"Error: Examples directory not found: {examplesDir}");
  56. return 1;
  57. }
  58. Console.WriteLine ($"Searching for examples in: {examplesDir}\n");
  59. // Discover all examples - look specifically in each example's bin directory
  60. List<ExampleInfo> examples = [];
  61. HashSet<string> seen = [];
  62. foreach (string dir in Directory.GetDirectories (examplesDir))
  63. {
  64. string binDir = Path.Combine (dir, "bin", "Debug", "net8.0");
  65. if (!Directory.Exists (binDir))
  66. {
  67. continue;
  68. }
  69. foreach (ExampleInfo example in ExampleDiscovery.DiscoverFromDirectory (binDir, "*.dll", SearchOption.TopDirectoryOnly))
  70. {
  71. // Don't include this runner in the list and avoid duplicates
  72. if (example.Name != "Example Runner" && seen.Add (example.Name))
  73. {
  74. examples.Add (example);
  75. }
  76. }
  77. }
  78. Console.WriteLine ($"Discovered {examples.Count} examples\n");
  79. // Run all examples sequentially
  80. var successCount = 0;
  81. var failCount = 0;
  82. foreach (ExampleInfo example in examples)
  83. {
  84. Console.Write ($"Running: {example.Name,-40} ");
  85. // Create context for running the example
  86. // Note: When running with example mode, the demo keys from attributes will be used
  87. // We don't need to inject additional keys via the context
  88. ExampleContext context = new ()
  89. {
  90. DriverName = useFakeDriver ? "FakeDriver" : null,
  91. KeysToInject = [], // Empty - let example mode handle keys from attributes
  92. TimeoutMs = timeout,
  93. Mode = ExecutionMode.InProcess
  94. };
  95. try
  96. {
  97. ExampleResult result = ExampleRunner.Run (example, context);
  98. if (result.Success)
  99. {
  100. Console.WriteLine ($"✓ Success");
  101. successCount++;
  102. }
  103. else if (result.TimedOut)
  104. {
  105. Console.WriteLine ($"✗ Timeout");
  106. failCount++;
  107. }
  108. else
  109. {
  110. Console.WriteLine ($"✗ Failed: {result.ErrorMessage ?? "Unknown"}");
  111. failCount++;
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine ($"✗ Exception: {ex.Message}");
  117. failCount++;
  118. }
  119. }
  120. Console.WriteLine ($"\n=== Summary: {successCount} passed, {failCount} failed ===");
  121. if (useFakeDriver)
  122. {
  123. Console.WriteLine ("\nNote: Tests run with FakeDriver. Some examples may timeout if they don't respond to Esc key.");
  124. }
  125. // Flush logs before exiting
  126. Log.CloseAndFlush ();
  127. return failCount == 0 ? 0 : 1;