106 lines
5.2 KiB
PowerShell
106 lines
5.2 KiB
PowerShell
#Requires -Version 5.1
|
|
# Offline binding tests: keep the real parameter declaration and shim, replace
|
|
# the inventory body with a probe, and mock downloads. No registry/COM operations.
|
|
$ErrorActionPreference = 'Stop'
|
|
$DriveTestRepo = Split-Path $PSScriptRoot -Parent
|
|
$DriveTestTokens = $null
|
|
$DriveTestErrors = $null
|
|
$DriveTestToolsAst = [Management.Automation.Language.Parser]::ParseFile(
|
|
(Join-Path $DriveTestRepo 'Tools.ps1'), [ref]$DriveTestTokens, [ref]$DriveTestErrors)
|
|
If ($DriveTestErrors.Count) { Throw ($DriveTestErrors | Out-String) }
|
|
$DriveTestRunDefinition = $DriveTestToolsAst.Find({
|
|
param($Node)
|
|
$Node -is [Management.Automation.Language.FunctionDefinitionAst] -and $Node.Name -eq 'Run-Script'
|
|
}, $true)
|
|
If (!$DriveTestRunDefinition) { Throw 'Missing helper: Run-Script' }
|
|
. ([scriptblock]::Create($DriveTestRunDefinition.Extent.Text))
|
|
|
|
$DriveTestInventoryAst = [Management.Automation.Language.Parser]::ParseFile(
|
|
(Join-Path $DriveTestRepo 'Get-AllNetworkDrives.ps1'), [ref]$DriveTestTokens, [ref]$DriveTestErrors)
|
|
If ($DriveTestErrors.Count) { Throw ($DriveTestErrors | Out-String) }
|
|
$DriveTestProbeSource = $DriveTestInventoryAst.ParamBlock.Extent.Text + [Environment]::NewLine + @'
|
|
[pscustomobject]@{
|
|
Probe = 'NetworkDrives'
|
|
SavedOnly = $SavedOnly.IsPresent
|
|
CallerValue = $DriveTestCallerValue
|
|
}
|
|
'@
|
|
$DriveTestProbe = [scriptblock]::Create($DriveTestProbeSource)
|
|
|
|
Function IsURLValid { Return $true }
|
|
Function Download-File { Throw 'This fix must retain the existing Run-Script download path.' }
|
|
Function New-Object {
|
|
param([string]$TypeName)
|
|
If ($TypeName -ne 'Net.WebClient') { Throw "Unexpected object in test: $TypeName" }
|
|
$Client = [pscustomobject]@{}
|
|
$Client | Add-Member -MemberType ScriptMethod -Name DownloadString -Value {
|
|
param($URL)
|
|
$script:DriveTestWebDownloads++
|
|
Switch ($URL) {
|
|
'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1' {
|
|
Return $script:DriveTestRunDefinition.Extent.Text
|
|
}
|
|
'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Get-AllNetworkDrives.ps1' {
|
|
Return $script:DriveTestProbeSource
|
|
}
|
|
default { Throw "Unexpected URL in test: $URL" }
|
|
}
|
|
}
|
|
Return $Client
|
|
}
|
|
|
|
Function Test-DriveRmmBinding {
|
|
param([string]$Name, [hashtable]$Parameters, [switch]$UseShim)
|
|
$script:DriveTestWebDownloads = 0
|
|
$DriveTestCallerValue = 'Caller value with spaces & symbols'
|
|
$Output = @(If ($UseShim) {
|
|
. (Join-Path $DriveTestRepo 'rmm\Get-AllNetworkDrives.ps1')
|
|
} Else {
|
|
Run-Script -LivePSScript Get-AllNetworkDrives @Parameters
|
|
})
|
|
$Probe = @($Output | Where-Object { $_.PSObject.Properties['Probe'] })
|
|
If ($Probe.Count -ne 1 -or $Probe[0].SavedOnly -ne $false -or
|
|
$Probe[0].CallerValue -cne $DriveTestCallerValue) {
|
|
Throw "${Name}: must run with SavedOnly false and retain access to caller variables."
|
|
}
|
|
$ExpectedDownloads = If ($UseShim) { 2 } Else { 1 }
|
|
If ($script:DriveTestWebDownloads -ne $ExpectedDownloads) { Throw "${Name}: unexpected download path." }
|
|
Write-Host "PASS: ${Name}: empty argument tolerated, caller variables visible, existing download path retained."
|
|
}
|
|
|
|
Test-DriveRmmBinding -Name 'Omitted Arguments' -Parameters @{}
|
|
Test-DriveRmmBinding -Name 'Null Arguments' -Parameters @{Arguments=$null}
|
|
Test-DriveRmmBinding -Name 'Empty Arguments' -Parameters @{Arguments=''}
|
|
Test-DriveRmmBinding -Name 'Legacy null sentinel' -Parameters @{Arguments='null'}
|
|
Test-DriveRmmBinding -Name 'Whitespace Arguments' -Parameters @{Arguments=" `t`r`n "}
|
|
Test-DriveRmmBinding -Name 'Real Syncro shim' -UseShim
|
|
|
|
$DriveTestCallerValue = 'Direct caller'
|
|
Foreach ($Case in @(
|
|
@{Name='No parameters'; Parameters=@{}; Expected=$false}
|
|
@{Name='SavedOnly true'; Parameters=@{SavedOnly=$true}; Expected=$true}
|
|
@{Name='SavedOnly false'; Parameters=@{SavedOnly=$false}; Expected=$false}
|
|
)) {
|
|
$Parameters = $Case.Parameters
|
|
$Probe = @(& $DriveTestProbe @Parameters)
|
|
If ($Probe.Count -ne 1 -or $Probe[0].SavedOnly -ne $Case.Expected) { Throw "Direct invocation failed: $($Case.Name)" }
|
|
}
|
|
$Probe = & $DriveTestProbe -SavedOnly
|
|
If ($Probe.SavedOnly -ne $true) { Throw 'The bare -SavedOnly switch must still enable saved-only inventory.' }
|
|
& {
|
|
$Probe = . $DriveTestProbe -SavedOnly:$false
|
|
If ($Probe.SavedOnly -ne $false -or $Probe.CallerValue -cne $DriveTestCallerValue) {
|
|
Throw 'Dot-sourced invocation must preserve switch behavior and caller-variable access.'
|
|
}
|
|
}
|
|
Write-Host 'PASS: direct and dot-sourced invocations preserve optional SavedOnly behavior.'
|
|
|
|
Foreach ($UnexpectedArgument in @('false', 'true', '-SavedOnly', 'unexpected')) {
|
|
$Caught = $null
|
|
Try { Invoke-Command -ScriptBlock $DriveTestProbe -ArgumentList $UnexpectedArgument | Out-Null }
|
|
Catch { $Caught = $_ }
|
|
If ($null -eq $Caught) { Throw "Unexpected positional input was silently accepted: $UnexpectedArgument" }
|
|
}
|
|
Write-Host 'PASS: nonblank positional input is rejected instead of silently ignored.'
|
|
Write-Host "All Get-AllNetworkDrives binding tests passed on PowerShell $($PSVersionTable.PSVersion), $([IntPtr]::Size * 8)-bit."
|