|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.ComponentModel.DataAnnotations; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
6 | 8 | using Xunit; |
7 | 9 | using Xunit.Abstractions; |
8 | 10 |
|
@@ -305,5 +307,48 @@ public void ParentOptionsAreValidated_AttributeBinding() |
305 | 307 | var rc = CommandLineApplication.Execute<ValidationParent>("sub"); |
306 | 308 | Assert.Equal(10, rc); |
307 | 309 | } |
| 310 | + |
| 311 | + [Fact] |
| 312 | + public void DoesNotValidate_WhenShowingInfo() |
| 313 | + { |
| 314 | + var sb = new StringBuilder(); |
| 315 | + var console = new TestConsole(_output) |
| 316 | + { |
| 317 | + Out = new StringWriter(sb), |
| 318 | + }; |
| 319 | + var app = new CommandLineApplication(console); |
| 320 | + app.HelpOption(); |
| 321 | + var errorMessage = "Version arg is required"; |
| 322 | + app.Argument("version", "Arg").IsRequired(errorMessage: errorMessage); |
| 323 | + app.OnValidationError((_) => Assert.False(true, "Validation callbacks should not be executed")); |
| 324 | + |
| 325 | + Assert.Equal(0, app.Execute("--help")); |
| 326 | + Assert.DoesNotContain(errorMessage, sb.ToString()); |
| 327 | + } |
| 328 | + |
| 329 | + [HelpOption] |
| 330 | + private class ProgramWithRequiredArg |
| 331 | + { |
| 332 | + [Argument(0), Required(ErrorMessage = "Arg is required")] |
| 333 | + public string Version { get; } |
| 334 | + |
| 335 | + private void OnValidationError() |
| 336 | + { |
| 337 | + throw new InvalidOperationException("Validation callback should not be executed"); |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + [Fact] |
| 342 | + public void DoesNotValidate_WhenShowingInfo_AttributeBinding() |
| 343 | + { |
| 344 | + var sb = new StringBuilder(); |
| 345 | + var console = new TestConsole(_output) |
| 346 | + { |
| 347 | + Out = new StringWriter(sb), |
| 348 | + }; |
| 349 | + |
| 350 | + Assert.Equal(0, CommandLineApplication.Execute<ProgramWithRequiredArg>(console, "--help")); |
| 351 | + Assert.DoesNotContain("Arg is required", sb.ToString()); |
| 352 | + } |
308 | 353 | } |
309 | 354 | } |
0 commit comments