From 04dac24e224966bd85364b9e15912aaa70b2cf82 Mon Sep 17 00:00:00 2001 From: Gloria Gallego Date: Mon, 7 Jan 2019 10:17:11 -0600 Subject: [PATCH 1/2] To be able to use the task in a SQL Server environment without Azure, an additional parameter was added to control which type of execution you want to perform. Added logic to check SQL module availability (SqlServer or SQLPS, depending on the version of PowerShell) to speed up execution where this is already available. --- buildAndReleaseTask/EnsureSQL.ps1 | 45 +++++++++++++++++++ buildAndReleaseTask/Install-Dependencies.ps1 | 37 +++++++++++++-- .../Invoke-tSQLtTests-WithCodeCoverage.ps1 | 12 ++++- buildAndReleaseTask/Run-tSQLtTests.ps1 | 3 +- buildAndReleaseTask/task.json | 9 ++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 buildAndReleaseTask/EnsureSQL.ps1 diff --git a/buildAndReleaseTask/EnsureSQL.ps1 b/buildAndReleaseTask/EnsureSQL.ps1 new file mode 100644 index 0000000..3e5889e --- /dev/null +++ b/buildAndReleaseTask/EnsureSQL.ps1 @@ -0,0 +1,45 @@ +function Ensure-SQLTools{ + param( + [int]$majorPSVersion + ) + +if($majorPSVersion -gt 4) +{ + $confirmSqlServer = Get-Module SqlServer -ListAvailable + if(!$confirmSqlServer) + { + Try + { + Install-Module -Name SqlServer -AllowClobber + Write-Host "Loaded SqlServer module" + } + Catch + { + Write-Host "SqlServer not available via PowerShell Gallery, check connection settings" + } + } + else + { + Write-Host "SqlServer module is loaded" + } +} +else +{ + $confirmSQLPS = Get-Module SQLPS -ListAvailable + if(!$confirmSQLPS) + { + Try + { + Import-Module "SQLPS" -DisableNameChecking + Write-Host "Loaded SQLPS module" + } + Catch + { + Write-Host "SQLPS not available to import; installation needed" + } + } + else + { + Write-Host "SQLPS module is loaded" + } +} \ No newline at end of file diff --git a/buildAndReleaseTask/Install-Dependencies.ps1 b/buildAndReleaseTask/Install-Dependencies.ps1 index b5a7ac6..80bd5b1 100644 --- a/buildAndReleaseTask/Install-Dependencies.ps1 +++ b/buildAndReleaseTask/Install-Dependencies.ps1 @@ -2,9 +2,9 @@ param( # [string]$installDirectory = $PSScriptRoot ) -if(!(Get-Module SqlServer)){ - Install-Module -Name SqlServer -Scope CurrentUser -Force -AllowClobber -} +#if(!(Get-Module SqlServer)){ +# Install-Module -Name SqlServer -Scope CurrentUser -Force -AllowClobber +#} # $packagesDirectory = Join-Path -Path $installDirectory -ChildPath "packages" @@ -22,4 +22,33 @@ if(!(Get-Module SqlServer)){ # Write-Output "Installing OpenCoverToCoberturaConverter..." # Install-Package OpenCoverToCoberturaConverter -RequiredVersion 0.3.4 -Destination $packagesDirectory -# Write-Output "Finished installing dependencies." \ No newline at end of file +# Write-Output "Finished installing dependencies." + + +$majorPSVersion = $PSVersionTable.PSVersion.Major + +if($majorPSVersion -gt 4) +{ + $confirmSqlServer = Get-Module SqlServer -ListAvailable; + if(!$confirmSqlServer) + { + Ensure-SQLTools $majorPSVersion + } + else + { + #Write-Verbose "SqlServer module already loaded" + Write-Host "SqlServer module already loaded" + } +} +else +{ + $confirmSQLPS = Get-Module SQLPS -ListAvailable; + if(!$confirmSQLPS) + { + Ensure-SQLTools $majorPSVersion + } + else + { + Write-Verbose "SQLPS module already loaded" + } +} \ No newline at end of file diff --git a/buildAndReleaseTask/Invoke-tSQLtTests-WithCodeCoverage.ps1 b/buildAndReleaseTask/Invoke-tSQLtTests-WithCodeCoverage.ps1 index d5ee0e2..78f57d8 100644 --- a/buildAndReleaseTask/Invoke-tSQLtTests-WithCodeCoverage.ps1 +++ b/buildAndReleaseTask/Invoke-tSQLtTests-WithCodeCoverage.ps1 @@ -11,6 +11,7 @@ param ( [string]$coberturaFileName, [string]$htmlReportsOutput, [string]$queryTimeout + [string]$enableAzure ) $sqlCoverPath = "$PSScriptRoot\dependencies\sqlcover\SQLCover.dll" @@ -25,7 +26,16 @@ $connectionStringBuilder.set_ConnectionString($connectionString) $database = $connectionStringBuilder["initial catalog"] Write-Output "Database set to $database" -$coverage = new-object SQLCover.CodeCoverage($connectionString, $database, $null, $true, $false, [SQLCover.Trace.TraceControllerType]::Azure) +#to be able to work with Azure and SQL -- Glomaga +Write-Output "EnableAzure set to $enableAzure" + +if ($enableAzure -eq "true") { +Write-Output "SQLCover with Azure SQL" + $coverage = new-object SQLCover.CodeCoverage($connectionString, $database, $null, $true, $false, [SQLCover.Trace.TraceControllerType]::Azure) +}Else{ +Write-Output "SQLCover with SQL" + $coverage = new-object SQLCover.CodeCoverage($connectionString, $database, $null, $true, $false, [SQLCover.Trace.TraceControllerType]::sql) +} $startResult = $coverage.Start() diff --git a/buildAndReleaseTask/Run-tSQLtTests.ps1 b/buildAndReleaseTask/Run-tSQLtTests.ps1 index 5b0b148..de2af6b 100644 --- a/buildAndReleaseTask/Run-tSQLtTests.ps1 +++ b/buildAndReleaseTask/Run-tSQLtTests.ps1 @@ -11,6 +11,7 @@ param ( # Code Coverage parameters [string]$enableCodeCoverage = "false", + [string]$enableAzure = "false", [string]$openCoverSourceFolder = "OpenCoverSourceFiles", [string]$coberturaFileName = "Cobertura.xml", [string]$htmlReportsOutput = "AzurePipelines", @@ -69,5 +70,5 @@ else { $htmlReportsOutput = Join-Path -Path $rootOutput -ChildPath $htmlReportsOutput Write-Output "htmlReportsOutput set to $htmlReportsOutput" - . .\Invoke-tSQLtTests-WithCodeCoverage.ps1 -connectionString $connectionString -rootOutput $rootOutput -testResultsFileName $testResultsFileName -openCoverSourceFolder $openCoverSourceFolder -openCoverXmlFile $openCoverXmlFile -coberturaFileName $coberturaFileName -htmlReportsOutput $htmlReportsOutput -queryTimeout $queryTimeout + . .\Invoke-tSQLtTests-WithCodeCoverage.ps1 -connectionString $connectionString -rootOutput $rootOutput -testResultsFileName $testResultsFileName -openCoverSourceFolder $openCoverSourceFolder -openCoverXmlFile $openCoverXmlFile -coberturaFileName $coberturaFileName -htmlReportsOutput $htmlReportsOutput -queryTimeout $queryTimeout -enableAzure $enableAzure } \ No newline at end of file diff --git a/buildAndReleaseTask/task.json b/buildAndReleaseTask/task.json index e33e1aa..17c6df1 100644 --- a/buildAndReleaseTask/task.json +++ b/buildAndReleaseTask/task.json @@ -76,6 +76,15 @@ "required": false, "helpMarkDown": "If you want Cobertura reports to be generated for the test execution, enable this.", "groupName": "codeCoverage" + }, + { + "name": "enableAzure", + "type": "boolean", + "label": "Enable Azure SQL", + "defaultValue": "false", + "required": false, + "helpMarkDown": "If you want to get the result from a Azure SQL Server, enable this.", + "groupName": "codeCoverage" }, { "name": "coberturaFileName", From 686a76df47f46c50ce6056020af9db94119a9b13 Mon Sep 17 00:00:00 2001 From: Gloria Gallego Date: Mon, 7 Jan 2019 13:41:01 -0600 Subject: [PATCH 2/2] added load function Ensure-SQLTools and fix function. --- buildAndReleaseTask/EnsureSQL.ps1 | 1 + buildAndReleaseTask/Run-tSQLtTests.ps1 | 2 ++ 2 files changed, 3 insertions(+) diff --git a/buildAndReleaseTask/EnsureSQL.ps1 b/buildAndReleaseTask/EnsureSQL.ps1 index 3e5889e..04ad912 100644 --- a/buildAndReleaseTask/EnsureSQL.ps1 +++ b/buildAndReleaseTask/EnsureSQL.ps1 @@ -42,4 +42,5 @@ else { Write-Host "SQLPS module is loaded" } +} } \ No newline at end of file diff --git a/buildAndReleaseTask/Run-tSQLtTests.ps1 b/buildAndReleaseTask/Run-tSQLtTests.ps1 index de2af6b..f2453fb 100644 --- a/buildAndReleaseTask/Run-tSQLtTests.ps1 +++ b/buildAndReleaseTask/Run-tSQLtTests.ps1 @@ -27,6 +27,8 @@ if(!$workingDirectory) { # Dependencies are then embeded to the extension VSIX # . .\InstallDependencies.ps1 -installDirectory $workingDirectory +. .\EnsureSQL.ps1 + . .\Install-Dependencies.ps1 $ErrorActionPreference = "Continue"