win coverage - Fix untrusted coverage collection (#85197)

Fixes the logic when running a module through App Control when the
module is not trusted to run in Full Language Mode. This ensures
coverage will still run as expected and that the trust verification only
happens in the wrappers that actually run/prepare the code.

Also expands on a comment to clarify why only that branch is set to set
the internal file encoding to UTF-8.

(cherry picked from commit 17cee7a982)
pull/85255/head
Jordan Borean 7 months ago committed by Matt Davis
parent 457b40d659
commit abb1de2780

@ -28,8 +28,12 @@ $Host.Runspace.Debugger.SetDebugMode([DebugModes]::RemoteScript)
Function New-CoverageBreakpointsForScriptBlock { Function New-CoverageBreakpointsForScriptBlock {
Param ( Param (
[Parameter(Mandatory)] [Parameter(Mandatory)]
[ScriptBlock] [string]
$ScriptBlock, $ScriptName,
[Parameter(Mandatory)]
[ScriptBlockAst]
$ScriptBlockAst,
[Parameter(Mandatory)] [Parameter(Mandatory)]
[String] [String]
@ -39,7 +43,7 @@ Function New-CoverageBreakpointsForScriptBlock {
$predicate = { $predicate = {
$args[0] -is [CommandBaseAst] $args[0] -is [CommandBaseAst]
} }
$scriptCmds = $ScriptBlock.Ast.FindAll($predicate, $true) $scriptCmds = $ScriptBlockAst.FindAll($predicate, $true)
# Create an object that tracks the Ansible path of the file and the breakpoints that have been set in it # Create an object that tracks the Ansible path of the file and the breakpoints that have been set in it
$info = [PSCustomObject]@{ $info = [PSCustomObject]@{
@ -68,7 +72,7 @@ Function New-CoverageBreakpointsForScriptBlock {
} }
# Action is explicitly $null as it will slow down the runtime quite dramatically. # Action is explicitly $null as it will slow down the runtime quite dramatically.
$b = $lineCtor.Invoke(@($ScriptBlock.File, $cmd.Extent.StartLineNumber, $cmd.Extent.StartColumnNumber, $null)) $b = $lineCtor.Invoke(@($ScriptName, $cmd.Extent.StartLineNumber, $cmd.Extent.StartColumnNumber, $null))
$info.Breakpoints.Add($b) $info.Breakpoints.Add($b)
} }
@ -102,11 +106,16 @@ try {
$coveragePathFilter = $PathFilter.Split(":", [StringSplitOptions]::RemoveEmptyEntries) $coveragePathFilter = $PathFilter.Split(":", [StringSplitOptions]::RemoveEmptyEntries)
$breakpointInfo = @( $breakpointInfo = @(
foreach ($scriptName in @($ModuleName; $actionParams.PowerShellModules)) { foreach ($scriptName in @($ModuleName; $actionParams.PowerShellModules)) {
$scriptInfo = Get-AnsibleScript -Name $scriptName -IncludeScriptBlock # We don't use -IncludeScriptBlock as the script might be untrusted
# and will run under CLM. While we recreate the ScriptBlock here it
# is only to get the AST that contains the statements and their
# line numbers to create the breakpoint info for.
$scriptInfo = Get-AnsibleScript -Name $scriptName
if (Compare-PathFilterPattern -Patterns $coveragePathFilter -Path $scriptInfo.Path) { if (Compare-PathFilterPattern -Patterns $coveragePathFilter -Path $scriptInfo.Path) {
$covParams = @{ $covParams = @{
ScriptBlock = $scriptInfo.ScriptBlock ScriptName = $scriptInfo.Name
ScriptBlockAst = [ScriptBlock]::Create($scriptInfo.Script).Ast
AnsiblePath = $scriptInfo.Path AnsiblePath = $scriptInfo.Path
} }
New-CoverageBreakpointsForScriptBlock @covParams New-CoverageBreakpointsForScriptBlock @covParams

@ -98,7 +98,9 @@ if ($ForModule) {
$ps.Runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "Stop") $ps.Runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "Stop")
} }
else { else {
# For script files we want to ensure we load it as UTF-8 # For script files we want to ensure we load it as UTF-8. We don't set this
# for modules as they are loaded from memory whereas a script is loaded
# from disk as part of the script being run than by us.
Set-WinPSDefaultFileEncoding Set-WinPSDefaultFileEncoding
} }

Loading…
Cancel
Save