Private/Get-TestFromScript.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Get-TestFromScript {
    param (
        [string]$scriptPath
    )

    $errs = $null
    $tok =[System.Management.Automation.PSParser]::Tokenize((get-content -read 0 -Path $scriptPath), [ref]$Errs)
    #write-verbose -Message $scriptPath

    for($i = 0; $i -lt $tok.count; $i++) {
        if ( $tok[$i].type -eq "Command" -and $tok[$i].content -eq "Describe" ) {
            $i++
            if ( $tok[$i].Type -eq "String" ) {
                $tok[$i].Content
            } else {
                # ok - we didn't get the describe text first,
                # we likely saw a "-Tags" statement, so that means that
                # the describe text will immediately preceed the scriptblock
                while($tok[$i].Type -ne "GroupStart") {
                    $i++
                }
                $i--
                $tok[$i].Content
            }
        }
    }
}