Program.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Examples;
  5. [assembly: ExampleMetadata ("Example Runner", "Discovers and runs all examples sequentially")]
  6. [assembly: ExampleCategory ("Infrastructure")]
  7. // Discover examples from the Examples directory
  8. string? assemblyDir = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location);
  9. if (assemblyDir is null)
  10. {
  11. Console.WriteLine ("Error: Could not determine assembly directory");
  12. return 1;
  13. }
  14. // Go up to find the Examples directory - from bin/Debug/net8.0 to Examples
  15. string examplesDir = Path.GetFullPath (Path.Combine (assemblyDir, "..", "..", "..", ".."));
  16. if (!Directory.Exists (examplesDir))
  17. {
  18. Console.WriteLine ($"Error: Examples directory not found: {examplesDir}");
  19. return 1;
  20. }
  21. Console.WriteLine ($"Searching for examples in: {examplesDir}\n");
  22. // Discover all examples - look specifically in each example's bin directory
  23. List<ExampleInfo> examples = [];
  24. HashSet<string> seen = [];
  25. foreach (string dir in Directory.GetDirectories (examplesDir))
  26. {
  27. string binDir = Path.Combine (dir, "bin", "Debug", "net8.0");
  28. if (!Directory.Exists (binDir))
  29. {
  30. continue;
  31. }
  32. foreach (ExampleInfo example in ExampleDiscovery.DiscoverFromDirectory (binDir, "*.dll", SearchOption.TopDirectoryOnly))
  33. {
  34. // Don't include this runner in the list and avoid duplicates
  35. if (example.Name != "Example Runner" && seen.Add (example.Name))
  36. {
  37. examples.Add (example);
  38. }
  39. }
  40. }
  41. Console.WriteLine ($"Discovered {examples.Count} examples\n");
  42. // Run all examples sequentially
  43. var successCount = 0;
  44. var failCount = 0;
  45. foreach (ExampleInfo example in examples)
  46. {
  47. Console.Write ($"Running: {example.Name,-40} ");
  48. // Create context for running the example
  49. ExampleContext context = new ()
  50. {
  51. KeysToInject = example.DemoKeyStrokes.OrderBy (ks => ks.Order)
  52. .SelectMany (ks => ks.KeyStrokes)
  53. .ToList (),
  54. TimeoutMs = 5000,
  55. Mode = ExecutionMode.InProcess
  56. };
  57. try
  58. {
  59. ExampleResult result = ExampleRunner.Run (example, context);
  60. if (result.Success)
  61. {
  62. Console.WriteLine ($"✓ Success");
  63. successCount++;
  64. }
  65. else if (result.TimedOut)
  66. {
  67. Console.WriteLine ($"✗ Timeout");
  68. failCount++;
  69. }
  70. else
  71. {
  72. Console.WriteLine ($"✗ Failed: {result.ErrorMessage ?? "Unknown"}");
  73. failCount++;
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. Console.WriteLine ($"✗ Exception: {ex.Message}");
  79. failCount++;
  80. }
  81. }
  82. Console.WriteLine ($"\n=== Summary: {successCount} passed, {failCount} failed ===");
  83. return failCount == 0 ? 0 : 1;