Skip to content

Commit b9d7e16

Browse files
authored
Merge pull request #20 from pwshdevs/feat/update-powershell-provider-for-parity
Updated powershell provider to be in parity with the other modules that use the new structure
2 parents 512b047 + 7489642 commit b9d7e16

16 files changed

Lines changed: 1550 additions & 515 deletions

DevSetup/Private/Commands/Uninstall-DevSetupEnv.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Function Uninstall-DevSetupEnv {
120120
Invoke-ChocolateyPackageUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null
121121

122122
# Uninstall Scoop package dependencies
123-
Invoke-ScoopComponentUninstall -YamlData $YamlData | Out-Null
123+
Invoke-ScoopComponentUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null
124124
} else {
125125
# Uninstall Homebrew package dependencies
126126
Invoke-HomebrewComponentsUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null

DevSetup/Private/Enums/TaskState.ps1

Lines changed: 0 additions & 9 deletions
This file was deleted.

DevSetup/Private/Providers/Powershell/Get-PowershellModuleScopeMap.Tests.ps1

Lines changed: 394 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Function Get-PowershellModuleScopeMap {
2+
[CmdletBinding()]
3+
[OutputType([array])]
4+
Param()
5+
6+
if((Test-OperatingSystem -Windows)) {
7+
$SearchPath = (Get-EnvironmentVariable USERPROFILE)
8+
} else {
9+
$SearchPath = (Get-EnvironmentVariable HOME)
10+
}
11+
12+
$InstallPaths = @(
13+
(Get-EnvironmentVariable PSModulePath) -split ([System.IO.Path]::PathSeparator) | Where-Object { $_ -ne $null -and $_.Trim() -ne "" } | ForEach-Object {
14+
$scope = if($SearchPath -and ($_ -match [regex]::Escape($SearchPath))) { "CurrentUser" } else { "AllUsers" }
15+
[PSCustomObject]@{ Path = $_; Scope = $scope }
16+
}
17+
)
18+
19+
return $InstallPaths
20+
}

DevSetup/Private/Providers/Powershell/Install-PowershellModule.Tests.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ Describe "Install-PowershellModule" {
1818
}
1919
}
2020

21+
Context "When Test-RunningAsAdmin throws an exception" {
22+
It "Should return false" {
23+
Mock Test-RunningAsAdmin { throw "Admin check failed" }
24+
$result = Install-PowershellModule -ModuleName "Az" -Scope "AllUsers"
25+
$result | Should -Be $false
26+
}
27+
}
28+
29+
Context "When Test-PowershellModuleInstalled throws an exception" {
30+
It "Should return false" {
31+
Mock Test-RunningAsAdmin { return $true }
32+
Mock Test-PowershellModuleInstalled { throw "Module test failed" }
33+
$result = Install-PowershellModule -ModuleName "Az"
34+
$result | Should -Be $false
35+
}
36+
}
37+
2138
Context "When module is already installed with correct version and scope" {
2239
It "Should return true and not call Uninstall-PowershellModule or Install-Module" {
2340
Mock Test-RunningAsAdmin { return $true }
@@ -133,4 +150,38 @@ Describe "Install-PowershellModule" {
133150
$installParams.RequiredVersion | Should -Be "9.0.1"
134151
}
135152
}
153+
154+
Context "When WhatIf is specified" {
155+
It "Should return true and not install the module" {
156+
Mock Test-RunningAsAdmin { return $true }
157+
Mock Test-PowershellModuleInstalled {
158+
return [InstalledState]::NotInstalled
159+
}
160+
Mock Install-Module { throw "Should not be called" }
161+
$result = Install-PowershellModule -ModuleName "Az" -WhatIf
162+
$result | Should -Be $true
163+
}
164+
}
165+
166+
Context "When module is installed with CurrentUser scope by default" {
167+
It "Should use CurrentUser scope when no scope is specified" {
168+
Mock Test-RunningAsAdmin { return $true }
169+
Mock Test-PowershellModuleInstalled {
170+
return [InstalledState]::NotInstalled
171+
}
172+
Mock Install-Module -MockWith {
173+
param(
174+
[string]$Name,
175+
[string]$Scope
176+
)
177+
$script:installParams = @{
178+
ModuleName = $Name
179+
Scope = $Scope
180+
}
181+
}
182+
$result = Install-PowershellModule -ModuleName "Az"
183+
$result | Should -Be $true
184+
$installParams.Scope | Should -Be "CurrentUser"
185+
}
186+
}
136187
}

