From 656e17b67e8468edc5c5bd51bdad55642a03d9a4 Mon Sep 17 00:00:00 2001 From: "Karsten A. M. Guenther" Date: Sun, 2 Jul 2023 12:41:35 +0200 Subject: [PATCH] fix: Handle cloning errors by falling back to downloading (#59) --- .github/workflows/ci.yml | 21 +++++++++++++++++++-- install.ps1 | 21 ++++++++++++++++----- test/install.Tests.ps1 | 13 +++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5df904c..beb1bb2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,8 +29,8 @@ jobs: - name: Test scoop command availability shell: powershell run: scoop help - test_pwsh: - name: PowerShell + test_pwsh_cloning: + name: PowerShell (with cloning) runs-on: windows-latest steps: - name: Checkout @@ -53,3 +53,20 @@ jobs: - name: Test scoop command availability shell: pwsh run: scoop help + test_pwsh_download: + name: PowerShell (with downloading) + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@main + with: + fetch-depth: 2 + - name: Test Scoop Install command + shell: pwsh + run: | + git config --global protocol.https.allow never + ./install.ps1 -RunAsAdmin + echo "~\scoop\shims" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + - name: Test scoop command availability + shell: pwsh + run: scoop help diff --git a/install.ps1 b/install.ps1 index 86b6d46..4e82228 100644 --- a/install.ps1 +++ b/install.ps1 @@ -133,7 +133,7 @@ function Test-Prerequisite { } # Ensure Robocopy.exe is accessible - if (!([bool](Get-Command -Name 'robocopy' -ErrorAction SilentlyContinue))) { + if (!(Test-CommandAvailable('robocopy'))) { Deny-Install "Scoop requires 'C:\Windows\System32\Robocopy.exe' to work. Please make sure 'C:\Windows\System32' is in your PATH." } @@ -149,7 +149,7 @@ function Test-Prerequisite { } # Test if scoop is installed, by checking if scoop command exists. - if ([bool](Get-Command -Name 'scoop' -ErrorAction SilentlyContinue)) { + if (Test-CommandAvailable('scoop')) { Deny-Install "Scoop is already installed. Run 'scoop update' to get the latest version." } } @@ -548,7 +548,7 @@ function Test-CommandAvailable { [Parameter(Mandatory = $True, Position = 0)] [String] $Command ) - return [Boolean](Get-Command $Command -ErrorAction Ignore) + return [Boolean](Get-Command $Command -ErrorAction SilentlyContinue) } function Install-Scoop { @@ -563,6 +563,7 @@ function Install-Scoop { # Download scoop from GitHub Write-InstallInfo "Downloading ..." $downloader = Get-Downloader + [bool]$downloadZipsRequired = $True if (Test-CommandAvailable('git')) { $old_https = $env:HTTPS_PROXY @@ -575,15 +576,25 @@ function Install-Scoop { } Write-Verbose "Cloning $SCOOP_PACKAGE_GIT_REPO to $SCOOP_APP_DIR" git clone -q $SCOOP_PACKAGE_GIT_REPO $SCOOP_APP_DIR + if (-Not $?) { + throw "Cloning failed. Falling back to downloading zip files." + } Write-Verbose "Cloning $SCOOP_MAIN_BUCKET_GIT_REPO to $SCOOP_MAIN_BUCKET_DIR" git clone -q $SCOOP_MAIN_BUCKET_GIT_REPO $SCOOP_MAIN_BUCKET_DIR + if (-Not $?) { + throw "Cloning failed. Falling back to downloading zip files." + } + $downloadZipsRequired = $False } catch { - Get-Error $_ + Write-Warning "$($_.Exception.Message)" + $Global:LastExitCode = 0 } finally { $env:HTTPS_PROXY = $old_https $env:HTTP_PROXY = $old_http } - } else { + } + + if ($downloadZipsRequired) { # 1. download scoop $scoopZipfile = "$SCOOP_APP_DIR\scoop.zip" if (!(Test-Path $SCOOP_APP_DIR)) { diff --git a/test/install.Tests.ps1 b/test/install.Tests.ps1 index 5cb3640..806aa0d 100644 --- a/test/install.Tests.ps1 +++ b/test/install.Tests.ps1 @@ -27,3 +27,16 @@ Describe 'Get-Downloader' -Tag 'Proxy' { } } } + +Describe 'Test-CommandAvailable' -Tag 'CommandLine' { + Context 'Command available' { + It 'Returns $true' { + Test-CommandAvailable -Command 'git' | Should -Be $true + } + } + Context 'Command not available' { + It 'Returns $false' { + Test-CommandAvailable -Command 'notavailable' | Should -Be $false + } + } +}