1
1
2
-
3
- Import-Module - Name " $PSScriptRoot \..\..\Task\HelperModule.psm1" - Force
4
-
5
2
Describe " Testing Helper Functions" {
6
3
4
+ BeforeAll {
5
+ Import-Module - Name " $PSScriptRoot \..\..\Task\HelperModule.psm1" - Force
6
+ }
7
7
Context " Testing Get-HashtableFromString" {
8
8
9
- it " Can parse empty block" {
9
+ It " Can parse empty block" {
10
10
$actual = Get-HashtableFromString - line " "
11
11
$actual.GetType () | Should - Be @ {}.GetType()
12
12
$actual.count | Should - Be 0
13
13
}
14
14
15
- it " Can parse block with no values but delimiter" {
15
+ It " Can parse block with no values but delimiter" {
16
16
$actual = Get-HashtableFromString - line " ;"
17
17
$actual.GetType () | Should - Be @ {}.GetType()
18
18
$actual.count | Should - Be 0
19
19
}
20
20
21
- it " Cannot parse block with invalid string" {
21
+ It " Cannot parse block with invalid string" {
22
22
$actual = Get-HashtableFromString - line " ;"
23
23
$actual.GetType () | Should - Be @ {}.GetType()
24
24
$actual.count | Should - Be 0
25
25
}
26
26
27
- it " Can parse two part block" {
27
+ It " Can parse two part block" {
28
28
$actual = Get-HashtableFromString - line " @{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}}"
29
29
$actual.Path | Should - Be " C:\path\123"
30
30
$actual.Parameters.param1 | Should - Be " 111"
31
31
$actual.Parameters.param2 | Should - Be " 222"
32
32
}
33
33
34
- it " Can parse two hashtable part block" {
34
+ It " Can parse two hashtable part block" {
35
35
$actual = Get-HashtableFromString - line " @{Parameters1=@{param1='111'; param2='222'}; Parameters2=@{param1='111'; param2='222'}}"
36
36
$actual.Parameters1.param1 | Should - Be " 111"
37
37
$actual.Parameters1.param2 | Should - Be " 222"
38
38
$actual.Parameters2.param1 | Should - Be " 111"
39
39
$actual.Parameters2.param2 | Should - Be " 222"
40
40
}
41
41
42
- it " Can parse block with trailing ;" {
42
+ It " Can parse block with trailing ;" {
43
43
$actual = Get-HashtableFromString - line " @{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}};"
44
44
$actual.Path | Should - Be " C:\path\123"
45
45
$actual.Parameters.param1 | Should - Be " 111"
46
46
$actual.Parameters.param2 | Should - Be " 222"
47
47
}
48
48
49
- it " Can parse three part block 1" {
49
+ It " Can parse three part block 1" {
50
50
$actual = Get-HashtableFromString - line " @{Path='C:\path\123'; x='y'; Parameters=@{param1='111'; param2='222'}}"
51
51
$actual.Path | Should - Be " C:\path\123"
52
52
$actual.Parameters.param1 | Should - Be " 111"
53
53
$actual.Parameters.param2 | Should - Be " 222"
54
54
$actual.x | Should - Be " y"
55
55
}
56
56
57
- it " Can parse three part block 2" {
57
+ It " Can parse three part block 2" {
58
58
$actual = Get-HashtableFromString - line " @{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}; x='y'}"
59
59
$actual.Path | Should - Be " C:\path\123"
60
60
$actual.Parameters.param1 | Should - Be " 111"
61
61
$actual.Parameters.param2 | Should - Be " 222"
62
62
$actual.x | Should - Be " y"
63
63
}
64
- it " Can parse hashtable with ; in a value" {
64
+ It " Can parse hashtable with ; in a value" {
65
65
$actual = Get-HashtableFromString - line " @{Path='.\tests\script.tests.ps1'; Parameters=@{someVar='this'}},@{Path='.\tests\script2.tests.ps1'; Parameters=@{otherparam='foo.txt;bar.txt'}}"
66
66
$actual.GetType ().BaseType | Should - Be " Array"
67
67
$actual [0 ].Path | Should - Be ' .\tests\script.tests.ps1'
68
68
$actual [0 ].Parameters.SomeVar | Should - Be ' this'
69
69
$actual [1 ].Path | Should - Be ' .\tests\script2.tests.ps1'
70
70
$actual [1 ].Parameters.otherparam | Should - Be ' foo.txt;bar.txt'
71
71
}
72
- it " Can parse hashtable with commas in a value" {
72
+ It " Can parse hashtable with commas in a value" {
73
73
$actual = Get-HashtableFromString - line " @{Path='.\tests\script.tests.ps1'; Parameters=@{someVar='this'}},@{Path='.\tests\script2.tests.ps1'; Parameters=@{otherparam='foo.txt;bar.txt';Param2='ValueGoesHere'}},@{path='.\tests\script3.tests.ps1';Parameters=@{inputvar='var,this,string'}}"
74
74
$actual.GetType ().BaseType | Should - Be " Array"
75
75
$actual [0 ].Path | Should - Be ' .\tests\script.tests.ps1'
@@ -80,7 +80,92 @@ Describe "Testing Helper Functions" {
80
80
$actual [2 ].Path | Should - Be ' .\tests\script3.tests.ps1'
81
81
$actual [2 ].Parameters.inputvar | Should - Be ' var,this,string'
82
82
}
83
+ }
84
+
85
+ Context " Testing Import-Pester" {
86
+
87
+ BeforeAll {
88
+ Mock - CommandName Import-Module - MockWith { }
89
+ Mock - CommandName Install-Module - MockWith { $true }
90
+ Mock - CommandName Write-host - MockWith { }
91
+ Mock - CommandName Get-PSRepository - MockWith {[PSCustomObject ]@ {Name = ' PSGallery' }}
92
+ Mock - CommandName Get-Command - MockWith { [PsCustomObject ]@ {Parameters = @ {SkipPublisherCheck = ' SomeValue' }}} - ParameterFilter {$Name -eq ' Install-Module' }
93
+ Mock - CommandName Get-Command - MockWith { [PsCustomObject ]@ {Parameters = @ {AllowPrerelease = ' SomeValue' }}} - ParameterFilter {$Name -eq ' Find-Module' }
94
+ }
95
+
96
+ It " Installs the latest version of Pester when on PS5+ and PowerShellGet is available" {
97
+ Mock - CommandName Find-Module - MockWith { [PsCustomObject ]@ {Version = [version ]::new(9 , 9 , 9 );Repository = ' PSGallery' }}
98
+ Mock - CommandName Get-PackageProvider - MockWith { $True }
99
+
100
+ Import-Pester - Version " latest"
101
+
102
+ Assert-MockCalled - CommandName Import-Module - ParameterFilter {$RequiredVersion -eq " 9.9.9" } - Scope It - Times 1
103
+ }
104
+
105
+ It " Installs the latest version of Pester from PSGallery when multiple repositories are available" {
106
+ Mock - CommandName Find-Module - MockWith { @ (
107
+ [PsCustomObject ]@ {Version = [version ]::new(4 , 3 , 0 );Repository = ' OtherRepository' }
108
+ [PsCustomObject ]@ {Version = [version ]::new(9 , 9 , 9 );Repository = ' PSGallery' }
109
+ )
110
+ }
111
+ Mock - CommandName Get-PackageProvider - MockWith { $True }
112
+
113
+ Import-Pester - Version " latest"
114
+
115
+ Assert-MockCalled - CommandName Install-Module - Scope It - ParameterFilter {$Repository -eq ' PSGallery' }
116
+ }
117
+
118
+ It " Installs the required version of NuGet provider when PowerShellGet is available and NuGet isn't already installed" {
119
+ Mock - CommandName Find-Module - MockWith { [PsCustomObject ]@ {Version = [version ]::new(9 , 9 , 9 );Repository = ' PSGallery' }}
120
+ Mock - CommandName Get-PackageProvider - MockWith { throw }
121
+ Mock - CommandName Install-PackageProvider - MockWith {}
122
+
123
+ Import-Pester - Version " latest"
124
+
125
+ Assert-MockCalled - CommandName Install-PackageProvider
126
+ }
127
+
128
+ It " Should not install a new version of Pester when the latest is already installed" {
129
+ Mock - CommandName Find-Module - MockWith { [PsCustomObject ]@ {Version = (Get-Module Pester).Version;Repository = ' PSGallery' }}
130
+ Mock - CommandName Get-PackageProvider - MockWith { $True }
131
+
132
+ Import-Pester - Version " latest"
133
+
134
+ Assert-MockCalled - CommandName Install-Module - Times 0 - Scope It
135
+ }
136
+
137
+ It " Should install and import the specified version of Pester regardless of what is avaialble locally" {
138
+ Mock - CommandName Find-Module - MockWith { [PsCustomObject ]@ {Version = [version ]::new(4 , 2 , 0 );Repository = ' PSGallery' }}
139
+ Mock - CommandName Get-PackageProvider - MockWith { $True }
140
+
141
+ Import-Pester - Version 4.2 .0
142
+
143
+ Assert-MockCalled - CommandName Install-Module - Times 1 - ParameterFilter { $RequiredVersion -eq " 4.2.0" }
144
+ Assert-MockCalled - CommandName Import-Module - Times 1 - ParameterFilter {$RequiredVersion -eq " 4.2.0" }
145
+ }
146
+
147
+ It " Should not Install the latest version of Pester when on PowerShellGet is available but SkipPublisherCheck is not available" {
148
+ Mock - CommandName Find-Module - MockWith { [PsCustomObject ]@ {Version = [version ]::new(9 , 9 , 9 );Repository = ' PSGallery' }}
149
+ Mock - CommandName Get-PackageProvider - MockWith { $True }
150
+ Mock - CommandName Get-Command - MockWith { [PsCustomObject ]@ {Parameters = @ {OtherProperty = ' SomeValue' }} } - ParameterFilter {$Name -eq ' Install-Module' }
151
+
152
+ Import-Pester - Version " latest"
153
+
154
+ Assert-MockCalled - CommandName Install-Module - Times 0 - Scope It
155
+ Assert-MockCalled - CommandName Import-Module - Times 1 - ParameterFilter {$Name -like ' *\4.6.0\Pester.psd1' }
156
+ }
83
157
158
+ <# It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
159
+ Mock -CommandName Invoke-Pester -MockWith { }
160
+ Mock -CommandName Import-Module -MockWith { }
161
+ Mock -CommandName Write-Host -MockWith { }
162
+ Mock -CommandName Write-Warning -MockWith { }
163
+ Mock -CommandName Write-Error -MockWith { }
164
+ Mock -CommandName Get-Module -MockWith { }
84
165
166
+ &$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
167
+ Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.6.0\Pester.psd1" }
168
+ Assert-MockCalled Invoke-Pester
169
+ }#>
85
170
}
86
171
}
0 commit comments