Skip to content

Commit e43cb45

Browse files
Add AvoidConflicts integration tests
- Created test-avoidconflicts.ps1 with comprehensive test scenarios - Created test-avoidconflicts.yml workflow file - Tests validate default import, SqlServer module compatibility, and verbose logging - Workflow uses psmodulecache for efficient SqlServer module installation Co-authored-by: Chrissy LeMaire <potatoqualitee@users.noreply.github.com>
1 parent 7cf9efe commit e43cb45

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

tests/test-avoidconflicts.ps1

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

tests/test-avoidconflicts.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Test AvoidConflicts
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
paths:
7+
- 'dbatools.library.psm1'
8+
- 'tests/test-avoidconflicts.ps1'
9+
- '.github/workflows/test-avoidconflicts.yml'
10+
11+
defaults:
12+
run:
13+
shell: pwsh
14+
15+
jobs:
16+
test-avoidconflicts:
17+
runs-on: windows-latest
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Setup .NET
23+
uses: actions/setup-dotnet@v4
24+
with:
25+
dotnet-version: |
26+
8.0.x
27+
6.0.x
28+
29+
- name: Install .NET Framework targeting packs
30+
shell: pwsh
31+
run: |
32+
choco install netfx-4.7.2-devpack -y --no-progress
33+
34+
- name: Cache SqlServer PowerShell module
35+
uses: potatoqualitee/psmodulecache@v6.2
36+
with:
37+
modules-to-cache: SqlServer
38+
shell: pwsh
39+
40+
- name: Build the library
41+
shell: pwsh
42+
run: |
43+
./build/build.ps1
44+
45+
- name: Install SqlServer module
46+
shell: pwsh
47+
run: |
48+
if (-not (Get-Module -ListAvailable -Name SqlServer)) {
49+
Write-Host "Installing SqlServer module..." -ForegroundColor Cyan
50+
Install-Module -Name SqlServer -Force -AllowClobber -SkipPublisherCheck
51+
} else {
52+
Write-Host "SqlServer module already installed" -ForegroundColor Green
53+
}
54+
55+
# Verify installation
56+
$module = Get-Module -ListAvailable -Name SqlServer | Select-Object -First 1
57+
Write-Host "SqlServer module version: $($module.Version)" -ForegroundColor Cyan
58+
59+
- name: Run AvoidConflicts tests
60+
shell: pwsh
61+
run: |
62+
./tests/test-avoidconflicts.ps1

0 commit comments

Comments
 (0)