diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index 87bc02d287..c66a68b917 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -1,3 +1,6 @@ +# Imports +. "$PSScriptRoot/core.ps1" # "$scoopdir" + $bucketsdir = "$scoopdir\buckets" function Find-BucketDirectory { diff --git a/lib/core.ps1 b/lib/core.ps1 index c271ce8f94..369a85b971 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -544,6 +544,9 @@ function Test-HelperInstalled { } function app_status($app, $global) { + if ([string]::IsNullOrWhiteSpace($app)) { + throw 'The value of $app may not be null, empty, or whitespace!' + } $status = @{} $status.installed = installed $app $global $status.version = Select-CurrentVersion -AppName $app -Global:$global diff --git a/lib/download.ps1 b/lib/download.ps1 index 70641cca7f..572ee4dd25 100644 --- a/lib/download.ps1 +++ b/lib/download.ps1 @@ -457,9 +457,9 @@ function Invoke-CachedAria2Download ($app, $version, $manifest, $architecture, $ warn "Download failed! (Error $lastexitcode) $(aria_exit_code $lastexitcode)" warn $urlstxt_content warn $aria2 - warn $(new_issue_msg $app $bucket "download via aria2 failed") + warn $(new_issue_msg $app $bucket 'download via aria2 failed') - Write-Host "Fallback to default downloader ..." + Write-Host 'Fallback to default downloader ...' try { foreach ($url in $urls) { @@ -550,6 +550,9 @@ function Get-UserAgent() { function setup_proxy() { # note: '@' and ':' in password must be escaped, e.g. 'p@ssword' -> p\@ssword' + if (-not (Test-Path Function:/get_config)) { + . "$PSScriptRoot/core.ps1" + } $proxy = get_config PROXY if (!$proxy) { return diff --git a/lib/manifest.ps1 b/lib/manifest.ps1 index 9ca618158b..61a6faa86a 100644 --- a/lib/manifest.ps1 +++ b/lib/manifest.ps1 @@ -14,14 +14,15 @@ function parse_json($path) { function url_manifest($url) { $str = $null try { + if (-not (Test-Path Function:/Get-UserAgent)) { + . "$PSScriptRoot/download.ps1" + } $wc = New-Object Net.Webclient $wc.Headers.Add('User-Agent', (Get-UserAgent)) $data = $wc.DownloadData($url) $str = (Get-Encoding($wc)).GetString($data) - } catch [system.management.automation.methodinvocationexception] { - warn "error: $($_.exception.innerexception.message)" - } catch { - throw + } catch [System.Management.Automation.MethodInvocationException] { + warn "error: $($_.Exception.InnerException.Message)" } if (!$str) { return $null } try { @@ -68,6 +69,9 @@ function Get-Manifest($app) { } function manifest($app, $bucket, $url) { + if ([string]::IsNullOrWhiteSpace($app)) { + throw [System.ArgumentException]::new('The value of $app may not be null, empty, or whitespace!') + } if ($url) { return url_manifest $url } parse_json (manifest_path $app $bucket) } diff --git a/libexec/scoop-status.ps1 b/libexec/scoop-status.ps1 index a62cad2dd1..dfa619da5b 100644 --- a/libexec/scoop-status.ps1 +++ b/libexec/scoop-status.ps1 @@ -6,6 +6,7 @@ . "$PSScriptRoot\..\lib\manifest.ps1" # 'manifest' 'parse_json' "install_info" . "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion' +. "$PSScriptRoot\..\lib\buckets.ps1" # 'Get-LocalBucket' # check if scoop needs updating $currentdir = versiondir 'scoop' 'current' @@ -23,7 +24,7 @@ function Test-UpdateStatus($repopath) { if (Test-Path "$repopath\.git") { Invoke-Git -Path $repopath -ArgumentList @('fetch', '-q', 'origin') $script:network_failure = 128 -eq $LASTEXITCODE - $branch = Invoke-Git -Path $repopath -ArgumentList @('branch', '--show-current') + $branch = Invoke-Git -Path $repopath -ArgumentList @('branch', '--show-current') $commits = Invoke-Git -Path $repopath -ArgumentList @('log', "HEAD..origin/$branch", '--oneline') if ($commits) { return $true } else { return $false } @@ -55,19 +56,22 @@ $true, $false | ForEach-Object { # local and global apps $dir = appsdir $global if (!(Test-Path $dir)) { return } - Get-ChildItem $dir | Where-Object name -NE 'scoop' | ForEach-Object { - $app = $_.name + $appNames = @(Get-ChildItem $dir -Directory -Name -Exclude 'scoop' | Where-Object Length -GT 0) + + if ($appNames.Length -eq 0) { return } + + foreach ($app in $appNames) { $status = app_status $app $global - if (!$status.outdated -and !$status.failed -and !$status.removed -and !$status.missing_deps) { return } + if (-not ($status.outdated -or $status.failed -or $status.removed -or $status.missing_deps)) { continue } $item = [ordered]@{} $item.Name = $app $item.'Installed Version' = $status.version - $item.'Latest Version' = if ($status.outdated) { $status.latest_version } else { "" } + $item.'Latest Version' = if ($status.outdated) { $status.latest_version } else { '' } $item.'Missing Dependencies' = $status.missing_deps -Split ' ' -Join ' | ' $info = @() - if ($status.failed) { $info += 'Install failed' } - if ($status.hold) { $info += 'Held package' } + if ($status.failed) { $info += 'Install failed' } + if ($status.hold) { $info += 'Held package' } if ($status.removed) { $info += 'Manifest removed' } $item.Info = $info -join ', ' $list += [PSCustomObject]$item diff --git a/test/Scoop-Status.Tests.ps1 b/test/Scoop-Status.Tests.ps1 new file mode 100644 index 0000000000..6b41549068 --- /dev/null +++ b/test/Scoop-Status.Tests.ps1 @@ -0,0 +1,33 @@ +# todo +Describe -Skip 'Show status and check for new app versions' -Tag 'Scoop' { + # scoop-status.ps1 is not structured in a way that makes this easy to test + # todo: refactor scoop-status.ps1's loop bodies to functions for easier testing + It 'throws when Global or User appdir is empty or does not exist' -Skip { } +} + +# todo: add bucket fixtures for CI tests +Describe 'Test-UpdateStatus' -Tag 'Scoop' -Skip:($env:CI -eq $true) { + BeforeAll { + . "$PSScriptRoot/../libexec/scoop-status.ps1" + } + # todo + It -Skip 'should return $true if $commits is falsy' {} + # todo + It -Skip 'should return $false if $commits is truthy' {} + It 'should return $true if "$repopath\.git`" does not exist' { + function makeTmpDir { + [OutputType([System.IO.DirectoryInfo])] + Param () + + [string] $tmpDirPath = Join-Path $env:TEMP $(New-Guid) + + while (Test-Path $tmpDirPath) { + $tmpDirPath = Join-Path $env:TEMP $(New-Guid) + } + + return (New-Item $tmpDirPath -ItemType Directory) + } + [System.IO.DirectoryInfo] $emptyDir = makeTmpDir + Test-UpdateStatus $emptyDir.FullName | Should -Be $true + } +}