-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Pending] Finally we add the code linting and its tests! #2108
Conversation
What’s the reason it puts $null first in comparisons ( |
@rasa PSScriptAnalyzer RuleDocumentation: PSPossibleIncorrectComparisonWithNull |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 👍
I have issues with a couple of these. The rest are ok I guess, although personally I'd allow PSUseBOMForUnicodeEncodedFileIs there a specific reason for this? BOMs are "neither required not recommended [for utf8]" by the Unicode standard (section 2.6) and create more problems than they solve in my experience, expecially with cross-platform tools. PowerShell will also be removing the BOM from the default UTF-8 encoding. If we could instead ensure that say, all files were utf8 encoded (without BOM), that would be good imo. PSPossibleIncorrectComparisonWithNullI don't see the usefulness of this. The first reason given is that it can produce an unexpected result when comparing The second reason given: PowerShell will perform type casting left to right, resulting in incorrect comparisons when $null is cast to other scalar types. |
Cool! 😎 This variant shows which file violates a rule: $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$scoop_modules = Get-ChildItem $repo_dir -Filter "*.ps1" -Recurse | Select-Object -ExpandProperty Directory -Unique
$linting_settings = Get-Item -Path "$repo_dir\PSScriptAnalyzerSettings.psd1"
Describe "Linting all modules" {
$rules = Get-ScriptAnalyzerRule
foreach($directory in $scoop_modules) {
Context "Linting $directory\*.ps1" {
foreach($rule in $rules) {
It "Should not return any violation for the rule: $($rule.RuleName)" {
$result = Invoke-ScriptAnalyzer -Path $directory.FullName -Settings $linting_settings.FullName -IncludeRule $rule.RuleName
if($result) {
Write-Warning ($result | Out-String)
}
$result.Count | Should Be 0
}
}
}
}
} Two interesting articles: |
|
@r15ch13 Current implementation does show which file violates a rule if it violates a rule. e.g. Context Linting scoop-which.ps1
[-] Passes PSScriptAnalyzer 68ms
Expected 0, but got 1.
9: (Invoke-ScriptAnalyzer $module.FullName -Settings $linting_settings.FullName).count | Should Be 0
at Invoke-LegacyAssertion, C:\Users\<omit>\Documents\WindowsPowerShell\Modules\Pester\4.3.1\Functions\Assertions\Should.ps1: line 188
at <ScriptBlock>, C:\Users\<omit>\workspace\scoop\test\Scoop-Linting.Tests.ps1: line 9 |
test/Scoop-Linting.Tests.ps1
Outdated
@@ -0,0 +1,13 @@ | |||
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName | |||
$scoop_modules = Get-ChildItem $repo_dir -Filter "*.ps1" -Recurse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to consider using a "*.ps*1
filter to also include .psm1
and .psd1
files (there are even specific rules just for manifests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for pointing it out that.
I'm glad you find the tool useful. Here are some remarks:
$a=@()
if ($a -eq $null){"True"}else{"false"}
if ($a -ne $null){"True"}else{"false"} We expect a new release in the next weeks with a ton of improvements and features. With that come also 2 new rules about assignment to automatic variables and possible incorrect/unintentional usage of assignment operators. |
@h404bi yes it shows the file, but it doesn't show which violation at which line. I think it's important to just take a look at the CI result and know what has to be fixed. Another approach based on @bergmeister's example file $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$scoop_modules = Get-ChildItem $repo_dir -Filter "*.ps*1" -Recurse | Select-Object -ExpandProperty Directory -Unique
$linting_settings = Get-Item -Path "$repo_dir\PSScriptAnalyzerSettings.psd1"
$rules = Get-ScriptAnalyzerRule
Describe "Linting all modules" {
foreach($directory in $scoop_modules) {
Context "Linting $directory\*.ps1" {
$analysis = Invoke-ScriptAnalyzer -Path $directory.FullName -Settings $linting_settings.FullName
foreach($rule in $rules) {
It "Should pass: $rule" {
If ($analysis.RuleName -contains $rule) {
$analysis | Where-Object RuleName -eq $rule -outvariable failures
Write-Warning ($failures | Out-String)
$failures.Count | Should Be 0
}
}
}
}
}
} |
@r15ch13 minor tweaks as follow: $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
$scoop_modules = Get-ChildItem $repo_dir -Filter "*.ps*1" -Recurse | Select-Object -ExpandProperty Directory -Unique
$linting_settings = Get-Item -Path "$repo_dir\PSScriptAnalyzerSettings.psd1"
$rules = Get-ScriptAnalyzerRule
Describe "Linting all *.ps*1 modules" {
foreach($directory in $scoop_modules) {
Context "Linting *.ps*1 modules in '$directory'" {
$analysis = Invoke-ScriptAnalyzer -Path $directory.FullName -Settings $linting_settings.FullName
foreach($rule in $rules) {
It "Should pass: $rule" {
If ($analysis.RuleName -contains $rule) {
$analysis | Where-Object RuleName -eq $rule -outvariable failures
Write-Warning ($failures | Out-String)
$failures.Count | Should Be 0
}
}
}
}
}
} But I want to point it out that is it necessary to log out all rules into testresults? It looks like a spam, what I think is that if it passes, no mess logs, if failed, output the error stack. 😂 Would commit this tweak if approved. |
@h404bi Tests should show detailed information and AppVeyor doesn't care about spam 😁 But I worked a bit more on it. Now with less spam and better output (showing clickable file path with line number) $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
Describe "PSScriptAnalyzer" {
$scoop_modules = Get-ChildItem $repo_dir -Recurse -Include *.psd1, *.psm1, *.ps1
$scoop_modules = $scoop_modules | Where-Object { $_.DirectoryName -notlike '*\supporting*' -and $_.DirectoryName -notlike '*\test*' }
$scoop_modules = $scoop_modules | Select-Object -ExpandProperty Directory -Unique
Context "Checking ScriptAnalyzer" {
It "Invoke-ScriptAnalyzer Cmdlet should exist" {
{ Get-Command Invoke-ScriptAnalyzer -ErrorAction Stop } | Should Not Throw
}
It "PSScriptAnalyzerSettings.ps1 should exist" {
Test-Path "$repo_dir\PSScriptAnalyzerSettings.psd1" | Should Be $true
}
It "There should be files to test" {
$scoop_modules.Count | Should Not Be 0
}
}
$linting_settings = Get-Item -Path "$repo_dir\PSScriptAnalyzerSettings.psd1"
Context "Linting all *.psd1, *.psm1 and *.ps1 files" {
foreach($directory in $scoop_modules) {
$analysis = Invoke-ScriptAnalyzer -Path $directory.FullName -Settings $linting_settings.FullName
It "Should pass: $directory" {
$analysis.Count | Should Be 0
}
if($analysis) {
foreach($result in $analysis) {
switch -wildCard ($result.ScriptName) {
'*.psm1' { $type = 'Module' }
'*.ps1' { $type = 'Script' }
'*.psd1' { $type = 'Manifest' }
}
Write-Host -f Yellow " [*] $($result.Severity): $($result.Message)"
Write-Host -f Yellow " $($result.RuleName) in $type`: $directory\$($result.ScriptName):$($result.Line)"
}
}
}
}
} |
@r15ch13 Cool, that looks nice, I updated the test with your variant. |
What does this big pull-request do: we use PSScriptAnalyzer for code linting.
*.ps1
files to pass PSScriptAnalyzer check. notes: some rules have been excluded for special reason, you can take a look atPSScriptAnalyzerSettings.psd1
.TestResults.xml
.There are some opinionated changes, so this pull-request is pending and looking for reviews.