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