1
- $CommandName = $MyInvocation.MyCommand.Name.Replace (" .Tests.ps1" , " " )
1
+ # Requires -Module @ { ModuleName = " Pester" ; ModuleVersion = " 5.0" }
2
+ param (
3
+ $ModuleName = " dbatools" ,
4
+ $CommandName = " Get-DbaBuild" ,
5
+ $PSDefaultParameterValues = $TestConfig.Defaults
6
+ )
7
+
2
8
Write-Host - Object " Running $PSCommandPath " - ForegroundColor Cyan
3
9
$global :TestConfig = Get-TestConfig
4
10
5
- Describe " $CommandName Unit Tests" - Tag ' UnitTests' {
6
- Context " Validate parameters" {
7
- [object []]$params = (Get-Command $CommandName ).Parameters.Keys | Where-Object { $_ -notin (' whatif' , ' confirm' ) }
8
- [object []]$knownParameters = ' Build' , ' Kb' , ' MajorVersion' , ' ServicePack' , ' CumulativeUpdate' , ' SqlInstance' , ' SqlCredential' , ' Update' , ' EnableException'
9
- $knownParameters += [System.Management.Automation.PSCmdlet ]::CommonParameters
10
- It " Should only contain our specific parameters" {
11
- (@ (Compare-Object - ReferenceObject ($knownParameters | Where-Object { $_ }) - DifferenceObject $params ).Count ) | Should Be 0
11
+ Describe $CommandName - Tag UnitTests {
12
+ Context " Parameter validation" {
13
+ BeforeAll {
14
+ $hasParameters = (Get-Command $CommandName ).Parameters.Values.Name | Where-Object { $PSItem -notin (" WhatIf" , " Confirm" ) }
15
+ $expectedParameters = $TestConfig.CommonParameters
16
+ $expectedParameters += @ (
17
+ " Build" ,
18
+ " Kb" ,
19
+ " MajorVersion" ,
20
+ " ServicePack" ,
21
+ " CumulativeUpdate" ,
22
+ " SqlInstance" ,
23
+ " SqlCredential" ,
24
+ " Update" ,
25
+ " EnableException"
26
+ )
27
+ }
28
+
29
+ It " Should have the expected parameters" {
30
+ Compare-Object - ReferenceObject $expectedParameters - DifferenceObject $hasParameters | Should - BeNullOrEmpty
12
31
}
13
32
}
14
33
}
15
34
16
- Describe " $CommandName Unit Test " - Tags Unittest {
35
+ Describe $CommandName - Tag UnitTests {
17
36
BeforeAll {
18
37
$ModuleBase = (Get-Module - Name dbatools | Where-Object ModuleBase -NotMatch net).ModuleBase
19
38
$idxfile = " $ModuleBase \bin\dbatools-buildref-index.json"
20
39
}
21
40
22
- Context ' Validate data in json is correct' {
41
+ Context " Validate data in json is correct" {
23
42
It " the json file is there" {
24
43
$result = Test-Path $idxfile
25
44
$result | Should - Be $true
@@ -29,7 +48,7 @@ Describe "$CommandName Unit Test" -Tags Unittest {
29
48
$IdxRef | Should - BeOfType System.Object
30
49
}
31
50
}
32
- Context ' Validate LastUpdated property' {
51
+ Context " Validate LastUpdated property" {
33
52
BeforeAll {
34
53
$IdxRef = Get-Content $idxfile - Raw | ConvertFrom-Json
35
54
}
@@ -46,13 +65,13 @@ Describe "$CommandName Unit Test" -Tags Unittest {
46
65
$lastupdate | Should - BeLessThan (Get-Date )
47
66
}
48
67
}
49
- Context ' Validate Data property' {
68
+ Context " Validate Data property" {
50
69
BeforeAll {
51
70
$IdxRef = Get-Content $idxfile - Raw | ConvertFrom-Json
52
71
$Groups = @ { }
53
72
$OrderedKeys = @ ()
54
73
foreach ($el in $IdxRef.Data ) {
55
- $ver = $el.Version.Split (' . ' )[0 .. 1 ] -join ' . '
74
+ $ver = $el.Version.Split (" . " )[0 .. 1 ] -join " . "
56
75
if (! ($Groups.ContainsKey ($ver ))) {
57
76
$Groups [$ver ] = New-Object System.Collections.ArrayList
58
77
$OrderedKeys += $ver
@@ -65,14 +84,14 @@ Describe "$CommandName Unit Test" -Tags Unittest {
65
84
}
66
85
It " Each Datum has a Version property" {
67
86
$DataLength = $IdxRef.Data.Length
68
- $DataWithVersion = ($IdxRef.Data.Version | Where-Object { $_ }).Length
87
+ $DataWithVersion = ($IdxRef.Data.Version | Where-Object { $PSItem }).Length
69
88
$DataLength | Should - Be $DataWithVersion
70
89
}
71
90
It " Each version is correctly parsable" {
72
- $Versions = $IdxRef.Data.Version | Where-Object { $_ }
91
+ $Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
73
92
foreach ($ver in $Versions ) {
74
- $splitted = $ver.split (' . ' )
75
- $dots = $ver.split (' . ' ).Length - 1
93
+ $splitted = $ver.split (" . " )
94
+ $dots = $ver.split (" . " ).Length - 1
76
95
if ($dots -ne 2 ) {
77
96
if ($dots [0 ] -le 15 ) {
78
97
$dots | Should - Be 3
@@ -81,82 +100,82 @@ Describe "$CommandName Unit Test" -Tags Unittest {
81
100
}
82
101
}
83
102
try {
84
- $splitted | ForEach-Object { [convert ]::ToInt32($_ ) }
103
+ $splitted | ForEach-Object { [convert ]::ToInt32($PSItem ) }
85
104
} catch {
86
105
# I know. But someone can find a method to output a custom message ?
87
- $splitted -join ' . ' | Should - Be " Composed by integers"
106
+ $splitted -join " . " | Should - Be " Composed by integers"
88
107
}
89
108
}
90
109
}
91
110
It " Versions are ordered, the way versions are ordered" {
92
- $Versions = $IdxRef.Data.Version | Where-Object { $_ }
111
+ $Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
93
112
$Naturalized = $Versions | ForEach-Object {
94
- $splitted = $_ .split (' . ' ) | ForEach-Object { [convert ]::ToInt32($_ ) }
95
- " $ ( $splitted [0 ].toString(' 00 ' )) $ ( $splitted [1 ].toString(' 00 ' )) $ ( $splitted [2 ].toString(' 0000' )) "
113
+ $splitted = $PSItem .split (" . " ) | ForEach-Object { [convert ]::ToInt32($PSItem ) }
114
+ " $ ( $splitted [0 ].toString(" 00 " )) $ ( $splitted [1 ].toString(" 00 " )) $ ( $splitted [2 ].toString(" 0000" )) "
96
115
}
97
116
$SortedVersions = $Naturalized | Sort-Object
98
117
($SortedVersions -join " ," ) | Should - Be ($Naturalized -join " ," )
99
118
}
100
119
It " Names are at least 8" {
101
- $Names = $IdxRef.Data.Name | Where-Object { $_ }
120
+ $Names = $IdxRef.Data.Name | Where-Object { $PSItem }
102
121
$Names.Length | Should - BeGreaterThan 7
103
122
}
104
123
}
105
124
Context " Params mutual exclusion" {
106
125
It " Doesn't accept 'Build', 'Kb', 'SqlInstance" {
107
- { Get-DbaBuild - Build ' 10.0.1600' - Kb ' 4052908' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
126
+ { Get-DbaBuild - Build " 10.0.1600" - Kb " 4052908" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
108
127
}
109
128
It " Doesn't accept 'Build', 'Kb'" {
110
- { Get-DbaBuild - Build ' 10.0.1600' - Kb ' 4052908' - EnableException - ErrorAction Stop } | Should - Throw
129
+ { Get-DbaBuild - Build " 10.0.1600" - Kb " 4052908" - EnableException - ErrorAction Stop } | Should - Throw
111
130
}
112
131
It " Doesn't accept 'Build', 'SqlInstance'" {
113
- { Get-DbaBuild - Build ' 10.0.1600' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
132
+ { Get-DbaBuild - Build " 10.0.1600" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
114
133
}
115
134
It " Doesn't accept 'Build', 'SqlInstance'" {
116
- { Get-DbaBuild - Build ' 10.0.1600' - SqlInstance ' localhost' - EnableException - ErrorAction Stop } | Should - Throw
135
+ { Get-DbaBuild - Build " 10.0.1600" - SqlInstance " localhost" - EnableException - ErrorAction Stop } | Should - Throw
117
136
}
118
137
It " Doesn't accept 'Build', 'MajorVersion'" {
119
- { Get-DbaBuild - Build ' 10.0.1600' - MajorVersion ' 2016' - EnableException - ErrorAction Stop } | Should - Throw
138
+ { Get-DbaBuild - Build " 10.0.1600" - MajorVersion " 2016" - EnableException - ErrorAction Stop } | Should - Throw
120
139
}
121
140
It " Doesn't accept 'Build', 'ServicePack'" {
122
- { Get-DbaBuild - Build ' 10.0.1600' - ServicePack ' SP2' - EnableException - ErrorAction Stop } | Should - Throw
141
+ { Get-DbaBuild - Build " 10.0.1600" - ServicePack " SP2" - EnableException - ErrorAction Stop } | Should - Throw
123
142
}
124
143
It " Doesn't accept 'Build', 'CumulativeUpdate'" {
125
- { Get-DbaBuild - Build ' 10.0.1600' - CumulativeUpdate ' CU2' - EnableException - ErrorAction Stop } | Should - Throw
144
+ { Get-DbaBuild - Build " 10.0.1600" - CumulativeUpdate " CU2" - EnableException - ErrorAction Stop } | Should - Throw
126
145
}
127
146
It " Doesn't accept 'ServicePack' without 'MajorVersion'" {
128
- { Get-DbaBuild - ServicePack ' SP2' - EnableException - ErrorAction Stop } | Should - Throw
147
+ { Get-DbaBuild - ServicePack " SP2" - EnableException - ErrorAction Stop } | Should - Throw
129
148
}
130
149
It " Doesn't accept 'CumulativeUpdate' without 'MajorVersion'" {
131
- { Get-DbaBuild - CumulativeUpdate ' CU2' - EnableException - ErrorAction Stop } | Should - Throw
150
+ { Get-DbaBuild - CumulativeUpdate " CU2" - EnableException - ErrorAction Stop } | Should - Throw
132
151
}
133
152
}
134
153
Context " Passing just -Update works, see #6823" {
135
- It ' works with -Update' {
154
+ It " works with -Update" {
136
155
function Get-DbaBuildReferenceIndexOnline { }
137
156
Mock Get-DbaBuildReferenceIndexOnline - MockWith { } - ModuleName dbatools
138
157
Get-DbaBuild - Update - WarningVariable warnings 3> $null
139
158
$warnings | Should - BeNullOrEmpty
140
159
}
141
160
}
142
161
Context " Retired KBs" {
143
- It ' Handles retired KBs' {
144
- $result = Get-DbaBuild - Build ' 13.0.5479'
145
- $result.Warning | Should - Be ' This version has been officially retired by Microsoft'
162
+ It " Handles retired KBs" {
163
+ $result = Get-DbaBuild - Build " 13.0.5479"
164
+ $result.Warning | Should - Be " This version has been officially retired by Microsoft"
146
165
}
147
166
}
148
167
149
168
Context " Recognizes version 'aliases', see #8915" {
150
- It ' works with versions with the minor being either not 0 or 50' {
151
- $result2016 = Get-DbaBuild - Build ' 13.3.6300'
152
- $result2016.Build | Should - Be ' 13.3.6300'
153
- $result2016.BuildLevel | Should - Be ' 13.0.6300'
154
- $result2016.MatchType | Should - Be ' Exact'
169
+ It " works with versions with the minor being either not 0 or 50" {
170
+ $result2016 = Get-DbaBuild - Build " 13.3.6300"
171
+ $result2016.Build | Should - Be " 13.3.6300"
172
+ $result2016.BuildLevel | Should - Be " 13.0.6300"
173
+ $result2016.MatchType | Should - Be " Exact"
155
174
156
- $result2008R2 = Get-DbaBuild - Build ' 10.53.6220'
157
- $result2008R2.Build | Should - Be ' 10.53.6220'
158
- $result2008R2.BuildLevel | Should - Be ' 10.50.6220'
159
- $result2008R2.MatchType | Should - Be ' Exact'
175
+ $result2008R2 = Get-DbaBuild - Build " 10.53.6220"
176
+ $result2008R2.Build | Should - Be " 10.53.6220"
177
+ $result2008R2.BuildLevel | Should - Be " 10.50.6220"
178
+ $result2008R2.MatchType | Should - Be " Exact"
160
179
}
161
180
}
162
181
# These are groups by major release (aka "Name")
@@ -167,31 +186,31 @@ Describe "$CommandName Unit Test" -Tags Unittest {
167
186
$Versions [0 ].Name | Should - BeLike " 20*"
168
187
}
169
188
It " No multiple Names around" {
170
- ($Versions.Name | Where-Object { $_ }).Count | Should - Be 1
189
+ ($Versions.Name | Where-Object { $PSItem }).Count | Should - Be 1
171
190
}
172
191
# Skip for now bc a prerelease has been added
173
192
It - Skip " has one version tagged as RTM" {
174
- ($Versions.SP -eq ' RTM' ).Count | Should - Be 1
193
+ ($Versions.SP -eq " RTM" ).Count | Should - Be 1
175
194
}
176
195
It " SP Property is formatted correctly" {
177
- $Versions.SP | Where-Object { $_ } | Should -Match ' ^RTM$|^SP[\d]+$|^RC'
196
+ $Versions.SP | Where-Object { $PSItem } | Should -Match " ^RTM$|^SP[\d]+$|^RC"
178
197
}
179
198
It " CU Property is formatted correctly" {
180
- $CUMatch = $Versions.CU | Where-Object { $_ }
199
+ $CUMatch = $Versions.CU | Where-Object { $PSItem }
181
200
if ($CUMatch ) {
182
- $CUMatch | Should -Match ' ^CU[\d]+$'
201
+ $CUMatch | Should -Match " ^CU[\d]+$"
183
202
}
184
203
}
185
204
It " SPs are ordered correctly" {
186
- $SPs = $Versions.SP | Where-Object { $_ }
187
- ($SPs | Select-Object - First 1 ) | Should - BeIn ' RTM' , ' RC '
188
- $ActualSPs = $SPs | Where-Object { $_ -match ' ^SP[\d]+$' }
205
+ $SPs = $Versions.SP | Where-Object { $PSItem }
206
+ ($SPs | Select-Object - First 1 ) | Should - BeIn " RTM" , " RC "
207
+ $ActualSPs = $SPs | Where-Object { $PSItem -match " ^SP[\d]+$" }
189
208
$OrderedActualSPs = $ActualSPs | Sort-Object
190
- ($ActualSPs -join ' , ' ) | Should - Be ($OrderedActualSPs -join ' , ' )
209
+ ($ActualSPs -join " , " ) | Should - Be ($OrderedActualSPs -join " , " )
191
210
}
192
211
# see https://github.com/dataplat/dbatools/pull/2466
193
212
It " KBList has only numbers on it" {
194
- $NotNumbers = $Versions.KBList | Where-Object { $_ } | Where-Object { $_ -notmatch ' ^[\d]+$' }
213
+ $NotNumbers = $Versions.KBList | Where-Object { $PSItem } | Where-Object { $PSItem -notmatch " ^[\d]+$" }
195
214
if ($NotNumbers.Count -ne 0 ) {
196
215
foreach ($Nn in $NotNumbers ) {
197
216
$Nn | Should - Be " Composed by integers"
@@ -202,7 +221,7 @@ Describe "$CommandName Unit Test" -Tags Unittest {
202
221
}
203
222
}
204
223
205
- Describe " $commandname Integration Tests " - Tags ' IntegrationTests' {
224
+ Describe $CommandName - Tag IntegrationTests {
206
225
Context " piping and params" {
207
226
BeforeAll {
208
227
$server1 = Connect-DbaInstance - SqlInstance $TestConfig.instance1
@@ -217,7 +236,9 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
217
236
}
218
237
}
219
238
Context " Test retrieving version from instances" {
220
- $results = Get-DbaBuild - SqlInstance $TestConfig.instance1 , $TestConfig.instance2
239
+ BeforeAll {
240
+ $results = Get-DbaBuild - SqlInstance $TestConfig.instance1 , $TestConfig.instance2
241
+ }
221
242
It " Should return an exact match" {
222
243
$results | Should -Not - BeNullOrEmpty
223
244
foreach ($r in $results ) {
@@ -237,7 +258,7 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
237
258
$m.KBLevel | Should - BeIn $r.KBLevel
238
259
}
239
260
}
240
- $spLevel = $r.SPLevel | Where-Object { $_ -ne ' LATEST' }
261
+ $spLevel = $r.SPLevel | Where-Object { $PSItem -ne " LATEST" }
241
262
$versionMatch = Get-DbaBuild - MajorVersion $r.NameLevel - ServicePack $spLevel - CumulativeUpdate $r.CULevel
242
263
$versionMatch | Should -Not - BeNullOrEmpty
243
264
foreach ($v in $versionMatch ) {
@@ -249,4 +270,4 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
249
270
}
250
271
}
251
272
}
252
- }
273
+ }
0 commit comments