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