Skip to content

Commit

Permalink
Updating check and import of PowerShell modules (#240)
Browse files Browse the repository at this point in the history
* Creating new PR

* Update build-virtualbox.ps1

Co-Authored-By: Matt Richardson <[email protected]>

* Update build-azure.ps1

Co-Authored-By: Matt Richardson <[email protected]>

* Update build-aws.ps1

Co-Authored-By: Matt Richardson <[email protected]>

* Update Tests/powershell-helpers.ps1

Co-Authored-By: Matt Richardson <[email protected]>
  • Loading branch information
twerthi and matt-richardson authored Oct 30, 2019
1 parent bb98cd5 commit 253506b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Tests/OctopusDSC.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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'
)
Expand Down
23 changes: 16 additions & 7 deletions Tests/powershell-helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -207,4 +216,4 @@ function Remove-OldLogsBeforeNewRun {
if (Test-Path "PesterTestResults.xml") {
Remove-Item "PesterTestResults.xml" -Force | Out-Null
}
}
}
5 changes: 2 additions & 3 deletions build-aws.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions build-azure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions build-hyperv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 2 additions & 6 deletions build-virtualbox.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 253506b

Please sign in to comment.