Skip to content

Export-NUnitXml.psm1 fails when no line # is found #8

Description

@jasonrush

Expected Behavior

If no line number is found, an empty string should probably be used, instead of the script failing.

Current Behavior

If Invoke-ScriptAnalyzer returns an error where no line number is indicated, PowerShell throws a Runtime exception.

Possible Solution

Steps to Reproduce (for bugs)

Apparently I had a file that has non-ASCII characters in it. I run $ScriptAnalyzerResult = Invoke-ScriptAnalyzer -Path '.\MyProject\' -Recurse -ErrorAction SilentlyContinue and it returns a warning for the RuleName PSUseBOMForUnicodeEncodedFile, but no line number is indicated. This is subsequently passed through to Export-NUnitXml.psm1 which proceeds to throw a RuntimeException as it is not able to call $Result.Line.ToString() on $null.

Context

I am trying to integrate this into appveyor, along with some other stuff (like Pester testing) for a PowerShell module I'm trying to write, and this is causing me issues while trying to figure out if my appveyor settings are right or not (won't successfully complete, so I can't tell if it's showing all the individual error lines or not).

Your Environment

Not sure this is relevant as the issue is the module assumes a line # is returned, but a line # is not always returned. Feel free to reach out to me if you do need more details.

Additional info

RuleName Severity ScriptName Line Message -------- -------- ---------- ---- ------- PSAvoidTrailingWhitespace Information Invoke-aaa 74 Line has trailing whitespace aaaaaaaaaa .ps1 PSUseBOMForUnicodeEncodedFile Warning Invoke-aaa Missing BOM encoding for non-ASCII encoded file aaaaaaaaaa 'Invoke-aaaaaaaaaaaaa.ps1' .ps1 PSAvoidTrailingWhitespace Information Get-aaaaaa 55 Line has trailing whitespace a.ps1 PSAvoidTrailingWhitespace Information Get-aaaaaa 50 Line has trailing whitespace aaaaaa.ps1 You cannot call a method on a null-valued expression.
At C:\windows\system32\Export-NUnitXml.psm1:74 char:111
+ ... f $TestDescription, $($Result.ScriptName), $($Result.Line.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions