TylerLeonhardt / vscode-pester-test-adapter

MIT License
33 stars 13 forks source link

Native Testing Fails with 'Reset-TestSuiteState' not recognized #49

Closed JustinGrote closed 3 years ago

JustinGrote commented 3 years ago
[2021-06-01 16:25:24.491] [INFO] Test Explorer found
[2021-06-01 16:25:24.491] [INFO] Creating adapter for c:\Users\JGrote\Projects\Press
[2021-06-01 16:25:24.491] [INFO] Initializing Pester adapter
[2021-06-01 16:25:24.491] [INFO] Initializing Pester test runner.
[2021-06-01 16:25:24.493] [INFO] Registering adapter for c:\Users\JGrote\Projects\Press
[2021-06-01 16:25:24.493] [INFO] Loading Pester tests
[2021-06-01 16:25:24.493] [INFO] Initialization finished
[2021-06-01 16:25:24.494] [INFO] Loading Pester tests
[2021-06-01 16:25:24.618] [DEBUG] Found 3 paths
[2021-06-01 16:25:24.618] [DEBUG] Found 3 paths
[2021-06-01 16:25:27.063] [DEBUG] Using PowerShell (x64) at: C:\Program Files\PowerShell\7\pwsh.exe
[2021-06-01 16:25:27.064] [DEBUG] 
$Path = @(
    'c:\Users\JGrote\Projects\Press\Source\Public\Update-GithubRelease.Tests.ps1'
'c:\Users\JGrote\Projects\Press\Source\Public\Get-Version.Tests.ps1'
'c:\Users\JGrote\Projects\Press\Source\Public\Build-ReleaseNotes.Tests.ps1'
)

$VerbosePreference = 'Ignore'
$WarningPreference = 'Ignore'
$DebugPreference = 'Ignore'
Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
function Discover-Test
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String[]] $Path,
        [String[]] $ExcludePath
    )
    & (Get-Module Pester) { 
        param (
            $Path, 
            $ExcludePath,
            $SessionState)

        Reset-TestSuiteState
        # to avoid Describe thinking that we run in interactive mode
        $invokedViaInvokePester = $true
        $files = Find-File -Path $Path -ExcludePath $ExcludePath -Extension $PesterPreference.Run.TestExtension.Value
        $containers = foreach ($f in $files) {
            <# HACK: We check to see if there is a single Describe block in the file so that we don't accidentally execute code that shouldn't need to be executed. #>
            if (!(Select-String -Path $f -SimpleMatch 'Describe')) {
                continue
            }
            New-BlockContainerObject -File (Get-Item $f)
        }
        Find-Test -BlockContainer $containers -SessionState $SessionState } -Path $Path -ExcludePath $ExcludePath -SessionState $PSCmdlet.SessionState
}

function New-SuiteObject ($Block) { 
    [PSCustomObject]@{
        type = 'suite'
        id = $Block.ScriptBlock.File + ';' + $Block.StartLine
        file = $Block.ScriptBlock.File
        line = $Block.StartLine - 1
        label = $Block.Name
        children = [Collections.Generic.List[Object]]@()
    }
}

function New-TestObject ($Test) { 
    [PSCustomObject]@{
        type = 'test'
        id = $Test.ScriptBlock.File + ';' + $Test.StartLine
        file = $Test.ScriptBlock.File
        line = $Test.StartLine - 1
        label = $Test.Name
    }
}

function fold ($children, $Block) {
    foreach ($b in $Block.Blocks) { 
        $o = (New-SuiteObject $b)
        $children.Add($o)
        fold $o.children $b
    }

    $hashset = [System.Collections.Generic.HashSet[string]]::new()
    foreach ($t in $Block.Tests) {
        $key = "$($t.ExpandedPath):$($t.StartLine)"
        if ($hashset.Contains($key)) {
            continue
        }
        $children.Add((New-TestObject $t))
        $hashset.Add($key) | Out-Null
    }
    $hashset.Clear() | Out-Null
}

$found = Discover-Test -Path $Path

# whole suite
$suite = [PSCustomObject]@{
    Blocks = [Collections.Generic.List[Object]] $found
    Tests = [Collections.Generic.List[Object]]@()
}

$testSuiteInfo = [PSCustomObject]@{
    type = 'suite'
    id = 'root'
    label = 'Pester'
    children = [Collections.Generic.List[Object]]@()
}

foreach ($file in $found) {
    $fileSuite = [PSCustomObject]@{
        type = 'suite'
        id = $file.BlockContainer.Item.FullName
        file = $file.BlockContainer.Item.FullName
        label = $file.BlockContainer.Item.Name
        children = [Collections.Generic.List[Object]]@()
    }
    $testSuiteInfo.children.Add($fileSuite)
    fold $fileSuite.children $file
}

$testSuiteInfo | ConvertTo-Json -Depth 100

