55
66Describe ' Azure Functions PowerShell Langauge Worker Helper Module Tests' {
77
8- # Helper function that tests hashtable equality
9- function IsEqualHashtable ($h1 , $h2 ) {
10- # Handle nulls
11- if (! $h1 ) {
12- if (! $h2 ) {
13- return $true
8+ BeforeAll {
9+ # Move the .psd1 and .psm1 files to the publish folder so that the dlls can be found
10+ $binFolder = Resolve-Path - Path " $PSScriptRoot /../bin"
11+ $workerDll = Get-ChildItem - Path $binFolder - Filter " Microsoft.Azure.Functions.PowerShellWorker.dll" - Recurse | Select-Object - First 1
12+
13+ $moduleFolder = Join-Path - Path $workerDll.Directory.FullName - ChildPath " Modules\Microsoft.Azure.Functions.PowerShellWorker"
14+ $pubFolder = Join-Path - Path $workerDll.Directory.FullName - ChildPath " publish"
15+ Copy-Item - Path $moduleFolder /* - Destination $pubFolder - ErrorAction SilentlyContinue
16+
17+ $modulePath = Join-Path - Path $pubFolder - ChildPath " Microsoft.Azure.Functions.PowerShellWorker.psd1"
18+ Import-Module $modulePath
19+
20+ # Helper function that tests hashtable equality
21+ function IsEqualHashtable ($h1 , $h2 ) {
22+ # Handle nulls
23+ if (! $h1 ) {
24+ if (! $h2 ) {
25+ return $true
26+ }
27+ return $false
1428 }
15- return $false
16- }
17- if (! $h2 ) {
18- return $false
19- }
20-
21- # If they don't have the same amount of key value pairs, fail early
22- if ($h1.Count -ne $h2.Count ){
23- return $false
24- }
25-
26- # Check to make sure every key exists in the other and that the values are the same
27- foreach ($key in $h1.Keys ) {
28- if (! $h2.ContainsKey ($key ) -or $h1 [$key ] -ne $h2 [$key ]) {
29+ if (! $h2 ) {
30+ return $false
31+ }
32+
33+ # If they don't have the same amount of key value pairs, fail early
34+ if ($h1.Count -ne $h2.Count ){
2935 return $false
3036 }
37+
38+ # Check to make sure every key exists in the other and that the values are the same
39+ foreach ($key in $h1.Keys ) {
40+ if (! $h2.ContainsKey ($key ) -or $h1 [$key ] -ne $h2 [$key ]) {
41+ return $false
42+ }
43+ }
44+ return $true
3145 }
32- return $true
3346 }
3447
3548 Context ' Push-OutputBinding tests' {
36- BeforeEach {
37- Import-Module " $PSScriptRoot /../../src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 " - Force
38- $module = ( Get-Module Microsoft.Azure.Functions.PowerShellWorker)[ 0 ]
49+
50+ AfterAll {
51+ Get-OutputBinding - Purge > $null
3952 }
4053
4154 It ' Can add a value via parameters' {
4255 $Key = ' Test'
4356 $Value = 5
4457
4558 Push-OutputBinding - Name $Key - Value $Value
46- $result = & $module { $ script :_OutputBindings }
59+ $result = Get-OutputBinding - Purge
4760 $result [$Key ] | Should - BeExactly $Value
4861 }
4962
@@ -65,35 +78,41 @@ Describe 'Azure Functions PowerShell Langauge Worker Helper Module Tests' {
6578 )
6679
6780 $InputData | Push-OutputBinding
68- $result = & $module { $ script :_OutputBindings }
81+ $result = Get-OutputBinding - Purge
6982 IsEqualHashtable $result $Expected | Should - BeTrue `
7083 - Because ' The hashtables should be identical'
7184 }
7285
7386 It ' Throws if you attempt to overwrite an Output binding' {
74- Push-OutputBinding Foo 5
75- { Push-OutputBinding Foo 6 } | Should - Throw
87+ try {
88+ Push-OutputBinding Foo 5
89+ { Push-OutputBinding Foo 6 } | Should - Throw
90+ } finally {
91+ Get-OutputBinding - Purge > $null
92+ }
7693 }
7794
7895 It ' Can overwrite values if "-Force" is specified' {
79- $internalHashtable = & $module { $script :_OutputBindings }
8096 Push-OutputBinding Foo 5
81- IsEqualHashtable @ {Foo = 5 } $internalHashtable | Should - BeTrue `
97+ $result = Get-OutputBinding - Purge
98+ IsEqualHashtable @ {Foo = 5 } $result | Should - BeTrue `
8299 - Because ' The hashtables should be identical'
83100
84101 Push-OutputBinding Foo 6 - Force
85- IsEqualHashtable @ {Foo = 6 } $internalHashtable | Should - BeTrue `
102+ $result = Get-OutputBinding - Purge
103+ IsEqualHashtable @ {Foo = 6 } $result | Should - BeTrue `
86104 - Because ' -Force should let you overwrite the output binding'
87105 }
88106 }
89107
90108 Context ' Get-OutputBinding tests' {
91109 BeforeAll {
92- Import-Module " $PSScriptRoot /../../src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1" - Force
93- $module = (Get-Module Microsoft.Azure.Functions.PowerShellWorker)[0 ]
94- & $module {
95- $script :_OutputBindings = @ { Foo = 1 ; Bar = ' Baz' ; Food = ' apple' }
96- }
110+ $inputData = @ { Foo = 1 ; Bar = ' Baz' ; Food = ' apple' }
111+ $inputData | Push-OutputBinding
112+ }
113+
114+ AfterAll {
115+ Get-OutputBinding - Purge
97116 }
98117
99118 It ' Can get the output binding hashmap - <Description>' - TestCases @ (
@@ -129,13 +148,11 @@ Describe 'Azure Functions PowerShell Langauge Worker Helper Module Tests' {
129148 }
130149
131150 It ' Can use the "-Purge" flag to clear the Output bindings' {
132- $initialState = (& $module { $script :_OutputBindings }).Clone()
133-
134151 $result = Get-OutputBinding - Purge
135- IsEqualHashtable $result $initialState | Should - BeTrue `
152+ IsEqualHashtable $result $inputData | Should - BeTrue `
136153 - Because ' The full hashtable should be returned'
137154
138- $newState = & $module { $ script :_OutputBindings }
155+ $newState = Get-OutputBinding
139156 IsEqualHashtable @ {} $newState | Should - BeTrue `
140157 - Because ' The OutputBindings should be empty'
141158 }
0 commit comments