updates
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
#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."
|
||||
@@ -77,10 +77,13 @@ try {
|
||||
[IO.File]::WriteAllText($OutFile, 'downloaded fixture')
|
||||
}
|
||||
$script:downloadCalls=0; $script:downloadRecovers=$true
|
||||
Save-OfficeDownload 'https://fixture.invalid/' (Join-Path $testRoot 'download') 2>$null
|
||||
$output = @(Save-OfficeDownload 'https://fixture.invalid/' (Join-Path $testRoot 'download') 2>&1)
|
||||
Assert-OfficeTest ($script:downloadCalls -eq 2 -and (Get-Content -LiteralPath (Join-Path $testRoot 'download') -Raw) -eq 'downloaded fixture') 'A transient download error should recover.'
|
||||
Assert-OfficeTest ($output.Count -eq 0) 'A recovered download must not emit success-stream diagnostics or Syncro errors.'
|
||||
$log = Get-Content -LiteralPath $Global:LogFile -Raw
|
||||
Assert-OfficeTest ($log -match 'Download attempt 1 of 3 failed for https://fixture.invalid/' -and $log -match 'Temporary network failure Retrying in 3 seconds' -and $log -match 'Download recovered on attempt 2: https://fixture.invalid/') 'Retry details and recovery must remain in the Tools diagnostic log.'
|
||||
$script:downloadCalls=0; $script:downloadRecovers=$false
|
||||
Assert-OfficeThrows { Save-OfficeDownload 'https://fixture.invalid/' (Join-Path $testRoot 'download') 2>$null } 'Temporary network failure'
|
||||
Assert-OfficeThrows { Save-OfficeDownload 'https://fixture.invalid/' (Join-Path $testRoot 'download') } 'Download failed after 3 attempts: https://fixture.invalid/.*Temporary network failure'
|
||||
Assert-OfficeTest ($script:downloadCalls -eq (Get-OfficeDeploymentDefinitions).DownloadAttempts) 'Persistent download failure must remain bounded.'
|
||||
}
|
||||
& {
|
||||
|
||||
Reference in New Issue
Block a user