-
Notifications
You must be signed in to change notification settings - Fork 3
Add options validation support to cli hosting and some minor changes for running the CliHost #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c293e3a
9360cc3
0f2e476
831e9c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| [PublicAPI] | ||
| public class CommandResult | ||
| { | ||
| public CommandResult() { } | ||
|
|
||
| public CommandResult(int exitCode) | ||
| { | ||
| ExitCode = exitCode; | ||
| } | ||
|
|
||
| public int ExitCode { get; init; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| /// <summary> | ||
| /// Interface for command options validation. | ||
| /// </summary> | ||
| public interface IOptionsValidation | ||
| { | ||
| /// <summary> | ||
| /// Validates the command options asynchronously. | ||
| /// </summary> | ||
| /// <returns>A <see cref="OptionsValidationResult"/> indicating the result of the validation.</returns> | ||
| Task<OptionsValidationResult> ValidateAsync(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| namespace CreativeCoders.Cli.Core; | ||
|
|
||
| /// <summary> | ||
| /// Represents the result of a command options validation. | ||
| /// </summary> | ||
| /// <param name="isValid">A boolean value indicating whether the options are valid.</param> | ||
| /// <param name="messages">An optional collection of validation messages.</param> | ||
| public class OptionsValidationResult(bool isValid, IEnumerable<string>? messages = null) | ||
| { | ||
| /// <summary> | ||
| /// Gets a value indicating whether the validation was successful. | ||
| /// </summary> | ||
| public bool IsValid { get; } = isValid; | ||
|
|
||
| /// <summary> | ||
| /// Gets the validation messages. | ||
| /// </summary> | ||
| public IEnumerable<string> Messages { get; } = messages ?? []; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using CreativeCoders.Cli.Core; | ||
| using CreativeCoders.Core; | ||
|
|
||
| namespace CreativeCoders.Cli.Hosting.Exceptions; | ||
|
|
||
| public class CliCommandOptionsInvalidException(OptionsValidationResult validationResult) | ||
| : CliExitException("Command options are invalid", CliExitCodes.CommandOptionsInvalid) | ||
| { | ||
| public OptionsValidationResult ValidationResult { get; } = Ensure.NotNull(validationResult); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -267,6 +267,70 @@ public void UseContext_AddsContextToServices() | |||||||||||||||||||||
| .Be(42); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||
| public void UseValidation_AddsSettingsToServices() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| // Arrange | ||||||||||||||||||||||
| var builder = CreateBuilder(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| builder.SkipScanEntryAssembly(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| builder.UseValidation(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var commandScanner = A.Fake<IAssemblyCommandScanner>(); | ||||||||||||||||||||||
| var commandStore = A.Fake<ICliCommandStore>(); | ||||||||||||||||||||||
| var validator = A.Fake<ICliCommandStructureValidator>(); | ||||||||||||||||||||||
| var cliHost = A.Fake<ICliHost>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| A.CallTo(() => commandScanner.ScanForCommands(A<Assembly[]>.Ignored)) | ||||||||||||||||||||||
| .Returns(CreateScanResult()); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SubstituteServices(builder, commandScanner, commandStore, validator, cliHost); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var services = GetBuiltServiceProvider(builder); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Act | ||||||||||||||||||||||
| builder.Build(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Assert | ||||||||||||||||||||||
| var settings = services.GetRequiredService<CliHostSettings>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| settings.UseValidation | ||||||||||||||||||||||
| .Should().Be(true); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||
| public void DontUseValidation_SettingsNotAddedToServicesOrSettingsUseValidationIsFalse() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| // Arrange | ||||||||||||||||||||||
| var builder = CreateBuilder(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| builder.SkipScanEntryAssembly(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| builder.UseValidation(false); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var commandScanner = A.Fake<IAssemblyCommandScanner>(); | ||||||||||||||||||||||
| var commandStore = A.Fake<ICliCommandStore>(); | ||||||||||||||||||||||
| var validator = A.Fake<ICliCommandStructureValidator>(); | ||||||||||||||||||||||
| var cliHost = A.Fake<ICliHost>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| A.CallTo(() => commandScanner.ScanForCommands(A<Assembly[]>.Ignored)) | ||||||||||||||||||||||
| .Returns(CreateScanResult()); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SubstituteServices(builder, commandScanner, commandStore, validator, cliHost); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var services = GetBuiltServiceProvider(builder); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Act | ||||||||||||||||||||||
| builder.Build(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Assert | ||||||||||||||||||||||
| var settings = services.GetService<CliHostSettings>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| settings?.UseValidation | ||||||||||||||||||||||
| .Should().NotBe(true); | ||||||||||||||||||||||
|
Comment on lines
+330
to
+331
|
||||||||||||||||||||||
| settings?.UseValidation | |
| .Should().NotBe(true); | |
| if (settings == null) | |
| { | |
| settings.Should().BeNull(); | |
| } | |
| else | |
| { | |
| settings.UseValidation.Should().BeFalse(); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.