-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
SQLCover.ps1
161 lines (123 loc) · 5.06 KB
/
SQLCover.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
function Get-CoverTSql{
param(
[string]$coverDllPath
,[string]$connectionString
,[string]$databaseName
,[string]$query
)
if(!(Test-Path $coverDllPath)){
Write-Error "SQLCover.dll path was not found ($coverDllPath)"
return
}
Unblock-File -Path $coverDllPath
Add-Type -Path $coverDllPath
$coverage = new-object SQLCover.CodeCoverage ($connectionString, $databaseName)
$coverage.Cover($query)
}
function Get-CoverExe{
param(
[string]$coverDllPath
,[string]$connectionString
,[string]$databaseName
,[string]$exeName
,[string]$args
)
if(!(Test-Path $coverDllPath)){
Write-Error "SQLCover.dll path was not found ($coverDllPath)"
return
}
Unblock-File -Path $coverDllPath
Add-Type -Path $coverDllPath
$coverage = new-object SQLCover.CodeCoverage ($connectionString, $databaseName)
$coverage.CoverExe($exeName, $args)
}
function Get-CoverRedgateCITest{
param(
[string]$coverDllPath
,[string]$connectionString
,[string]$nugetPackage
,[string]$server
,[string]$database
)
if(!(Get-Module -ListAvailable -Name "SQLRelease"))
{
Write-Error "Redgate SQLRelease Module missing."
return
}
Import-Module -Name "SQLRelease"
if(!(Test-Path $coverDllPath)){
Write-Error "SQLCover.dll path was not found ($coverDllPath)"
return
}
Unblock-File -Path $coverDllPath
Add-Type -Path $coverDllPath
$coverage = new-object SQLCover.CodeCoverage($connectionString, $database)
$coverage.Start()
$temporaryDatabase = New-DlmDatabaseConnection -ServerInstance $server -Database $database
$testResults = $nugetPackage | Invoke-DlmDatabaseTests -TemporaryDatabase $temporaryDatabase
$coverageResults = $coverage.Stop()
$testResults, $coverageResults
}
function Export-OpenXml{
param(
[SQLCover.CoverageResult] $result
,[string]$outputPath
)
$xmlPath = Join-Path -Path $outputPath -ChildPath "Coverage.opencoverxml"
$result.OpenCoverXml() | Out-File $xmlPath
$result.SaveSourceFiles($outputPath)
}
function Start-ReportGenerator{
param(
[string]$outputPath
,[string]$reportGeneratorPath
)
$xmlPath = Join-Path -Path $outputPath -ChildPath "Coverage.opencoverxml"
$sourcePath = $outputPath
if(!(Test-Path $sourcePath)){
Write-Error "Cannot find source path to convert into html report. Path = $sourcePath"
}
$outputPath = Join-Path $outputPath -ChildPath "out"
New-Item -Type Directory -Force -Path $outputPath | out-Null
$report = "-reports:$xmlPath"
$targetDir = "-targetDir:$outputPath"
$args = $report, $targetDir
Start-Process -FilePath $reportGeneratorPath -ArgumentList $args -WorkingDirectory $sourcePath -Wait
Write-Verbose "Coverage Report Written to: $outputPath"
}
function Export-Html{
param(
[SQLCover.CoverageResult] $result
,[string]$outputPath
)
$xmlPath = Join-Path -Path $outputPath -ChildPath "Coverage.html"
$result.Html() | Out-File $xmlPath
$result.SaveSourceFiles($outputPath)
}
#EXAMPLE:
<#
#Use the Redgate DLM suite to deploy a nuget package and run the tSQLt tests
$results = Get-CoverRedgateCITest "path\to\SQLCover.dll" "server=.;integrated security=sspi;initial catalog=tSQLt_Example" "tSQLt_Example"
Export-DlmDatabaseTestResults $results[0] -OutputFile c:\temp\junit.xml -force
Export-OpenXml $results[1] "c:\output\path\for\xml\results"
Start-ReportGenerator "c:\output\path\for\xml\results" "c:\path\to\reportgenerator.exe"
#Cover whatever sql query you would like to run, this example runs tSQLt.RunAll
$result = Get-CoverTSql "path\to\SQLCover.dll" "server=.;integrated security=sspi;initial catalog=tSQLt_Example" "tSQLt_Example" "tSQLt.RunAll"
#Output the results as a basic html file
Export-Html $result "path\to\write\Log"
$result = Get-CoverTSql "path\to\SQLCover.dll" "server=.;integrated security=sspi;initial catalog=tSQLt_Example" "tSQLt_Example" "tSQLt.RunAll"
Export-OpenXml $result "output\path\for\xml\results"
Start-ReportGenerator "output\path\for\xml\results" "path\to\reportgenerator.exe"
#>
# Set-ExecutionPolicy RemoteSigned
# mkdir Log
# launch docker sql server
# docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
# deploy DatabaseWithTests project to sql server
# call .\example\SQLCover.ps1 from powershell
#EXAMPLE2: uncomment
<#
$result = Get-CoverTSql "src\SQLCover\SQLCover\bin\Debug\SQLCover.dll" "Server=localhost;Database=master;User ID=sa;Password=yourStrong(!)Password" "DatabaseWithTests" "tSQLt.RunAll"
#Output the results as a basic html file
Export-Html $result "Log"
#>