DevSetup/Private/Providers/Powershell/Install-PowershellModule.ps1

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,54 +97,71 @@ Function Install-PowershellModule {
9797
[ValidateSet('CurrentUser', 'AllUsers')]
9898
[String] $Scope = 'CurrentUser'
9999
)
100-
100+
101101
try {
102102
# Check if running as administrator only when installing for all users
103103
if ($Scope -eq 'AllUsers' -and (-not (Test-RunningAsAdmin))) {
104-
throw "PowerShell module installation to AllUsers scope requires administrator privileges. Please run as administrator or use CurrentUser scope."
105-
}
106-
107-
$installParams = @{
108-
Name = $ModuleName
109-
Force = $Force
110-
Scope = $Scope
111-
AllowClobber = $AllowClobber
112-
SkipPublisherCheck = $true
104+
Write-StatusMessage "PowerShell module installation to AllUsers scope requires administrator privileges. Please run as administrator or use CurrentUser scope." -Verbosity Error
105+
return $false
113106
}
107+
} catch {
108+
Write-StatusMessage "Failed to validate administrator privileges: $_" -Verbosity Error
109+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
110+
return $false
111+
}
112+
113+
$installParams = @{
114+
Name = $ModuleName
115+
Force = $Force
116+
Scope = $Scope
117+
AllowClobber = $AllowClobber
118+
SkipPublisherCheck = $true
119+
}
114120

115-
$testParams = @{
116-
ModuleName = $ModuleName
117-
Scope = $Scope
118-
}
121+
$testParams = @{
122+
ModuleName = $ModuleName
123+
Scope = $Scope
124+
}
119125

120-
if($PSBoundParameters.ContainsKey('Version')) {
121-
$testParams.Version = $Version
122-
$installParams.RequiredVersion = $Version
123-
}
126+
if($PSBoundParameters.ContainsKey('Version')) {
127+
$testParams.Version = $Version
128+
$installParams.RequiredVersion = $Version
129+
}
124130

131+
try {
125132
$testResult = Test-PowershellModuleInstalled @testParams
133+
} catch {
134+
Write-StatusMessage "Failed to test if PowerShell module is installed: $_" -Verbosity Error
135+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
136+
return $false
137+
}
126138

127-
if($testResult.HasFlag([InstalledState]::Pass)) {
128-
return $true
129-
}
139+
if($testResult.HasFlag([InstalledState]::Pass)) {
140+
return $true
141+
}
130142

131-
if($testResult.HasFlag([InstalledState]::Installed)) {
132-
try {
133-
Uninstall-PowershellModule -ModuleName $ModuleName -WhatIf:$WhatIf
134-
} catch {
135-
# Uninstall might have failed, we keep going anyways
136-
Write-StatusMessage "Failed to uninstall existing module '$ModuleName': $_" -Verbosity Error
137-
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
138-
}
143+
if($testResult.HasFlag([InstalledState]::Installed)) {
144+
try {
145+
Uninstall-PowershellModule -ModuleName $ModuleName -WhatIf:$WhatIf
146+
} catch {
147+
# Uninstall might have failed, we keep going anyways
148+
Write-StatusMessage "Failed to uninstall existing module '$ModuleName': $_" -Verbosity Error
149+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
139150
}
151+
}
140152

141-
# Install the PowerShell module
142-
if ($PSCmdlet.ShouldProcess($ModuleName, "Install-Module")) {
153+
# Install the PowerShell module
154+
if ($PSCmdlet.ShouldProcess($ModuleName, "Install-Module")) {
155+
try {
143156
Install-Module @installParams
157+
} catch {
158+
Write-StatusMessage "Failed to install PowerShell module '$ModuleName': $_" -Verbosity Error
159+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
160+
return $false
144161
}
162+
} else {
163+
Write-StatusMessage "Installation of module '$ModuleName' was skipped due to ShouldProcess." -Verbosity Warning
145164
return $true
146165
}
147-
catch {
148-
return $false
149-
}
166+
return $true
150167
}

0 commit comments

Comments
 (0)