-
Notifications
You must be signed in to change notification settings - Fork 479
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
Version 2.4.x System.ArgumentNullException parsing options in unit-test #389
Comments
Thats interesting... 3 levels deep into BCL reflection code. Are you running on framework 4.7.2 or net core or what? |
So this works on my machine, running xunit. Also I checked in your test code into branch fix/issue-389 and the build server ran the test (look for issue389_tests) https://ci.appveyor.com/project/commandlineparser/commandline/build/job/o3uhjxvn8xdha46g/tests Not sure why you'd get this error... |
I am using a Console-Application with .Net Framework 4.7.2. thanks for your investigation. I will re-check this. I'll can give you a feedback to this within next week. |
TL;DR,I could reproduce the same error in a test project of target framework net472 , but sucess in netcoreapp2.1 using commaline v2.4.3. This problem is due to the behavior of This a long story and I have previously posted an issue to vstest project to correct that behavior. Commandlineparser consider AseemplyAttributes copyright and company shouldn't be null and raise exception (I think it should be relaxed), which may be null in hosttest and cause troubles with commandlineparser. To workaround the problem in net472 (compliant with netstandard2)
I tested your code in multi-target project in Xunit for net472 and netcoreapp2.1 and both are sucess and NO exception is raised.
The output in the multi-target test project is:
Conclusion: The xunit test project is working fine when you inherit the TestBase class in net472. |
I have the exception with NUnit. I think it's because Assembly.GetEntryAssembly() returns null on this line. Here's a simple repro project: IssueDemo.zip (might have to build, clean, rebuild since NUnit test adapter is quirky). |
I test your demo and it fail with the error message
As a workaround solution, I modified your project by inheriting the class `BaseTest' that i described in my comment
The root error is that the Assemplyattribute copyright and company are null in the hosttest of vstest which is used by NUnit and Xunit, so Commandlineparser raise an exception when execute This bug is in net4x only. In netcoreapp2.x test pass successfully. |
I can confirm this issue using Unity 2018.3.0f1 (.NET standard 2.0). |
Issue seems to linked to Some quick tests: Debug.Log(Assembly.GetEntryAssembly()); // null
Debug.Log(Assembly.GetExecutingAssembly()); // my assembly
Debug.Log(Assembly.GetCallingAssembly()); // my assembly I don't know too much of reflection that's why I don't know why you're using the entry assembly instead of the executing or calling one. Can you please explain? |
Probably the issue is the method commandline/src/CommandLine/Infrastructure/ReflectionHelper.cs Lines 97 to 100 in 4bd363a
|
Hmmm. Interesting. Its odd to be running commnadlineparser through unity though... I dont know much about it, but seems counterproductive since (I'm guessing) unity has its own bootloader before your library starts running? Thats the only reason I can surmise, without more information, about how GetEntryAssembly would return null. Can you provide more info about how this is being run? |
Yes you're right unity has it's own native bootloader calling my library. This is the default unity way. I don't think there is a way around this in any way without modifying the engine itself. I need multiple unity instances started with custom command line args each. I thought it would be wise to use a library for this purpose but it seems to conflict with unity's arguments like I mentioned in my own issue. |
Just a reminder this exception happens without Unity. |
@moh-hassan thank you for your workaround. That fixed my tests. This workaround works good for .NET Framework. Thank you again for providing a solution. @ericnewton76 i created a unit test to ensure the application exits after e.g. Maybe this ticket can be closed? When i compile my console-project using .NET Framework 4.7.2 and call it via CMD it is executed as expected. It seems the failure is caused by my Unit-Test project. |
@mbedded when you say "Furthermore it should exit when some parameters are missing"... eh, yes and no... realistically some implementations can move forward with what they have. It's really left up to the implementation to decide if thats actually the case, and we shouldn't push that choice out onto the library consumers. You mentioned you added a unit test. Thanks for that! I'll look into this. |
Looks like I'm getting this same error in LINQPad too, which is fairly annoying. In this case it might not have anything to do with native assemblies, but more to do with dynamic compilation of queries? Here's a minimal repro script (needs the library from NuGet of course):
Gives an at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) |
Passing Null values:
Cause an exception and that exception is expected because 'Opt ' is required (Required = true)
It will work fine. You can catch the exception using try .. catch I test your code using LinqPad v5.36.03 and CommandLineParser v2.4.3 (and also 2.3.0) and it's working as I described above. This behavior is different than the Unit test, because the executor of the program is the Unit test framework and that can be workaround using that class |
@moh-hassan: I understand that Opt is required, I explicitly set up that example to fail parsing and trigger the help text to be printed. Is that not supposed to happen? I get this same exception with and without Personally I did not expect an exception to be thrown just because the command line arguments were incorrect. Even if an exception was going to be thrown, I would very much expect it would have sensible and contextual help text about the option in question, and I would be monumentally surprised for it be thrown from inside a system function like I'm not interested in the behaviour of the "successfully parsed" path at this point, that does seem to be working fine. Just purely reporting that there seems to be a bug in the "could not parse path", and providing another environment where it seems to manifest. It seems to me to be the same bug, as the exception message and the lower part of the stack trace is identical. Maybe it can't be worked around in the same way using that class, but that doesn't make it a different bug. |
YES, you are correct. I did more investigation and found that LinqPad has for every query it run a random assembly, something like:
This assembly, which is considered for CommandLineParser, as the Executing assembly has no AssemblyAttributes at all. so, the problematic TWO lines
raise an exception This why I provided a Proposal for change: Remove raising exception when Copyright or company assembly attributes are null and set with default values #403 I hope we can get a forward step to resolve this exception. |
This issue is resolved in the release 2.5.0 |
Hello,
i tried all 2.4.x versions (2.4.0, 2.4.1, 2.4.2, 2.4.3) and all have the same exception. In my unit-test i call my program only with "--help" after that, i receive the following stacktrace:
My code snippets are the following ones:
I did a downgrade to version 2.3.0 without any issues. Calling the compiled Program.exe by a command line seems to be working as well. I created that test to ensure my application stops when the version or help is requested only without any additional information.
The text was updated successfully, but these errors were encountered: