|
| 1 | +# Custom test runner for OSDCloudCustomBuilder |
| 2 | +# This script provides a simple way to test module functions without requiring Pester |
| 3 | +param( |
| 4 | + [Parameter()] |
| 5 | + [ValidateSet('All', 'NewMedia', 'Driver', 'Script', 'Pwsh7')] |
| 6 | + [string]$TestType = 'All' |
| 7 | +) |
| 8 | + |
| 9 | +# Import required modules |
| 10 | +$ModuleRoot = Split-Path -Parent $PSScriptRoot |
| 11 | +Import-Module "$ModuleRoot\OSDCloudCustomBuilder.psd1" -Force |
| 12 | +Import-Module "$PSScriptRoot\TestHelpers\Admin-MockHelper.psm1" -Force |
| 13 | +Import-Module "$PSScriptRoot\TestHelpers\Interactive-MockHelper.psm1" -Force |
| 14 | + |
| 15 | +# Set up common test environment |
| 16 | +function Initialize-TestEnvironment { |
| 17 | + [CmdletBinding()] |
| 18 | + param() |
| 19 | + |
| 20 | + # Set admin context to true for all tests |
| 21 | + Set-TestAdminContext -IsAdmin $true -Verbose |
| 22 | + |
| 23 | + # Initialize logger |
| 24 | + Initialize-TestLogger -Verbose |
| 25 | + |
| 26 | + # Set up common test paths |
| 27 | + $script:TestPaths = @{ |
| 28 | + MediaPath = "C:\TestMedia" |
| 29 | + DriverPath = "C:\TestDrivers" |
| 30 | + ScriptPath = "C:\TestScripts" |
| 31 | + WimPath = "C:\TestWim" |
| 32 | + } |
| 33 | + |
| 34 | + # Initialize mock file system |
| 35 | + Initialize-MockFileSystem -MockedDirectories @( |
| 36 | + $script:TestPaths.MediaPath, |
| 37 | + $script:TestPaths.DriverPath, |
| 38 | + $script:TestPaths.ScriptPath, |
| 39 | + $script:TestPaths.WimPath |
| 40 | + ) -MockedFiles @( |
| 41 | + "$($script:TestPaths.WimPath)\boot.wim", |
| 42 | + "$($script:TestPaths.WimPath)\install.wim", |
| 43 | + "$($script:TestPaths.ScriptPath)\script.ps1" |
| 44 | + ) |
| 45 | + |
| 46 | + # Define common test output function |
| 47 | + function script:Test-Assert { |
| 48 | + param ( |
| 49 | + [Parameter(Mandatory)] |
| 50 | + [bool]$Condition, |
| 51 | + |
| 52 | + [Parameter(Mandatory)] |
| 53 | + [string]$Message, |
| 54 | + |
| 55 | + [Parameter()] |
| 56 | + [string]$TestName = "Unnamed Test" |
| 57 | + ) |
| 58 | + |
| 59 | + if ($Condition) { |
| 60 | + Write-Host "✓ PASS: $TestName - $Message" -ForegroundColor Green |
| 61 | + return $true |
| 62 | + } else { |
| 63 | + Write-Host "✗ FAIL: $TestName - $Message" -ForegroundColor Red |
| 64 | + return $false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + # Common functions to mock |
| 69 | + Mock -CommandName Test-ValidPath -MockWith { param($Path) return -not [string]::IsNullOrWhiteSpace($Path) } |
| 70 | + Mock -CommandName Initialize-OSDEnvironment -MockWith { return $true } |
| 71 | + Mock -CommandName New-Item -MockWith { |
| 72 | + param($Path, $ItemType) |
| 73 | + return [PSCustomObject]@{ |
| 74 | + PSPath = "FileSystem::$Path" |
| 75 | + FullName = $Path |
| 76 | + Exists = $true |
| 77 | + PSIsContainer = ($ItemType -eq 'Directory') |
| 78 | + } |
| 79 | + } |
| 80 | + Mock -CommandName Copy-Item -MockWith { return $true } |
| 81 | + |
| 82 | + Write-Host "Test environment initialized successfully" -ForegroundColor Cyan |
| 83 | +} |
| 84 | + |
| 85 | +function Test-NewOSDCloudCustomMedia { |
| 86 | + [CmdletBinding()] |
| 87 | + param() |
| 88 | + |
| 89 | + Write-Host "`n=== Testing New-OSDCloudCustomMedia ===" -ForegroundColor Cyan |
| 90 | + |
| 91 | + # Set up test parameters |
| 92 | + $mediaName = "TestMedia" |
| 93 | + $mediaPath = $script:TestPaths.MediaPath |
| 94 | + |
| 95 | + # Test valid parameters |
| 96 | + try { |
| 97 | + New-OSDCloudCustomMedia -Name $mediaName -Path $mediaPath -ErrorAction Stop |
| 98 | + Test-Assert -Condition $true -Message "Created media with valid parameters" -TestName "New-OSDCloudCustomMedia" |
| 99 | + } catch { |
| 100 | + Test-Assert -Condition $false -Message "Failed to create media: $_" -TestName "New-OSDCloudCustomMedia" |
| 101 | + } |
| 102 | + |
| 103 | + # Test invalid parameters |
| 104 | + try { |
| 105 | + New-OSDCloudCustomMedia -Name $mediaName -Path "" -ErrorAction Stop |
| 106 | + Test-Assert -Condition $false -Message "Should have failed with empty path" -TestName "New-OSDCloudCustomMedia-InvalidPath" |
| 107 | + } catch { |
| 108 | + Test-Assert -Condition $true -Message "Correctly failed with empty path" -TestName "New-OSDCloudCustomMedia-InvalidPath" |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function Test-AddOSDCloudCustomDriver { |
| 113 | + [CmdletBinding()] |
| 114 | + param() |
| 115 | + |
| 116 | + Write-Host "`n=== Testing Add-OSDCloudCustomDriver ===" -ForegroundColor Cyan |
| 117 | + |
| 118 | + # Test parameters |
| 119 | + $driverPath = $script:TestPaths.DriverPath |
| 120 | + |
| 121 | + # Test valid path |
| 122 | + try { |
| 123 | + Add-OSDCloudCustomDriver -DriverPath $driverPath -ErrorAction Stop |
| 124 | + Test-Assert -Condition $true -Message "Added driver with valid path" -TestName "Add-OSDCloudCustomDriver" |
| 125 | + } catch { |
| 126 | + Test-Assert -Condition $false -Message "Failed to add driver: $_" -TestName "Add-OSDCloudCustomDriver" |
| 127 | + } |
| 128 | + |
| 129 | + # Test invalid path |
| 130 | + try { |
| 131 | + Add-OSDCloudCustomDriver -DriverPath "" -ErrorAction Stop |
| 132 | + Test-Assert -Condition $false -Message "Should have failed with empty path" -TestName "Add-OSDCloudCustomDriver-InvalidPath" |
| 133 | + } catch { |
| 134 | + Test-Assert -Condition $true -Message "Correctly failed with empty path" -TestName "Add-OSDCloudCustomDriver-InvalidPath" |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +# Set verbose debugging output |
| 139 | +$VerbosePreference = "Continue" |
| 140 | + |
| 141 | +try { |
| 142 | + # Verify Test-IsAdmin is working |
| 143 | + Write-Host "Testing admin status:" -ForegroundColor Cyan |
| 144 | + try { |
| 145 | + $isAdmin = Test-IsAdmin |
| 146 | + Write-Host "Test-IsAdmin returns: $isAdmin" -ForegroundColor Cyan |
| 147 | + } catch { |
| 148 | + Write-Host "Error calling Test-IsAdmin: $_" -ForegroundColor Red |
| 149 | + # Check if the function is available |
| 150 | + if (Get-Command -Name Test-IsAdmin -ErrorAction SilentlyContinue) { |
| 151 | + Write-Host "Test-IsAdmin function is available" -ForegroundColor Yellow |
| 152 | + } else { |
| 153 | + Write-Host "Test-IsAdmin function is NOT available" -ForegroundColor Red |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + # Initialize test environment |
| 158 | + Initialize-TestEnvironment |
| 159 | + |
| 160 | + # Run appropriate tests based on test type |
| 161 | + switch ($TestType) { |
| 162 | + 'All' { |
| 163 | + Test-NewOSDCloudCustomMedia |
| 164 | + Test-AddOSDCloudCustomDriver |
| 165 | + } |
| 166 | + 'NewMedia' { |
| 167 | + Test-NewOSDCloudCustomMedia |
| 168 | + } |
| 169 | + 'Driver' { |
| 170 | + Test-AddOSDCloudCustomDriver |
| 171 | + } |
| 172 | + default { |
| 173 | + Write-Host "No tests selected to run" -ForegroundColor Yellow |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + Write-Host "`nAll tests completed!" -ForegroundColor Cyan |
| 178 | +} catch { |
| 179 | + Write-Host "Error in test execution: $_" -ForegroundColor Red |
| 180 | + Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red |
| 181 | +} |
0 commit comments