[2021-06-01 16:25:27.069] [DEBUG] Using PowerShell (x64) at: C:\Program Files\PowerShell\7\pwsh.exe
[2021-06-01 16:25:27.069] [DEBUG] 
$Path = @(
    'c:\Users\JGrote\Projects\Press\Source\Public\Build-ReleaseNotes.Tests.ps1'
'c:\Users\JGrote\Projects\Press\Source\Public\Get-Version.Tests.ps1'
'c:\Users\JGrote\Projects\Press\Source\Public\Update-GithubRelease.Tests.ps1'
)

$VerbosePreference = 'Ignore'
$WarningPreference = 'Ignore'
$DebugPreference = 'Ignore'
Import-Module Pester -MinimumVersion 5.0.0 -ErrorAction Stop
function Discover-Test
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String[]] $Path,
        [String[]] $ExcludePath
    )
    & (Get-Module Pester) { 
        param (
            $Path, 
            $ExcludePath,
            $SessionState)

        Reset-TestSuiteState
        # to avoid Describe thinking that we run in interactive mode
        $invokedViaInvokePester = $true
        $files = Find-File -Path $Path -ExcludePath $ExcludePath -Extension $PesterPreference.Run.TestExtension.Value
        $containers = foreach ($f in $files) {
            <# HACK: We check to see if there is a single Describe block in the file so that we don't accidentally execute code that shouldn't need to be executed. #>
            if (!(Select-String -Path $f -SimpleMatch 'Describe')) {
                continue
            }
            New-BlockContainerObject -File (Get-Item $f)
        }
        Find-Test -BlockContainer $containers -SessionState $SessionState } -Path $Path -ExcludePath $ExcludePath -SessionState $PSCmdlet.SessionState
}

function New-SuiteObject ($Block) { 
    [PSCustomObject]@{
        type = 'suite'
        id = $Block.ScriptBlock.File + ';' + $Block.StartLine
        file = $Block.ScriptBlock.File
        line = $Block.StartLine - 1
        label = $Block.Name
        children = [Collections.Generic.List[Object]]@()
    }
}

function New-TestObject ($Test) { 
    [PSCustomObject]@{
        type = 'test'
        id = $Test.ScriptBlock.File + ';' + $Test.StartLine
        file = $Test.ScriptBlock.File
        line = $Test.StartLine - 1
        label = $Test.Name
    }
}

function fold ($children, $Block) {
    foreach ($b in $Block.Blocks) { 
        $o = (New-SuiteObject $b)
        $children.Add($o)
        fold $o.children $b
    }

    $hashset = [System.Collections.Generic.HashSet[string]]::new()
    foreach ($t in $Block.Tests) {
        $key = "$($t.ExpandedPath):$($t.StartLine)"
        if ($hashset.Contains($key)) {
            continue
        }
        $children.Add((New-TestObject $t))
        $hashset.Add($key) | Out-Null
    }
    $hashset.Clear() | Out-Null
}

$found = Discover-Test -Path $Path

# whole suite
$suite = [PSCustomObject]@{
    Blocks = [Collections.Generic.List[Object]] $found
    Tests = [Collections.Generic.List[Object]]@()
}

$testSuiteInfo = [PSCustomObject]@{
    type = 'suite'
    id = 'root'
    label = 'Pester'
    children = [Collections.Generic.List[Object]]@()
}

foreach ($file in $found) {
    $fileSuite = [PSCustomObject]@{
        type = 'suite'
        id = $file.BlockContainer.Item.FullName
        file = $file.BlockContainer.Item.FullName
        label = $file.BlockContainer.Item.Name
        children = [Collections.Generic.List[Object]]@()
    }
    $testSuiteInfo.children.Add($fileSuite)
    fold $fileSuite.children $file
}

$testSuiteInfo | ConvertTo-Json -Depth 100

[2021-06-01 16:25:28.330] [ERROR] stderr: Reset-TestSuiteState: 
Line |
  26 |          Reset-TestSuiteState
     |          ~~~~~~~~~~~~~~~~~~~~

[2021-06-01 16:25:28.330] [ERROR] stderr:      | The term 'Reset-TestSuiteState' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[2021-06-01 16:25:28.359] [ERROR] stderr: Reset-TestSuiteState: 

[2021-06-01 16:25:28.359] [ERROR] stderr: Line |
  26 |          Reset-TestSuiteState
     |          ~~~~~~~~~~~~~~~~~~~~
     | The term 'Reset-TestSuiteState' is not recognized as a name of a cmdlet, function, script file, or executable program.

[2021-06-01 16:25:28.360] [ERROR] stderr: Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[2021-06-01 16:25:28.676] [DEBUG] stdout: {
  "type": "suite",
  "id": "root",
  "label": "Pester",
  "children": [
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    },
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    },
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    }
  ]
}

[2021-06-01 16:25:28.706] [DEBUG] child process exited with code 0
[2021-06-01 16:25:28.708] [DEBUG] stdout: {
  "type": "suite",
  "id": "root",
  "label": "Pester",
  "children": [
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    },
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    },
    {
      "type": "suite",
      "id": null,
      "file": null,
      "label": null,
      "children": []
    }
  ]
}

[2021-06-01 16:25:28.731] [DEBUG] child process exited with code 0
JustinGrote commented 3 years ago

Maybe related to #46

JustinGrote commented 3 years ago

Dupe of #46