| 1 |
# (c) 2025 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
using namespace System.Collections.Generic |
| 5 |
using namespace System.IO |
| 6 |
using namespace System.Management.Automation |
| 7 |
using namespace System.Management.Automation.Language |
| 8 |
using namespace System.Reflection |
| 9 |
using namespace System.Text |
| 10 |
|
| 11 |
param( |
| 12 |
[Parameter(Mandatory)] |
| 13 |
[string] |
| 14 |
$ModuleName, |
| 15 |
|
| 16 |
[Parameter(Mandatory)] |
| 17 |
[string] |
| 18 |
$OutputPath, |
| 19 |
|
| 20 |
[Parameter(Mandatory)] |
| 21 |
[string] |
| 22 |
$PathFilter |
| 23 |
) |
| 24 |
|
| 25 |
# Required to be set for psrp so we can set a breakpoint in the remote runspace |
| 26 |
$Host.Runspace.Debugger.SetDebugMode([DebugModes]::RemoteScript) |
| 27 |
|
| 28 |
Function New-CoverageBreakpointsForScriptBlock { |
| 29 |
Param ( |
| 30 |
[Parameter(Mandatory)] |
| 31 |
[string] |
| 32 |
$ScriptName, |
| 33 |
|
| 34 |
[Parameter(Mandatory)] |
| 35 |
[ScriptBlockAst] |
| 36 |
$ScriptBlockAst, |
| 37 |
|
| 38 |
[Parameter(Mandatory)] |
| 39 |
[String] |
| 40 |
$AnsiblePath |
| 41 |
) |
| 42 |
|
| 43 |
$predicate = { |
| 44 |
$args[0] -is [CommandBaseAst] |
| 45 |
} |
| 46 |
$scriptCmds = $ScriptBlockAst.FindAll($predicate, $true) |
| 47 |
|
| 48 |
# Create an object that tracks the Ansible path of the file and the breakpoints that have been set in it |
| 49 |
$info = [PSCustomObject]@{ |
| 50 |
Path = $AnsiblePath |
| 51 |
Breakpoints = [List[Breakpoint]]@() |
| 52 |
} |
| 53 |
|
| 54 |
# LineBreakpoint was only made public in PowerShell 6.0 so we need to use |
| 55 |
# reflection to achieve the same thing in 5.1. |
| 56 |
$lineCtor = if ($PSVersionTable.PSVersion -lt '6.0') { |
| 57 |
[LineBreakpoint].GetConstructor( |
| 58 |
[BindingFlags]'NonPublic, Instance', |
| 59 |
$null, |
| 60 |
[type[]]@([string], [int], [int], [scriptblock]), |
| 61 |
$null) |
| 62 |
} |
| 63 |
else { |
| 64 |
[LineBreakpoint]::new |
| 65 |
} |
| 66 |
|
| 67 |
# Keep track of lines that are already scanned. PowerShell can contains multiple commands in 1 line |
| 68 |
$scannedLines = [HashSet[int]]@() |
| 69 |
foreach ($cmd in $scriptCmds) { |
| 70 |
if (-not $scannedLines.Add($cmd.Extent.StartLineNumber)) { |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
# Action is explicitly $null as it will slow down the runtime quite dramatically. |
| 75 |
$b = $lineCtor.Invoke(@($ScriptName, $cmd.Extent.StartLineNumber, $cmd.Extent.StartColumnNumber, $null)) |
| 76 |
$info.Breakpoints.Add($b) |
| 77 |
} |
| 78 |
|
| 79 |
[Runspace]::DefaultRunspace.Debugger.SetBreakpoints($info.Breakpoints) |
| 80 |
|
| 81 |
$info |
| 82 |
} |
| 83 |
|
| 84 |
Function Compare-PathFilterPattern { |
| 85 |
Param ( |
| 86 |
[String[]]$Patterns, |
| 87 |
[String]$Path |
| 88 |
) |
| 89 |
|
| 90 |
foreach ($pattern in $Patterns) { |
| 91 |
if ($Path -like $pattern) { |
| 92 |
return $true |
| 93 |
} |
| 94 |
} |
| 95 |
return $false |
| 96 |
} |
| 97 |
|
| 98 |
$actionInfo = Get-NextAnsibleAction |
| 99 |
$actionParams = $actionInfo.Parameters |
| 100 |
|
| 101 |
# A PS Breakpoint needs a path to be associated with the ScriptBlock, luckily |
| 102 |
# the Get-AnsibleScript does this for us. |
| 103 |
$breakpointInfo = @() |
| 104 |
|
| 105 |
try { |
| 106 |
$coveragePathFilter = $PathFilter.Split(":", [StringSplitOptions]::RemoveEmptyEntries) |
| 107 |
$breakpointInfo = @( |
| 108 |
foreach ($scriptName in @($ModuleName; $actionParams.PowerShellModules)) { |
| 109 |
# We don't use -IncludeScriptBlock as the script might be untrusted |
| 110 |
# and will run under CLM. While we recreate the ScriptBlock here it |
| 111 |
# is only to get the AST that contains the statements and their |
| 112 |
# line numbers to create the breakpoint info for. |
| 113 |
$scriptInfo = Get-AnsibleScript -Name $scriptName |
| 114 |
|
| 115 |
if (Compare-PathFilterPattern -Patterns $coveragePathFilter -Path $scriptInfo.Path) { |
| 116 |
$ast = [Parser]::ParseInput($scriptInfo.Script, [ref]$null, [ref]$null) |
| 117 |
$covParams = @{ |
| 118 |
ScriptName = $scriptInfo.Name |
| 119 |
ScriptBlockAst = $ast |
| 120 |
AnsiblePath = $scriptInfo.Path |
| 121 |
} |
| 122 |
New-CoverageBreakpointsForScriptBlock @covParams |
| 123 |
} |
| 124 |
} |
| 125 |
) |
| 126 |
|
| 127 |
if ($breakpointInfo) { |
| 128 |
$actionParams.Breakpoints = $breakpointInfo.Breakpoints |
| 129 |
} |
| 130 |
|
| 131 |
try { |
| 132 |
& $actionInfo.ScriptBlock @actionParams |
| 133 |
} |
| 134 |
finally { |
| 135 |
# Processing here is kept to an absolute minimum to make sure each task runtime is kept as small as |
| 136 |
# possible. Once all the tests have been run ansible-test will collect this info and process it locally in |
| 137 |
# one go. |
| 138 |
$coverageInfo = @{} |
| 139 |
foreach ($info in $breakpointInfo) { |
| 140 |
$coverageInfo[$info.Path] = $info.Breakpoints | Select-Object -Property Line, HitCount |
| 141 |
} |
| 142 |
|
| 143 |
$psVersion = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" |
| 144 |
$coverageOutputPath = "$OutputPath=powershell-$psVersion=coverage.$($env:COMPUTERNAME).$PID.$(Get-Random)" |
| 145 |
$codeCovJson = ConvertTo-Json -InputObject $coverageInfo -Compress |
| 146 |
|
| 147 |
# Ansible controller expects these files to be UTF-8 without a BOM, use .NET for this. |
| 148 |
$utf8 = [UTF8Encoding]::new($false) |
| 149 |
[File]::WriteAllText($coverageOutputPath, $codeCovJson, $utf8) |
| 150 |
} |
| 151 |
} |
| 152 |
finally { |
| 153 |
foreach ($b in $breakpointInfo.Breakpoints) { |
| 154 |
Remove-PSBreakpoint -Breakpoint $b |
| 155 |
} |
| 156 |
} |