Program.cs 3.9 KB

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