Skip to content

Commit a621092

Browse files
authored
refactor!: guard install flow against dot-sourcing (#136)
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
1 parent 3f16afb commit a621092

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎install.ps1‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,10 @@ function Test-CommandAvailable {
575575

576576
function Install-Scoop {
577577
Write-InstallInfo 'Initializing...'
578-
# Validate install parameters
579-
Test-ValidateParameter
580578
# Check prerequisites
581579
Test-Prerequisite
580+
# Validate install parameters
581+
Test-ValidateParameter
582582
# Enable TLS 1.2
583583
Optimize-SecurityProtocol
584584

@@ -683,9 +683,6 @@ function Write-DebugInfo {
683683
# Prepare variables
684684
$IS_EXECUTED_FROM_IEX = ($null -eq $MyInvocation.MyCommand.Path)
685685

686-
# Abort when the language mode is restricted
687-
Test-LanguageMode
688-
689686
# Scoop root directory
690687
$SCOOP_DIR = $ScoopDir, $env:SCOOP, "$env:USERPROFILE\scoop" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1
691688
# Scoop global apps directory
@@ -709,14 +706,18 @@ $SCOOP_MAIN_BUCKET_REPO = 'https://github.com/ScoopInstaller/Main/archive/master
709706
$SCOOP_PACKAGE_GIT_REPO = 'https://github.com/ScoopInstaller/Scoop.git'
710707
$SCOOP_MAIN_BUCKET_GIT_REPO = 'https://github.com/ScoopInstaller/Main.git'
711708

712-
# Quit if anything goes wrong
713-
$oldErrorActionPreference = $ErrorActionPreference
714-
$ErrorActionPreference = 'Stop'
715-
716-
# Logging debug info
717-
Write-DebugInfo $PSBoundParameters
718-
# Bootstrap function
719-
Install-Scoop
720-
721-
# Reset $ErrorActionPreference to original value
722-
$ErrorActionPreference = $oldErrorActionPreference
709+
# The install flow triggers only when the script is executed directly, but
710+
# not when dot-sourced. Dot-sourcing the installer will not trigger the
711+
# installation, and only the functions will be loaded, e.g., for testing.
712+
# Downstreams can call `Install-Scoop` explicitly to start the installation.
713+
if ($MyInvocation.InvocationName -ne '.') {
714+
$oldErrorActionPreference = $ErrorActionPreference
715+
try {
716+
$ErrorActionPreference = 'Stop'
717+
Test-LanguageMode
718+
Write-DebugInfo $PSBoundParameters
719+
Install-Scoop
720+
} finally {
721+
$ErrorActionPreference = $oldErrorActionPreference
722+
}
723+
}

‎test/install.Tests.ps1‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
BeforeAll {
2+
$env:SCOOP = Join-Path $TestDrive 'scoop'
3+
$env:SCOOP_GLOBAL = Join-Path $TestDrive 'scoop-global'
4+
25
# Load SUT
36
$sut = (Split-Path -Leaf $PSCommandPath).Replace('.Tests.ps1', '.ps1')
47
. ".\$sut"

0 commit comments

Comments
 (0)