sbergwall / RobocopyPS

RobocopyPS
MIT License
51 stars 9 forks source link

when line cannot be parsed, function may be called on null-valued expression in Invoke-RobocopyParser #23

Closed WiredSharp closed 1 year ago

WiredSharp commented 1 year ago

with robocopy v10.0.14393 .5582, output parsing fails with the following:

You cannot call a method on a null-valued expression. At RobocopyPS\0.2.13\internal\Invoke-RobocopyParser.ps1:84 char:13

You cannot call a method on a null-valued expression. At RobocopyPS\0.2.13\internal\Invoke-RobocopyParser.ps1:85 char:13

You cannot call a method on a null-valued expression. At RobocopyPS\0.2.13\internal\Invoke-RobocopyParser.ps1:88 char:33

sbergwall commented 1 year ago

Hi @WiredSharp.

Can you please send the text that is not parsed?

WiredSharp commented 1 year ago

Hello, here is an InputObject raising the previously mentioned error: ' 424 2022/10/11 13:40:54 E:\projects\Jee\lo.tools.jee\Tmp\modules\LO.Core\2.1.0\security\private\New-Credential2.ps1'

Notice that line has leading mix of tabs and spaces

BeforeDiscovery {
    $ModuleName = 'RobocopyPS'
    $ModuleBase = Split-Path -Parent $PSScriptRoot
    $ModulePath = Join-Path $ModuleBase -ChildPath $ModuleName

    # Removes all versions of the module from the session before importing
    Get-Module $ModuleName | Remove-Module
    Import-Module $ModulePath -PassThru -ErrorAction Stop
}
Describe "when parsing robocopy output" {
    InModuleScope $ModuleName {
        It "should not raise exception when parsing fails" -ForEach @(
            @{ line = '              424 2022/10/11 13:40:54    E:\projects\New-Credential2.ps1';Source='E:\projects\New-Credential2.ps1' }
        ) {
            { Invoke-RobocopyParser -InputObject $line } | Should -Not -Throw
        }
    }
}
skaravos commented 1 year ago

Another case that will trigger this same code path occurs when specifying a logfile that resides inside the 'Source' directory. (***Normally you wouldn't put the log in the source dir, but when copying a single file it's technically safe)

For example:

Invoke-Robocopy -Source "C:\tmp\src" -Destination "C:\tmp\dst" -Files "abc.txt" -LogFile "C:\tmp\src\robocopy.log" -Force

The first line of output from robocopy.exe is something akin to:

 Log File : C:\tmp\src\robocopy.log

Because this log file line matches *$Source* it enters the wrong branch and triggers the 'You cannot call a method on a null-valued expression.' The offending line is here: https://github.com/sbergwall/RobocopyPS/blob/d34253da7058a6fa2ea5d9d145a632b3c4d37bc5/RobocopyPS/internal/Invoke-RobocopyParser.ps1#L86

I've only briefly tested @WiredSharp's PR, but it seems to work perfectly and has also inadvertently solved this logfile bug!

sbergwall commented 1 year ago

Hi.

This is merged to master in Github, will push it to the Powershell Gallery as soon as possible. Thank you @WiredSharp for your help, much appreciated!

sbergwall commented 1 year ago

Hi.

This is now released to Powershell Gallery with version 0.2.19.