Skip to content

Commit feb080a

Browse files
committed
search: guard empty latest-row input
1 parent 34b3597 commit feb080a

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/database.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ if (-not (Get-Command Compare-Version -ErrorAction Ignore)) {
44
. "$PSScriptRoot\versions.ps1"
55
}
66

7+
<#
8+
.SYNOPSIS
9+
Get the latest row from a set of Scoop database rows.
10+
.DESCRIPTION
11+
Compares the `version` property of each row semantically and returns the
12+
latest row. Returns `$null` when no rows are provided.
13+
#>
714
function Get-LatestScoopDBRow {
815
param(
916
[Parameter(Mandatory)]
1017
[object[]]
1118
$Rows
1219
)
1320

21+
if (-not $Rows -or $Rows.Count -eq 0) {
22+
return $null
23+
}
24+
1425
$latest = $Rows[0]
1526
foreach ($row in ($Rows | Select-Object -Skip 1)) {
1627
if ((Compare-Version -ReferenceVersion $latest.version -DifferenceVersion $row.version) -gt 0) {

test/Scoop-Database.Tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Describe 'database version selection' -Tag 'Scoop' {
1515
(Get-LatestScoopDBRow -Rows $rows).version | Should -Be '1.0.31'
1616
}
1717

18+
It 'returns null when no rows are provided' {
19+
Get-LatestScoopDBRow -Rows @() | Should -Be $null
20+
}
21+
1822
It 'returns the latest semantic version per name and bucket' {
1923
$result = New-Object System.Data.DataTable
2024
[void]$result.Columns.Add('name', [string])

0 commit comments

Comments
 (0)