Skip to content

Commit

Permalink
force import modules to avoid errors when importing duplicate types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arjgupta authored Aug 22, 2018
1 parent 153fe7d commit b04cf29
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 11
"Patch": 12
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 11
"Patch": 12
},
"demands": [
"azureps"
Expand Down
20 changes: 10 additions & 10 deletions Tasks/Common/VstsAzureHelpers_/ImportFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ function Import-FromModulePath {
}

# Import the module.
Write-Host "##[command]Import-Module -Name $($module.Path) -Global"
$module = Import-Module -Name $module.Path -Global -PassThru
Write-Host "##[command]Import-Module -Name $($module.Path) -Global -Force"
$module = Import-Module -Name $module.Path -Global -PassThru -Force
Write-Verbose "Imported module version: $($module.Version)"

if ($Classic) {
Expand All @@ -117,8 +117,8 @@ function Import-FromModulePath {
throw (Get-VstsLocString -Key AZ_AzureRMProfileModuleNotFound)
}
# Import and then store the AzureRM.profile module.
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global"
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global -Force"
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru -Force
} else {
$script:azureRMProfileModule = $profileModule
}
Expand Down Expand Up @@ -156,8 +156,8 @@ function Import-FromSdkPath {
continue
}
# Import the module.
Write-Host "##[command]Import-Module -Name $path -Global"
$module = Import-Module -Name $path -Global -PassThru
Write-Host "##[command]Import-Module -Name $path -Global -Force"
$module = Import-Module -Name $path -Global -PassThru -Force
Write-Verbose "Imported module version: $($module.Version)"
# Store the imported module.
if ($Classic) {
Expand Down Expand Up @@ -197,8 +197,8 @@ function Import-AzureRmSubmodulesFromSdkPath {
try {
# Azure.Storage submodule needs to be imported first
$azureStorageModulePath = [System.IO.Path]::Combine($programFiles, "Microsoft SDKs\Azure\PowerShell\Storage\Azure.Storage\Azure.Storage.psd1")
Write-Host "##[command]Import-Module -Name $azureStorageModulePath -Global"
$azureStorageModule = Import-Module -Name $azureStorageModulePath -Global -PassThru
Write-Host "##[command]Import-Module -Name $azureStorageModulePath -Global -Force"
$azureStorageModule = Import-Module -Name $azureStorageModulePath -Global -PassThru -Force
Write-Verbose "Imported module version: $($azureStorageModule.Version)"
}
catch {
Expand All @@ -215,8 +215,8 @@ function Import-AzureRmSubmodulesFromSdkPath {
}
$azureRmNestedModulePath = [System.IO.Path]::Combine($azureRmNestedModule.FullName, $azureRmNestedModule.Name + ".psd1")
try {
Write-Verbose "##[command]Import-Module -Name $azureRmNestedModulePath -Global"
$azureRmSubmodule = Import-Module -Name $azureRmNestedModulePath -Global -PassThru
Write-Verbose "##[command]Import-Module -Name $azureRmNestedModulePath -Global -Force"
$azureRmSubmodule = Import-Module -Name $azureRmNestedModulePath -Global -PassThru -Force
Write-Verbose "Imported module version: $($azureRmSubmodule.Version)"
}
catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ foreach ($variableSet in $variableSets) {
Unregister-Mock Get-Module
Unregister-Mock Import-Module
Register-Mock Get-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Name -ListAvailable
Register-Mock Import-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Path -Global -PassThru
Register-Mock Import-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Path -Global -PassThru -Force
if ($variableSet.Modules.Length -eq 2) {
Register-Mock Get-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Name
Register-Mock Import-Module { $variableSet.Modules[1] } -- -Name $variableSet.Modules[1].Path -Global -PassThru
Register-Mock Import-Module { $variableSet.Modules[1] } -- -Name $variableSet.Modules[1].Path -Global -PassThru -Force
}

# Act.
$result = & $module Import-FromModulePath -Classic:($variableSet.Classic) -azurePsVersion "4.1.0"

# Assert.
Assert-AreEqual $true $result
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[0].Path -Global -PassThru
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[0].Path -Global -PassThru -Force
if ($variableSet.Modules.Length -eq 2) {
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[1].Path -Global -PassThru
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[1].Path -Global -PassThru -Force
}

if ($variableSet.Classic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ foreach ($variableSet in $variableSets) {
# Setup Import-Module.
if ($variableSet.FoundInProgramFilesX86) {
$expectedModule = @{ Version = [version]'1.2.3.4' }
Register-Mock Import-Module { $expectedModule } -Name $wowPsd1 -Global -PassThru
Register-Mock Import-Module { $expectedModule } -Name $wowPsd1 -Global -PassThru -Force
}

if ($variableSet.FoundInProgramFiles) {
$expectedModule = @{ Version = [version]'2.3.4.5' }
Register-Mock Import-Module { $expectedModule } -Name $psd1 -Global -PassThru
Register-Mock Import-Module { $expectedModule } -Name $psd1 -Global -PassThru -Force
}

if($variableSet.Classic -eq $false) {
Expand All @@ -100,9 +100,9 @@ foreach ($variableSet in $variableSets) {
# Assert.
Assert-AreEqual $true $result
if ($variableSet.FoundInProgramFilesX86) {
Assert-WasCalled Import-Module -- -Name $wowPsd1 -Global -PassThru
Assert-WasCalled Import-Module -- -Name $wowPsd1 -Global -PassThru -Force
} else {
Assert-WasCalled Import-Module -- -Name $psd1 -Global -PassThru
Assert-WasCalled Import-Module -- -Name $psd1 -Global -PassThru -Force
}

if ($variableSet.Classic) {
Expand Down

0 comments on commit b04cf29

Please sign in to comment.