Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DevSetup/Private/Commands/Uninstall-DevSetupEnv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Function Uninstall-DevSetupEnv {
Invoke-ChocolateyPackageUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null

# Uninstall Scoop package dependencies
Invoke-ScoopComponentUninstall -YamlData $YamlData | Out-Null
Invoke-ScoopComponentUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null
} else {
# Uninstall Homebrew package dependencies
Invoke-HomebrewComponentsUninstall -YamlData $YamlData -DryRun:$DryRun | Out-Null
Expand Down
9 changes: 0 additions & 9 deletions DevSetup/Private/Enums/TaskState.ps1

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Function Get-PowershellModuleScopeMap {
[CmdletBinding()]
[OutputType([array])]
Param()

if((Test-OperatingSystem -Windows)) {
$SearchPath = (Get-EnvironmentVariable USERPROFILE)
} else {
$SearchPath = (Get-EnvironmentVariable HOME)
}

$InstallPaths = @(
(Get-EnvironmentVariable PSModulePath) -split ([System.IO.Path]::PathSeparator) | Where-Object { $_ -ne $null -and $_.Trim() -ne "" } | ForEach-Object {
$scope = if($SearchPath -and ($_ -match [regex]::Escape($SearchPath))) { "CurrentUser" } else { "AllUsers" }
[PSCustomObject]@{ Path = $_; Scope = $scope }
}
)

return $InstallPaths
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ Describe "Install-PowershellModule" {
}
}

Context "When Test-RunningAsAdmin throws an exception" {
It "Should return false" {
Mock Test-RunningAsAdmin { throw "Admin check failed" }
$result = Install-PowershellModule -ModuleName "Az" -Scope "AllUsers"
$result | Should -Be $false
}
}

Context "When Test-PowershellModuleInstalled throws an exception" {
It "Should return false" {
Mock Test-RunningAsAdmin { return $true }
Mock Test-PowershellModuleInstalled { throw "Module test failed" }
$result = Install-PowershellModule -ModuleName "Az"
$result | Should -Be $false
}
}

Context "When module is already installed with correct version and scope" {
It "Should return true and not call Uninstall-PowershellModule or Install-Module" {
Mock Test-RunningAsAdmin { return $true }
Expand Down Expand Up @@ -133,4 +150,38 @@ Describe "Install-PowershellModule" {
$installParams.RequiredVersion | Should -Be "9.0.1"
}
}

Context "When WhatIf is specified" {
It "Should return true and not install the module" {
Mock Test-RunningAsAdmin { return $true }
Mock Test-PowershellModuleInstalled {
return [InstalledState]::NotInstalled
}
Mock Install-Module { throw "Should not be called" }
$result = Install-PowershellModule -ModuleName "Az" -WhatIf
$result | Should -Be $true
}
}

Context "When module is installed with CurrentUser scope by default" {
It "Should use CurrentUser scope when no scope is specified" {
Mock Test-RunningAsAdmin { return $true }
Mock Test-PowershellModuleInstalled {
return [InstalledState]::NotInstalled
}
Mock Install-Module -MockWith {
param(
[string]$Name,
[string]$Scope
)
$script:installParams = @{
ModuleName = $Name
Scope = $Scope
}
}
$result = Install-PowershellModule -ModuleName "Az"
$result | Should -Be $true
$installParams.Scope | Should -Be "CurrentUser"
}
}
}
85 changes: 51 additions & 34 deletions DevSetup/Private/Providers/Powershell/Install-PowershellModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,54 +97,71 @@ Function Install-PowershellModule {
[ValidateSet('CurrentUser', 'AllUsers')]
[String] $Scope = 'CurrentUser'
)

try {
# Check if running as administrator only when installing for all users
if ($Scope -eq 'AllUsers' -and (-not (Test-RunningAsAdmin))) {
throw "PowerShell module installation to AllUsers scope requires administrator privileges. Please run as administrator or use CurrentUser scope."
}

$installParams = @{
Name = $ModuleName
Force = $Force
Scope = $Scope
AllowClobber = $AllowClobber
SkipPublisherCheck = $true
Write-StatusMessage "PowerShell module installation to AllUsers scope requires administrator privileges. Please run as administrator or use CurrentUser scope." -Verbosity Error
return $false
}
} catch {
Write-StatusMessage "Failed to validate administrator privileges: $_" -Verbosity Error
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
return $false
}

$installParams = @{
Name = $ModuleName
Force = $Force
Scope = $Scope
AllowClobber = $AllowClobber
SkipPublisherCheck = $true
}

$testParams = @{
ModuleName = $ModuleName
Scope = $Scope
}
$testParams = @{
ModuleName = $ModuleName
Scope = $Scope
}

if($PSBoundParameters.ContainsKey('Version')) {
$testParams.Version = $Version
$installParams.RequiredVersion = $Version
}
if($PSBoundParameters.ContainsKey('Version')) {
$testParams.Version = $Version
$installParams.RequiredVersion = $Version
}

try {
$testResult = Test-PowershellModuleInstalled @testParams
} catch {
Write-StatusMessage "Failed to test if PowerShell module is installed: $_" -Verbosity Error
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
return $false
}

if($testResult.HasFlag([InstalledState]::Pass)) {
return $true
}
if($testResult.HasFlag([InstalledState]::Pass)) {
return $true
}

if($testResult.HasFlag([InstalledState]::Installed)) {
try {
Uninstall-PowershellModule -ModuleName $ModuleName -WhatIf:$WhatIf
} catch {
# Uninstall might have failed, we keep going anyways
Write-StatusMessage "Failed to uninstall existing module '$ModuleName': $_" -Verbosity Error
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
}
if($testResult.HasFlag([InstalledState]::Installed)) {
try {
Uninstall-PowershellModule -ModuleName $ModuleName -WhatIf:$WhatIf
} catch {
# Uninstall might have failed, we keep going anyways
Write-StatusMessage "Failed to uninstall existing module '$ModuleName': $_" -Verbosity Error
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
}
}

# Install the PowerShell module
if ($PSCmdlet.ShouldProcess($ModuleName, "Install-Module")) {
# Install the PowerShell module
if ($PSCmdlet.ShouldProcess($ModuleName, "Install-Module")) {
try {
Install-Module @installParams
} catch {
Write-StatusMessage "Failed to install PowerShell module '$ModuleName': $_" -Verbosity Error
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
return $false
}
} else {
Write-StatusMessage "Installation of module '$ModuleName' was skipped due to ShouldProcess." -Verbosity Warning
return $true
}
catch {
return $false
}
return $true
}
Loading
Loading