Make IConsoleDriver public and add ConsoleDriverFactory for extensibility#330
Make IConsoleDriver public and add ConsoleDriverFactory for extensibility#330
Conversation
…lity Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
|
@copilot Please add test cases |
Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
Added
|
|
@copilot Please add IConsoleDriver test cases |
Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
Added
|
There was a problem hiding this comment.
Pull request overview
This PR makes the console driver abstraction extensible by exposing IConsoleDriver publicly and introducing Prompt.ConsoleDriverFactory so consumers (and tests) can inject custom console implementations instead of hardwiring DefaultConsoleDriver.
Changes:
- Promote
IConsoleDriverandDefaultConsoleDrivertopublicand addPrompt.ConsoleDriverFactory(with null-guard on setter). - Update
FormBaseto construct its driver viaPrompt.ConsoleDriverFactory()and setCancellationCallback. - Add new unit tests covering the factory behavior and a stub implementation exercising the interface members.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Sharprompt/Prompt.Configuration.cs | Adds global ConsoleDriverFactory configuration for driver injection. |
| Sharprompt/Forms/FormBase.cs | Uses Prompt.ConsoleDriverFactory() instead of directly instantiating DefaultConsoleDriver. |
| Sharprompt/Drivers/IConsoleDriver.cs | Makes the console driver interface public for external implementations/mocks. |
| Sharprompt/Drivers/DefaultConsoleDriver.cs | Makes the default driver public (and no longer sealed). |
| Sharprompt.Tests/ConsoleDriverFactoryTests.cs | Verifies default/custom factory assignment and null-guard behavior. |
| Sharprompt.Tests/IConsoleDriverTests.cs | Adds a recording stub to exercise the public interface contract. |
Comments suppressed due to low confidence (1)
Sharprompt/Drivers/DefaultConsoleDriver.cs:17
DefaultConsoleDriveris nowpublicand the type initializer throws when IO is redirected. Exceptions from static constructors are surfaced asTypeInitializationException, which is a confusing failure mode for public consumers. Consider moving the environment checks into the instance constructor (or a factory method) so callers get the intendedInvalidOperationExceptiondirectly, and avoid making type-loading fail for merely referencing the type.
public class DefaultConsoleDriver : IConsoleDriver
{
static DefaultConsoleDriver()
{
if (Console.IsInputRedirected || Console.IsOutputRedirected)
{
throw new InvalidOperationException(Resource.Message_NotSupportedEnvironment);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
IConsoleDriverandDefaultConsoleDriverwereinternal, blocking custom console driver implementations and making forms untestable without a real interactive console.Changes
IConsoleDriver— promoted frominternaltopublicDefaultConsoleDriver— promoted frominternal sealedtopublic(unsealed to allow subclassing)Prompt.ConsoleDriverFactory— newFunc<IConsoleDriver>property onPrompt, defaults to() => new DefaultConsoleDriver(); setter guards against nullFormBase— constructor now callsPrompt.ConsoleDriverFactory()instead of hardcodingnew DefaultConsoleDriver()ConsoleDriverFactoryTests— new test class covering: default factory is non-null, custom factory assignment, null factory throwsArgumentNullException, factory reset, and customIConsoleDriverimplementationIConsoleDriverTests— new test class covering all 17 interface members via aRecordingConsoleDriverstub: all methods (Beep,Reset,ClearLine,ReadKey,Write,WriteLine,SetCursorPosition,Dispose), all properties (KeyAvailable,CursorVisible,CursorLeft,CursorTop,BufferWidth,BufferHeight,WindowWidth,WindowHeight), andCancellationCallbackUsage
Fully backward-compatible — no changes required for existing consumers.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.