-
-
Notifications
You must be signed in to change notification settings - Fork 31
chore: add code linting and formatting #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| pull_request: | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Tests | ||
| runs-on: windows-latest | ||
| defaults: | ||
| run: | ||
| shell: pwsh | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
| - name: Install dependencies | ||
| uses: potatoqualitee/psmodulecache@ee5e9494714abf56f6efbfa51527b2aec5c761b8 # v6.2.1 | ||
| with: | ||
| modules-to-cache: PSScriptAnalyzer, Pester | ||
| shell: pwsh | ||
| - name: Run tests | ||
| run: ./tests/run.ps1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| @{ | ||
| # OTBS | ||
| IncludeRules = @( | ||
| 'PSPlaceOpenBrace', | ||
| 'PSPlaceCloseBrace', | ||
| 'PSUseConsistentWhitespace', | ||
| 'PSUseConsistentIndentation', | ||
| 'PSAlignAssignmentStatement', | ||
| 'PSAvoidSemicolonsAsLineTerminators', | ||
| 'PSAvoidUsingDoubleQuotesForConstantString', | ||
| 'PSUseCorrectCasing' | ||
| ) | ||
|
|
||
| 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 = $true | ||
| CheckOpenParen = $true | ||
| CheckOperator = $true | ||
| CheckPipe = $true | ||
| CheckPipeForRedundantWhitespace = $false | ||
| CheckSeparator = $true | ||
| CheckParameter = $false | ||
| IgnoreAssignmentOperatorInsideHashTable = $true | ||
| } | ||
|
|
||
| PSAlignAssignmentStatement = @{ | ||
| Enable = $true | ||
| CheckHashtable = $true | ||
| } | ||
|
|
||
| PSAvoidSemicolonsAsLineTerminators = @{ | ||
| Enable = $true | ||
| } | ||
|
|
||
| PSAvoidUsingDoubleQuotesForConstantString = @{ | ||
| Enable = $true | ||
| } | ||
|
|
||
| PSUseCorrectCasing = @{ | ||
| Enable = $true | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| @{ | ||
| # Only diagnostic records of the specified severity will be generated. | ||
| # Uncomment the following line if you only want Errors and Warnings but | ||
| # not Information diagnostic records. | ||
| Severity = @('Error', 'Warning') | ||
|
|
||
| # Analyze **only** the following rules. Use IncludeRules when you want | ||
| # to invoke only a small subset of the default rules. | ||
| # IncludeRules = @('PSAvoidDefaultValueSwitchParameter', | ||
| # 'PSMisleadingBacktick', | ||
| # 'PSMissingModuleManifestField', | ||
| # 'PSReservedCmdletChar', | ||
| # 'PSReservedParams', | ||
| # 'PSShouldProcess', | ||
| # 'PSUseApprovedVerbs', | ||
| # 'PSAvoidUsingCmdletAliases', | ||
| # 'PSUseDeclaredVarsMoreThanAssignments') | ||
|
|
||
| # Do not analyze the following rules. Use ExcludeRules when you have | ||
| # commented out the IncludeRules settings above and want to include all | ||
| # the default rules except for those you exclude below. | ||
| # Note: if a rule is in both IncludeRules and ExcludeRules, the rule | ||
| # will be excluded. | ||
| ExcludeRules = @( | ||
| # PSUseDeclaredVarsMoreThanAssignments doesn't currently work due to: | ||
| # https://github.com/PowerShell/PSScriptAnalyzer/issues/636 | ||
| 'PSUseDeclaredVarsMoreThanAssignments', | ||
| # `Write-Log` uses `Write-Host` currently. | ||
| 'PSAvoidUsingWriteHost' | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,9 @@ | ||
| // Configure PSScriptAnalyzer settings | ||
| { | ||
| "powershell.scriptAnalysis.settingsPath": "PSScriptAnalyzerSettings.psd1", | ||
| "[powershell]": { | ||
| "editor.formatOnSave": true, | ||
| }, | ||
| "powershell.scriptAnalysis.settingsPath": ".pslintrules.psd1", | ||
| "powershell.codeFormatting.preset": "OTBS", | ||
| "powershell.codeFormatting.alignPropertyValuePairs": true, | ||
| "powershell.codeFormatting.ignoreOneLineBlock": true, | ||
| "powershell.codeFormatting.useConstantStrings": true, | ||
| "powershell.codeFormatting.useCorrectCasing": true, | ||
| "powershell.codeFormatting.whitespaceBetweenParameters": true, | ||
| "files.exclude": { | ||
| "**/.git": true, | ||
| "**/.svn": true, | ||
| "**/.hg": true, | ||
| "**/CVS": true, | ||
| "**/.DS_Store": true, | ||
| "**/tmp": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| Describe 'PowerShell Code Style' -Tag 'PSScriptAnalyzer' { | ||
| BeforeAll { | ||
| $formatSettings = "$PSScriptRoot\..\.psformatrules.psd1" | ||
| $lintSettings = "$PSScriptRoot\..\.pslintrules.psd1" | ||
| } | ||
|
|
||
| It 'PSScriptAnalyzer rules files should exist' { | ||
| $formatSettings | Should -Exist | ||
| $lintSettings | Should -Exist | ||
| } | ||
|
|
||
| Context 'PowerShell code formatting' { | ||
| BeforeAll { | ||
| $records = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\..\" ` | ||
| -Recurse -Settings $formatSettings | ||
| } | ||
| It 'Code should be formatted' { | ||
| $records.Count | Should -Be 0 | ||
| } | ||
| AfterAll { | ||
| if ($records) { | ||
| foreach ($r in $records) { | ||
| $type = 'Unknown' | ||
| switch -wildCard ($r.ScriptName) { | ||
| '*.psm1' { $type = 'Module' } | ||
| '*.ps1' { $type = 'Script' } | ||
| '*.psd1' { $type = 'Manifest' } | ||
| default { $type = 'Unknown' } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| $scriptPath = Resolve-Path -Relative $r.ScriptPath | ||
| $color = switch ($r.Severity) { | ||
| 'Error' { 'Red' } | ||
| 'Warning' { 'Yellow' } | ||
| 'Information' { 'White' } | ||
| default { 'White' } | ||
| } | ||
| Write-Host -f $color " [!] $($r.Severity): $($r.Message)" | ||
| Write-Host -f $color " $($r.RuleName) in $type`: $($scriptPath):$($r.Line)" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'PowerShell code linting' { | ||
| BeforeAll { | ||
| $records = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\..\" ` | ||
| -Recurse -Settings $lintSettings | ||
| } | ||
| It 'Code should be linted' { | ||
| $records.Count | Should -Be 0 | ||
| } | ||
| AfterAll { | ||
| if ($records) { | ||
| foreach ($r in $records) { | ||
| $type = 'Unknown' | ||
| switch -wildCard ($r.ScriptName) { | ||
| '*.psm1' { $type = 'Module' } | ||
| '*.ps1' { $type = 'Script' } | ||
| '*.psd1' { $type = 'Manifest' } | ||
| default { $type = 'Unknown' } | ||
| } | ||
| $scriptPath = Resolve-Path -Relative $r.ScriptPath | ||
| $color = switch ($r.Severity) { | ||
| 'Error' { 'Red' } | ||
| 'Warning' { 'Yellow' } | ||
| 'Information' { 'White' } | ||
| default { 'White' } | ||
| } | ||
| Write-Host -f $color " [!] $($r.Severity): $($r.Message)" | ||
| Write-Host -f $color " $($r.RuleName) in $type`: $($scriptPath):$($r.Line)" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #Requires -Version 5.1 | ||
| #Requires -Modules Pester | ||
| #Requires -Modules PSScriptAnalyzer | ||
|
|
||
| $pesterConfig = New-PesterConfiguration -Hashtable @{ | ||
| Run = @{ | ||
| Path = "$PSScriptRoot" | ||
| PassThru = $true | ||
| } | ||
| Should = @{ | ||
| # Continue running tests even if some assertions fail. This allows | ||
| # Pester to collect and report all failures at the end of a test | ||
| # instead of stopping at the first failing assertion. | ||
| ErrorAction = 'Continue' | ||
| } | ||
| Output = @{ | ||
| StackTraceVerbosity = 'None' | ||
| Verbosity = 'Detailed' | ||
| } | ||
| } | ||
| $result = Invoke-Pester -Configuration $pesterConfig | ||
| exit $result.FailedCount |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.