-
Notifications
You must be signed in to change notification settings - Fork 7
/
moduleReloadingTests.ps1
200 lines (191 loc) · 7.3 KB
/
moduleReloadingTests.ps1
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
$guid = '2282bfa0-cfdc-4ee5-a7a4-7aca5b33dc9f'
$workingFolderPath = "$([System.IO.Path]::GetTempPath())$guid"
$moduleContents = [ordered]@{
NoClasses = @{
Root = @'
Import-Module "$PSScriptRoot\Sub.psm1" -Args $Args
$passedArgs = $Args
function Get-PassedArgsRoot { $passedArgs }
'@
Sub = @'
$passedArgs = $Args
function Get-PassedArgsSub { $passedArgs }
'@
}
Classes = @{
Root = @'
Import-Module "$PSScriptRoot\Sub.psm1" -Args $Args
$passedArgs = $Args
class Root { $passedArgs = $passedArgs }
function Get-PassedArgsRoot { [Root]::new().passedArgs }
'@
Sub = @'
$passedArgs = $Args
class Sub { $passedArgs = $passedArgs }
function Get-PassedArgsSub { [Sub]::new().passedArgs }
'@
}
}
foreach ( $contentsName in $moduleContents.Keys )
{
$values = @{
# args1, root1, sub1, args2, root2, sub2
# NoClasses = 'value 1', 'value 1', 'value 1', 'value 2', 'value 2', 'value 2' # <== how it should be
NoClasses = 'value 1', 'value 1', 'value 1', 'value 2', 'value 2', 'value 1' # <== how it actually is
# Classes = 'value 1', 'value 1', 'value 1', 'value 2', 'value 2', 'value 2' # <== how it should be
Classes = 'value 1', 'value 1', 'value 1', 'value 2', 'value 1', 'value 1' # <== how it actually is
}
$args1,$root1,$sub1,$args2,$root2,$sub2 = $values.$contentsName
$moduleName = "DifferentArgs_$contentsName"
Describe "$moduleName" {
$rootModuleFolderPath = "$workingFolderPath\$moduleName"
Context 'set up the module files' {
It 'create the root module folder' {
New-Item $rootModuleFolderPath -ItemType Container -Force
}
It 'create the root module file' {
$moduleContents.$contentsName.Root | Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop
}
It 'create the sub module file' {
$moduleContents.$contentsName.Sub | Set-Content "$rootModuleFolderPath\Sub.psm1"
}
}
Context "Import-Module with argument $args1 and test" {
It 'import the root module with value 1' {
Import-Module $rootModuleFolderPath -Args $args1 -Force -PassThru -ea Stop
}
It "the root module returns $root1" {
$r = Get-PassedArgsRoot
$r | Should be $root1
}
It "the sub module returns $sub1" {
$r = Get-PassedArgsSub
$r | Should be $sub1
}
}
Context "Import-Module with argument $args2 and test" {
It 'import the root module with value 1' {
Import-Module $rootModuleFolderPath -Args $args2 -Force -PassThru -ea Stop
}
It "the root module returns $root2" {
$r = Get-PassedArgsRoot
$r | Should be $root2
}
It "the sub module returns $sub2" {
$r = Get-PassedArgsSub
$r | Should be $sub2
}
}
}
}
$moduleContents = [ordered]@{
NoClasses = @{
RootContent1 = @'
Import-Module "$PSScriptRoot\Sub.psm1"
function Get-ValueRoot { 'value 1' }
'@
RootContent2 = @'
Import-Module "$PSScriptRoot\Sub.psm1"
function Get-ValueRoot { 'value 2' }
'@
SubContent1 = @'
function Get-ValueSub { 'value 1' }
'@
SubContent2 = @'
function Get-ValueSub { 'value 2' }
'@
}
Classes = @{
RootContent1 = @'
Import-Module "$PSScriptRoot\Sub.psm1"
class Root { $p = 'value 1' }
function Get-ValueRoot { [Root]::new().p }
'@
RootContent2 = @'
Import-Module "$PSScriptRoot\Sub.psm1"
class Root { $p = 'value 2' }
function Get-ValueRoot { [Root]::new().p }
'@
SubContent1 = @'
class Sub { $p = 'value 1' }
function Get-ValueSub { [Sub]::new().p }
'@
SubContent2 = @'
class Sub { $p = 'value 2' }
function Get-ValueSub { [Sub]::new().p }
'@
}
}
foreach ( $contentsName in $moduleContents.Keys )
{
$tests = @(
# | | Expected Values |
# | modulePrefix| changeRoot | changeSub | rootBefore | rootAfter | subBefore | subAfter |
# @( 'ChangeBoth', $true, $true, 'value 1', 'value 2', 'value 1', 'value 2' ), # <== how it should be
@( 'ChangeBoth', $true, $true, 'value 1', 'value 2', 'value 1', 'value 1' ), # <== how it actually is
# @( 'ChangeSub', $false, $true, 'value 1', 'value 1', 'value 1', 'value 2' ), # <== how it should be
@( 'ChangeSub', $false, $true, 'value 1', 'value 1', 'value 1', 'value 1' ), # <== how it actually is
@( 'ChangeRoot', $true, $false, 'value 1', 'value 2', 'value 1', 'value 1' )
)
foreach ( $values in $tests )
{
$modulePrefix,$changeRoot,$changeSub,$rootBefore,$rootAfter,$subBefore,$subAfter = $values
$moduleName = "$modulePrefix`_$contentsName"
Describe "reloading modules after changes - $moduleName" {
$rootModuleFolderPath = "$workingFolderPath\$moduleName"
Context 'set up the module files' {
It 'create the root module folder' {
New-Item $rootModuleFolderPath -ItemType Container -Force
}
It 'create the root module file' {
$moduleContents.$contentsName.RootContent1 | Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop
}
It 'create the sub module file' {
$moduleContents.$contentsName.SubContent1 | Set-Content "$rootModuleFolderPath\Sub.psm1"
}
}
Context 'import the module and test' {
It 'import the module' {
Import-Module $rootModuleFolderPath -Force -ea Stop
}
It "root returns $rootBefore" {
$r = Get-ValueRoot
$r | Should be $rootBefore
}
It "sub returns $subBefore" {
$r = Get-ValueSub
$r | Should be $subBefore
}
}
Context 'change contents' {
if ( $changeRoot )
{
It 'overwrite the root module file' {
$moduleContents.$contentsName.RootContent2 |
Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop
}
}
if ( $changeSub )
{
It 'overwrite the sub module file' {
$moduleContents.$contentsName.SubContent2 |
Set-Content "$rootModuleFolderPath\Sub.psm1" -Force -ea Stop
}
}
}
Context 're-import the module and test' {
It 're-import the module' {
Import-Module $rootModuleFolderPath -Force -ea Stop
}
It "root returns $rootAfter" {
$r = Get-ValueRoot
$r | Should be $rootAfter
}
It "sub returns $subAfter" {
$r = Get-ValueSub
$r | Should be $subAfter
}
}
}
}
}