Import-Module "$PSScriptRoot/../src/PortablePackages.psm1" -Force Describe 'Portable package lifecycle' { InModuleScope PortablePackages { BeforeEach { $root = Join-Path $TestDrive ([guid]::NewGuid().ToString('N') + ' espace é') $bundle = Join-Path $TestDrive ([guid]::NewGuid().ToString('N')) [void][IO.Directory]::CreateDirectory("$bundle/bundle") [IO.File]::WriteAllText("$bundle/bundle/app.exe", 'test executable') [IO.File]::WriteAllText("$bundle/bundle/library.dll", 'test dependency') $script:fixtureZip = "$bundle.zip" [IO.Compression.ZipFile]::CreateFromDirectory($bundle, $script:fixtureZip) $package = [pscustomobject]@{ id = 'presenter'; provider = 'portable'; source = 'gitea'; version = '1.2.3'; update = 'pinned'; url = 'https://example.org/app.zip'; sha256 = (Get-FileHash $script:fixtureZip).Hash; executable = 'bundle/app.exe' } Mock Get-PortableProgramsPath { Join-Path $root 'test-programs' } Mock Assert-PortablePlatform {} Mock Assert-PortableStopped {} Mock Set-PortableShortcut { [void][IO.Directory]::CreateDirectory((Split-Path -Parent $Paths.Shortcut)) [IO.File]::WriteAllText($Paths.Shortcut, 'mock shortcut') } Mock Invoke-WebRequest { Copy-Item -LiteralPath $script:fixtureZip -Destination $OutFile } } It 'plans without writes or downloads, installs, and verifies an unchanged rerun' { $plan = Get-PortablePackagePlan $package $root $plan.action | Should -Be 'Install' Test-Path $root | Should -BeFalse Should -Invoke Invoke-WebRequest -Times 0 (Invoke-PortablePackageOperation $plan).status | Should -Be 'Succeeded' $again = Get-PortablePackagePlan $package $root $again.action | Should -Be 'None' (Invoke-PortablePackageOperation $again).status | Should -Be 'None' Should -Invoke Invoke-WebRequest -Times 1 } It 'retains the previous version and unmanaged settings on update' { $paths = Get-PortablePaths $package $root (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' $old = Get-Content $paths.Receipt -Raw | ConvertFrom-Json $settings = Join-Path $paths.Root "$($old.directory)/settings.json" 'user settings' | Set-Content $settings $package.version = '2.0' (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' Test-Path (Join-Path $paths.Root "$($old.directory)/bundle/app.exe") | Should -BeTrue Get-Content $settings | Should -Be 'user settings' (Get-Content $paths.Receipt -Raw | ConvertFrom-Json).version | Should -Be '2.0' } It 'repairs missing files but refuses modified files' { $paths = Get-PortablePaths $package $root [void](Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)) $receipt = Get-Content $paths.Receipt -Raw | ConvertFrom-Json Remove-Item (Join-Path $paths.Root "$($receipt.directory)/bundle/library.dll") (Get-PortablePackagePlan $package $root).action | Should -Be 'Install' (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' $receipt = Get-Content $paths.Receipt -Raw | ConvertFrom-Json 'local edit' | Set-Content (Join-Path $paths.Root "$($receipt.directory)/bundle/app.exe") (Get-PortablePackagePlan $package $root).action | Should -Be 'Conflict' (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Conflict' } It 'keeps the active receipt on checksum or shortcut failure and recovers on retry' { $paths = Get-PortablePaths $package $root [void](Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)) $before = Get-Content $paths.Receipt -Raw $package.version = '2.0' $hash = $package.sha256 $package.sha256 = '0' * 64 $result = Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root) $result.status | Should -Be 'Failed' $result.message | Should -Match 'SHA-256' Get-Content $paths.Receipt -Raw | Should -Be $before $package.sha256 = $hash Mock Set-PortableShortcut { throw 'Shortcut failure' } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Failed' Get-Content $paths.Receipt -Raw | Should -Be $before Mock Set-PortableShortcut { [void][IO.Directory]::CreateDirectory((Split-Path -Parent $Paths.Shortcut)) [IO.File]::WriteAllText($Paths.Shortcut, 'mock shortcut') } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' } It 'recovers a first install interrupted after launcher creation' { Mock Set-PortableShortcut { throw 'Shortcut failure' } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Failed' Mock Set-PortableShortcut { [void][IO.Directory]::CreateDirectory((Split-Path -Parent $Paths.Shortcut)) [IO.File]::WriteAllText($Paths.Shortcut, 'mock shortcut') } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' } It 'blocks a running application before downloading' { Mock Assert-PortableStopped { throw 'Close presenter before updating' } $result = Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root) $result.status | Should -Be 'Failed' $result.message | Should -Match 'Close presenter' Should -Invoke Invoke-WebRequest -Times 0 } It 'honors install-only and newer-version policies and rejects a republished hash' { [void](Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)) $package.update = 'install-only'; $package.version = '2.0' (Get-PortablePackagePlan $package $root).action | Should -Be 'None' $package.update = 'pinned'; $package.version = '1.0' (Get-PortablePackagePlan $package $root).action | Should -Be 'Warning' $package.version = '1.2.3'; $package.sha256 = '0' * 64 (Get-PortablePackagePlan $package $root).action | Should -Be 'Conflict' } It 'rejects a missing executable and returns a structured failure without activating it' { $package.executable = 'absent.exe' $result = Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root) $result.status | Should -Be 'Failed' $result.message | Should -Match 'does not contain' Test-Path (Get-PortablePaths $package $root).Receipt | Should -BeFalse { $result | ConvertTo-Json | ConvertFrom-Json } | Should -Not -Throw } It 'rejects unsafe archive entries before extraction' -ForEach @( @{ Name = '../escape.exe'; Attributes = 0 } @{ Name = 'C:/escape.exe'; Attributes = 0 } @{ Name = 'safe/../../escape.exe'; Attributes = 0 } @{ Name = 'app.exe:stream'; Attributes = 0 } @{ Name = 'con/file.exe'; Attributes = 0 } @{ Name = 'link'; Attributes = -1577123840 } ) { $bad = "$bundle-bad.zip" $zip = [IO.Compression.ZipFile]::Open($bad, 'Create') $entry = $zip.CreateEntry($Name) $entry.ExternalAttributes = $Attributes $zip.Dispose() { Expand-PortableArchive $bad "$bundle-expanded" } | Should -Throw Test-Path "$bundle-expanded" | Should -BeFalse } It 'rejects archive paths that collide on Windows' { $bad = "$bundle-bad.zip" $zip = [IO.Compression.ZipFile]::Open($bad, 'Create') [void]$zip.CreateEntry('App.exe') [void]$zip.CreateEntry('app.exe') $zip.Dispose() { Expand-PortableArchive $bad "$bundle-expanded" } | Should -Throw '*colliding*' } It 'rejects linked installation directories' -Skip:$IsWindows { [void][IO.Directory]::CreateDirectory("$root/apps") [void](New-Item -ItemType SymbolicLink -Path "$root/apps/gitea" -Target $bundle) { Get-PortablePackagePlan $package $root } | Should -Throw '*link*' } } } Describe 'Portable launcher presentation' { It 'reports missing receipts concisely with a failing process exit code' { $launcher = Join-Path $TestDrive 'launch.ps1' Copy-Item "$PSScriptRoot/../src/portable-launcher.ps1" $launcher $output = & (Join-Path $PSHOME $(if ($IsWindows) { 'pwsh.exe' } else { 'pwsh' })) -NoProfile -File $launcher 2>&1 $LASTEXITCODE | Should -Be 1 ($output | Out-String) | Should -Match 'Cannot launch portable application:.*Run cc apply' ($output | Out-String) | Should -Not -Match 'CategoryInfo|FullyQualifiedErrorId|\x1b' } } Describe 'Portable launcher process status' { It 'preserves the application exit code and literal arguments' -Skip:$IsWindows { $root = Join-Path $TestDrive 'launcher status' [void][IO.Directory]::CreateDirectory("$root/versions/1.0") Copy-Item "$PSScriptRoot/../src/portable-launcher.ps1" "$root/launch.ps1" [IO.File]::WriteAllText("$root/versions/1.0/app.exe", "#!/bin/sh`nprintf '%s' `"`$1`"`nexit 7`n") & chmod +x "$root/versions/1.0/app.exe" @{ directory = 'versions/1.0'; executable = 'app.exe' } | ConvertTo-Json | Set-Content "$root/current.json" $output = & (Join-Path $PSHOME 'pwsh') -NoProfile -File "$root/launch.ps1" 'document with spaces.pdf' $LASTEXITCODE | Should -Be 7 $output | Should -Be 'document with spaces.pdf' } }