|
| 1 | +#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' } |
| 2 | + |
| 3 | +BeforeAll { |
| 4 | + if (-not $env:BHProjectPath) { |
| 5 | + Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force |
| 6 | + } |
| 7 | + Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue |
| 8 | + Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force |
| 9 | + |
| 10 | + Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force |
| 11 | + |
| 12 | + $script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/FileDownload.ps1' |
| 13 | +} |
| 14 | + |
| 15 | +Describe 'FileDownload script' { |
| 16 | + |
| 17 | + BeforeAll { |
| 18 | + InModuleScope PSDepend { |
| 19 | + Mock Get-WebFile { } |
| 20 | + Mock Add-ToItemCollection { } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + It 'Downloads to Target with filename parsed from the URL when Target is an existing folder' { |
| 25 | + $targetDir = (New-Item 'TestDrive:/dl' -ItemType Directory -Force).FullName |
| 26 | + $dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $targetDir |
| 27 | + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath; T = $targetDir } { |
| 28 | + & $ScriptPath -Dependency $Dep |
| 29 | + } |
| 30 | + Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter { |
| 31 | + $URL -eq 'https://example.com/sample.dll' -and ($Path -like "*sample.dll") |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + It 'Uses Source to override the URL when supplied' { |
| 36 | + $targetDir = (New-Item 'TestDrive:/dl2' -ItemType Directory -Force).FullName |
| 37 | + $dep = New-PSDependFixture -DependencyName 'ignored-key' -DependencyType 'FileDownload' -Target $targetDir -Source 'https://example.com/other.dll' |
| 38 | + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { |
| 39 | + & $ScriptPath -Dependency $Dep |
| 40 | + } |
| 41 | + Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter { |
| 42 | + $URL -eq 'https://example.com/other.dll' |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + It 'Skips download when the target file already exists' { |
| 47 | + $targetDir = (New-Item 'TestDrive:/dl3' -ItemType Directory -Force).FullName |
| 48 | + $existingFile = Join-Path $targetDir 'sample.dll' |
| 49 | + Set-Content -Path $existingFile -Value 'existing' |
| 50 | + |
| 51 | + $dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $existingFile |
| 52 | + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { |
| 53 | + & $ScriptPath -Dependency $Dep |
| 54 | + } |
| 55 | + Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 0 |
| 56 | + } |
| 57 | + |
| 58 | + It 'PSDependAction Test returns $true when the file exists' { |
| 59 | + $targetDir = (New-Item 'TestDrive:/dl4' -ItemType Directory -Force).FullName |
| 60 | + $existingFile = Join-Path $targetDir 'sample.dll' |
| 61 | + Set-Content -Path $existingFile -Value 'existing' |
| 62 | + |
| 63 | + $dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $existingFile |
| 64 | + $result = InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { |
| 65 | + & $ScriptPath -Dependency $Dep -PSDependAction Test |
| 66 | + } |
| 67 | + $result | Should -Be $true |
| 68 | + } |
| 69 | +} |
0 commit comments