bjorkstromm / gazorator

MIT License
31 stars 9 forks source link

NullReference exception while using in test case #6

Closed pascalberger closed 4 years ago

pascalberger commented 4 years ago

Using Gazorator in an XUnit test case and running from Visual Studio Test Explorer as part of https://github.com/cake-contrib/Cake.Issues.Reporting.Generic/pull/276 leads to a null reference exception:

Message:
    System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace:
    <GetMetadataReferences>d__4.MoveNext()
    AppendPrependIterator`1.LoadFromEnumerator()
    AppendPrependN`1.MoveNext()
    ParameterValidationHelpers.AddRangeChecked[T](ArrayBuilder`1 builder, IEnumerable`1 items, String parameterName)
    ParameterValidationHelpers.ToImmutableArrayChecked[T](IEnumerable`1 items, String parameterName)
    ScriptOptions.WithReferences(IEnumerable`1 references)
    RazorContentGeneratorBase.Generate(String csharpScript)
    <ProcessTemplateAsync>d__17.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    ExceptionDispatchInfo.Throw()
    TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    ConfiguredTaskAwaiter.GetResult()
    GenericIssueReportGenerator.InternalCreateReport(IEnumerable`1 issues)
    GenericIssueReportFixture.CreateReport(IEnumerable`1 issues)
    TheInternalCreateReportMethod.Should_Generate_Report_From_Custom_Template()

Checking for entryAssembly != null and entryAssembly.Location != null in https://github.com/mholo65/gazorator/blob/develop/src/Gazorator/Scripting/RazorContentGenerator.cs#L57 worked around the issue.

gep13 commented 4 years ago

@mholo65 what were your thoughts on how best to proceed with this one?

bjorkstromm commented 4 years ago

Just a null check would be ok. AFAIK, you'll only hit this with unit tests. It's a known issue that entry assembly will be null when running unit tests.

gep13 commented 4 years ago

@mholo65 what about when running under the context of Cake, would you foresee any issues there?

bjorkstromm commented 4 years ago

Nope, no issue. The entry assembly will be Cake.exe. I've successfully tested this with Cake for a PoC for Xamarin team (Redth?). I can add Cake integration tests also.

pascalberger commented 4 years ago

This seems to be only an issue if target framework in consuming application is net461, with netstandard2.0, netcoreapp2.1 or netcoreapp3.1 the error didn't occur