#!/usr/bin/env pwsh # fix-duplicate-packages.ps1 - Remove duplicate PackageReference items from test projects # These are already provided by Directory.Build.props param([switch]$DryRun) $packagesToRemove = @( "coverlet.collector", "Microsoft.NET.Test.Sdk", "Microsoft.AspNetCore.Mvc.Testing", "xunit", "xunit.runner.visualstudio", "Microsoft.Extensions.TimeProvider.Testing" ) $sharpCompressPackage = "SharpCompress" # Find all test project files $testProjects = Get-ChildItem -Path "src" -Filter "*.Tests.csproj" -Recurse $corpusProjects = Get-ChildItem -Path "src" -Filter "*.Corpus.*.csproj" -Recurse Write-Host "=== Fix Duplicate Package References ===" -ForegroundColor Cyan Write-Host "Found $($testProjects.Count) test projects" -ForegroundColor Yellow Write-Host "Found $($corpusProjects.Count) corpus projects (SharpCompress)" -ForegroundColor Yellow $fixedCount = 0 foreach ($proj in $testProjects) { $content = Get-Content $proj.FullName -Raw $modified = $false # Skip projects that opt out of common test infrastructure if ($content -match "\s*false\s*") { Write-Host " Skipped (UseConcelierTestInfra=false): $($proj.Name)" -ForegroundColor DarkGray continue } foreach ($pkg in $packagesToRemove) { # Match PackageReference for this package (various formats) $patterns = @( "(?s)\s*\r?\n?", "(?s)\s*\s*\r?\n?" ) foreach ($pattern in $patterns) { if ($content -match $pattern) { $content = $content -replace $pattern, "" $modified = $true } } } # Clean up empty ItemGroups $content = $content -replace "(?s)\s*\s*", "" # Clean up ItemGroups with only whitespace/comments $content = $content -replace "(?s)\s*\s*", "" if ($modified) { $fixedCount++ Write-Host " Fixed: $($proj.Name)" -ForegroundColor Green if (-not $DryRun) { $content | Set-Content $proj.FullName -NoNewline } } } # Fix SharpCompress in corpus projects foreach ($proj in $corpusProjects) { $content = Get-Content $proj.FullName -Raw $modified = $false $patterns = @( "(?s)\s*\r?\n?", "(?s)\s*\s*\r?\n?" ) foreach ($pattern in $patterns) { if ($content -match $pattern) { $content = $content -replace $pattern, "" $modified = $true } } # Clean up empty ItemGroups $content = $content -replace "(?s)\s*\s*", "" if ($modified) { $fixedCount++ Write-Host " Fixed: $($proj.Name)" -ForegroundColor Green if (-not $DryRun) { $content | Set-Content $proj.FullName -NoNewline } } } Write-Host "" Write-Host "Fixed $fixedCount projects" -ForegroundColor Cyan if ($DryRun) { Write-Host "(Dry run - no changes made)" -ForegroundColor Yellow }