-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathInstall-DbaSSMS.Tests.ps1
286 lines (257 loc) · 12 KB
/
Install-DbaSSMS.Tests.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Describe "Install-DbaSSMS" {
Mock Invoke-WebRequest {}
Mock Write-Warning {}
Mock Test-Path {$true}
function Start-DbaFileDownload {}
Mock Start-DbaFileDownload {}
Mock Get-Process {}
Mock Stop-Process {}
Mock Start-Process {}
Context "Input" {
It "Should have a parameter of URL" {
(Get-Command Install-DbaSSMS).Parameters['URL'].Count | Should -Be 1 -Because "We may want to change it"
}
It "URL Parameter should not be mandatory" {
(Get-Command Install-DbaSSMS).Parameters['URL'].Attributes.Mandatory | Should -BeFalse -Because "We generally want to use the default value"
}
It "URL Parameter should be of type string" {
(Get-Command Install-DbaSSMS).Parameters['URL'].ParameterType.Name | Should -Be 'String' -Because "What else would an URL be?"
}
It "Should have a parameter of DownloadPath" {
(Get-Command Install-DbaSSMS).Parameters['DownloadPath'].Count | Should -Be 1 -Because "We may need to set a folder we want to download"
}
It "DownloadPath Parameter should not be mandatory" {
(Get-Command Install-DbaSSMS).Parameters['DownloadPath'].Attributes.Mandatory | Should -BeFalse -Because "We generally want to use the default value"
}
It "DownloadPath Parameter should be of type string" {
(Get-Command Install-DbaSSMS).Parameters['DownloadPath'].ParameterType.Name | Should -Be 'String' -Because "What else would an URL be?"
}
It "Should throw if no access to Download Path if it is specified" {
Mock Test-Path {$false}
{ Install-DbaSSMS -DownloadPath 'madeup'} | Should -Throw -Because "We need to be able to access the path"
$assertMockParams = @{
'CommandName' = 'Test-Path'
'Times' = 1
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
It "Should write a warning if no access to default Download Path" {
Mock Test-Path {$false}
Install-DbaSSMS
$assertMockParams = @{
'CommandName' = 'Test-Path'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
It "Should have a parameter of Upgrade" {
(Get-Command Install-DbaSSMS).Parameters['Upgrade'].Count | Should -Be 1 -Because "We want to enable upgrades"
}
It "Upgrade Parameter should not be mandatory" {
(Get-Command Install-DbaSSMS).Parameters['Upgrade'].Attributes.Mandatory | Should -BeFalse -Because "The default should be full install"
}
It "Upgrade Parameter should be of type Switch" {
(Get-Command Install-DbaSSMS).Parameters['Upgrade'].ParameterType.Name | Should -Be 'SwitchParameter' -Because "It only needs to be a switch"
}
It "Should have a parameter of OffLine" {
(Get-Command Install-DbaSSMS).Parameters['OffLine'].Count | Should -Be 1 -Because "We want to enable offline installation"
}
It "OffLine Parameter should not be mandatory" {
(Get-Command Install-DbaSSMS).Parameters['OffLine'].Attributes.Mandatory | Should -BeFalse -Because "The default should be online"
}
It "OffLine Parameter should be of type Switch" {
(Get-Command Install-DbaSSMS).Parameters['OffLine'].ParameterType.Name | Should -Be 'SwitchParameter' -Because "It only needs to be a switch"
}
It "Should have a parameter of FilePath" {
(Get-Command Install-DbaSSMS).Parameters['FilePath'].Count | Should -Be 1 -Because "We want to enable offline installation"
}
It "FilePath Parameter should not be mandatory" {
(Get-Command Install-DbaSSMS).Parameters['FilePath'].Attributes.Mandatory | Should -BeFalse -Because "The default should be online"
}
It "FilePath Parameter should be of type String" {
(Get-Command Install-DbaSSMS).Parameters['FilePath'].ParameterType.Name | Should -Be 'String' -Because "What else would a filepath be?"
}
}
Context "Execution" {
It "Should Not Throw" {
{ Install-dbassms }| Should -Not -Throw
}
Install-DbaSSMS
It "Should get the download link from the web-site" {
$assertMockParams = @{
'CommandName' = 'Invoke-WebRequest'
'Times' = 2
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
It "Should Download the file" {
$assertMockParams = @{
'CommandName' = 'Start-DbaFileDownload'
'Times' = 2
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
It "Should run the install fiile" {
$assertMockParams = @{
'CommandName' = 'Start-Process'
'Times' = 2
'Exactly' = $true
}
Assert-MockCalled @assertMockParams
}
It "Should close SSMS if it is running" {
# Just a thing to pass the if
Mock Get-Process {'a thing'}
Install-DbaSSMS
$assertMockParams = @{
'CommandName' = 'Stop-Process'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
It "Should not contact the web if offline switch is used" {
Install-DbaSSMS -Offline -FilePath 'DummyFile'
$assertMockParams = @{
'CommandName' = 'Start-DbaFileDownload'
'Times' = 0
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
It "Should Write a warning if it errors installing" {
Mock Start-Process {Throw}
Install-DbaSSMS
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
It "Should Stop if it fails to get download link" {
Mock Invoke-WebRequest {Throw}
Install-DbaSSMS
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
Mock Invoke-WebRequest {}
It "Should Write a warning if it errors downloading the file" {
Mock Start-DbaFileDownload {Throw}
Install-DbaSSMS
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 1
'Exactly' = $true
'Scope' = 'It'
}
Assert-MockCalled @assertMockParams
}
}
Context "Output" {
}
## Add Script Analyser Rules
Context "Testing Install-DbaSSMS for Script Analyser" {
$Rules = Get-ScriptAnalyzerRule
$Name = $sut.Split('.')[0]
foreach ($rule in $rules) {
$i = $rules.IndexOf($rule)
It "passes the PSScriptAnalyzer Rule number $i - $rule " {
(Invoke-ScriptAnalyzer -Path "$here\$sut" -IncludeRule $rule.RuleName ).Count | Should Be 0
}
}
}
##
## .NOTES
## ===========================================================================
## Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.119
## Created on: 4/12/2016 1:11 PM
## Created by: June Blender
## Organization: SAPIEN Technologies, Inc
## Filename: *.Help.Tests.ps1
## ===========================================================================
## .DESCRIPTION
## To test help for the commands in a module, place this file in the module folder.
## To test any module from any path, use https://github.com/juneb/PesterTDD/Module.Help.Tests.ps1
##
## ## ALTERED FOR ONE COMMAND - Rob Sewell 10/05/2017
##
$commandName = 'Install-DbaSSMS'
Describe "Test help for $commandName" {
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
$Help = Get-Help $commandName -ErrorAction SilentlyContinue
# If help is not found, synopsis in auto-generated help is the syntax diagram
It "should not be auto-generated" {
$Help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
}
# Should be a description for every function
It "gets description for $commandName" {
$Help.Description | Should Not BeNullOrEmpty
}
# Should be at least one example
It "gets example code from $commandName" {
($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
}
# Should be at least one example description
It "gets example help from $commandName" {
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
}
Context "Test parameter help for $commandName" {
$command = Get-Command $CommandName
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable',
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Confirm', 'Whatif'
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common
$parameterNames = $parameters.Name
$HelpParameterNames = $Help.Parameters.Parameter.Where{$_.Name -notin $common}.Name | Sort-Object -Unique
foreach ($parameter in $parameters) {
$parameterName = $parameter.Name
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
# Should be a description for every parameter
It "gets help for parameter: $parameterName : in $commandName" {
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
}
# Required value in Help should match IsMandatory property of parameter
It "help for $parameterName parameter in $commandName has correct Mandatory value" {
$codeMandatory = $parameter.IsMandatory.toString()
$parameterHelp.Required | Should Be $codeMandatory
}
# Parameter type in Help should match code
It "help for $commandName has correct parameter type for $parameterName" {
$codeType = $parameter.ParameterType.Name
# To avoid calling Trim method on a null object.
$helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
$helpType | Should be $codeType
}
}
foreach ($helpParm in $HelpParameterNames) {
# Shouldn't find extra parameters in help.
It "finds help parameter in code: $helpParm" {
$helpParm -in $parameterNames | Should Be $true
}
}
}
}
}