Skip to content
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

WithNotParsed() not being called during test #424

Closed
shyassure opened this issue Apr 2, 2019 · 5 comments
Closed

WithNotParsed() not being called during test #424

shyassure opened this issue Apr 2, 2019 · 5 comments

Comments

@shyassure
Copy link

When running a test and command line parser inside a unit test, the HandleParseError function is never being called. I created a gist with a test and a class showing the example.

If I execute the class using regular F5 debugging inside Visual Studio, the HandleParseError function will be called.

https://gist.github.com/shyassure/78fcb2e5a497d2d266f44f579e0c37d3

@moh-hassan
Copy link
Collaborator

I created a vs2017 Test project as multi-target in net461 and netcoreapp2.1 using your test cases and used commanslineparser v2.4.3

I modified the code of ParseArgumentsAndRun by removing the try .. catch as given below:

      public void ParseArgumentsAndRun(string[] args)
        {             
            Parser.Default.ParseArguments<Options>(args)
                .WithParsed(ExecuteTaskWithOptions)
                .WithNotParsed(HandleParseError);            
       }

The HandleParseError function is called in both net461 and netcoreapp2.1 and raise the exception:

System.NotImplementedException : The method or operation is not implemented

The try ..catch in the method 'ParseArgumentsAndRun' catched the exception before 'HandleParseError ' function catch it.

Try it and let me know if it's not working as you expect.

@moh-hassan
Copy link
Collaborator

@shyassure
This issue is resolved in v2.5.0?

@shyassure
Copy link
Author

@moh-hassan Sorry I haven't replied or tried without try catch. I've been working on other projects that doesn't use this library meanwhile. I will let you know if the issue is resolved.

@shyassure
Copy link
Author

shyassure commented May 23, 2019

Doesn't seem like v2.5.0 solves the issue.

@moh-hassan
Copy link
Collaborator

When you catch the Exception in DisplayErrorMessage:

		private void DisplayErrorMessage(Exception ex)
			{        
				Console.WriteLine($"DisplayErrorMessage Exception {ex}");
			}

You get the following exception:

DisplayErrorMessage Exception System.NotImplementedException: ++thrown by HandleParseError
at MailAndSmsWarningSender.HandleParseError(IEnumerable1 errs) in F:\github_issues\commandline424\Mstest\class1.cs:line 40 at CommandLine.ParserResultExtensions.WithNotParsed[T](ParserResult1 result, Action`1 action)
at MailAndSmsWarningSender.ParseArgumentsAndRun(String[] args) in F:\github_issues\commandline424\Mstest\class1.cs:line 28

So, WithNotParsed exception is fired and catched by try .. catch

To catch the NotImplementedException Exception of in HandleParseError , remove try ..catch and run the test:

         [TestMethod]
         [ExpectedException(typeof(System.NotImplementedException))]
       public void SendSmsOnWarning()
       {
        _sut.ParseArgumentsAndRun(new []{"--task", "MailAndSmsWarningSender", "--test", "hejtest"});            
       }
}

And the test pass successfully
So, there is no issue in CommandLineParser and WithNotParsed is fired in unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants