diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index 2a2b5131f..a83ab7831 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -54,4 +54,5 @@ Export-ModuleMember -Function @( 'Get-VisualStudioComponents' 'Get-WindowsUpdatesHistory' 'New-ItemPath' + 'Get-ModuleVersionAsJob' ) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index 0e5401c21..c617dcec7 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -615,3 +615,4 @@ function Get-GitHubPackageDownloadUrl { return $downloadUrl } + diff --git a/images/win/scripts/ImageHelpers/TestsHelpers.ps1 b/images/win/scripts/ImageHelpers/TestsHelpers.ps1 index 9e6efc43e..585543840 100644 --- a/images/win/scripts/ImageHelpers/TestsHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/TestsHelpers.ps1 @@ -172,4 +172,29 @@ If (Get-Command -Name Add-ShouldOperator -ErrorAction SilentlyContinue) { Add-ShouldOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode} Add-ShouldOperator -Name ReturnZeroExitCodeWithParam -InternalName ShouldReturnZeroExitCodeWithParam -Test ${function:ShouldReturnZeroExitCodeWithParam} Add-ShouldOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput} -} \ No newline at end of file +} + +Function Get-ModuleVersionAsJob { + Param ( + [Parameter(Mandatory)] + [String] $modulePath, + [Parameter(Mandatory)] + [String] $moduleName + ) + # Script block to run commands in separate PowerShell environment + $testJob = Start-Job -ScriptBlock { + param ( + $modulePath, + $moduleName + ) + # Disable warning messages to prevent additional warnings about Az and Azurerm modules in the same session + $WarningPreference = "SilentlyContinue" + $env:PsModulePath = "$modulePath;$env:PsModulePath" + Import-Module -Name $moduleName + (Get-Module -Name $moduleName).Version.ToString() + + } -ArgumentList $modulePath, $moduleName + + $testJob | Wait-Job | Receive-Job | Out-File -FilePath "${env:TEMP}\module-version.txt" + Remove-Job $testJob +} diff --git a/images/win/scripts/Installers/Install-AzureModules.ps1 b/images/win/scripts/Installers/Install-AzureModules.ps1 index 1c98a8c3d..d5a96ffb4 100644 --- a/images/win/scripts/Installers/Install-AzureModules.ps1 +++ b/images/win/scripts/Installers/Install-AzureModules.ps1 @@ -44,7 +44,7 @@ foreach ($module in $modules) foreach ($version in $module.zip_versions) { # Install modules from GH Release - if($null -ne $assets) + if($null -ne $assets) { $asset = $assets | Where-Object version -eq $version ` | Select-Object -ExpandProperty files ` @@ -58,8 +58,8 @@ foreach ($module in $modules) exit 1 } } - # Install modules from vsts blob - else + # Install modules from vsts blob + else { $modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}" $filename = "${moduleName}_${version}.zip" @@ -90,4 +90,4 @@ foreach ($module in $modules) $psModuleMachinePath += $env:PSModulePath [Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine") -Invoke-PesterTests -TestFile "PowerShellModules" -TestName "AzureModules" \ No newline at end of file +Invoke-PesterTests -TestFile "PowerShellAzModules" -TestName "AzureModules" \ No newline at end of file diff --git a/images/win/scripts/Tests/PowerShellAzModules.Tests.ps1 b/images/win/scripts/Tests/PowerShellAzModules.Tests.ps1 new file mode 100644 index 000000000..0fea778f3 --- /dev/null +++ b/images/win/scripts/Tests/PowerShellAzModules.Tests.ps1 @@ -0,0 +1,40 @@ +Describe "AzureModules" { + + $modules = (Get-ToolsetContent).azureModules + $modulesRootPath = "C:\\Modules" + + foreach ($module in $modules) { + $moduleName = $module.name + + Context "$moduleName" { + + foreach ($version in $module.versions) { + $modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}" + $moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version } + It " exists in modules directory" -TestCases $moduleInfo { + $ScriptBlock = { + param ($modulePath, $moduleName) + Import-Module ImageHelpers + Get-ModuleVersionAsJob -modulePath $modulePath -moduleName $moduleName + } + if ($moduleName -eq "Az"){ + Start-Process -FilePath pwsh.exe -ArgumentList "-Command (Invoke-Command -ScriptBlock {$ScriptBlock} -ArgumentList $modulePath, $moduleName)" -Wait -PassThru + } else { + Start-Process -FilePath powershell.exe -ArgumentList "-Command (Invoke-Command -ScriptBlock {$ScriptBlock} -ArgumentList $modulePath, $moduleName)" -Wait -PassThru + } + $moduleVersion = Get-Content -Path "$env:TEMP\module-version.txt" + Remove-Item -Path "${env:TEMP}\module-version.txt" -Force + $moduleVersion | Should -Match $expectedVersion + } + } + + if ($module.default) { + $moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default } + It " set as default" -TestCases $moduleInfo { + $moduleVersions = Get-Module -ListAvailable -Name $moduleName | ForEach-Object { $_.Version.ToString() } + $moduleVersions | Should -Contain $moduleDefault + } + } + } + } +} diff --git a/images/win/scripts/Tests/PowerShellModules.Tests.ps1 b/images/win/scripts/Tests/PowerShellModules.Tests.ps1 index 48254d48b..9276609d0 100644 --- a/images/win/scripts/Tests/PowerShellModules.Tests.ps1 +++ b/images/win/scripts/Tests/PowerShellModules.Tests.ps1 @@ -21,48 +21,3 @@ Describe "PowerShellModules" { } } } - -Describe "AzureModules" { - $modules = (Get-ToolsetContent).azureModules - $modulesRootPath = "C:\\Modules" - - foreach ($module in $modules) { - $moduleName = $module.name - - Context "$moduleName" { - - foreach ($version in $module.versions) { - $modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}" - $moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version } - It " exists in modules directory" -TestCases $moduleInfo { - $testJob = Start-Job -ScriptBlock { - param ( - $modulePath, - $moduleName - ) - - # Disable warning messages to prevent additional warnings about Az and Azurerm modules in the same session - $WarningPreference = "SilentlyContinue" - - $env:PsModulePath = "$modulePath;$env:PsModulePath" - Import-Module -Name $moduleName - (Get-Module -Name $moduleName).Version.ToString() - - } -ArgumentList $modulePath, $moduleName - - $moduleVersion = $testJob | Wait-Job | Receive-Job - Remove-Job $testJob - $moduleVersion | Should -Match $expectedVersion - } - } - - if ($module.default) { - $moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default } - It " set as default" -TestCases $moduleInfo { - $moduleVersions = Get-Module -ListAvailable -Name $moduleName | ForEach-Object { $_.Version.ToString() } - $moduleVersions | Should -Contain $moduleDefault - } - } - } - } -} \ No newline at end of file