[Windows] Update Az module pester test (#6902)

This commit is contained in:
Alexey-Ayupov
2023-01-12 23:25:12 +03:00
committed by GitHub
parent 7750a08f65
commit a86ee04ce6
6 changed files with 72 additions and 50 deletions

View File

@@ -54,4 +54,5 @@ Export-ModuleMember -Function @(
'Get-VisualStudioComponents' 'Get-VisualStudioComponents'
'Get-WindowsUpdatesHistory' 'Get-WindowsUpdatesHistory'
'New-ItemPath' 'New-ItemPath'
'Get-ModuleVersionAsJob'
) )

View File

@@ -615,3 +615,4 @@ function Get-GitHubPackageDownloadUrl {
return $downloadUrl return $downloadUrl
} }

View File

@@ -173,3 +173,28 @@ If (Get-Command -Name Add-ShouldOperator -ErrorAction SilentlyContinue) {
Add-ShouldOperator -Name ReturnZeroExitCodeWithParam -InternalName ShouldReturnZeroExitCodeWithParam -Test ${function:ShouldReturnZeroExitCodeWithParam} Add-ShouldOperator -Name ReturnZeroExitCodeWithParam -InternalName ShouldReturnZeroExitCodeWithParam -Test ${function:ShouldReturnZeroExitCodeWithParam}
Add-ShouldOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput} Add-ShouldOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput}
} }
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
}

View File

@@ -90,4 +90,4 @@ foreach ($module in $modules)
$psModuleMachinePath += $env:PSModulePath $psModuleMachinePath += $env:PSModulePath
[Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine") [Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine")
Invoke-PesterTests -TestFile "PowerShellModules" -TestName "AzureModules" Invoke-PesterTests -TestFile "PowerShellAzModules" -TestName "AzureModules"

View File

@@ -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 "<expectedVersion> 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 "<moduleDefault> set as default" -TestCases $moduleInfo {
$moduleVersions = Get-Module -ListAvailable -Name $moduleName | ForEach-Object { $_.Version.ToString() }
$moduleVersions | Should -Contain $moduleDefault
}
}
}
}
}

View File

@@ -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 "<expectedVersion> 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 "<moduleDefault> set as default" -TestCases $moduleInfo {
$moduleVersions = Get-Module -ListAvailable -Name $moduleName | ForEach-Object { $_.Version.ToString() }
$moduleVersions | Should -Contain $moduleDefault
}
}
}
}
}