|
| 1 | +# Test the AvoidConflicts parameter |
| 2 | +Write-Host "=== Testing AvoidConflicts Parameter ===" -ForegroundColor Cyan |
| 3 | + |
| 4 | +$testsPassed = 0 |
| 5 | +$testsFailed = 0 |
| 6 | + |
| 7 | +# Test 1: Import dbatools.library normally (without AvoidConflicts) |
| 8 | +Write-Host "`n--- Test 1: Default import without AvoidConflicts ---" -ForegroundColor Yellow |
| 9 | +try { |
| 10 | + Remove-Module dbatools.library -ErrorAction SilentlyContinue |
| 11 | + Import-Module ./artifacts/dbatools.library/dbatools.library.psd1 -Force |
| 12 | + |
| 13 | + $loaded = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Microsoft.Data.SqlClient' } |
| 14 | + if ($loaded) { |
| 15 | + Write-Host "[PASS] Microsoft.Data.SqlClient loaded by default" -ForegroundColor Green |
| 16 | + $testsPassed++ |
| 17 | + } else { |
| 18 | + Write-Host "[FAIL] Microsoft.Data.SqlClient not loaded" -ForegroundColor Red |
| 19 | + $testsFailed++ |
| 20 | + } |
| 21 | +} catch { |
| 22 | + Write-Host "[FAIL] Exception: $_" -ForegroundColor Red |
| 23 | + $testsFailed++ |
| 24 | +} |
| 25 | + |
| 26 | +# Test 2: Import SqlServer module first, then dbatools.library with AvoidConflicts |
| 27 | +Write-Host "`n--- Test 2: Import with AvoidConflicts after SqlServer module ---" -ForegroundColor Yellow |
| 28 | +try { |
| 29 | + Remove-Module dbatools.library -ErrorAction SilentlyContinue |
| 30 | + Remove-Module SqlServer -ErrorAction SilentlyContinue |
| 31 | + |
| 32 | + # Import SqlServer module first |
| 33 | + Write-Host "Importing SqlServer module..." -ForegroundColor Gray |
| 34 | + Import-Module SqlServer -ErrorAction Stop |
| 35 | + |
| 36 | + # Get the SqlClient version loaded by SqlServer |
| 37 | + $sqlServerClient = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Microsoft.Data.SqlClient' } |
| 38 | + if ($sqlServerClient) { |
| 39 | + Write-Host "SqlServer module loaded Microsoft.Data.SqlClient version: $($sqlServerClient.GetName().Version)" -ForegroundColor Gray |
| 40 | + } |
| 41 | + |
| 42 | + # Import dbatools.library with AvoidConflicts and capture verbose output |
| 43 | + Write-Host "Importing dbatools.library with -AvoidConflicts..." -ForegroundColor Gray |
| 44 | + $verboseOutput = Import-Module ./artifacts/dbatools.library/dbatools.library.psd1 -ArgumentList $true -Verbose 4>&1 | Out-String |
| 45 | + |
| 46 | + if ($verboseOutput -match "Skipping.*already loaded") { |
| 47 | + Write-Host "[PASS] Verbose output shows assemblies were skipped" -ForegroundColor Green |
| 48 | + Write-Host " Verbose message: $($verboseOutput -split "`n" | Where-Object { $_ -match 'Skipping' } | Select-Object -First 1)" -ForegroundColor Gray |
| 49 | + $testsPassed++ |
| 50 | + } else { |
| 51 | + Write-Host "[FAIL] No verbose output about skipping assemblies" -ForegroundColor Red |
| 52 | + Write-Host " Verbose output was: $verboseOutput" -ForegroundColor Gray |
| 53 | + $testsFailed++ |
| 54 | + } |
| 55 | +} catch { |
| 56 | + Write-Host "[FAIL] Exception: $_" -ForegroundColor Red |
| 57 | + $testsFailed++ |
| 58 | +} |
| 59 | + |
| 60 | +# Test 3: Verify module still functions with AvoidConflicts |
| 61 | +Write-Host "`n--- Test 3: Module functional after AvoidConflicts ---" -ForegroundColor Yellow |
| 62 | +try { |
| 63 | + if ([Microsoft.SqlServer.Management.Smo.Server] -as [type]) { |
| 64 | + Write-Host "[PASS] SMO types available after AvoidConflicts" -ForegroundColor Green |
| 65 | + $testsPassed++ |
| 66 | + } else { |
| 67 | + Write-Host "[FAIL] SMO types not available" -ForegroundColor Red |
| 68 | + $testsFailed++ |
| 69 | + } |
| 70 | +} catch { |
| 71 | + Write-Host "[FAIL] Exception: $_" -ForegroundColor Red |
| 72 | + $testsFailed++ |
| 73 | +} |
| 74 | + |
| 75 | +# Test 4: Verify no conflicts when importing both modules |
| 76 | +Write-Host "`n--- Test 4: No conflicts with both modules loaded ---" -ForegroundColor Yellow |
| 77 | +try { |
| 78 | + # Try using both modules |
| 79 | + $smoType = [Microsoft.SqlServer.Management.Smo.Server] |
| 80 | + $sqlClientType = [Microsoft.Data.SqlClient.SqlConnection] |
| 81 | + |
| 82 | + Write-Host "[PASS] Both modules can coexist without conflicts" -ForegroundColor Green |
| 83 | + Write-Host " SMO type available: $($smoType.FullName)" -ForegroundColor Gray |
| 84 | + Write-Host " SqlClient type available: $($sqlClientType.FullName)" -ForegroundColor Gray |
| 85 | + $testsPassed++ |
| 86 | +} catch { |
| 87 | + Write-Host "[FAIL] Exception: $_" -ForegroundColor Red |
| 88 | + $testsFailed++ |
| 89 | +} |
| 90 | + |
| 91 | +# Summary |
| 92 | +Write-Host "`n========================================" -ForegroundColor White |
| 93 | +Write-Host " TEST SUMMARY" -ForegroundColor White |
| 94 | +Write-Host "========================================" -ForegroundColor White |
| 95 | +Write-Host "Passed: $testsPassed" -ForegroundColor Green |
| 96 | +Write-Host "Failed: $testsFailed" -ForegroundColor $(if ($testsFailed -gt 0) { 'Red' } else { 'Green' }) |
| 97 | +Write-Host "Total: $($testsPassed + $testsFailed)" -ForegroundColor White |
| 98 | + |
| 99 | +if ($testsFailed -eq 0) { |
| 100 | + Write-Host "`nALL AVOIDCONFLICTS TESTS PASSED!" -ForegroundColor Green |
| 101 | + exit 0 |
| 102 | +} else { |
| 103 | + Write-Host "`nSOME TESTS FAILED - REVIEW BEFORE MERGE" -ForegroundColor Red |
| 104 | + exit 1 |
| 105 | +} |
0 commit comments