From 253506b341a03653624322fe55f8d67b02720e7f Mon Sep 17 00:00:00 2001 From: Shawn Sesna Date: Wed, 30 Oct 2019 09:02:41 -0700 Subject: [PATCH] Updating check and import of PowerShell modules (#240) * Creating new PR * Update build-virtualbox.ps1 Co-Authored-By: Matt Richardson * Update build-azure.ps1 Co-Authored-By: Matt Richardson * Update build-aws.ps1 Co-Authored-By: Matt Richardson * Update Tests/powershell-helpers.ps1 Co-Authored-By: Matt Richardson --- Tests/OctopusDSC.Tests.ps1 | 5 ++++- Tests/powershell-helpers.ps1 | 23 ++++++++++++++++------- build-aws.ps1 | 5 ++--- build-azure.ps1 | 5 ++--- build-hyperv.ps1 | 6 ++---- build-virtualbox.ps1 | 8 ++------ 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Tests/OctopusDSC.Tests.ps1 b/Tests/OctopusDSC.Tests.ps1 index c4074ed39..a7fa98ed6 100644 --- a/Tests/OctopusDSC.Tests.ps1 +++ b/Tests/OctopusDSC.Tests.ps1 @@ -1,5 +1,8 @@ +$path = Join-Path -Path $PSScriptRoot -ChildPath "powershell-helpers.ps1" +. $path + Describe "PSScriptAnalyzer" { - Import-Module PSScriptAnalyzer + Import-PowerShellModule -Name PSScriptAnalyzer -MinimumVersion "1.18.3" $excludedRules = @( 'PSUseShouldProcessForStateChangingFunctions' ) diff --git a/Tests/powershell-helpers.ps1 b/Tests/powershell-helpers.ps1 index 46075f300..81228b5fa 100644 --- a/Tests/powershell-helpers.ps1 +++ b/Tests/powershell-helpers.ps1 @@ -37,18 +37,27 @@ function Test-AppExists($appName) { return $null -ne $command } -function Test-PowershellModuleInstalled($moduleName, $version) { - $command = Get-Module $moduleName -listavailable +function Import-PowerShellModule ($Name, $MinimumVersion) { + $command = Get-Module $Name -listavailable if ($null -eq $command) { - write-host "Please install $($moduleName): Install-Module -Name $moduleName -Force" -foregroundcolor red + write-host "Please install $($Name): Install-Module -Name $Name -Force" -foregroundcolor red exit 1 } - if ($null -ne $version) { - if ($command.Version -lt [System.Version]::Parse($version)) { - write-host "Please install $($moduleName) $version or higher (you have version $($command.Version)): Update-Module -Name $moduleName -Force" -foregroundcolor red + + if ($command.Count -gt 1) { + $command = $command | Sort-Object -Property Version -Descending | Select-Object -First 1 + } + + + if ($null -ne $MinimumVersion) { + if ($command.Version -lt [System.Version]::Parse($MinimumVersion)) { + write-host "Please install $($Name) $MinimumVersion or higher (you have version $($command.Version)): Update-Module -Name $Name -Force" -foregroundcolor red exit 1 } } + + Write-Output "Importing module $Name with version $($command.Version)..." + Import-Module -Name $Name -Version $command.Version } function Test-CustomVersionOfVagrantDscPluginIsInstalled() { # does not deal well with machines that have two versioned folders under /gems/ @@ -207,4 +216,4 @@ function Remove-OldLogsBeforeNewRun { if (Test-Path "PesterTestResults.xml") { Remove-Item "PesterTestResults.xml" -Force | Out-Null } -} \ No newline at end of file +} diff --git a/build-aws.ps1 b/build-aws.ps1 index 2be74d6ea..cb293485c 100755 --- a/build-aws.ps1 +++ b/build-aws.ps1 @@ -43,9 +43,8 @@ Remove-OldLogsBeforeNewRun if(-not $SkipPester) { Write-Output "##teamcity[blockOpened name='Pester tests']" Write-Output "Importing Pester module" - Test-PowershellModuleInstalled "Pester" "4.9.0" - Test-PowershellModuleInstalled "PSScriptAnalyzer" "1.18.3" - Import-Module Pester -force + Import-PowerShellModule -Name "Pester" -MinimumVersion "4.9.0" + Write-Output "Running Pester Tests" $result = Invoke-Pester -OutputFile PesterTestResults.xml -OutputFormat NUnitXml -PassThru if ($result.FailedCount -gt 0) { diff --git a/build-azure.ps1 b/build-azure.ps1 index 156cc65f3..27916c047 100755 --- a/build-azure.ps1 +++ b/build-azure.ps1 @@ -42,9 +42,8 @@ Remove-OldLogsBeforeNewRun if(-not $SkipPester) { Write-Output "Importing Pester module" - Test-PowershellModuleInstalled "Pester" "4.9.0" - Test-PowershellModuleInstalled "PSScriptAnalyzer" "1.18.3" - Import-Module Pester -verbose -force + Import-PowerShellModule -Name "Pester" -MinimumVersion "4.9.0" + Write-Output "Running Pester Tests" $result = Invoke-Pester -OutputFile PesterTestResults.xml -OutputFormat NUnitXml -PassThru diff --git a/build-hyperv.ps1 b/build-hyperv.ps1 index 86fbedd19..400881b10 100644 --- a/build-hyperv.ps1 +++ b/build-hyperv.ps1 @@ -56,10 +56,8 @@ Test-PluginInstalled "vagrant-winrm-file-download" Remove-OldLogsBeforeNewRun if(-not $SkipPester) { - Write-Output "Importing Pester module" - Test-PowershellModuleInstalled "Pester" "4.9.0" - Test-PowershellModuleInstalled "PSScriptAnalyzer" "1.18.3" - Import-Module Pester -force + Import-PowerShellModule -Name "Pester" -MinimumVersion "4.9.0" + Write-Output "Running Pester Tests" $result = Invoke-Pester -OutputFile PesterTestResults.xml -OutputFormat NUnitXml -PassThru if ($result.FailedCount -gt 0) { diff --git a/build-virtualbox.ps1 b/build-virtualbox.ps1 index 20f6a8fa4..6b173b35d 100644 --- a/build-virtualbox.ps1 +++ b/build-virtualbox.ps1 @@ -10,13 +10,10 @@ param( ) . Tests/powershell-helpers.ps1 - Start-Transcript .\vagrant-virtualbox.log Set-OctopusDscEnvVars @PSBoundParameters -. Tests/powershell-helpers.ps1 - if (-not (Test-AppExists "vagrant")) { Write-Output "Please install vagrant from vagrantup.com." exit 1 @@ -37,9 +34,8 @@ Remove-OldLogsBeforeNewRun if(-not $SkipPester) { Write-Output "Importing Pester module" - Test-PowershellModuleInstalled "Pester" "4.9.0" - Test-PowershellModuleInstalled "PSScriptAnalyzer" "1.18.3" - Import-Module Pester -force + Import-PowerShellModule -Name "Pester" -MinimumVersion "4.9.0" + Write-Output "Running Pester Tests" $result = Invoke-Pester -OutputFile PesterTestResults.xml -OutputFormat NUnitXml -PassThru if ($result.FailedCount -gt 0) {