Description
I have multi-target project (Project1) (net45;netcoreapp2.0) that reference CommandLineParaser library.
Project1 have a static class StartUp with a property:
private static string _copyRight= CopyrightInfo. Default;
The project is working fine for both frameworks
I have other NUnit Test Project (Project1.Test) which is multi-target project (net45;netcoreapp2.0)
For Test I use dotnet CLI command dotnet test
.
I run for both frameworks the following command:
dotnet test
For net45 framework: Test Run Successful
For netcoreapp2.0: Test Fail with an exception:
Failed DefaultSettingTest()
Error Message:
System.AggregateException : One or more errors occurred. (The type initializerfor 'project1.StartUp' threw an exception.)
----> System.TypeInitializationException : The type initializer for 'project1.StartUp' threw an exception.
----> System.ArgumentException : author
Stack Trace:
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task
1.get_Result()
..... other messages-----
--- End of stack trace from previous location where exception was thrown ---
at Project1.StartUp.RunOptionsAsync(String[] args) in F:\project1\Program.cs:line 103
--ArgumentException
at CommandLine.Text.CopyrightInfo..ctor(Boolean isSymbolUpper, String author, Int32[] copyrightYears)
at CommandLine.Text.CopyrightInfo.get_Default()
What is the root problem:
When I investigated CopyrightInfo class,
public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears)
{
if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author");
...
}
I find an exception is fired when AuthorAttribute is null.
Suggestion for a solution:
For The CommandLineParaser library to be independent of the initialization of AssemplyAttributes in Unit Test environment in Full Framework or netcoreapp2.x and avoid cause of test failure as described in this issue, I suggest removing this exception from the line and setting a reasonable value:
//if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author");
if (string.IsNullOrWhiteSpace(author)) author="<<UnDefined Author>>";
Note:
It's better to avoid throwing exception of any Null assemplyAttributes
and initialize it with a suitable default value,e.g., CompanyAssemplyAttribute, CopyWrightAssemplyAttributes