-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CachedLocation.Tests.ps1
More file actions
100 lines (87 loc) · 4.41 KB
/
Get-CachedLocation.Tests.ps1
File metadata and controls
100 lines (87 loc) · 4.41 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
BeforeDiscovery { Import-Module -Force -Name $PSScriptRoot/../../../src/common/Cache.psm1 }
Describe 'Get-CachedLocation Tests' {
BeforeAll {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'UseDeclaredVarsMoreThanAssignments',
$null
)]$CacheName = 'UNIQUE_CACHE_NAME';
InModuleScope Cache {
$Script:Folder = "$((Get-PSDrive TestDrive).Root)\PSCache"
}
}
AfterEach {
InModuleScope Cache {
Remove-Item -Path $Script:Folder -Recurse -Force
}
}
It 'Should return the correct path' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName } {
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return "" }
$CachePath | Should -Be (Join-Path -Path $Script:Folder -ChildPath "Cached-$CacheName")
}
}
It 'Create the file if it does not exist' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName; } {
$Content = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $Content }
Test-Path -Path $CachePath | Should -Be $true
Get-Content -Path $CachePath | Should -Be $Content
}
}
It 'Removes existing file if -NoCache is used' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName; } {
$Content = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $Content }
Get-Content -Path $CachePath | Should -Be $Content
$NewContent = [System.Guid]::NewGuid().ToString()
Get-CachedLocation -Name $CacheName -CreateBlock { return $NewContent } -NoCache
Get-Content -Path $CachePath | Should -Be $NewContent
}
}
It 'Should not override existing file' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName; } {
$Content = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $Content }
Get-Content -Path $CachePath | Should -Be $Content
Get-CachedLocation -Name $CacheName -CreateBlock { return [System.Guid]::NewGuid().ToString() }
Get-Content -Path $CachePath | Should -Be $Content
}
}
It 'Removes cache if MaxAge is exceeded' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName; } {
$Content = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $Content }
Test-Path -Path $CachePath | Should -Be $true
Start-Sleep -Seconds 1
$NewContent = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $NewContent } -MaxAge (New-TimeSpan -Seconds 1)
Get-Content -Path $CachePath | Should -Be $NewContent
}
}
Context 'IsValidBlock Parameter Tests' {
It 'Uses IsValidBlock to replace cache' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName } {
$InvalidContent = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $InvalidContent }
Get-Content -Path $CachePath | Should -Be $InvalidContent
$ValidContent = [System.Guid]::NewGuid().ToString()
Get-CachedLocation -Name $CacheName `
-CreateBlock { return $ValidContent } `
-IsValidBlock { param($Path) return $false }
Get-Content -Path $CachePath | Should -Be $ValidContent
}
}
It 'Uses IsValidBlock to keep valid cache' {
InModuleScope Cache -Parameters @{ CacheName = $CacheName } {
$ValidContent = [System.Guid]::NewGuid().ToString()
$CachePath = Get-CachedLocation -Name $CacheName -CreateBlock { return $ValidContent }
Get-Content -Path $CachePath | Should -Be $ValidContent
$InvalidContent = [System.Guid]::NewGuid().ToString()
Get-CachedLocation -Name $CacheName `
-CreateBlock { return $InvalidContent } `
-IsValidBlock { param($Path) return $true }
Get-Content -Path $CachePath | Should -Be $ValidContent
}
}
}
}