|
| 1 | +BeforeDiscovery { |
| 2 | + Set-Location -Path $PSScriptRoot |
| 3 | + $ModuleName = 'pwshBedrock' |
| 4 | + $PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1") |
| 5 | + $script:assetPath = [System.IO.Path]::Combine('..', 'assets') |
| 6 | + #if the module is already in memory, remove it |
| 7 | + Get-Module $ModuleName -ErrorAction SilentlyContinue | Remove-Module -Force |
| 8 | + Import-Module $PathToManifest -Force |
| 9 | +} |
| 10 | +$script:supportedMediaExtensions = @( |
| 11 | + 'JPG' |
| 12 | + 'JPEG' |
| 13 | + 'PNG' |
| 14 | + 'GIF' |
| 15 | + 'WEBP' |
| 16 | +) |
| 17 | + |
| 18 | +InModuleScope 'pwshBedrock' { |
| 19 | + Describe 'Test-MetaMedia Private Function Tests' -Tag Unit { |
| 20 | + BeforeAll { |
| 21 | + $WarningPreference = 'SilentlyContinue' |
| 22 | + $ErrorActionPreference = 'SilentlyContinue' |
| 23 | + |
| 24 | + } #beforeAll |
| 25 | + |
| 26 | + Context 'Error' { |
| 27 | + |
| 28 | + It 'Should return false if an error is encountered running Test-Path' { |
| 29 | + Mock -CommandName Test-Path -MockWith { throw 'Test-Path Error' } |
| 30 | + $mediaPath = 'C:\path\to\image.jpg' |
| 31 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 32 | + $result | Should -Be $false |
| 33 | + } #it |
| 34 | + |
| 35 | + It 'Should return false if an error is encountered running Get-Item' { |
| 36 | + Mock -CommandName Test-Path -MockWith { $true } |
| 37 | + Mock -CommandName Get-Item -MockWith { throw 'Get-Item Error' } |
| 38 | + $mediaPath = 'C:\path\to\image.jpg' |
| 39 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 40 | + $result | Should -Be $false |
| 41 | + } #it |
| 42 | + |
| 43 | + It 'Should return false if the resolution of the image exceeds requirements' { |
| 44 | + Mock -CommandName Test-Path -MockWith { $true } |
| 45 | + Mock -CommandName Get-Item -Mockwith { |
| 46 | + [PSCustomObject]@{ |
| 47 | + Length = 10000 |
| 48 | + } |
| 49 | + } #endMock |
| 50 | + Mock -CommandName Get-ImageResolution -MockWith { |
| 51 | + [PSCustomObject]@{ |
| 52 | + Width = 2000 |
| 53 | + Height = 2000 |
| 54 | + } |
| 55 | + } #endMock |
| 56 | + Mock Write-Warning {} |
| 57 | + $mediaPath = 'C:\path\to\image.jpg' |
| 58 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 59 | + $result | Should -Be $false |
| 60 | + } #it |
| 61 | + |
| 62 | + } #context_Error |
| 63 | + |
| 64 | + Context 'Success' { |
| 65 | + |
| 66 | + It 'Should return true for <_> type if checks pass' -ForEach $supportedMediaExtensions { |
| 67 | + Mock -CommandName Test-Path -MockWith { $true } |
| 68 | + Mock -CommandName Get-Item -Mockwith { |
| 69 | + [PSCustomObject]@{ |
| 70 | + Length = 10000 |
| 71 | + } |
| 72 | + } #endMock |
| 73 | + Mock -CommandName Get-ImageResolution -MockWith { |
| 74 | + [PSCustomObject]@{ |
| 75 | + Width = 100 |
| 76 | + Height = 100 |
| 77 | + } |
| 78 | + } #endMock |
| 79 | + $mediaPath = 'C:\path\to\image.' + $_.ToLower() |
| 80 | + $result = Test-MetaMedia -MediaPath $MediaPath |
| 81 | + $result | Should -Be $true |
| 82 | + } #it |
| 83 | + |
| 84 | + It 'Should return false if file can not be found' { |
| 85 | + Mock -CommandName Test-Path -MockWith { $false } |
| 86 | + $mediaPath = 'C:\path\to\image.jpg' |
| 87 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 88 | + $result | Should -Be $false |
| 89 | + } #it |
| 90 | + |
| 91 | + It 'Should return false if file type is not supported' { |
| 92 | + Mock -CommandName Test-Path -MockWith { $true } |
| 93 | + Mock -CommandName Get-ImageResolution -MockWith { |
| 94 | + [PSCustomObject]@{ |
| 95 | + Width = 100 |
| 96 | + Height = 100 |
| 97 | + } |
| 98 | + } #endMock |
| 99 | + $mediaPath = 'C:\path\to\image.zip' |
| 100 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 101 | + $result | Should -Be $false |
| 102 | + } #it |
| 103 | + |
| 104 | + It 'Should return false if the file is too large' { |
| 105 | + Mock -CommandName Test-Path -MockWith { $true } |
| 106 | + Mock -CommandName Get-Item -MockWith { |
| 107 | + [PSCustomObject]@{ |
| 108 | + Length = 10000000 |
| 109 | + } |
| 110 | + } |
| 111 | + Mock -CommandName Get-ImageResolution -MockWith { |
| 112 | + [PSCustomObject]@{ |
| 113 | + Width = 100 |
| 114 | + Height = 100 |
| 115 | + } |
| 116 | + } #endMock |
| 117 | + $mediaPath = 'C:\path\to\image.jpg' |
| 118 | + $result = Test-MetaMedia -MediaPath $mediaPath |
| 119 | + $result | Should -Be $false |
| 120 | + } #it |
| 121 | + |
| 122 | + } #context_Success |
| 123 | + |
| 124 | + } #describe_Test-MetaMedia |
| 125 | +} #inModule |
0 commit comments