Skip to content

Commit e646c8f

Browse files
committed
Change logic for running tests on PSv3 and PSv4
Versioned module directories are not supported, so we need to change the name of the directory from <version> to PSScriptAnalyzer so it can be added to env:PSModulePath and work as expected
1 parent 787bd8a commit e646c8f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

build.psm1

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,38 @@ function Test-ScriptAnalyzer
287287
param ( [Parameter()][switch]$InProcess, [switch]$ShowAll )
288288

289289
END {
290-
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
290+
# versions 3 and 4 don't understand versioned module paths, so we need to rename the directory of the version to
291+
# the module name, and then set the ModulePath to that
292+
#
293+
# the layout of the build location is
294+
# .../out
295+
# /PSScriptAnalyzer
296+
# /1.18.0
297+
# /<modulefiles live here>
298+
# and ".../out" is added to env:PSModulePath
299+
# on v3 and v4, it will be
300+
# .../out
301+
# /PSScriptAnalyzer
302+
# /PSScriptAnalyzer
303+
# /<modulefiles live here>
304+
# and ".../out/PSScriptAnalyzer" is added to env:PSModulePath
305+
#
306+
#
307+
$major = $PSVersionTable.PSVersion.Major
308+
if ( $major -lt 5 ) {
309+
# get the directory name of the destination, we need to change it
310+
$versionDirectoryRoot = Split-Path $script:destinationDir
311+
$testModulePath = Join-Path $versionDirectoryRoot $analyzerName
312+
}
313+
else {
314+
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
315+
}
291316
$testResultsFile = "'$(Join-Path ${projectRoot} -childPath TestResults.xml)'"
292317
$testScripts = "'${projectRoot}\Tests\Engine','${projectRoot}\Tests\Rules','${projectRoot}\Tests\Documentation'"
293318
try {
319+
if ( $major -lt 5 ) {
320+
Rename-Item $script:destinationDir ${testModulePath}
321+
}
294322
$savedModulePath = $env:PSModulePath
295323
$env:PSModulePath = "${testModulePath}{0}${env:PSModulePath}" -f [System.IO.Path]::PathSeparator
296324
if ($ShowAll)
@@ -311,6 +339,9 @@ function Test-ScriptAnalyzer
311339
}
312340
finally {
313341
$env:PSModulePath = $savedModulePath
342+
if ( $major -lt 5 ) {
343+
Rename-Item ${testModulePath} ${script:destinationDir}
344+
}
314345
}
315346
}
316347
}

0 commit comments

Comments
 (0)