|
| 1 | +#! requires -Module ModuleBuilder |
| 2 | +Describe "CompressToBase64" { |
| 3 | + |
| 4 | + Context "It compresses and encodes a file for embedding into a script" { |
| 5 | + BeforeAll { |
| 6 | + $Base64 = InModuleScope ModuleBuilder { |
| 7 | + CompressToBase64 $PSCommandPath |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + It "Returns a base64 encoded string" { |
| 12 | + $Base64 | Should -BeOfType [string] |
| 13 | + $Base64 | Should -Match "^[A-Za-z0-9\+\/]+=*$" |
| 14 | + $Base64.Length | Should -BeGreaterThan 0 |
| 15 | + } |
| 16 | + |
| 17 | + It "Returns the gzipped and encoded script" { |
| 18 | + $OutputStream = [System.IO.MemoryStream]::new() |
| 19 | + $InputStream = [System.IO.MemoryStream][System.Convert]::FromBase64String($Base64) |
| 20 | + $DeflateStream = [System.IO.Compression.DeflateStream]::new($InputStream, [System.IO.Compression.CompressionMode]::Decompress) |
| 21 | + $DeflateStream.CopyTo($OutputStream) |
| 22 | + $OutputStream.Seek(0, "Begin") |
| 23 | + $Source = [System.IO.StreamReader]::new($OutputStream, $true).ReadToEnd() |
| 24 | + |
| 25 | + $Source | Should -Be (Get-Content $PSCommandPath -Raw) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + Context "It wraps the Base64 encoded content in the specified command" { |
| 30 | + BeforeAll { |
| 31 | + $Base64 = InModuleScope ModuleBuilder { |
| 32 | + CompressToBase64 $PSCommandPath -ExpandScriptName ImportBase64Module |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + It "Returns a string" { |
| 37 | + $Base64 | Should -BeOfType [string] |
| 38 | + } |
| 39 | + |
| 40 | + It "Pipes the encoding into the command" { |
| 41 | + $Block = InModuleScope ModuleBuilder { |
| 42 | + (Get-Command ImportBase64Module).ScriptBlock |
| 43 | + } |
| 44 | + |
| 45 | + $Base64 | Should -Match "|.{`n$Block`n}$" |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Context "It wraps the Base64 encoded content in the specified scriptblock" { |
| 50 | + BeforeAll { |
| 51 | + $Base64 = InModuleScope ModuleBuilder { |
| 52 | + Get-ChildItem $PSCommandPath | CompressToBase64 -ExpandScript { ImportBase64Module } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + It "Returns a string" { |
| 57 | + $Base64 | Should -BeOfType [string] |
| 58 | + } |
| 59 | + It "Pipes the encoding into the scriptblock" { |
| 60 | + $Base64 | Should -Match "|.{`nImportBase64Module`n}$" |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments