From ba25c16fe2dca348c1e17a98f8236e638e336cdf Mon Sep 17 00:00:00 2001 From: Jeff Tong Date: Tue, 29 Jan 2019 14:36:48 -0800 Subject: [PATCH 1/5] Fixing build break and updating test. --- src/CommandLine/Core/ReflectionExtensions.cs | 21 ++++-------- src/CommandLine/Error.cs | 17 ++++++++-- .../Unit/Core/InstanceBuilderTests.cs | 32 ++++++++++--------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/CommandLine/Core/ReflectionExtensions.cs b/src/CommandLine/Core/ReflectionExtensions.cs index 87fe97db..68c19f8b 100644 --- a/src/CommandLine/Core/ReflectionExtensions.cs +++ b/src/CommandLine/Core/ReflectionExtensions.cs @@ -14,8 +14,6 @@ namespace CommandLine.Core { static class ReflectionExtensions { - public const string CannotSetValueToTargetInstance = "Cannot set value to target instance."; - public static IEnumerable GetSpecifications(this Type type, Func selector) { return from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties()) @@ -93,10 +91,6 @@ public static IEnumerable SetProperties( private static IEnumerable SetValue(this SpecificationProperty specProp, T instance, object value) { - Action fail = inner => { - throw new InvalidOperationException(CannotSetValueToTargetInstance, inner); - }; - try { specProp.Property.SetValue(instance, value, null); @@ -106,17 +100,16 @@ private static IEnumerable SetValue(this SpecificationProperty specPro { return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) }; } - catch (Exception e) + catch (ArgumentException e) { - return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; + var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e); + + return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), argEx, value) }; } - catch(ArgumentException e) + catch (Exception e) { - var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e); - fail(argEx); + return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; } - - return instance; } public static object CreateEmptyArray(this Type type) @@ -214,4 +207,4 @@ public static bool IsPrimitiveEx(this Type type) || Convert.GetTypeCode(type) != TypeCode.Object; } } -} \ No newline at end of file +} diff --git a/src/CommandLine/Error.cs b/src/CommandLine/Error.cs index b1bc3e94..5a562689 100644 --- a/src/CommandLine/Error.cs +++ b/src/CommandLine/Error.cs @@ -64,8 +64,7 @@ public enum ErrorType /// /// Value of type. /// - SetValueExceptionError - VersionRequestedError, + SetValueExceptionError, /// /// Value of type. /// @@ -511,6 +510,18 @@ public object Value { get { return value; } } + } + /// + /// Models an error generated when an invalid token is detected. + /// + public sealed class InvalidAttributeConfigurationError : Error + { + public const string ErrorMessage = "Check if Option or Value attribute values are set properly for the given type."; + + internal InvalidAttributeConfigurationError() + : base(ErrorType.InvalidAttributeConfigurationError, true) + { + } } -} \ No newline at end of file +} diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index 8eb36080..de96bb43 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -1007,6 +1007,22 @@ public void Parse_option_with_exception_thrown_from_setter_generates_SetValueExc // Teardown } + [Fact] + public void Parse_default_bool_type_string_SetValueExceptionError() + { + // Fixture setup + string name = nameof(Options_With_InvalidDefaults.FileName).ToLower(); + var expectedResult = new[] { new SetValueExceptionError(new NameInfo("", name), + new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage), "bad") }; + + // Exercize system + var result = InvokeBuild( + new[] { name, "bad" }); + + // Verify outcome + ((NotParsed)result).Errors.Should().BeEquivalentTo(expectedResult); + } + [Theory] [InlineData(new[] { "--stringvalue", "x-" }, "x-")] @@ -1133,22 +1149,8 @@ public void Parse_TimeSpan() // Teardown } - [Fact] - public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException() - { - // Exercize system - Action test = () => InvokeBuild( - new string[] { }); - - // Verify outcome - test.ShouldThrow() - .WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance) - .WithInnerException() - .WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage); - } - - public static IEnumerable RequiredValueStringData + public static IEnumerable RequiredValueStringData { get { From 0dac5acb2644db7e444e67e82d4ebacfc570192f Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Fri, 1 Feb 2019 12:46:36 -0500 Subject: [PATCH 2/5] add specific image to make sure Visual Studio 2017 environment used --- appveyor.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index a3b73034..c7979afb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,8 @@ #version should be only changed with RELEASE eminent, see RELEASE.md version: 2.4.{build} +image: Visual Studio 2017 + clone_depth: 1 pull_requests: do_not_increment_build_number: true From 6d2dc12f0c31600ba7c00a7385982d9487b63f79 Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Fri, 1 Feb 2019 12:54:02 -0500 Subject: [PATCH 3/5] fixed bad merge of Error.cs in build --- src/CommandLine/Error.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CommandLine/Error.cs b/src/CommandLine/Error.cs index b1bc3e94..90ee26d8 100644 --- a/src/CommandLine/Error.cs +++ b/src/CommandLine/Error.cs @@ -64,7 +64,8 @@ public enum ErrorType /// /// Value of type. /// - SetValueExceptionError + SetValueExceptionError, + VersionRequestedError, /// /// Value of type. From 030eb8d431c0c56b62277c654d0951308e873960 Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Fri, 1 Feb 2019 13:01:01 -0500 Subject: [PATCH 4/5] @moh-hassan helped fix FSharp side of build --- src/CommandLine/CommandLine.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CommandLine/CommandLine.csproj b/src/CommandLine/CommandLine.csproj index 46e6d82d..3764bcb6 100644 --- a/src/CommandLine/CommandLine.csproj +++ b/src/CommandLine/CommandLine.csproj @@ -14,7 +14,7 @@ gsscoder;nemec;ericnewton76 Command Line Parser Library $(VersionSuffix) - 2.4.0 + 0.0.0 Terse syntax C# command line parser for .NET. For FSharp support see CommandLineParser.FSharp. The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks. Terse syntax C# command line parser for .NET with F# support. The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks. Copyright (c) 2005 - 2018 Giacomo Stelluti Scala & Contributors @@ -35,7 +35,7 @@ - + From e3bd8609f34ec3fdc64919d6a3fec4084feab65f Mon Sep 17 00:00:00 2001 From: Jeff Tong Date: Sat, 23 Feb 2019 15:26:37 -0800 Subject: [PATCH 5/5] Resolving conflicts and fixing build. --- src/CommandLine/Core/ReflectionExtensions.cs | 2 ++ .../Unit/Core/InstanceBuilderTests.cs | 16 +--------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/CommandLine/Core/ReflectionExtensions.cs b/src/CommandLine/Core/ReflectionExtensions.cs index 514bef75..70f1afc5 100644 --- a/src/CommandLine/Core/ReflectionExtensions.cs +++ b/src/CommandLine/Core/ReflectionExtensions.cs @@ -14,6 +14,8 @@ namespace CommandLine.Core { static class ReflectionExtensions { + public const string CannotSetValueToTargetInstance = "Cannot set value to target instance."; + public static IEnumerable GetSpecifications(this Type type, Func selector) { return from pi in type.FlattenHierarchy().SelectMany(x => x.GetTypeInfo().GetProperties()) diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index 38f2a85a..c6234089 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -1149,20 +1149,6 @@ public void Parse_TimeSpan() // Teardown } - [Fact] - public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException() - { - // Exercize system - Action test = () => InvokeBuild( - new string[] { }); - - // Verify outcome - test.ShouldThrow() - .WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance) - .WithInnerException() - .WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage); - } - [Fact] public void OptionClass_IsImmutable_HasNoCtor() @@ -1179,7 +1165,7 @@ private class ValueWithNoSetterOptions } - public static IEnumerable RequiredValueStringData + public static IEnumerable RequiredValueStringData { get {