This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
New-DbaTestGenerator.ps1
95 lines (77 loc) · 3.82 KB
/
New-DbaTestGenerator.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
function New-DbaTestGenerator {
<#
.SYNOPSIS
Creates a new Pester Test for new cmdlets without exisiting tests
.DESCRIPTION
Creates a new Pester Test for new cmdlets without exisiting tests
Uses a sample test as the format, but will also append tests as well if desired
.PARAMETER DevelopmentPath
The Path to the location of your local GitHub branch of dbatools
.NOTES
Tags: Tests, Pester
Author: Joshua Corrick (@joshcorr), corrick.io
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/New-DbaTestGenerator
.EXAMPLE
PS C:\GitHub\> . .\dbatools-templates\New-DbaTestGenerator.ps1
PS C:\GitHub\> New-DbaTestGenerator -DevelopmentPath .\dbatools\
Creates Unit Tests for all named parameters in the files found
#>
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$DevelopmentPath
)
begin {
$functions = Get-ChildItem $DevelopmentPath\functions\ -Recurse -Include *.ps1
$tests = Get-ChildItem $DevelopmentPath\tests\ -Recurse -Include *.ps1
$null = Import-Module $DevelopmentPath\dbatools.psd1
$commonParameters = @('Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Whatif', 'Confirm')
$sampleTest = @'
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'Computer', 'SqlInstance', 'SqlCredential', 'Credential', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
<#
Integration test are custom to the command you are writing for.
Read https://github.com/sqlcollaborative/dbatools/blob/development/contributing.md#tests
for more guidence
#>
'@
$NoTests = (Compare-Object $functions.basename $tests.basename.replace('.Tests', '')).Where( {$_.SideIndicator -eq '<='})
}
process {
foreach ($t in $NoTests) {
try{
$currentcmdlet = Get-command -Name $($t.inputobject) -ErrorAction stop
if (Test-Path $DevelopmentPath\tests\$($t.inputobject).Tests.ps1 ) {
$currentTestFile = Get-Content -Path $DevelopmentPath\tests\$($t.inputobject).Tests.ps1
}
$currentTest = $sampleTest
$currentcmdletparameters = $currentcmdlet.Parameters.Keys.Where( {$_ -notin $commonParameters})
$currentTest = $currentTest.Replace("`$knownParameters = 'Computer', 'SqlInstance', 'SqlCredential', 'Credential', 'EnableException'", "`$knownParameters = '$($currentcmdletparameters -join ''',''')'")
if ($currentFile) {
Out-File -InputObject $currentTestFile -FilePath $DevelopmentPath\tests\$($t.inputobject).Tests.ps1 -Encoding utf8 -Append -NoNewline
}
else {
Out-File -InputObject $currentTest -FilePath $DevelopmentPath\tests\$($t.inputobject).Tests.ps1 -Encoding utf8 -NoNewline
}
}
catch{
Write-Error "Cmdlet $($t.inputobject) is not globally accessable"
}
}
}
}