Skip to content

Commit f3608bf

Browse files
committed
Merge branch 'development' of https://github.com/PowerShell/PSScriptAnalyzer into SaveDscDependencyOnLinux
# Conflicts: # Tests/Engine/ModuleDependencyHandler.tests.ps1
2 parents 1261df2 + 6c946c0 commit f3608bf

File tree

6 files changed

+103
-47
lines changed

6 files changed

+103
-47
lines changed

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Describe "Resolve DSC Resource Dependency" {
1717
}
1818
}
1919

20-
$SavedPSModulePath = $env:PSModulePath
20+
$savedPSModulePath = $env:PSModulePath
2121
$violationFileName = 'MissingDSCResource.ps1'
2222
$violationFilePath = Join-Path $directory $violationFileName
2323
$testRootDirectory = Split-Path -Parent $directory
@@ -36,15 +36,19 @@ Describe "Resolve DSC Resource Dependency" {
3636
}
3737
AfterAll {
3838
if ( $skipTest ) { return }
39-
$env:PSModulePath = $SavedPSModulePath
39+
$env:PSModulePath = $savedPSModulePath
4040
}
4141

4242
Context "Module handler class" {
4343
BeforeAll {
4444
if ($PSversionTable.PSVersion -lt [Version]'5.0.0') { return }
4545
$moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]
4646
$oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key
47-
$oldPSModulePath = $env:PSModulePath
47+
$savedPSModulePath = $env:PSModulePath
48+
}
49+
AfterAll {
50+
if ( $skipTest ) { return }
51+
$env:PSModulePath = $savedPSModulePath
4852
}
4953
It "Sets defaults correctly" -Skip:($PSversionTable.PSVersion -lt [Version]'5.0.0') {
5054
$rsp = [runspacefactory]::CreateRunspace()
@@ -66,7 +70,7 @@ Describe "Resolve DSC Resource Dependency" {
6670
$expectedPssaAppDataPath = Join-Path $depHandler.LocalAppDataPath "PSScriptAnalyzer"
6771
$depHandler.PSSAAppDataPath | Should -Be $expectedPssaAppDataPath
6872

69-
$expectedPSModulePath = $oldPSModulePath + [System.IO.Path]::PathSeparator + $depHandler.TempModulePath
73+
$expectedPSModulePath = $savedPSModulePath + [System.IO.Path]::PathSeparator + $depHandler.TempModulePath
7074
$env:PSModulePath | Should -Be $expectedPSModulePath
7175

7276
$depHandler.Dispose()
@@ -186,7 +190,7 @@ Describe "Resolve DSC Resource Dependency" {
186190
# Save the current environment variables
187191
$oldLocalAppDataPath = $env:LOCALAPPDATA
188192
$oldTempPath = $env:TEMP
189-
$oldPSModulePath = $env:PSModulePath
193+
$savedPSModulePath = $env:PSModulePath
190194

191195
# set the environment variables
192196
$tempPath = Join-Path $oldTempPath ([guid]::NewGUID()).ToString()
@@ -196,8 +200,8 @@ Describe "Resolve DSC Resource Dependency" {
196200
$env:TEMP = $newTempPath
197201

198202
# create the temporary directories
199-
New-Item -Type Directory -Path $newLocalAppDataPath
200-
New-Item -Type Directory -Path $newTempPath
203+
New-Item -Type Directory -Path $newLocalAppDataPath -force
204+
New-Item -Type Directory -Path $newTempPath -force
201205

202206
# create and dispose module dependency handler object
203207
# to setup the temporary module
@@ -215,7 +219,8 @@ Describe "Resolve DSC Resource Dependency" {
215219
}
216220

217221
AfterAll {
218-
$env:PSModulePath = $oldPSModulePath
222+
if ( $skipTest ) { return }
223+
$env:PSModulePath = $savedPSModulePath
219224
}
220225

221226
It "has a single parse error" -skip:$skipTest {
@@ -229,7 +234,7 @@ Describe "Resolve DSC Resource Dependency" {
229234

230235
It "Keeps PSModulePath unchanged before and after invocation" -skip:$skipTest {
231236
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
232-
$env:PSModulePath | Should -Be $oldPSModulePath
237+
$env:PSModulePath | Should -Be $savedPSModulePath
233238
}
234239

235240
if (!$skipTest)

Tests/Rules/DscExamplesPresent.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
2323
}
2424

2525
Context "When examples present" {
26-
New-Item -Path $examplesPath -ItemType Directory
27-
New-Item -Path "$examplesPath\FileResource_Example.psm1" -ItemType File
26+
New-Item -Path $examplesPath -ItemType Directory -force
27+
New-Item -Path "$examplesPath\FileResource_Example.psm1" -ItemType File -force
2828

2929
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName}
3030

@@ -57,8 +57,8 @@ Describe "DscExamplesPresent rule in regular (non-class) based resource" {
5757
}
5858

5959
Context "When examples present" {
60-
New-Item -Path $examplesPath -ItemType Directory
61-
New-Item -Path "$examplesPath\MSFT_WaitForAll_Example.psm1" -ItemType File
60+
New-Item -Path $examplesPath -ItemType Directory -force
61+
New-Item -Path "$examplesPath\MSFT_WaitForAll_Example.psm1" -ItemType File -force
6262

6363
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName}
6464

Tests/Rules/DscTestsPresent.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
2323
}
2424

2525
Context "When tests present" {
26-
New-Item -Path $testsPath -ItemType Directory
27-
New-Item -Path "$testsPath\FileResource_Test.psm1" -ItemType File
26+
New-Item -Path $testsPath -ItemType Directory -force
27+
New-Item -Path "$testsPath\FileResource_Test.psm1" -ItemType File -force
2828

2929
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName}
3030

@@ -57,8 +57,8 @@ Describe "DscTestsPresent rule in regular (non-class) based resource" {
5757
}
5858

5959
Context "When tests present" {
60-
New-Item -Path $testsPath -ItemType Directory
61-
New-Item -Path "$testsPath\MSFT_WaitForAll_Test.psm1" -ItemType File
60+
New-Item -Path $testsPath -ItemType Directory -force
61+
New-Item -Path "$testsPath\MSFT_WaitForAll_Test.psm1" -ItemType File -force
6262

6363
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName}
6464

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Describe "UseIdenticalMandatoryParametersForDSC" {
6363
}
6464
"@
6565
# and under it a directory called dscresources\something
66-
New-Item -ItemType Directory -Path $noParentClassDir
66+
New-Item -ItemType Directory -Path $noParentClassDir -force
6767
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.psm1'
6868
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof'
6969

@@ -102,7 +102,7 @@ class ClassWithNoParent
102102
}
103103
"@
104104
# and under it a directory called dscresources\something
105-
New-Item -ItemType Directory -Path $noParentClassDir
105+
New-Item -ItemType Directory -Path $noParentClassDir -force
106106
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.psm1'
107107
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.schema.mof'
108108

build.psm1

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33

44
# Build module for PowerShell ScriptAnalyzer
55
$projectRoot = $PSScriptRoot
6-
$destinationDir = Join-Path -Path $projectRoot -ChildPath (Join-Path -Path "out" -ChildPath "PSScriptAnalyzer")
6+
$analyzerName = "PSScriptAnalyzer"
7+
8+
function Get-AnalyzerVersion
9+
{
10+
$csprojPath = [io.path]::Combine($projectRoot,"Engine","Engine.csproj")
11+
$xml = [xml](Get-Content "${csprojPath}")
12+
$xml.SelectSingleNode(".//VersionPrefix")."#text"
13+
}
14+
15+
$analyzerVersion = Get-AnalyzerVersion
16+
# location where analyzer goes
17+
$script:destinationDir = [io.path]::Combine($projectRoot,"out","${analyzerName}", $analyzerVersion)
718

819
function Publish-File
920
{
@@ -40,7 +51,7 @@ function Get-UserModulePath
4051
function Uninstall-ScriptAnalyzer
4152
{
4253
[CmdletBinding(SupportsShouldProcess)]
43-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
54+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
4455
END {
4556
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
4657
Remove-Item -Recurse -Path "$ModulePath" -Force
@@ -52,10 +63,10 @@ function Uninstall-ScriptAnalyzer
5263
function Install-ScriptAnalyzer
5364
{
5465
[CmdletBinding(SupportsShouldProcess)]
55-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
66+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
5667
END {
5768
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
58-
Copy-Item -Recurse -Path "$destinationDir" -Destination "$ModulePath\." -Force
69+
Copy-Item -Recurse -Path "$script:destinationDir" -Destination "$ModulePath\." -Force
5970
}
6071
}
6172
}
@@ -64,7 +75,7 @@ function Install-ScriptAnalyzer
6475
function Uninstall-ScriptAnalyzer
6576
{
6677
[CmdletBinding(SupportsShouldProcess)]
67-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
78+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
6879
END {
6980
if ((Test-Path $ModulePath) -and (Get-Item $ModulePath).PSIsContainer )
7081
{
@@ -79,9 +90,9 @@ function Remove-Build
7990
[CmdletBinding(SupportsShouldProcess=$true)]
8091
param ()
8192
END {
82-
if ( $PSCmdlet.ShouldProcess("${destinationDir}")) {
83-
if ( Test-Path ${destinationDir} ) {
84-
Remove-Item -Force -Recurse ${destinationDir}
93+
if ( $PSCmdlet.ShouldProcess("${script:destinationDir}")) {
94+
if ( Test-Path ${script:destinationDir} ) {
95+
Remove-Item -Force -Recurse ${script:destinationDir}
8596
}
8697
}
8798
}
@@ -92,7 +103,7 @@ function Start-DocumentationBuild
92103
{
93104
$docsPath = Join-Path $projectRoot docs
94105
$markdownDocsPath = Join-Path $docsPath markdown
95-
$outputDocsPath = Join-Path $destinationDir en-US
106+
$outputDocsPath = Join-Path $script:destinationDir en-US
96107
$platyPS = Get-Module -ListAvailable platyPS
97108
if ($null -eq $platyPS -or ($platyPS | Sort-Object Version -Descending | Select-Object -First 1).Version -lt [version]0.12)
98109
{
@@ -118,12 +129,12 @@ function Copy-CompatibilityProfiles
118129
}
119130

120131
$profileDir = [System.IO.Path]::Combine($PSScriptRoot, 'PSCompatibilityCollector', 'profiles')
121-
$destinationDir = [System.IO.Path]::Combine($PSScriptRoot, 'out', 'PSScriptAnalyzer', "compatibility_profiles")
122-
if ( -not (Test-Path $destinationDir) ) {
123-
$null = New-Item -Type Directory $destinationDir
132+
$targetProfileDir = [io.path]::Combine($script:destinationDir,"compatibility_profiles")
133+
if ( -not (Test-Path $targetProfileDir) ) {
134+
$null = New-Item -Type Directory $targetProfileDir
124135
}
125136

126-
Copy-Item -Force $profileDir/* $destinationDir
137+
Copy-Item -Force $profileDir/* $targetProfileDir
127138
}
128139

129140
# build script analyzer (and optionally build everything with -All)
@@ -162,9 +173,6 @@ function Start-ScriptAnalyzerBuild
162173
Start-DocumentationBuild
163174
}
164175

165-
# Destination for the composed module when built
166-
$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
167-
168176
if ( $All )
169177
{
170178
# Build all the versions of the analyzer
@@ -205,24 +213,23 @@ function Start-ScriptAnalyzerBuild
205213
"$projectRoot\Engine\ScriptAnalyzer.format.ps1xml", "$projectRoot\Engine\ScriptAnalyzer.types.ps1xml"
206214
)
207215

208-
$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
209216
switch ($PSVersion)
210217
{
211218
3
212219
{
213-
$destinationDirBinaries = "$destinationDir\PSv3"
220+
$destinationDirBinaries = "$script:destinationDir\PSv3"
214221
}
215222
4
216223
{
217-
$destinationDirBinaries = "$destinationDir\PSv4"
224+
$destinationDirBinaries = "$script:destinationDir\PSv4"
218225
}
219226
5
220227
{
221-
$destinationDirBinaries = "$destinationDir"
228+
$destinationDirBinaries = "$script:destinationDir"
222229
}
223230
6
224231
{
225-
$destinationDirBinaries = "$destinationDir\coreclr"
232+
$destinationDirBinaries = "$script:destinationDir\coreclr"
226233
}
227234
default
228235
{
@@ -252,7 +259,7 @@ function Start-ScriptAnalyzerBuild
252259
Pop-Location
253260
}
254261

255-
Publish-File $itemsToCopyCommon $destinationDir
262+
Publish-File $itemsToCopyCommon $script:destinationDir
256263

257264
$itemsToCopyBinaries = @(
258265
"$projectRoot\Engine\bin\${config}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
@@ -262,7 +269,7 @@ function Start-ScriptAnalyzerBuild
262269
Publish-File $itemsToCopyBinaries $destinationDirBinaries
263270

264271
$settingsFiles = Get-Childitem "$projectRoot\Engine\Settings" | ForEach-Object -MemberName FullName
265-
Publish-File $settingsFiles (Join-Path -Path $destinationDir -ChildPath Settings)
272+
Publish-File $settingsFiles (Join-Path -Path $script:destinationDir -ChildPath Settings)
266273

267274
if ($framework -eq 'net452') {
268275
Copy-Item -path "$projectRoot\Rules\bin\${config}\${framework}\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
@@ -280,10 +287,38 @@ function Test-ScriptAnalyzer
280287
param ( [Parameter()][switch]$InProcess, [switch]$ShowAll )
281288

282289
END {
283-
$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+
}
284316
$testResultsFile = "'$(Join-Path ${projectRoot} -childPath TestResults.xml)'"
285317
$testScripts = "'${projectRoot}\Tests\Engine','${projectRoot}\Tests\Rules','${projectRoot}\Tests\Documentation'"
286318
try {
319+
if ( $major -lt 5 ) {
320+
Rename-Item $script:destinationDir ${testModulePath}
321+
}
287322
$savedModulePath = $env:PSModulePath
288323
$env:PSModulePath = "${testModulePath}{0}${env:PSModulePath}" -f [System.IO.Path]::PathSeparator
289324
if ($ShowAll)
@@ -304,6 +339,9 @@ function Test-ScriptAnalyzer
304339
}
305340
finally {
306341
$env:PSModulePath = $savedModulePath
342+
if ( $major -lt 5 ) {
343+
Rename-Item ${testModulePath} ${script:destinationDir}
344+
}
307345
}
308346
}
309347
}

tools/appveyor.psm1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,23 @@ function Invoke-AppveyorTest {
5151
Write-Verbose -Verbose ("Running tests on PowerShell version " + $PSVersionTable.PSVersion)
5252
Write-Verbose -Verbose "Language set to '${env:LANG}'"
5353

54-
# Copy the generated modules into the out directory
55-
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1
56-
Copy-Item "${CheckoutPath}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force
57-
Copy-Item "${CheckoutPath}\PSCompatibilityCollector\out\PSCompatibilityCollector" "$modulePath\" -Recurse -Force
54+
# set up env:PSModulePath to the build location, don't copy it to the "normal place"
55+
$analyzerVersion = ([xml](Get-Content "${CheckoutPath}\Engine\Engine.csproj")).SelectSingleNode(".//VersionPrefix")."#text".Trim()
56+
$majorVersion = ([System.Version]$analyzerVersion).Major
57+
$psMajorVersion = $PSVersionTable.PSVersion.Major
58+
59+
if ( $psMajorVersion -lt 5 ) {
60+
$versionModuleDir = "${CheckoutPath}\out\PSScriptAnalyzer\${analyzerVersion}"
61+
$renameTarget = "${CheckoutPath}\out\PSScriptAnalyzer\PSScriptAnalyzer"
62+
Rename-Item "${versionModuleDir}" "${renameTarget}"
63+
$moduleDir = "${CheckoutPath}\out\PSScriptAnalyzer"
64+
}
65+
else{
66+
$moduleDir = "${CheckoutPath}\out"
67+
}
68+
69+
$env:PSModulePath = "${moduleDir}","${env:PSModulePath}" -join [System.IO.Path]::PathSeparator
70+
Write-Verbose -Verbose "module path: ${env:PSModulePath}"
5871

5972
# Set up testing assets
6073
$testResultsPath = Join-Path ${CheckoutPath} TestResults.xml

0 commit comments

Comments
 (0)