mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
- Added --fake-driver/-f option to force FakeDriver via ConfigurationManager - Added --timeout/-t option to configure timeout in milliseconds - ExampleRunner no longer injects additional keys (relies on example mode) - Updated RunnableWrapperExample with longer delays (200ms) for better reliability - Examples remain clean with only metadata and Create(example: true) Note: RunnableWrapperExample intentionally doesn't quit on Esc key (tests timeout handling) Tests need additional work to properly coordinate key injection with example mode. Co-authored-by: tig <585482+tig@users.noreply.github.com>
135 lines
3.9 KiB
C#
135 lines
3.9 KiB
C#
#nullable enable
|
|
// Example Runner - Demonstrates discovering and running all examples using the example infrastructure
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Terminal.Gui.Configuration;
|
|
using Terminal.Gui.Examples;
|
|
|
|
[assembly: ExampleMetadata ("Example Runner", "Discovers and runs all examples sequentially")]
|
|
[assembly: ExampleCategory ("Infrastructure")]
|
|
|
|
// Parse command line arguments
|
|
bool useFakeDriver = args.Contains ("--fake-driver") || args.Contains ("-f");
|
|
int timeout = 5000; // Default timeout in milliseconds
|
|
|
|
for (var i = 0; i < args.Length; i++)
|
|
{
|
|
if ((args [i] == "--timeout" || args [i] == "-t") && i + 1 < args.Length)
|
|
{
|
|
if (int.TryParse (args [i + 1], out int parsedTimeout))
|
|
{
|
|
timeout = parsedTimeout;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Configure ForceDriver via ConfigurationManager if requested
|
|
if (useFakeDriver)
|
|
{
|
|
Console.WriteLine ("Using FakeDriver (forced via ConfigurationManager)\n");
|
|
ConfigurationManager.RuntimeConfig = """{ "ForceDriver": "FakeDriver" }""";
|
|
ConfigurationManager.Enable (ConfigLocations.All);
|
|
}
|
|
|
|
// Discover examples from the Examples directory
|
|
string? assemblyDir = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location);
|
|
|
|
if (assemblyDir is null)
|
|
{
|
|
Console.WriteLine ("Error: Could not determine assembly directory");
|
|
|
|
return 1;
|
|
}
|
|
|
|
// Go up to find the Examples directory - from bin/Debug/net8.0 to Examples
|
|
string examplesDir = Path.GetFullPath (Path.Combine (assemblyDir, "..", "..", "..", ".."));
|
|
|
|
if (!Directory.Exists (examplesDir))
|
|
{
|
|
Console.WriteLine ($"Error: Examples directory not found: {examplesDir}");
|
|
|
|
return 1;
|
|
}
|
|
|
|
Console.WriteLine ($"Searching for examples in: {examplesDir}\n");
|
|
|
|
// Discover all examples - look specifically in each example's bin directory
|
|
List<ExampleInfo> examples = [];
|
|
HashSet<string> seen = [];
|
|
|
|
foreach (string dir in Directory.GetDirectories (examplesDir))
|
|
{
|
|
string binDir = Path.Combine (dir, "bin", "Debug", "net8.0");
|
|
|
|
if (!Directory.Exists (binDir))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (ExampleInfo example in ExampleDiscovery.DiscoverFromDirectory (binDir, "*.dll", SearchOption.TopDirectoryOnly))
|
|
{
|
|
// Don't include this runner in the list and avoid duplicates
|
|
if (example.Name != "Example Runner" && seen.Add (example.Name))
|
|
{
|
|
examples.Add (example);
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine ($"Discovered {examples.Count} examples\n");
|
|
|
|
// Run all examples sequentially
|
|
var successCount = 0;
|
|
var failCount = 0;
|
|
|
|
foreach (ExampleInfo example in examples)
|
|
{
|
|
Console.Write ($"Running: {example.Name,-40} ");
|
|
|
|
// Create context for running the example
|
|
// Note: When running with example mode, the demo keys from attributes will be used
|
|
// We don't need to inject additional keys via the context
|
|
ExampleContext context = new ()
|
|
{
|
|
DriverName = useFakeDriver ? "FakeDriver" : null,
|
|
KeysToInject = [], // Empty - let example mode handle keys from attributes
|
|
TimeoutMs = timeout,
|
|
Mode = ExecutionMode.InProcess
|
|
};
|
|
|
|
try
|
|
{
|
|
ExampleResult result = ExampleRunner.Run (example, context);
|
|
|
|
if (result.Success)
|
|
{
|
|
Console.WriteLine ($"✓ Success");
|
|
successCount++;
|
|
}
|
|
else if (result.TimedOut)
|
|
{
|
|
Console.WriteLine ($"✗ Timeout");
|
|
failCount++;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine ($"✗ Failed: {result.ErrorMessage ?? "Unknown"}");
|
|
failCount++;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine ($"✗ Exception: {ex.Message}");
|
|
failCount++;
|
|
}
|
|
}
|
|
|
|
Console.WriteLine ($"\n=== Summary: {successCount} passed, {failCount} failed ===");
|
|
|
|
if (useFakeDriver)
|
|
{
|
|
Console.WriteLine ("\nNote: Tests run with FakeDriver. Some examples may timeout if they don't respond to Esc key.");
|
|
}
|
|
|
|
return failCount == 0 ? 0 : 1;
|