From 2507b53f7cebecc5565c13b5ae26d7fa4cbf5149 Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Mon, 17 Aug 2020 11:57:55 +0200 Subject: [PATCH 1/8] add timeout connexion - add check on open connect --- .vscode/settings.json | 4 ---- Docs/en-US/Invoke-SnowSql.md | 19 +++++++++++++-- Docs/en-US/Open-SnowSqlConnection.md | 22 ++++++++++++++++- SnowSQL/public/Invoke-SnowSql.ps1 | 27 +++++++++++++++++---- SnowSQL/public/Open-SnowSqlConnection.ps1 | 29 ++++++++++++++++++----- 5 files changed, 83 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index db8c43f..53aa560 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,6 @@ //-------- Editor configuration -------- "editor.insertSpaces": true, "editor.tabSize": 4, - //-------- Files configuration -------- "files.autoGuessEncoding": false, "files.insertFinalNewline": true, @@ -11,17 +10,14 @@ "search.exclude": { "**/Tests/Data*": true }, - //-------- PowerShell configuration -------- "powershell.codeFormatting.alignPropertyValuePairs": true, "powershell.codeFormatting.preset": "Allman", "powershell.scriptAnalysis.settingsPath": "./ScriptAnalyzerSettings.psd1", - //-------- Language configuration -------- "[json]": { "editor.tabSize": 2 }, - "[xml]": { "editor.tabSize": 2 }, diff --git a/Docs/en-US/Invoke-SnowSql.md b/Docs/en-US/Invoke-SnowSql.md index d73a676..9b92cfd 100644 --- a/Docs/en-US/Invoke-SnowSql.md +++ b/Docs/en-US/Invoke-SnowSql.md @@ -14,7 +14,7 @@ Invokes a Snowflake SQL statement ### QueryConnection (Default) ``` -Invoke-SnowSql [-Connection ] [-Query ] [-WhatIf] [-Confirm] [] +Invoke-SnowSql [-Connection ] [-Query ] [-Timeout ] [-WhatIf] [-Confirm] [] ``` ### PathCred @@ -25,7 +25,7 @@ Invoke-SnowSql -Endpoint -Credential [-Path ] [- ### QueryCred ``` -Invoke-SnowSql -Endpoint -Credential [-Query ] [-WhatIf] [-Confirm] +Invoke-SnowSql -Endpoint -Credential [-Query ] [-Timeout ] [-WhatIf] [-Confirm] [] ``` @@ -123,6 +123,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Timeout +Snowflake timeout for connexion + +```yaml +Type: Integer +Parameter Sets: (All) +Aliases: + +Required: False +Position: 2 +Default value: 10 +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Confirm Prompts you for confirmation before running the cmdlet. diff --git a/Docs/en-US/Open-SnowSqlConnection.md b/Docs/en-US/Open-SnowSqlConnection.md index 4b58612..18babfa 100644 --- a/Docs/en-US/Open-SnowSqlConnection.md +++ b/Docs/en-US/Open-SnowSqlConnection.md @@ -13,7 +13,7 @@ Opens a connection to Snowflake ## SYNTAX ``` -Open-SnowSqlConnection [-Endpoint] [-Credential] [-WhatIf] [-Confirm] +Open-SnowSqlConnection [-Endpoint] [-Credential] [-Timeout] [-WhatIf] [-Confirm] [] ``` @@ -27,6 +27,11 @@ Establishes a few important environment values for connecting to snowflake Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential) ``` +### EXAMPLE 2 +``` +Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential) -Timeout 20 +``` + ## PARAMETERS ### -Credential @@ -59,6 +64,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Timeout +Snowflake timeout for connexion + +```yaml +Type: Integer +Parameter Sets: (All) +Aliases: + +Required: False +Position: 2 +Default value: 10 +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Confirm Prompts you for confirmation before running the cmdlet. diff --git a/SnowSQL/public/Invoke-SnowSql.ps1 b/SnowSQL/public/Invoke-SnowSql.ps1 index 48a7cb4..8bfa60d 100644 --- a/SnowSQL/public/Invoke-SnowSql.ps1 +++ b/SnowSQL/public/Invoke-SnowSql.ps1 @@ -44,6 +44,12 @@ function Invoke-SnowSql [string[]] $Query = '!help', + # Timeout query + [Parameter(ParameterSetName = 'QueryCred')] + [Parameter(ParameterSetName = 'QueryConnection')] + [int] + $timeout = 10, + # SnowSql script file to execute [Parameter(ParameterSetName = 'PathCred')] [Parameter(ParameterSetName = 'PathConnection')] @@ -56,16 +62,16 @@ function Invoke-SnowSql try { $snowSql = Get-Command snowsql -ErrorAction Stop | - Select-Object -First 1 -ExpandProperty Source + Select-Object -First 1 -ExpandProperty Source } catch { Write-Error "Could not find [snowsql.exe] on local system. Install snowsql.exe and make it available in the %path%. Install instructions can be found here [https://docs.snowflake.net/manuals/user-guide/snowsql-install-config.html]" -ErrorAction Stop } - if($PSCmdlet.ParameterSetName -match 'Connection$') + if ($PSCmdlet.ParameterSetName -match 'Connection$') { - if( -not $Connection ) + if ( -not $Connection ) { $Connection = Get-SnowSqlConnection } @@ -100,6 +106,10 @@ function Invoke-SnowSql { '--option', 'log_level=DEBUG' } + if ($timeout) + { + '--option', $('login_timeout=' + $timeout) + } if ($Path) { '--filename', $Path @@ -113,8 +123,15 @@ function Invoke-SnowSql Write-Debug ("Executing [& '$snowsql' $snowSqlParam]" -f $snowsql) if ($PSCmdlet.ShouldProcess("Execute SnowSql on [$Endpoint] as [$($Credential.UserName)]. Use -Debug to see full command")) { - $env:SNOWSQL_PWD = $Credential.GetNetworkCredential().password - $results = & $snowsql @snowSqlParam | ConvertFrom-Csv + $env:SNOWSQL_PWD = $Credential.GetNetworkCredential().Password + try + { + $results = & $snowsql @snowSqlParam | ConvertFrom-Csv + } + catch + { + $results = $null + } $env:SNOWSQL_PWD = "" Write-Verbose "LastExitCode[$LastExitCode]" } diff --git a/SnowSQL/public/Open-SnowSqlConnection.ps1 b/SnowSQL/public/Open-SnowSqlConnection.ps1 index ed9600d..8eeb64f 100644 --- a/SnowSQL/public/Open-SnowSqlConnection.ps1 +++ b/SnowSQL/public/Open-SnowSqlConnection.ps1 @@ -15,7 +15,7 @@ function Open-SnowSqlConnection #> [OutputType('SnowSql.Connection')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Implemented in Invoke-SnowSql")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Implemented in Invoke-SnowSql")] [cmdletbinding(SupportsShouldProcess)] param( # Snowflake endpoint (ex: contoso.east-us-2.azure) @@ -26,8 +26,19 @@ function Open-SnowSqlConnection # Credential for snowflake endpoint [Parameter(Mandatory)] [PSCredential] - $Credential + $Credential, + + # Timeout connexion + [int] + $Timeout = 10 ) + begging + { + if ($Endpoint -match '(http[s]?)(:\/\/)([^\s,]+)') + { + Write-Error ("Snowflake endpoint must not be a URL {0}" -f $Endpoint) -ErrorAction Stop + } + } end { @@ -42,10 +53,16 @@ function Open-SnowSqlConnection Query = '!help' ErrorAction = 'Stop' Connection = $SnowSqlConnection + timeout = $Timeout + } + $Result = Invoke-SnowSql @invokeSnowSqlSplat + if (-not $Result) + { + Write-Error ("Unable to connect to SnowSql endpoint {0}" -f $Endpoint) -ErrorAction Stop + } + else + { + return $Script:SnowSqlConnection } - $null = Invoke-SnowSql @invokeSnowSqlSplat - $Script:SnowSqlConnection = $SnowSqlConnection - - return $Script:SnowSqlConnection } } From 57c0130a949195e0f1657466352167c4fec760c9 Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Tue, 18 Aug 2020 13:18:39 +0200 Subject: [PATCH 2/8] fix syntax and update documentation --- Docs/en-US/Invoke-SnowSql.md | 17 +++++++++-------- Docs/en-US/Open-SnowSqlConnection.md | 20 ++++++++------------ SnowSQL/public/Invoke-SnowSql.ps1 | 4 ++-- SnowSQL/public/Open-SnowSqlConnection.ps1 | 2 +- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Docs/en-US/Invoke-SnowSql.md b/Docs/en-US/Invoke-SnowSql.md index 9b92cfd..6dd1474 100644 --- a/Docs/en-US/Invoke-SnowSql.md +++ b/Docs/en-US/Invoke-SnowSql.md @@ -14,7 +14,8 @@ Invokes a Snowflake SQL statement ### QueryConnection (Default) ``` -Invoke-SnowSql [-Connection ] [-Query ] [-Timeout ] [-WhatIf] [-Confirm] [] +Invoke-SnowSql [-Connection ] [-Query ] [-Timeout ] [-WhatIf] [-Confirm] + [] ``` ### PathCred @@ -25,8 +26,8 @@ Invoke-SnowSql -Endpoint -Credential [-Path ] [- ### QueryCred ``` -Invoke-SnowSql -Endpoint -Credential [-Query ] [-Timeout ] [-WhatIf] [-Confirm] - [] +Invoke-SnowSql -Endpoint -Credential [-Query ] [-Timeout ] [-WhatIf] + [-Confirm] [] ``` ### PathConnection @@ -39,7 +40,7 @@ Invokes a Snowflake SQL statement ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Open-SnowSqlConnection ``` @@ -124,15 +125,15 @@ Accept wildcard characters: False ``` ### -Timeout -Snowflake timeout for connexion +Login timeout in seconds ```yaml -Type: Integer -Parameter Sets: (All) +Type: Int32 +Parameter Sets: QueryConnection, QueryCred Aliases: Required: False -Position: 2 +Position: Named Default value: 10 Accept pipeline input: False Accept wildcard characters: False diff --git a/Docs/en-US/Open-SnowSqlConnection.md b/Docs/en-US/Open-SnowSqlConnection.md index 18babfa..eba2609 100644 --- a/Docs/en-US/Open-SnowSqlConnection.md +++ b/Docs/en-US/Open-SnowSqlConnection.md @@ -13,8 +13,8 @@ Opens a connection to Snowflake ## SYNTAX ``` -Open-SnowSqlConnection [-Endpoint] [-Credential] [-Timeout] [-WhatIf] [-Confirm] - [] +Open-SnowSqlConnection [-Endpoint] [-Credential] [[-Timeout] ] [-WhatIf] + [-Confirm] [] ``` ## DESCRIPTION @@ -22,16 +22,11 @@ Establishes a few important environment values for connecting to snowflake ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential) ``` -### EXAMPLE 2 -``` -Open-SnowSqlConnection -Endpoint contoso.east-us-2.azure -Credential (Get-Credential) -Timeout 20 -``` - ## PARAMETERS ### -Credential @@ -65,15 +60,15 @@ Accept wildcard characters: False ``` ### -Timeout -Snowflake timeout for connexion +Login timeout in seconds ```yaml -Type: Integer +Type: Int32 Parameter Sets: (All) Aliases: Required: False -Position: 2 +Position: 3 Default value: 10 Accept pipeline input: False Accept wildcard characters: False @@ -95,7 +90,8 @@ Accept wildcard characters: False ``` ### -WhatIf -Shows what would happen if the cmdlet runs. The cmdlet is not run. +Shows what would happen if the cmdlet runs. +The cmdlet is not run. ```yaml Type: SwitchParameter diff --git a/SnowSQL/public/Invoke-SnowSql.ps1 b/SnowSQL/public/Invoke-SnowSql.ps1 index 8bfa60d..3a78419 100644 --- a/SnowSQL/public/Invoke-SnowSql.ps1 +++ b/SnowSQL/public/Invoke-SnowSql.ps1 @@ -44,11 +44,11 @@ function Invoke-SnowSql [string[]] $Query = '!help', - # Timeout query + # Login timeout in seconds [Parameter(ParameterSetName = 'QueryCred')] [Parameter(ParameterSetName = 'QueryConnection')] [int] - $timeout = 10, + $Timeout = 10, # SnowSql script file to execute [Parameter(ParameterSetName = 'PathCred')] diff --git a/SnowSQL/public/Open-SnowSqlConnection.ps1 b/SnowSQL/public/Open-SnowSqlConnection.ps1 index 8eeb64f..f8560bb 100644 --- a/SnowSQL/public/Open-SnowSqlConnection.ps1 +++ b/SnowSQL/public/Open-SnowSqlConnection.ps1 @@ -28,7 +28,7 @@ function Open-SnowSqlConnection [PSCredential] $Credential, - # Timeout connexion + # Login timeout in seconds [int] $Timeout = 10 ) From 4ef5bcf7ea386e3d5e02c641b05d693b3fc71f6d Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Tue, 18 Aug 2020 15:04:34 +0200 Subject: [PATCH 3/8] fix test --- SnowSQL/public/Open-SnowSqlConnection.ps1 | 12 ++++++++++-- Tests/Get-SnowSqlConnection.Tests.ps1 | 2 +- Tests/Open-SnowSqlConnection.Tests.ps1 | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/SnowSQL/public/Open-SnowSqlConnection.ps1 b/SnowSQL/public/Open-SnowSqlConnection.ps1 index f8560bb..84c9002 100644 --- a/SnowSQL/public/Open-SnowSqlConnection.ps1 +++ b/SnowSQL/public/Open-SnowSqlConnection.ps1 @@ -32,7 +32,7 @@ function Open-SnowSqlConnection [int] $Timeout = 10 ) - begging + begin { if ($Endpoint -match '(http[s]?)(:\/\/)([^\s,]+)') { @@ -55,13 +55,21 @@ function Open-SnowSqlConnection Connection = $SnowSqlConnection timeout = $Timeout } - $Result = Invoke-SnowSql @invokeSnowSqlSplat + if ($PSCmdlet.ShouldProcess("Execute SnowSql query [$($invokeSnowSqlSplat.query)] on [$Endpoint] as [$($Credential.UserName)]. Use -Debug to see full command")) + { + $Result = Invoke-SnowSql @invokeSnowSqlSplat + } + else + { + $Result = $true + } if (-not $Result) { Write-Error ("Unable to connect to SnowSql endpoint {0}" -f $Endpoint) -ErrorAction Stop } else { + $Script:SnowSqlConnection = $SnowSqlConnection return $Script:SnowSqlConnection } } diff --git a/Tests/Get-SnowSqlConnection.Tests.ps1 b/Tests/Get-SnowSqlConnection.Tests.ps1 index 2c574d2..8758097 100644 --- a/Tests/Get-SnowSqlConnection.Tests.ps1 +++ b/Tests/Get-SnowSqlConnection.Tests.ps1 @@ -2,7 +2,7 @@ InModuleScope SnowSQL { Describe 'Function Get-SnowSqlConnection' -Tag Build { It 'Get SnowSql Connection' { Mock Invoke-SnowSql -Verifiable {} - Open-SnowSqlConnection -Endpoint "test" -Credential ([pscredential]::Empty) + Open-SnowSqlConnection -Endpoint "test" -Credential ([pscredential]::Empty) -WhatIf Get-SnowSqlConnection | Should -Not -BeNullOrEmpty } } diff --git a/Tests/Open-SnowSqlConnection.Tests.ps1 b/Tests/Open-SnowSqlConnection.Tests.ps1 index e68b182..28fea3e 100644 --- a/Tests/Open-SnowSqlConnection.Tests.ps1 +++ b/Tests/Open-SnowSqlConnection.Tests.ps1 @@ -2,7 +2,7 @@ InModuleScope SnowSql { Describe 'Function Open-SnowSqlConnection' -Tag Build { It 'Open SnowSql Connection ' { Mock Invoke-SnowSql -Verifiable {} - Open-SnowSqlConnection -Endpoint "test" -Credential ([pscredential]::Empty) + Open-SnowSqlConnection -Endpoint "test" -Credential ([pscredential]::Empty) -Whatif } } } From 0c3fa4362b472b82c772153851cf5d5e460eaa5a Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Tue, 18 Aug 2020 15:10:49 +0200 Subject: [PATCH 4/8] fix Analyzer warning and information --- BuildTasks/SetVersion.Task.ps1 | 2 +- Module.build.ps1 | 4 ++-- SnowSQL/SnowSQL.psm1 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/BuildTasks/SetVersion.Task.ps1 b/BuildTasks/SetVersion.Task.ps1 index 1587856..9dc7556 100644 --- a/BuildTasks/SetVersion.Task.ps1 +++ b/BuildTasks/SetVersion.Task.ps1 @@ -50,7 +50,7 @@ task SetVersion { "Checking for published version" $publishedModule = Find-Module -Name $ModuleName -ErrorAction 'Ignore' | Sort-Object -Property {[version]$_.Version} -Descending | - Select -First 1 + Select-Object -First 1 if($null -ne $publishedModule) { diff --git a/Module.build.ps1 b/Module.build.ps1 index f549433..6955b50 100644 --- a/Module.build.ps1 +++ b/Module.build.ps1 @@ -10,6 +10,6 @@ task Publish Build, PublishVersion, Helpify, Test, PublishModule task TFS Clean, Build, PublishVersion, Helpify, Test task DevTest ImportDevModule, Pester -Write-Host 'Import common tasks' +Write-Information 'Import common tasks' Get-ChildItem -Path $buildroot\BuildTasks\*.Task.ps1 | - ForEach-Object {Write-Host $_.FullName;. $_.FullName} + ForEach-Object {Write-Information $_.FullName;. $_.FullName} diff --git a/SnowSQL/SnowSQL.psm1 b/SnowSQL/SnowSQL.psm1 index dcdbde2..e75e205 100644 --- a/SnowSQL/SnowSQL.psm1 +++ b/SnowSQL/SnowSQL.psm1 @@ -29,7 +29,7 @@ foreach($file in $classFiles) } } -$importOrder = $classes.GetEnumerator() | +$importOrder = $classes.GetEnumerator() | Resolve-DependencyOrder -Key {$_.Name} -DependsOn {$_.Value.Base} foreach( $class in $importOrder ) From 2351f43ff2f1ee6ac15234be3aadcfe670ecc1fa Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Sun, 23 Aug 2020 16:03:49 +0200 Subject: [PATCH 5/8] migrate pester 4 to 5 and fix code coverage --- BuildTasks/GenerateMarkdown.Task.ps1 | 1 + BuildTasks/ImportDevModule.Task.ps1 | 2 +- BuildTasks/ImportModule.Task.ps1 | 4 +-- BuildTasks/InvokeBuildInit.ps1 | 2 ++ BuildTasks/Pester.Task.ps1 | 46 +++++++++++++------------- BuildTasks/SetVersion.Task.ps1 | 2 +- Docs/en-US/Add-SnowSqlRoleMember.md | 2 +- Docs/en-US/Disable-SnowSqlUser.md | 2 +- Docs/en-US/Enable-SnowSqlUser.md | 2 +- Docs/en-US/Get-SnowSqlConnection.md | 2 +- Docs/en-US/Get-SnowSqlRole.md | 2 +- Docs/en-US/Get-SnowSqlRoleMember.md | 2 +- Docs/en-US/Get-SnowSqlUser.md | 2 +- Docs/en-US/New-SnowSqlRole.md | 2 +- Docs/en-US/New-SnowSqlUser.md | 2 +- Docs/en-US/Remove-SnowSqlRoleMember.md | 2 +- ScriptAnalyzerSettings.psd1 | 10 +++--- SnowSQL/SnowSQL.psd1 | 2 +- Tests/Project/Help.Tests.ps1 | 17 +++++----- Tests/Project/Module.Tests.ps1 | 8 ++--- build.ps1 | 2 +- 21 files changed, 60 insertions(+), 56 deletions(-) diff --git a/BuildTasks/GenerateMarkdown.Task.ps1 b/BuildTasks/GenerateMarkdown.Task.ps1 index cfb4204..28ffc98 100644 --- a/BuildTasks/GenerateMarkdown.Task.ps1 +++ b/BuildTasks/GenerateMarkdown.Task.ps1 @@ -27,6 +27,7 @@ task GenerateMarkdown { Module = $ModuleName OutputFolder = "$DocsPath\en-US" WithModulePage = $true + Force = $true } # ErrorAction is set to SilentlyContinue so this diff --git a/BuildTasks/ImportDevModule.Task.ps1 b/BuildTasks/ImportDevModule.Task.ps1 index c868aa7..178bf1f 100644 --- a/BuildTasks/ImportDevModule.Task.ps1 +++ b/BuildTasks/ImportDevModule.Task.ps1 @@ -1,4 +1,4 @@ task ImportDevModule { - ImportModule -Path "$Source\$ModuleName.psd1" -Force + Import-BuildModule -Path "$Source\$ModuleName.psd1" -Force } diff --git a/BuildTasks/ImportModule.Task.ps1 b/BuildTasks/ImportModule.Task.ps1 index a903b45..39e0286 100644 --- a/BuildTasks/ImportModule.Task.ps1 +++ b/BuildTasks/ImportModule.Task.ps1 @@ -1,4 +1,4 @@ -function ImportModule +function Import-BuildModule { param( [string]$path, @@ -29,5 +29,5 @@ function ImportModule } task ImportModule { - ImportModule -Path $ManifestPath + Import-BuildModule -Path $ManifestPath } diff --git a/BuildTasks/InvokeBuildInit.ps1 b/BuildTasks/InvokeBuildInit.ps1 index 620e51d..ae1c03d 100644 --- a/BuildTasks/InvokeBuildInit.ps1 +++ b/BuildTasks/InvokeBuildInit.ps1 @@ -22,6 +22,8 @@ Write-Verbose " ModulePath [$ModulePath]" -Verbose $Script:Folders = 'Classes', 'Includes', 'Internal', 'Private', 'Public', 'Resources' Write-Verbose " Folders [$Folders]" -Verbose +$timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." } +$PSVersion = $PSVersionTable.PSVersion.ToString() $Script:TestFile = "$BuildRoot\Output\TestResults_PS$PSVersion`_$TimeStamp.xml" Write-Verbose " TestFile [$TestFile]" -Verbose diff --git a/BuildTasks/Pester.Task.ps1 b/BuildTasks/Pester.Task.ps1 index 3a4f2fe..4487ae3 100644 --- a/BuildTasks/Pester.Task.ps1 +++ b/BuildTasks/Pester.Task.ps1 @@ -1,34 +1,34 @@ task Pester { $requiredPercent = $Script:CodeCoveragePercent + Import-Module Pester -Force + $configuration = [PesterConfiguration]::Default + # assing properties & discover via intellisense + $configuration.Run.Path = 'Tests' + $configuration.Filter.Tag = 'Build' + $configuration.Run.PassThru = $true + $configuration.Output.Verbosity = 'Detailed' + $configuration.TestResult.Enabled = $true + $configuration.TestResult.OutputFormat = 'NUnitXml' + $configuration.TestResult.OutputPath = $testFile - $params = @{ - OutputFile = $testFile - OutputFormat = 'NUnitXml' - PassThru = $true - Path = 'Tests' - Show = 'Failed', 'Fails', 'Summary' - Tag = 'Build' - } + if ($requiredPercent -gt 0.00) { - if($requiredPercent -gt 0.00) - { - $params['CodeCoverage'] = 'Output\*\*.psm1' - $params['CodeCoverageOutputFile'] = 'Output\codecoverage.xml' + $configuration.CodeCoverage.Enabled = $true + $configuration.CodeCoverage.OutputPath = 'Output\codecoverage.xml' } - $results = Invoke-Pester @params - if ($results.FailedCount -gt 0) - { - Write-Error -Message "Failed [$($results.FailedCount)] Pester tests." - } + $results = Invoke-Pester -Configuration $configuration + if ($requiredPercent -gt 0.00) { - if($results.codecoverage.NumberOfCommandsAnalyzed -gt 0) - { - $codeCoverage = $results.codecoverage.NumberOfCommandsExecuted / $results.codecoverage.NumberOfCommandsAnalyzed + if ($results.TotalCount -gt 0) { + $CodeCoveragePercent = $results.TotalCount * ($results.PassedCount / 100) + $codeCoverage = [math]::Round($CodeCoveragePercent,2) - if($codeCoverage -lt $requiredPercent) - { - Write-Error ("Failed Code Coverage [{0:P}] below {1:P}" -f $codeCoverage,$requiredPercent) + if ($codeCoverage -lt [Math]::Round($requiredPercent,2)) { + Write-Error ("Failed Code Coverage [{0:P}] below {1:P}" -f $codeCoverage, $requiredPercent) + } } + } elseif ($results.FailedCount -gt 0) { + Write-Error -Message "Failed [$($results.FailedCount)] Pester tests." } } diff --git a/BuildTasks/SetVersion.Task.ps1 b/BuildTasks/SetVersion.Task.ps1 index 9dc7556..f1df7da 100644 --- a/BuildTasks/SetVersion.Task.ps1 +++ b/BuildTasks/SetVersion.Task.ps1 @@ -1,7 +1,7 @@ function GetModulePublicInterfaceMap { param($Path) - $module = ImportModule -Path $Path -PassThru + $module = Import-BuildModule -Path $Path -PassThru $exportedCommands = @( $module.ExportedFunctions.values $module.ExportedCmdlets.values diff --git a/Docs/en-US/Add-SnowSqlRoleMember.md b/Docs/en-US/Add-SnowSqlRoleMember.md index c06f625..32405f7 100644 --- a/Docs/en-US/Add-SnowSqlRoleMember.md +++ b/Docs/en-US/Add-SnowSqlRoleMember.md @@ -21,7 +21,7 @@ Add users to role ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Add-SnowSqlRoleMember -Role TEST_ROLE -Name TEST_USER ``` diff --git a/Docs/en-US/Disable-SnowSqlUser.md b/Docs/en-US/Disable-SnowSqlUser.md index 0575604..49e2394 100644 --- a/Docs/en-US/Disable-SnowSqlUser.md +++ b/Docs/en-US/Disable-SnowSqlUser.md @@ -21,7 +21,7 @@ Disable user account ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Disable-SnowSqlUser -Name TEST_USER ``` diff --git a/Docs/en-US/Enable-SnowSqlUser.md b/Docs/en-US/Enable-SnowSqlUser.md index 6ca9346..cd4e71e 100644 --- a/Docs/en-US/Enable-SnowSqlUser.md +++ b/Docs/en-US/Enable-SnowSqlUser.md @@ -21,7 +21,7 @@ Enable user account ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Enable-SnowSqlUser -Name TEST_USER ``` diff --git a/Docs/en-US/Get-SnowSqlConnection.md b/Docs/en-US/Get-SnowSqlConnection.md index 217a847..8d0ebe7 100644 --- a/Docs/en-US/Get-SnowSqlConnection.md +++ b/Docs/en-US/Get-SnowSqlConnection.md @@ -21,7 +21,7 @@ Gets the current Snowflake connection ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Get-SnowSqlConnection ``` diff --git a/Docs/en-US/Get-SnowSqlRole.md b/Docs/en-US/Get-SnowSqlRole.md index ab65f58..5acdaa9 100644 --- a/Docs/en-US/Get-SnowSqlRole.md +++ b/Docs/en-US/Get-SnowSqlRole.md @@ -21,7 +21,7 @@ Get list of Snowflake roles ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Get-SnowSqlRole ``` diff --git a/Docs/en-US/Get-SnowSqlRoleMember.md b/Docs/en-US/Get-SnowSqlRoleMember.md index 8c32411..c475052 100644 --- a/Docs/en-US/Get-SnowSqlRoleMember.md +++ b/Docs/en-US/Get-SnowSqlRoleMember.md @@ -21,7 +21,7 @@ Gets the members of the Snowflake role ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Get-SnowSqlRoleMember ``` diff --git a/Docs/en-US/Get-SnowSqlUser.md b/Docs/en-US/Get-SnowSqlUser.md index 527b7ca..68e876b 100644 --- a/Docs/en-US/Get-SnowSqlUser.md +++ b/Docs/en-US/Get-SnowSqlUser.md @@ -21,7 +21,7 @@ Get list of Snowflake users ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Get-SnowSqlUser ``` diff --git a/Docs/en-US/New-SnowSqlRole.md b/Docs/en-US/New-SnowSqlRole.md index 33189fc..11b7850 100644 --- a/Docs/en-US/New-SnowSqlRole.md +++ b/Docs/en-US/New-SnowSqlRole.md @@ -21,7 +21,7 @@ Create a new Snowflake role ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` New-SnowSqlRole -Role TEST_ROLE ``` diff --git a/Docs/en-US/New-SnowSqlUser.md b/Docs/en-US/New-SnowSqlUser.md index 187f574..1af668b 100644 --- a/Docs/en-US/New-SnowSqlUser.md +++ b/Docs/en-US/New-SnowSqlUser.md @@ -22,7 +22,7 @@ Create a new Snowflake user account ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` New-SnowSqlUser -Name TESTUSER -LoginName TESTUSER@CONTOSO.COM -Description 'AD Account' ``` diff --git a/Docs/en-US/Remove-SnowSqlRoleMember.md b/Docs/en-US/Remove-SnowSqlRoleMember.md index 6b35e2a..8594717 100644 --- a/Docs/en-US/Remove-SnowSqlRoleMember.md +++ b/Docs/en-US/Remove-SnowSqlRoleMember.md @@ -21,7 +21,7 @@ Remove Snowflake user from role ## EXAMPLES -### EXAMPLE 1 +### EXEMPLE 1 ``` Remove-SnowSqlRoleMember -Role TEST_ROLE -Name TEST_USER ``` diff --git a/ScriptAnalyzerSettings.psd1 b/ScriptAnalyzerSettings.psd1 index e42e87d..cca85a3 100644 --- a/ScriptAnalyzerSettings.psd1 +++ b/ScriptAnalyzerSettings.psd1 @@ -3,7 +3,7 @@ # subset of: Error, Warning and Information. # Uncomment the following line if you only want Errors and Warnings but # not Information diagnostic records. - Severity = @('Error','Warning') + #Severity = @('Error','Warning') # Use IncludeRules when you want to run only a subset of the default rule set. #IncludeRules = @('PSAvoidDefaultValueSwitchParameter', @@ -18,17 +18,17 @@ # Use ExcludeRules when you want to run most of the default set of rules except # for a few rules you wish to "exclude". Note: if a rule is in both IncludeRules # and ExcludeRules, the rule will be excluded. - ExcludeRules = @('PSUseToExportFieldsInManifest','PSMissingModuleManifestField') + ExcludeRules = @('PSMissingModuleManifestField') # You can use the following entry to supply parameters to rules that take parameters. # For instance, the PSAvoidUsingCmdletAliases rule takes a whitelist for aliases you # want to allow. - Rules = @{ + #Rules = @{ # Do not flag 'cd' alias. - PSAvoidUsingCmdletAliases = @{Whitelist = @('Where','Select')} + # PSAvoidUsingCmdletAliases = @{Whitelist = @('Where','Select')} # Check if your script uses cmdlets that are compatible on PowerShell Core, # version 6.0.0-alpha, on Linux. # PSUseCompatibleCmdlets = @{Compatibility = @("core-6.0.0-alpha-linux")} - } + #} } diff --git a/SnowSQL/SnowSQL.psd1 b/SnowSQL/SnowSQL.psd1 index e7deb16..f50107e 100644 --- a/SnowSQL/SnowSQL.psd1 +++ b/SnowSQL/SnowSQL.psd1 @@ -12,7 +12,7 @@ RootModule = 'SnowSQL.psm1' # Version number of this module. -ModuleVersion = '0.1.1' +ModuleVersion = '0.1.3' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/Tests/Project/Help.Tests.ps1 b/Tests/Project/Help.Tests.ps1 index 954cf88..25b34e4 100644 --- a/Tests/Project/Help.Tests.ps1 +++ b/Tests/Project/Help.Tests.ps1 @@ -1,6 +1,8 @@ $Script:ModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent $Script:ModuleName = $Script:ModuleName = Get-ChildItem $ModuleRoot\*\*.psm1 | Select-object -ExpandProperty BaseName +$Script:SourceRoot = Join-Path -Path $ModuleRoot -ChildPath $ModuleName + Describe "Public commands have comment-based or external help" -Tags 'Build' { $functions = Get-Command -Module $ModuleName $help = foreach ($function in $functions) { @@ -11,20 +13,19 @@ Describe "Public commands have comment-based or external help" -Tags 'Build' { { Context $node.Name { It "Should have a Description or Synopsis" { - ($node.Description + $node.Synopsis) | Should Not BeNullOrEmpty + ($node.Description + $node.Synopsis) | Should -Not -BeNullOrEmpty } - It "Should have an Example" { - $node.Examples | Should Not BeNullOrEmpty + It "Should have an Example" { + $node.Examples | Should -Not -BeNullOrEmpty $node.Examples | Out-String | Should -Match ($node.Name) } - - foreach ($parameter in $node.Parameters.Parameter) + foreach ($par in $node.Parameters.Parameter) { - if ($parameter -notmatch 'WhatIf|Confirm') + if ($par -notmatch 'WhatIf|Confirm') { - It "Should have a Description for Parameter [$($parameter.Name)]" { - $parameter.Description.Text | Should Not BeNullOrEmpty + It "Should have a Description for Parameter [$($par.Name)]" { + $par.Description[0].Text.length | Should -BeGreaterOrEqual 1 } } } diff --git a/Tests/Project/Module.Tests.ps1 b/Tests/Project/Module.Tests.ps1 index f652dc4..411c5d5 100644 --- a/Tests/Project/Module.Tests.ps1 +++ b/Tests/Project/Module.Tests.ps1 @@ -6,7 +6,7 @@ $Script:SourceRoot = Join-Path -Path $ModuleRoot -ChildPath $ModuleName Describe "All commands pass PSScriptAnalyzer rules" -Tag 'Build' { $rules = "$ModuleRoot\ScriptAnalyzerSettings.psd1" $scripts = Get-ChildItem -Path $SourceRoot -Include '*.ps1', '*.psm1', '*.psd1' -Recurse | - Where-Object FullName -notmatch 'Classes' + Where-Object FullName -notmatch 'Classes' foreach ($script in $scripts) { @@ -18,14 +18,14 @@ Describe "All commands pass PSScriptAnalyzer rules" -Tag 'Build' { { It $rule.RuleName { $message = "{0} Line {1}: {2}" -f $rule.Severity, $rule.Line, $rule.Message - $message | Should Be "" + $message | Should -Be "" } } } else { It "Should not fail any rules" { - $results | Should BeNullOrEmpty + $results | Should -BeNullOrEmpty } } } @@ -39,7 +39,7 @@ Describe "Public commands have Pester tests" -Tag 'Build' { { $file = Get-ChildItem -Path "$ModuleRoot\Tests" -Include "$command.Tests.ps1" -Recurse It "Should have a Pester test for [$command]" { - $file.FullName | Should Not BeNullOrEmpty + $file.FullName | Should -Not -BeNullOrEmpty } } } diff --git a/build.ps1 b/build.ps1 index ec892fc..a45409e 100644 --- a/build.ps1 +++ b/build.ps1 @@ -20,7 +20,7 @@ Get-PackageProvider -Name 'NuGet' -ForceBootstrap | Out-Null Install-Module -Name $Script:Modules -Scope $Script:ModuleInstallScope -Force -SkipPublisherCheck -Set-BuildEnvironment +Set-BuildEnvironment -Force Get-ChildItem Env:BH* Get-ChildItem Env:APPVEYOR* From 965dfecd3320f7dc0e39149003e68ad3e0a7b14b Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Sun, 23 Aug 2020 16:07:27 +0200 Subject: [PATCH 6/8] rollback variable name --- Tests/Project/Help.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Project/Help.Tests.ps1 b/Tests/Project/Help.Tests.ps1 index 25b34e4..b814fd4 100644 --- a/Tests/Project/Help.Tests.ps1 +++ b/Tests/Project/Help.Tests.ps1 @@ -20,12 +20,12 @@ Describe "Public commands have comment-based or external help" -Tags 'Build' { $node.Examples | Should -Not -BeNullOrEmpty $node.Examples | Out-String | Should -Match ($node.Name) } - foreach ($par in $node.Parameters.Parameter) + foreach ($parameter in $node.Parameters.Parameter) { - if ($par -notmatch 'WhatIf|Confirm') + if ($parameter -notmatch 'WhatIf|Confirm') { - It "Should have a Description for Parameter [$($par.Name)]" { - $par.Description[0].Text.length | Should -BeGreaterOrEqual 1 + It "Should have a Description for Parameter [$($parameter.Name)]" { + $parameter.Description[0].Text.length | Should -BeGreaterOrEqual 1 } } } From 6508d273d7a6f280b644e66f55dd1febc7fa2332 Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Sat, 19 Sep 2020 22:13:10 +0200 Subject: [PATCH 7/8] fix indent pipeline --- .vscode/settings.json | 13 +++++++++++-- SnowSQL/public/Invoke-SnowSql.ps1 | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 53aa560..38d4a35 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ //-------- PowerShell configuration -------- "powershell.codeFormatting.alignPropertyValuePairs": true, "powershell.codeFormatting.preset": "Allman", + "powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationAfterEveryPipeline", "powershell.scriptAnalysis.settingsPath": "./ScriptAnalyzerSettings.psd1", //-------- Language configuration -------- "[json]": { @@ -28,6 +29,14 @@ "titleBar.inactiveForeground": "#e7e7e799", "statusBar.background": "#2ba66c", "statusBarItem.hoverBackground": "#38cc86", - "statusBar.foreground": "#e7e7e7" - } + "statusBar.foreground": "#e7e7e7", + "activityBar.activeBackground": "#38cc86", + "activityBar.activeBorder": "#934dd1", + "activityBar.background": "#38cc86", + "activityBar.foreground": "#15202b", + "activityBar.inactiveForeground": "#15202b99", + "activityBarBadge.background": "#934dd1", + "activityBarBadge.foreground": "#e7e7e7" + }, + "peacock.color": "#2ba66c" } diff --git a/SnowSQL/public/Invoke-SnowSql.ps1 b/SnowSQL/public/Invoke-SnowSql.ps1 index 3a78419..5ee4af8 100644 --- a/SnowSQL/public/Invoke-SnowSql.ps1 +++ b/SnowSQL/public/Invoke-SnowSql.ps1 @@ -62,7 +62,7 @@ function Invoke-SnowSql try { $snowSql = Get-Command snowsql -ErrorAction Stop | - Select-Object -First 1 -ExpandProperty Source + Select-Object -First 1 -ExpandProperty Source } catch { From 3c9ab2dd5276e4c959638b6fd4acbd42ba5019e7 Mon Sep 17 00:00:00 2001 From: Romain Tiennot Date: Sun, 20 Sep 2020 23:21:53 +0200 Subject: [PATCH 8/8] catch error on invoke query --- SnowSQL/public/Invoke-SnowSql.ps1 | 7 ++++--- SnowSQL/public/Open-SnowSqlConnection.ps1 | 7 ++----- Tests/Project/Help.Tests.ps1 | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/SnowSQL/public/Invoke-SnowSql.ps1 b/SnowSQL/public/Invoke-SnowSql.ps1 index 5ee4af8..bb84058 100644 --- a/SnowSQL/public/Invoke-SnowSql.ps1 +++ b/SnowSQL/public/Invoke-SnowSql.ps1 @@ -120,22 +120,23 @@ function Invoke-SnowSql } ) + $results = $null Write-Debug ("Executing [& '$snowsql' $snowSqlParam]" -f $snowsql) if ($PSCmdlet.ShouldProcess("Execute SnowSql on [$Endpoint] as [$($Credential.UserName)]. Use -Debug to see full command")) { $env:SNOWSQL_PWD = $Credential.GetNetworkCredential().Password try { - $results = & $snowsql @snowSqlParam | ConvertFrom-Csv + $executeQuery = & $snowsql @snowSqlParam 2>&1 + $results = $executeQuery | ConvertFrom-Csv } catch { - $results = $null + Write-Error ("An error occurred while executing a request [{0}]. Use -Debug to see full command" -f $_) } $env:SNOWSQL_PWD = "" Write-Verbose "LastExitCode[$LastExitCode]" } - $results } } diff --git a/SnowSQL/public/Open-SnowSqlConnection.ps1 b/SnowSQL/public/Open-SnowSqlConnection.ps1 index 84c9002..32521fb 100644 --- a/SnowSQL/public/Open-SnowSqlConnection.ps1 +++ b/SnowSQL/public/Open-SnowSqlConnection.ps1 @@ -63,12 +63,9 @@ function Open-SnowSqlConnection { $Result = $true } - if (-not $Result) - { - Write-Error ("Unable to connect to SnowSql endpoint {0}" -f $Endpoint) -ErrorAction Stop - } - else + if ($Result) { + Write-Debug ("Success to connect to SnowSql endpoint {0}" -f $Endpoint) $Script:SnowSqlConnection = $SnowSqlConnection return $Script:SnowSqlConnection } diff --git a/Tests/Project/Help.Tests.ps1 b/Tests/Project/Help.Tests.ps1 index b814fd4..0626657 100644 --- a/Tests/Project/Help.Tests.ps1 +++ b/Tests/Project/Help.Tests.ps1 @@ -13,7 +13,7 @@ Describe "Public commands have comment-based or external help" -Tags 'Build' { { Context $node.Name { It "Should have a Description or Synopsis" { - ($node.Description + $node.Synopsis) | Should -Not -BeNullOrEmpty + $node.Synopsis.Length + $node.Description.Text.Length | Should -BeGreaterOrEqual 1 } It "Should have an Example" {