diff --git a/.github/workflows/source-bookmarks.yml b/.github/workflows/source-bookmarks.yml
new file mode 100644
index 0000000..f5bb895
--- /dev/null
+++ b/.github/workflows/source-bookmarks.yml
@@ -0,0 +1,15 @@
+name: Bookmarks source checks
+on:
+ push:
+ branches: [fix/build-bookmarks]
+ pull_request:
+ branches: [Bookmarks/develop]
+permissions:
+ contents: read
+jobs:
+ source:
+ uses: stadub/PowershellScripts/.github/workflows/validate-module-source.yml@d3fa5897213e3ff4e0d64897559bdfb516356512
+ with:
+ module: Bookmarks
+ source_sha: ${{ github.event.pull_request.head.sha || github.sha }}
+ automation_sha: d3fa5897213e3ff4e0d64897559bdfb516356512
diff --git a/.gitignore b/.gitignore
index 931f30c..e4d962c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
# File types to ignore globally
.history
.vscode
+TestResults/
diff --git a/Bookmarks/Bookmarks.Tests.ps1 b/Bookmarks/Bookmarks.Tests.ps1
index e097e2c..bf390b0 100644
--- a/Bookmarks/Bookmarks.Tests.ps1
+++ b/Bookmarks/Bookmarks.Tests.ps1
@@ -1,158 +1,103 @@
-$NoExport = $true
-$ModuleDevelopment = $true
-$DebugPreference = "Continue"
-
-Set-StrictMode -version Latest
-
-$here = Split-Path -Parent $MyInvocation.MyCommand.Path
-
-. $PSScriptRoot\Shared-Functions.ps1
-
-. $PSScriptRoot\Functions.ps1
-
-_Initalize
-
-Describe "Remove-AllPSBookmarks" {
-
- It "clear all bookmarks" {
- $_marks["a"]="A"
- $_marks["b"]="B"
-
- $_marks.Count | Should -Not -Be 0
-
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- Remove-AllPSBookmarks
-
- $_marks.Count | Should -Be 0
+# Exercise real CSV persistence in an isolated directory; never initialize a user profile.
+Set-StrictMode -Version Latest
+. $PSScriptRoot/Shared-Functions.ps1
+. $PSScriptRoot/Functions.ps1
+
+Describe 'Bookmark persistence and commands' {
+ BeforeEach {
+ $script:_marksPath = Join-Path $TestDrive 'Bookmarks.csv'
+ Remove-Item $script:_marksPath -ErrorAction SilentlyContinue
}
-}
-
-Describe "Add-PSBookmark" {
- It "adds current folder to bookmarks" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- Set-Location $here
- Add-PSBookmark testDir1
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir1"
- $_marks.Values | Should -Be $here
+ It 'starts with no bookmarks when the store does not exist' {
+ (Get-PSBookmarks).Count | Should -Be 0
}
- It "adds selected folder to bookmarks" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- Add-PSBookmark testDir2 "c:"
- $_marks = Get-PSBookmarks
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir2"
- $_marks.Values | Should -Be "c:"
+ It 'persists the current directory' {
+ $null = Add-PSBookmark here
+ (Get-PSBookmarks)['here'] | Should -BeExactly (Get-Location).Path
}
- It "adds path from pipeline to bookmarks" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- "c:" |Add-PSBookmark testDir3
- $_marks = Get-PSBookmarks
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir3"
- $_marks.Values | Should -Be "c:"
+ It 'persists explicit and pipeline paths including CSV punctuation and Unicode' {
+ $path = Join-Path $TestDrive ('a,b' + [char]0x2642)
+ $null = Add-PSBookmark explicit $path
+ $null = $path | Add-PSBookmark piped
+ $marks = Get-PSBookmarks
+ $marks.Count | Should -Be 2
+ $marks['explicit'] | Should -BeExactly $path
+ $marks['piped'] | Should -BeExactly $path
}
-}
-
-
-Describe "Remove-PSBookmark" {
-
- It "remove bookmark from list" {
- $_marks = @{ }
- $_marks["a"]="A";
- Mock -CommandName Import-Csv -MockWith {}
- Mock -CommandName Export-Csv -MockWith {}
-
- Mock -CommandName Restore-PSBookmarks -MockWith {} -Verifiable
- Mock -CommandName Save-PSBookmarks -MockWith {} -Verifiable
-
- $_marks.Count | Should -Be 1
- $_marks.Keys[0] | Should -Be "a"
- $_marks.Values[0] | Should -Be "A"
-
- Remove-PSBookmark "a"
-
- $_marks.Count | Should -Be 0
-
- Assert-VerifiableMock
+ It 'rejects duplicates without replacing the saved path' {
+ $null = Add-PSBookmark a original
+ { Add-PSBookmark a replaced } | Should -Throw
+ (Get-PSBookmarks)['a'] | Should -BeExactly 'original'
}
-}
-
-Describe "Open-PSBookmark" {
-
- It "open specific bookmark" {
- $_marks = @{ }
- $_marks["a"]="A";
-
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- Mock -CommandName Set-Location -MockWith {}
-
- Open-PSBookmark "a"
-
- Assert-MockCalled Set-Location 1
-
+ It 'removes one bookmark while preserving others' {
+ $null = Add-PSBookmark a first
+ $null = Add-PSBookmark b second
+ $null = Remove-PSBookmark a
+ $marks = Get-PSBookmarks
+ $marks.Count | Should -Be 1
+ $marks['b'] | Should -BeExactly 'second'
}
-}
-
-Describe "Update-PSBookmark" {
-
- It "update current folder to bookmarks" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
- Add-PSBookmark testDir1 "c:"
+ It 'clears the persisted store' {
+ $null = Add-PSBookmark a first
+ Remove-AllPSBookmarks
+ (Get-PSBookmarks).Count | Should -Be 0
+ }
- Set-Location $here
- Update-PSBookmark testDir1
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir1"
- $_marks.Values | Should -Be $here
+ It 'opens the persisted path' {
+ $null = Add-PSBookmark a $TestDrive
+ Mock Set-Location {}
+ Open-PSBookmark a
+ Assert-MockCalled Set-Location 1 -Exactly -ParameterFilter { $Path -eq $TestDrive }
}
- It "Update selected folder in bookmarks list" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
+ It 'updates paths from an argument, pipeline, and current location' {
+ $null = Add-PSBookmark a first
+ $null = Update-PSBookmark a second
+ (Get-PSBookmarks)['a'] | Should -BeExactly 'second'
+ $null = 'third' | Update-PSBookmark a
+ (Get-PSBookmarks)['a'] | Should -BeExactly 'third'
+ $null = Update-PSBookmark a
+ (Get-PSBookmarks)['a'] | Should -BeExactly (Get-Location).Path
+ }
- Add-PSBookmark testDir2 "c:"
+ It 'rejects updating a missing bookmark' {
+ { Update-PSBookmark missing anything } | Should -Throw
+ (Get-PSBookmarks).Count | Should -Be 0
+ }
- Update-PSBookmark testDir2 "$pwd"
- $_marks = Get-PSBookmarks
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir2"
- $_marks.Values | Should -Be "$pwd"
+ It 'reloads external session changes on every operation' {
+ $null = Add-PSBookmark a first
+ @([pscustomobject]@{ Key = 'a'; Value = 'external' },
+ [pscustomobject]@{ Key = 'b'; Value = 'other' }) |
+ Export-Csv $script:_marksPath -NoTypeInformation
+ $null = Add-PSBookmark c third
+ $marks = Get-PSBookmarks
+ $marks.Count | Should -Be 3
+ $marks['a'] | Should -BeExactly 'external'
+ $marks['b'] | Should -BeExactly 'other'
}
- It "Update path from pipeline to bookmarks" {
- $_marks = @{ }
- Mock -CommandName Import-Csv -MockWith {} -Verifiable
- Mock -CommandName Export-Csv -MockWith {} -Verifiable
-
- Add-PSBookmark testDir3 "$pwd"
+ It 'returns an independent snapshot' {
+ $null = Add-PSBookmark a first
+ $marks = Get-PSBookmarks
+ $marks['a'] = 'mutated'
+ (Get-PSBookmarks)['a'] | Should -BeExactly 'first'
+ }
+}
- "c:" |Update-PSBookmark testDir3
- $_marks = Get-PSBookmarks
- $_marks.Count | Should -Be 1
- $_marks.Keys | Should -Be "testDir3"
- $_marks.Values | Should -Be "c:"
+Describe 'Bookmark manifest export contract' {
+ It 'lists only implemented public functions without trailing whitespace' {
+ $manifest = Import-PowerShellDataFile (Join-Path $PSScriptRoot 'Bookmarks.psd1')
+ $expected = @('Add-PSBookmark', 'Remove-PSBookmark', 'Remove-AllPSBookmarks',
+ 'Open-PSBookmark', 'Get-PSBookmarks', 'Update-PSBookmark')
+ ($manifest.FunctionsToExport | Sort-Object) -join ',' |
+ Should -BeExactly (($expected | Sort-Object) -join ',')
+ foreach ($name in $expected) { (Get-Command $name).CommandType | Should -Be 'Function' }
}
-}
\ No newline at end of file
+}
diff --git a/Bookmarks/Bookmarks.psd1 b/Bookmarks/Bookmarks.psd1
index a501342..070f3bc 100644
--- a/Bookmarks/Bookmarks.psd1
+++ b/Bookmarks/Bookmarks.psd1
@@ -77,7 +77,7 @@ FunctionsToExport = @('Add-PSBookmark',
'Remove-AllPSBookmarks',
'Open-PSBookmark',
'Get-PSBookmarks',
-'Update-PSBookmark ')
+'Update-PSBookmark')
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
diff --git a/Bookmarks/Bookmarks.psm1 b/Bookmarks/Bookmarks.psm1
index c81e032..3754ab6 100644
--- a/Bookmarks/Bookmarks.psm1
+++ b/Bookmarks/Bookmarks.psm1
@@ -10,8 +10,6 @@ Export-ModuleMember -Function Add-PSBookmark
Export-ModuleMember -Function Remove-PSBookmark
Export-ModuleMember -Function Remove-AllPSBookmarks
Export-ModuleMember -Function Open-PSBookmark
-Export-ModuleMember -Function Restore-PSBookmarks
-Export-ModuleMember -Function Save-PSBookmarks
Export-ModuleMember -Function Get-PSBookmarks
Export-ModuleMember -Function Update-PSBookmark
. $PSScriptRoot\Aliases.ps1
\ No newline at end of file
diff --git a/Bookmarks/README.md b/Bookmarks/README.md
index d5d4b9c..a75c056 100644
--- a/Bookmarks/README.md
+++ b/Bookmarks/README.md
@@ -33,6 +33,46 @@ Module import:
Import-Module Bookmarks
```
+## Update checks
+
+Bookmarks checks PowerShell Gallery at most once every 24 hours per user. Set
+these environment variables **before importing** (for example, in your profile):
+
+```powershell
+# Disable automatic checks completely (no network or update-cache access).
+$env:BOOKMARKS_UPDATE_CHECK = '0'
+Import-Module Bookmarks
+```
+
+```powershell
+# Enable checks, but only once a week.
+$env:BOOKMARKS_UPDATE_CHECK = '1'
+$env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS = '168'
+Import-Module Bookmarks
+```
+
+`0`, `false`, and `off` disable checks (case insensitive). Unset or other values
+leave checks enabled. The interval accepts whole hours from 1 through 8760;
+unset, malformed, zero, negative, or out-of-range values use 24 hours.
+Remove the variables to restore defaults.
+
+A due check makes one request for the latest stable version, with a three-second
+HTTP timeout and no redirects or retries. DNS resolution can take longer than
+that timeout on some systems; disable checks for startup with no network delay.
+Notifications use host output, never success-pipeline data. Offline and HTTP
+errors are silent and attempts, including failures, wait the full interval before
+retrying. The timestamp persists across imports and new shells in
+`[Environment]::GetFolderPath('LocalApplicationData')/PowershellScripts/UpdateChecks/Bookmarks.timestamp`.
+If the cache cannot be written or is busy, the check is skipped; simultaneous
+imports claim the timestamp exclusively. Deleting this file makes the next
+import eligible again. Bookmark files and commands are unaffected.
+
+For maintainers: port the reviewed update-check functions and tests selectively
+to the 7Zip and Currency helper copies on their own development/issue branches,
+with module-specific control names and cache keys. Those ports are not included
+here. Keep profile/storage helpers intact, then reconcile release packages through
+the separate packaging/release process; do not copy whole stale helper files.
+
# Commands
@@ -189,4 +229,12 @@ a coffee or a beer is always appreciated. Thank you very much in advance.
If you have any idea or suggestion - please add a github issue.
-
\ No newline at end of file
+
+## Source regression checks
+
+Run `Invoke-Pester ./Bookmarks.Tests.ps1` with Pester 4.10.1. The suite uses real
+CSV files under Pester's TestDrive and covers save/reload, external session edits,
+add/update/remove/clear, pipeline paths, CSV punctuation, Unicode, and exports.
+It does not initialize or write the user's bookmark profile. The public update
+export is `Update-PSBookmark` (without trailing whitespace); storage helpers remain
+private. Startup/update-check work is maintained separately in #24/#27.
diff --git a/Bookmarks/Shared-Functions.ps1 b/Bookmarks/Shared-Functions.ps1
index 1ea6864..02ec1cb 100644
--- a/Bookmarks/Shared-Functions.ps1
+++ b/Bookmarks/Shared-Functions.ps1
@@ -91,28 +91,79 @@ filter Last {
}
}
- function CheckPsGalleryUpdate {
+# Kept separate from bookmark storage: update checks must not create profile data.
+function Get-PSGalleryUpdateCachePath {
+ param ([string]$moduleName)
+ $root = [Environment]::GetFolderPath('LocalApplicationData')
+ if ([string]::IsNullOrEmpty($root)) { return $null }
+ Join-Path (Join-Path $root 'PowershellScripts/UpdateChecks') "$moduleName.timestamp"
+}
+
+function Test-PSGalleryUpdateCheckDue {
+ param ([string]$moduleName, [int]$intervalHours)
+
+ $stream = $null
+ try {
+ $path = Get-PSGalleryUpdateCachePath $moduleName
+ if ([string]::IsNullOrEmpty($path)) { return $false }
+ $null = [IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($path))
+ # An exclusive, non-waiting claim also throttles simultaneous imports.
+ $stream = [IO.File]::Open($path, [IO.FileMode]::OpenOrCreate,
+ [IO.FileAccess]::ReadWrite, [IO.FileShare]::None)
+ $now = (Get-Date).ToUniversalTime().Ticks
+ $previous = 0L
+ if ($stream.Length -gt 0 -and $stream.Length -le 128) {
+ $buffer = New-Object byte[] ([int]$stream.Length)
+ $length = $stream.Read($buffer, 0, $buffer.Length)
+ $null = [long]::TryParse([Text.Encoding]::UTF8.GetString($buffer, 0, $length), [ref]$previous)
+ }
+ if ($previous -gt 0 -and ($now - $previous) -lt [TimeSpan]::FromHours($intervalHours).Ticks) {
+ return $false
+ }
+ # Record attempts before networking, so failures receive the same backoff.
+ $buffer = [Text.Encoding]::UTF8.GetBytes($now.ToString([Globalization.CultureInfo]::InvariantCulture))
+ $stream.Position = 0
+ $stream.SetLength(0)
+ $stream.Write($buffer, 0, $buffer.Length)
+ $stream.Flush()
+ return $true
+ }
+ catch {
+ # If storage is locked or unavailable, skip rather than issue unthrottled requests.
+ return $false
+ }
+ finally {
+ if ($null -ne $stream) { $stream.Dispose() }
+ }
+}
+
+function CheckPsGalleryUpdate {
param (
- [string] $moduleName,
- [string] $currentVersion
+ [string]$moduleName,
+ [string]$currentVersion
)
-
- Try
- {
- Write-Output "Update check..."
- $feed = Invoke-WebRequest -Uri "https://www.powershellgallery.com/api/v2/FindPackagesById()?id=%27$moduleName%27"
- $last=([xml]$feed.Content).feed.entry |Sort-Object -Property updated | Last
-
- $version= $last.properties.Version
-
- if ($version -gt $currentVersion) {
- Write-Output "Found a new module version {$version}."
- $notes=$last.properties.ReleaseNotes.'#text'
- Write-Output "Release notes: {$notes}."
- Write-Output "Recomendent to update module with command: Update-Module -Name $moduleName -Force"
- }
- }
- Catch
- {
- }
-}
\ No newline at end of file
+
+ try {
+ if ($env:BOOKMARKS_UPDATE_CHECK -match '^(0|false|off)$') { return }
+ $intervalHours = 24
+ $configuredHours = 0
+ if ([int]::TryParse($env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS, [ref]$configuredHours) -and
+ $configuredHours -ge 1 -and $configuredHours -le 8760) {
+ $intervalHours = $configuredHours
+ }
+ if (-not (Test-PSGalleryUpdateCheckDue $moduleName $intervalHours)) { return }
+
+ # Ask only for the latest stable package, not the entire version history.
+ $id = [Uri]::EscapeDataString($moduleName.Replace("'", "''"))
+ $uri = "https://www.powershellgallery.com/api/v2/FindPackagesById()?id=%27$id%27&`$filter=IsLatestVersion&`$top=1"
+ $feed = Invoke-WebRequest -Uri $uri -UseBasicParsing -TimeoutSec 3 -MaximumRedirection 0 -ErrorAction Stop
+ $entry = ([xml]$feed.Content).feed.entry
+ $version = [version]$entry.properties.Version
+ if ($version -gt [version]$currentVersion) {
+ Write-Host "Found a new module version {$version}. Update with: Update-Module -Name $moduleName -Force"
+ }
+ }
+ catch {
+ # Update checks are optional; offline, malformed feed and HTTP errors are quiet.
+ }
+}
diff --git a/Bookmarks/UpdateCheck.Tests.ps1 b/Bookmarks/UpdateCheck.Tests.ps1
new file mode 100644
index 0000000..bb33d76
--- /dev/null
+++ b/Bookmarks/UpdateCheck.Tests.ps1
@@ -0,0 +1,170 @@
+# Pester 4.x: no real network, profile data, or update-cache writes.
+. $PSScriptRoot/Shared-Functions.ps1
+
+Describe 'Bookmarks automatic update checks' {
+ BeforeEach {
+ $script:savedCheck = $env:BOOKMARKS_UPDATE_CHECK
+ $script:savedInterval = $env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS
+ $env:BOOKMARKS_UPDATE_CHECK = '1'
+ $env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS = $null
+ $script:updateCache = Join-Path $TestDrive ([guid]::NewGuid().ToString() + '.timestamp')
+ $script:checkNow = [datetime]'2026-01-01T00:00:00Z'
+ Mock Get-PSGalleryUpdateCachePath { $script:updateCache }
+ Mock Get-Date { $script:checkNow }
+ Mock Invoke-WebRequest {
+ @{ Content = '2.2.0' }
+ }
+ Mock Write-Host {}
+ }
+
+ AfterEach {
+ $env:BOOKMARKS_UPDATE_CHECK = $script:savedCheck
+ $env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS = $script:savedInterval
+ }
+
+ It 'does no network or cache work when disabled' {
+ foreach ($value in @('0', 'false', 'OFF')) {
+ $env:BOOKMARKS_UPDATE_CHECK = $value
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ }
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 0 -Exactly
+ Assert-MockCalled -Scope It Get-PSGalleryUpdateCachePath -Times 0 -Exactly
+ }
+
+ It 'imports the real module repeatedly with checks disabled and no requests' {
+ $savedAppData = $env:APPDATA
+ $modulePath = Join-Path $PSScriptRoot 'Bookmarks.psd1'
+ try {
+ $env:APPDATA = Join-Path $TestDrive 'profile'
+ # Avoid unrelated first-run directory output from the existing profile helper.
+ $null = New-Item -ItemType Directory -Path (Join-Path $env:APPDATA '.ps1/ScriptData/Bookmarks') -Force
+ $env:BOOKMARKS_UPDATE_CHECK = '0'
+ @(Import-Module $modulePath -Force).Count | Should -Be 0
+ @(Import-Module $modulePath -Force).Count | Should -Be 0
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 0 -Exactly
+ (Test-Path -LiteralPath $script:updateCache) | Should -Be $false
+ }
+ finally {
+ Remove-Module Bookmarks -ErrorAction SilentlyContinue
+ $env:APPDATA = $savedAppData
+ }
+ }
+
+ It 'defaults to 24 hours with checks enabled when configuration is absent' {
+ $env:BOOKMARKS_UPDATE_CHECK = $null
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ $script:checkNow = $script:checkNow.AddHours(23)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ $script:checkNow = $script:checkNow.AddHours(1)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 2 -Exactly
+ }
+
+ It 'persists the interval across repeated checks and helper reloads' {
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ # Like a forced import/new module scope, reload without retaining script state.
+ . $PSScriptRoot/Shared-Functions.ps1
+ Mock Get-PSGalleryUpdateCachePath { $script:updateCache }
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ }
+
+ It 'honors the configured interval and checks at expiry' {
+ $env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS = '168'
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ $script:checkNow = $script:checkNow.AddHours(167)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ $script:checkNow = $script:checkNow.AddHours(1)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 2 -Exactly
+ }
+
+ It 'falls back to 24 hours for invalid intervals' {
+ foreach ($value in @('garbage', '0', '-1', '1.5', '8761', '99999999999999')) {
+ $script:updateCache = Join-Path $TestDrive ([guid]::NewGuid().ToString() + '.timestamp')
+ $env:BOOKMARKS_UPDATE_CHECK_INTERVAL_HOURS = $value
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ $script:checkNow = $script:checkNow.AddHours(1)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ }
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 6 -Exactly
+ }
+
+ It 'bounds requests with timeout and disables redirects' {
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly -ParameterFilter {
+ $TimeoutSec -eq 3 -and $MaximumRedirection -eq 0 -and $UseBasicParsing -and
+ $Uri -like '*$filter=IsLatestVersion&$top=1'
+ }
+ }
+
+ It 'silently backs off remote errors including under ErrorActionPreference Stop' {
+ Mock Invoke-WebRequest { throw 'Offline' }
+ $oldPreference = $ErrorActionPreference
+ try {
+ $ErrorActionPreference = 'Stop'
+ { CheckPsGalleryUpdate Bookmarks '2.2.0' } | Should -Not -Throw
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ $script:checkNow = $script:checkNow.AddHours(24)
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 2 -Exactly
+ }
+ finally { $ErrorActionPreference = $oldPreference }
+ Assert-MockCalled -Scope It Write-Host -Times 0 -Exactly
+ }
+
+ It 'quietly backs off malformed feeds' {
+ Mock Invoke-WebRequest { @{ Content = 'not XML' } }
+ { CheckPsGalleryUpdate Bookmarks '2.2.0' } | Should -Not -Throw
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ Assert-MockCalled -Scope It Write-Host -Times 0 -Exactly
+ }
+
+ It 'uses numeric versions and host output without polluting the success stream' {
+ Mock Invoke-WebRequest {
+ @{ Content = '2.10.0' }
+ }
+ @(CheckPsGalleryUpdate Bookmarks '2.9.0').Count | Should -Be 0
+ Assert-MockCalled -Scope It Write-Host -Times 1 -Exactly -ParameterFilter {
+ $Object -like '*2.10.0*Update-Module -Name Bookmarks -Force*'
+ }
+ }
+
+ It 'does not announce an older numeric version' {
+ Mock Invoke-WebRequest {
+ @{ Content = '2.9.0' }
+ }
+ @(CheckPsGalleryUpdate Bookmarks '2.10.0').Count | Should -Be 0
+ Assert-MockCalled -Scope It Write-Host -Times 0 -Exactly
+ }
+
+ It 'recovers a corrupt timestamp and throttles subsequent checks' {
+ Set-Content -LiteralPath $script:updateCache -Value 'corrupt'
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ }
+
+ It 'skips requests when cache storage is unavailable' {
+ Mock Get-PSGalleryUpdateCachePath { throw 'Storage unavailable' }
+ { CheckPsGalleryUpdate Bookmarks '2.2.0' } | Should -Not -Throw
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 0 -Exactly
+ }
+
+ It 'skips a busy cache without waiting or requesting' {
+ $lock = [IO.File]::Open($script:updateCache, 'OpenOrCreate', 'ReadWrite', 'None')
+ try {
+ { CheckPsGalleryUpdate Bookmarks '2.2.0' } | Should -Not -Throw
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 0 -Exactly
+ }
+ finally { $lock.Dispose() }
+ CheckPsGalleryUpdate Bookmarks '2.2.0'
+ Assert-MockCalled -Scope It Invoke-WebRequest -Times 1 -Exactly
+ }
+}