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