-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTestLayout.ps1
More file actions
66 lines (55 loc) · 3.1 KB
/
TestTestLayout.ps1
File metadata and controls
66 lines (55 loc) · 3.1 KB
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
Import-Module -Name dbatools
$tests = Get-ChildItem .\tests -Filter *-Dba*.Tests.ps1
try {
foreach ($test in $tests) {
# $test = $tests[0]
$content = Get-Content -Path $test.FullName
# We test if every "$PSDefaultParameterValues["*-Dba*:EnableException"] = $true" has a "$PSDefaultParameterValues.Remove("*-Dba*:EnableException")"
$eeTrue = ($content -match [regex]::Escape('$PSDefaultParameterValues["*-Dba*:EnableException"] = $true')).Count
$eeFalse = ($content -match [regex]::Escape('$PSDefaultParameterValues.Remove("*-Dba*:EnableException")')).Count
$eeTrue | Should -Be $eeFalse
$content[0] | Should -BeExactly '#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }'
$content[1] | Should -BeExactly 'param('
$content[2] | Should -BeExactly ' $ModuleName = "dbatools",'
$content[3] | Should -BeExactly (' $CommandName = "{0}",' -f $test.Name.Replace('.Tests.ps1',''))
$content[4] | Should -BeExactly ' $PSDefaultParameterValues = $TestConfig.Defaults'
$content[5] | Should -BeExactly ')'
$content[6] | Should -BeExactly ''
if ($test.Name -in @(
# No parameters to test:
'Get-DbaConnectedInstance.Tests.ps1'
'Measure-DbatoolsImport.Tests.ps1'
'New-DbaScriptingOption.Tests.ps1'
# Needs to be rewritten:
'Update-DbaInstance.Tests.ps1'
)) {
continue
}
$content[7] | Should -BeIn 'Describe $CommandName -Tag UnitTests {', 'Describe $CommandName -Tag UnitTests -Skip:($PSVersionTable.PSVersion.Major -gt 5) {'
$content[8] | Should -BeExactly ' Context "Parameter validation" {'
$content[9] | Should -BeExactly ' It "Should have the expected parameters" {'
$content[10] | Should -BeExactly ' $hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }'
# Some commands don't use [CmdletBinding()]
if ($test.Name -in 'New-DbaReplCreationScriptOptions.Tests.ps1') {
$content[11] | Should -BeExactly ' $expectedParameters = @( ) # Command does not use [CmdletBinding()]'
} else {
$content[11] | Should -BeExactly ' $expectedParameters = $TestConfig.CommonParameters'
}
$content[12] | Should -BeExactly ' $expectedParameters += @('
$params = 0
while (1) {
if ($content[13+$params] -match '^ ".+",?$') {
$params++
} else {
break
}
}
$content[13+$params] | Should -BeExactly ' )'
$content[14+$params] | Should -BeExactly ' Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty'
$content[15+$params] | Should -BeExactly ' }'
$content[16+$params] | Should -BeExactly ' }'
}
} catch {
Write-Warning -Message "Failed test: $test`n$_"
code $test.FullName
}