Skip to content

Commit c326cbe

Browse files
committed
test: fix managed NUnit discovery
1 parent bd246f3 commit c326cbe

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

test.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,105 @@ if (-not (Test-Path $helpersPath)) {
9696
}
9797
Import-Module $helpersPath -Force
9898

99+
function Add-UniquePath {
100+
param(
101+
[System.Collections.Generic.List[string]]$Paths,
102+
[string]$Path
103+
)
104+
105+
if ([string]::IsNullOrWhiteSpace($Path)) {
106+
return
107+
}
108+
109+
$fullPath = [System.IO.Path]::GetFullPath($Path)
110+
foreach ($existingPath in $Paths) {
111+
if ([string]::Equals($existingPath, $fullPath, [System.StringComparison]::OrdinalIgnoreCase)) {
112+
return
113+
}
114+
}
115+
116+
$Paths.Add($fullPath)
117+
}
118+
119+
function Get-CentralPackageVersion {
120+
param(
121+
[string]$PackagesPropsPath,
122+
[string]$PackageName
123+
)
124+
125+
if (-not (Test-Path -LiteralPath $PackagesPropsPath -PathType Leaf)) {
126+
return $null
127+
}
128+
129+
try {
130+
[xml]$packagesProps = Get-Content -LiteralPath $PackagesPropsPath -Raw
131+
$packageNode = $packagesProps.Project.ItemGroup.PackageVersion |
132+
Where-Object { $_.Include -eq $PackageName -or $_.Update -eq $PackageName } |
133+
Select-Object -Last 1
134+
135+
if ($packageNode) {
136+
return $packageNode.Version
137+
}
138+
}
139+
catch {
140+
Write-Host "[WARN] Could not read $PackagesPropsPath for $PackageName version." -ForegroundColor Yellow
141+
}
142+
143+
return $null
144+
}
145+
146+
function Get-NUnitTestAdapterPaths {
147+
param(
148+
[string]$RepoRoot,
149+
[string[]]$TestDlls
150+
)
151+
152+
$adapterPaths = New-Object System.Collections.Generic.List[string]
153+
154+
$packagesPropsPath = Join-Path $RepoRoot 'Directory.Packages.props'
155+
$adapterVersion = Get-CentralPackageVersion -PackagesPropsPath $packagesPropsPath -PackageName 'NUnit3TestAdapter'
156+
if (-not [string]::IsNullOrWhiteSpace($adapterVersion)) {
157+
$adapterPath = Join-Path $RepoRoot "packages/nunit3testadapter/$adapterVersion/build/net462"
158+
if (Test-Path -LiteralPath (Join-Path $adapterPath 'NUnit3.TestAdapter.dll') -PathType Leaf) {
159+
Add-UniquePath -Paths $adapterPaths -Path $adapterPath
160+
return $adapterPaths.ToArray()
161+
}
162+
}
163+
164+
foreach ($testDll in $TestDlls) {
165+
if ([string]::IsNullOrWhiteSpace($testDll)) {
166+
continue
167+
}
168+
169+
$testDir = Split-Path $testDll -Parent
170+
if ($testDir -and
171+
(Test-Path -LiteralPath (Join-Path $testDir 'NUnit3.TestAdapter.dll') -PathType Leaf) -and
172+
(Test-Path -LiteralPath (Join-Path $testDir 'nunit.engine.dll') -PathType Leaf)) {
173+
Add-UniquePath -Paths $adapterPaths -Path $testDir
174+
}
175+
}
176+
177+
if ($adapterPaths.Count -gt 0) {
178+
return $adapterPaths.ToArray()
179+
}
180+
181+
$packagesRoot = Join-Path $RepoRoot 'packages/nunit3testadapter'
182+
if (Test-Path -LiteralPath $packagesRoot -PathType Container) {
183+
$packageDirs = Get-ChildItem -LiteralPath $packagesRoot -Directory -ErrorAction SilentlyContinue |
184+
Sort-Object Name -Descending
185+
186+
foreach ($packageDir in $packageDirs) {
187+
$adapterPath = Join-Path $packageDir.FullName 'build/net462'
188+
if (Test-Path -LiteralPath (Join-Path $adapterPath 'NUnit3.TestAdapter.dll') -PathType Leaf) {
189+
Add-UniquePath -Paths $adapterPaths -Path $adapterPath
190+
break
191+
}
192+
}
193+
}
194+
195+
return $adapterPaths.ToArray()
196+
}
197+
99198
# =============================================================================
100199
# Environment Setup
101200
# =============================================================================
@@ -429,6 +528,11 @@ try {
429528
$vstestArgs += "/Settings:$runSettingsPath"
430529
$vstestArgs += "/ResultsDirectory:$resultsDir"
431530

531+
$nunitAdapterPaths = @(Get-NUnitTestAdapterPaths -RepoRoot $PSScriptRoot -TestDlls $testDlls)
532+
foreach ($adapterPath in $nunitAdapterPaths) {
533+
$vstestArgs += "/TestAdapterPath:$adapterPath"
534+
}
535+
432536
# Logger configuration - verbosity goes with the console logger
433537
$verbosityMap = @{
434538
'quiet' = 'quiet'; 'q' = 'quiet'
@@ -454,6 +558,9 @@ try {
454558

455559
Write-Host ""
456560
Write-Host "Running tests..." -ForegroundColor Cyan
561+
foreach ($adapterPath in $nunitAdapterPaths) {
562+
Write-Host " NUnit adapter path: $adapterPath" -ForegroundColor DarkGray
563+
}
457564
Write-Host " vstest.console.exe $($vstestArgs -join ' ')" -ForegroundColor DarkGray
458565
Write-Host ""
459566

@@ -506,6 +613,9 @@ try {
506613
$singleArgs += "/Platform:x64"
507614
$singleArgs += "/Settings:$runSettingsPath"
508615
$singleArgs += "/ResultsDirectory:$resultsDir"
616+
foreach ($adapterPath in $nunitAdapterPaths) {
617+
$singleArgs += "/TestAdapterPath:$adapterPath"
618+
}
509619
$singleArgs += "/Logger:trx;LogFileName=${dllName}_${timestamp}.trx"
510620
$singleArgs += "/Logger:console;verbosity=$vstestVerbosity"
511621

0 commit comments

Comments
 (0)