summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_util/controller/tools/coverage_stub.ps1
blob: fcc45703daa5989bd4b75d4928b598c03c40b05d (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
<#
.SYNOPSIS
Gets the lines to hit from a sourcefile for coverage stubs.
#>
[CmdletBinding()]
param (
    [Parameter(Mandatory, ValueFromRemainingArguments)]
    [String[]]
    $Path
)

$stubInfo = @(
    foreach ($sourcePath in $Path) {
        # Default is to just no lines for missing files
        [Collections.Generic.HashSet[int]]$lines = @()

        if (Test-Path -LiteralPath $sourcePath) {
            $code = [ScriptBlock]::Create([IO.File]::ReadAllText($sourcePath))

            # We set our breakpoints with this predicate so our stubs should match
            # that logic.
            $predicate = {
                $args[0] -is [System.Management.Automation.Language.CommandBaseAst]
            }
            $cmds = $code.Ast.FindAll($predicate, $true)

            # We only care about unique lines not multiple commands on 1 line.
            $lines = @(foreach ($cmd in $cmds) {
                    $cmd.Extent.StartLineNumber
                })
        }

        [PSCustomObject]@{
            Path = $sourcePath
            Lines = $lines
        }
    }
)

ConvertTo-Json -InputObject $stubInfo -Depth 2 -Compress