From 822af5d140ff2e5e0d11cf1197384f5bffa9dce8 Mon Sep 17 00:00:00 2001 From: Robert McLeod Date: Thu, 20 Feb 2020 19:30:40 +1100 Subject: [PATCH] Added `PSScriptAnalyzerSettings.psd1` and code formatting fixes. --- PSScriptAnalyzerSettings.psd1 | 68 +++++++++++++++++++++++++ azure-pipelines.yml | 8 +-- functions/New-Ticket.ps1 | 2 +- functions/Update-Ticket.ps1 | 2 +- tests/Invoke-Method.tests.ps1 | 5 +- tests/New-UserIdentity.tests.ps1 | 4 +- tests/Routes-GroupMemberships.tests.ps1 | 2 +- tests/Routes-Tags.tests.ps1 | 8 +-- tests/Routes-Users.tests.ps1 | 2 +- 9 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 PSScriptAnalyzerSettings.psd1 diff --git a/PSScriptAnalyzerSettings.psd1 b/PSScriptAnalyzerSettings.psd1 new file mode 100644 index 0000000..999fb54 --- /dev/null +++ b/PSScriptAnalyzerSettings.psd1 @@ -0,0 +1,68 @@ +@{ + IncludeRules = @( + 'PSAlignAssignmentStatement', + 'PSAvoid*', + 'PSDSC*', + 'PSMisleadingBacktick', + 'PSMissingModuleManifestField', + 'PSPlaceCloseBrace', + 'PSPlaceOpenBrace', + 'PSPossibleIncorrectComparisonWithNull', + 'PSPossibleIncorrectUsageOfAssignmentOperator', + 'PSPossibleIncorrectUsageOfRedirectionOperator', + 'PSProvideCommentHelp', + 'PSReservedCmdletChar', + 'PSReservedParams', + 'PSShouldProcess', + 'PSUse*' + ) + + Rules = @{ + PSPlaceOpenBrace = @{ + Enable = $true + OnSameLine = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + } + + PSPlaceCloseBrace = @{ + Enable = $true + NewLineAfter = $false + IgnoreOneLineBlock = $true + NoEmptyLineBefore = $false + } + + PSUseConsistentIndentation = @{ + Enable = $true + Kind = 'space' + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + IndentationSize = 4 + } + + PSUseConsistentWhitespace = @{ + Enable = $true + CheckInnerBrace = $true + CheckOpenBrace = $false # incorrectly flags all `Should -Throw` Pester assertions + CheckOpenParen = $true + CheckOperator = $false # conflicts with `PSAlignAssignmentStatement` + CheckPipe = $true + CheckSeparator = $true + CheckParameter = $true + } + + PSAlignAssignmentStatement = @{ + Enable = $true + CheckHashtable = $true + } + + PSUseCorrectCasing = @{ + Enable = $true + } + } + + Severity = @( + 'Error' + 'Warning' + 'Information' + ) +} diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 90e66a4..bfe5863 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -69,7 +69,7 @@ stages: - pwsh: | Save-Module -Name PSScriptAnalyzer -Path '.' -Repository PSGallery -Confirm:$false Import-Module -Name './PSScriptAnalyzer/' - Invoke-ScriptAnalyzer -Path '$(Pipeline.Workspace)' -Recurse -EnableExit -ReportSummary | Format-Table -Autosize + Get-ChildItem -Path '$(Pipeline.Workspace)' -Directory | Invoke-ScriptAnalyzer -EnableExit | Format-Table -AutoSize displayName: Script Analyzer - pwsh: | Save-Module -Name Pester -Path '.' -Repository PSGallery -Confirm:$false @@ -90,7 +90,7 @@ stages: - pwsh: | Save-Module -Name PSScriptAnalyzer -Path '.' -Repository PSGallery -Confirm:$false Import-Module -Name './PSScriptAnalyzer/' - Invoke-ScriptAnalyzer -Path '$(Pipeline.Workspace)' -Recurse -EnableExit -ReportSummary | Format-Table -Autosize + Get-ChildItem -Path '$(Pipeline.Workspace)' -Directory | Invoke-ScriptAnalyzer -EnableExit | Format-Table -AutoSize displayName: Script Analyzer - pwsh: | Save-Module -Name Pester -Path '.' -Repository PSGallery -Confirm:$false @@ -111,7 +111,7 @@ stages: - pwsh: | Save-Module -Name PSScriptAnalyzer -Path '.' -Repository PSGallery -Confirm:$false Import-Module -Name './PSScriptAnalyzer/' - Invoke-ScriptAnalyzer -Path '$(Pipeline.Workspace)' -Recurse -EnableExit -ReportSummary | Format-Table -Autosize + Get-ChildItem -Path '$(Pipeline.Workspace)' -Directory | Invoke-ScriptAnalyzer -EnableExit | Format-Table -AutoSize displayName: Script Analyzer - pwsh: | Save-Module -Name Pester -Path '.' -Repository PSGallery -Confirm:$false @@ -132,7 +132,7 @@ stages: - powershell: | Save-Module -Name PSScriptAnalyzer -Path '.' -Repository PSGallery -Confirm:$false Resolve-Path -Path './PSScriptAnalyzer/*/PSScriptAnalyzer.psd1' | Import-Module - Invoke-ScriptAnalyzer -Path '$(Pipeline.Workspace)' -Recurse -EnableExit -ReportSummary | Format-Table -Autosize + Get-ChildItem -Path '$(Pipeline.Workspace)' -Directory | Invoke-ScriptAnalyzer -EnableExit | Format-Table -AutoSize displayName: Script Analyzer - powershell: | Save-Module -Name Pester -Path '.' -Repository PSGallery -Confirm:$false diff --git a/functions/New-Ticket.ps1 b/functions/New-Ticket.ps1 index 6a2cfa9..ec8bf81 100644 --- a/functions/New-Ticket.ps1 +++ b/functions/New-Ticket.ps1 @@ -168,7 +168,7 @@ function New-Ticket { $path = "/api/v2/tickets.json" $body = @{ - ticket = @{} + ticket = @{ } } $map = @{ diff --git a/functions/Update-Ticket.ps1 b/functions/Update-Ticket.ps1 index d891090..e056aa7 100644 --- a/functions/Update-Ticket.ps1 +++ b/functions/Update-Ticket.ps1 @@ -198,7 +198,7 @@ function Update-Ticket { } $body = @{ - ticket = @{} + ticket = @{ } } $map = @{ diff --git a/tests/Invoke-Method.tests.ps1 b/tests/Invoke-Method.tests.ps1 index 2d67b46..a805f58 100644 --- a/tests/Invoke-Method.tests.ps1 +++ b/tests/Invoke-Method.tests.ps1 @@ -76,13 +76,14 @@ Describe 'Invoke-Method' { } It 'Throws with no invalid connection type' { - { + $block = { Invoke-Method -Path '/' -Context [PSCustomObject]@{ Organization = 'company' BaseUrl = 'https://company.testdesk.com' Credential = $null } - } | Should -Throw + } + $block | Should -Throw } It 'Converts Body to JSON by default' { diff --git a/tests/New-UserIdentity.tests.ps1 b/tests/New-UserIdentity.tests.ps1 index 4f5fa25..beb48e7 100644 --- a/tests/New-UserIdentity.tests.ps1 +++ b/tests/New-UserIdentity.tests.ps1 @@ -41,11 +41,11 @@ Describe 'New-UserIdentity' { throw 'Please run test in non-interactive mode' } - { New-UserIdentity -Context $context -UserId -1 -Value 'name@company.com'} | Should -Throw + { New-UserIdentity -Context $context -UserId -1 -Value 'name@company.com' } | Should -Throw } It 'Requires Type to be a string' { - { New-UserIdentity -Context $context -UserId -1 -Type @{ A = 1 } -Value 'name@company.com'} | Should -Throw + { New-UserIdentity -Context $context -UserId -1 -Type @{ A = 1 } -Value 'name@company.com' } | Should -Throw } It 'Accepts Type: ' -TestCases @( diff --git a/tests/Routes-GroupMemberships.tests.ps1 b/tests/Routes-GroupMemberships.tests.ps1 index 1699f0a..914ef87 100644 --- a/tests/Routes-GroupMemberships.tests.ps1 +++ b/tests/Routes-GroupMemberships.tests.ps1 @@ -237,7 +237,7 @@ Describe 'Group Memberships Routes' { } # agent GET /api/v2/users/{user_id}/group_memberships/{id}.json Context 'Create Membership' { - $member = [PSCustomObject]@{ user_id = 1; group_id = 2} + $member = [PSCustomObject]@{ user_id = 1; group_id = 2 } It 'Matches the endpoint' { if ($IsInteractive) { diff --git a/tests/Routes-Tags.tests.ps1 b/tests/Routes-Tags.tests.ps1 index ca428f6..5068796 100644 --- a/tests/Routes-Tags.tests.ps1 +++ b/tests/Routes-Tags.tests.ps1 @@ -368,26 +368,26 @@ Describe 'Tags Routes' { $context.User.role = 'admin' - { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false} | Should -Not -Throw + { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false } | Should -Not -Throw Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Delete' -and $Uri -match '/api/v2/organizations/\d+/tags.json' } -Scope It } It 'Does not allow end users to call' { $context.User.role = 'end-user' - { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false} | Should -Throw 'Authenticated user must have role' + { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false } | Should -Throw 'Authenticated user must have role' } It 'Allows agents to call' { $context.User.role = 'agent' - { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false} | Should -Not -Throw + { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false } | Should -Not -Throw } It 'Allows admins to call' { $context.User.role = 'admin' - { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false} | Should -Not -Throw + { Remove-Tag -Context $context -OrganizationId 1 -Tag 'Delivery' -Confirm:$false } | Should -Not -Throw } } diff --git a/tests/Routes-Users.tests.ps1 b/tests/Routes-Users.tests.ps1 index febe81d..3b74f09 100644 --- a/tests/Routes-Users.tests.ps1 +++ b/tests/Routes-Users.tests.ps1 @@ -359,7 +359,7 @@ Describe 'Users Routes' { } - Context 'Merge Self With Another User' {} # end-user PUT /api/v2/users/me/merge.json + Context 'Merge Self With Another User' { } # end-user PUT /api/v2/users/me/merge.json Context 'Merge End Users' {