Skip to content

Commit 45b6099

Browse files
authored
Update the module tests to not depend on internal data structure (#74)
1 parent ee8229b commit 45b6099

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

test/Modules/Microsoft.Azure.Functions.PowerShellWorker.Tests.ps1

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,55 @@
55

66
Describe '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+
$modulePath = Join-Path -Path $moduleFolder -ChildPath "Microsoft.Azure.Functions.PowerShellWorker.psd1"
15+
Import-Module $modulePath
16+
17+
# Helper function that tests hashtable equality
18+
function IsEqualHashtable ($h1, $h2) {
19+
# Handle nulls
20+
if (!$h1) {
21+
if(!$h2) {
22+
return $true
23+
}
24+
return $false
1425
}
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]) {
26+
if (!$h2) {
27+
return $false
28+
}
29+
30+
# If they don't have the same amount of key value pairs, fail early
31+
if ($h1.Count -ne $h2.Count){
2932
return $false
3033
}
34+
35+
# Check to make sure every key exists in the other and that the values are the same
36+
foreach ($key in $h1.Keys) {
37+
if (!$h2.ContainsKey($key) -or $h1[$key] -ne $h2[$key]) {
38+
return $false
39+
}
40+
}
41+
return $true
3142
}
32-
return $true
3343
}
3444

3545
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]
46+
47+
AfterAll {
48+
Get-OutputBinding -Purge > $null
3949
}
4050

4151
It 'Can add a value via parameters' {
4252
$Key = 'Test'
4353
$Value = 5
4454

4555
Push-OutputBinding -Name $Key -Value $Value
46-
$result = & $module { $script:_OutputBindings }
56+
$result = Get-OutputBinding -Purge
4757
$result[$Key] | Should -BeExactly $Value
4858
}
4959

@@ -65,35 +75,41 @@ Describe 'Azure Functions PowerShell Langauge Worker Helper Module Tests' {
6575
)
6676

6777
$InputData | Push-OutputBinding
68-
$result = & $module { $script:_OutputBindings }
78+
$result = Get-OutputBinding -Purge
6979
IsEqualHashtable $result $Expected | Should -BeTrue `
7080
-Because 'The hashtables should be identical'
7181
}
7282

7383
It 'Throws if you attempt to overwrite an Output binding' {
74-
Push-OutputBinding Foo 5
75-
{ Push-OutputBinding Foo 6} | Should -Throw
84+
try {
85+
Push-OutputBinding Foo 5
86+
{ Push-OutputBinding Foo 6} | Should -Throw
87+
} finally {
88+
Get-OutputBinding -Purge > $null
89+
}
7690
}
7791

7892
It 'Can overwrite values if "-Force" is specified' {
79-
$internalHashtable = & $module { $script:_OutputBindings }
8093
Push-OutputBinding Foo 5
81-
IsEqualHashtable @{Foo = 5} $internalHashtable | Should -BeTrue `
94+
$result = Get-OutputBinding -Purge
95+
IsEqualHashtable @{Foo = 5} $result | Should -BeTrue `
8296
-Because 'The hashtables should be identical'
8397

8498
Push-OutputBinding Foo 6 -Force
85-
IsEqualHashtable @{Foo = 6} $internalHashtable | Should -BeTrue `
99+
$result = Get-OutputBinding -Purge
100+
IsEqualHashtable @{Foo = 6} $result | Should -BeTrue `
86101
-Because '-Force should let you overwrite the output binding'
87102
}
88103
}
89104

90105
Context 'Get-OutputBinding tests' {
91106
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-
}
107+
$inputData = @{ Foo = 1; Bar = 'Baz'; Food = 'apple'}
108+
$inputData | Push-OutputBinding
109+
}
110+
111+
AfterAll {
112+
Get-OutputBinding -Purge
97113
}
98114

99115
It 'Can get the output binding hashmap - <Description>' -TestCases @(
@@ -129,13 +145,11 @@ Describe 'Azure Functions PowerShell Langauge Worker Helper Module Tests' {
129145
}
130146

131147
It 'Can use the "-Purge" flag to clear the Output bindings' {
132-
$initialState = (& $module { $script:_OutputBindings }).Clone()
133-
134148
$result = Get-OutputBinding -Purge
135-
IsEqualHashtable $result $initialState | Should -BeTrue `
149+
IsEqualHashtable $result $inputData | Should -BeTrue `
136150
-Because 'The full hashtable should be returned'
137151

138-
$newState = & $module { $script:_OutputBindings }
152+
$newState = Get-OutputBinding
139153
IsEqualHashtable @{} $newState | Should -BeTrue `
140154
-Because 'The OutputBindings should be empty'
141155
}

0 commit comments

Comments
 (0)