Psscriptanalyzer: Add Parameter to include/exclude certain files/folders when using -Path parameter set

Created on 8 Jun 2016  路  10Comments  路  Source: PowerShell/PSScriptAnalyzer

Is there a way to ignore any ps1 file that happens to be a Pester test script? It is not uncommon to having to break a few rules when constructing tests, especially when mocking.

Issue - Discussion

Most helpful comment

I'm having the same problem, especially with the PowerShell Extension (but it's not a PS Extension problem).

I'd like to exclude some folders and files, that might be present in my workspace, but I don't want them to be analyzed.

"As a vscode extension user, I need a way in the Analyzer Settings file to exclude some folders, such as my "output/RequiredModules" folder, my "tests" folder as requested by @jdhitsolutions or anything that might be present in my folder without being part of the source of the current project".

Although it should not be automatic, you can think of most things in .gitignore to fall into that category, but it's for the user to chose and configure per-repo.

/cc @bergmeister as we discussed that on slack
Please rename this issue as it's not just pester tests but exclude any file and folder in PSSA config.
I know the vscode extension may be using -ScriptDefinition and not -Script, but having an exclude in settings would open the door to the same behaviour within and outside the vscode PowerShell extension.

All 10 comments

Could the script analyzer simply look for a comment like #NoAnalyze ? Or it should be simple enough to detect Pester commands like Describe and ignore the file.

cc @joeyaiello

Right now, Invoke-ScriptAnalyzer by itself doesn't have such a capability. However, a workaround would be to filter out the *.tests.ps1 files and pipe the remaining files to Invoke-ScriptAnalyzer. For example

Get-ChildItem -Path <targetPath> -Exclude "*.tests.ps1" | Invoke-ScriptAnalyzer

Having said that, I think this would be a good feature to have in the settings file.

And it isn't merely Pester tests. I often use a scratch.ps1 file as a temporary dumping ground or a place to hold snippets of code I'm working on. It is a bit of a nuisance to have that file constantly checked for rules.

What about having a -ExcludeTestFiles parameter?

Except with a better name.

Why should your tests be held to lower standards than the rest of your code?

because they are tests, what kind of question is that, why don't you write tests for tests? why should you have lower standards for test than the rest of your code?

Good point @RobFaie, only issue is that some times Pester syntax can trigger false positives.

I'm having the same problem, especially with the PowerShell Extension (but it's not a PS Extension problem).

I'd like to exclude some folders and files, that might be present in my workspace, but I don't want them to be analyzed.

"As a vscode extension user, I need a way in the Analyzer Settings file to exclude some folders, such as my "output/RequiredModules" folder, my "tests" folder as requested by @jdhitsolutions or anything that might be present in my folder without being part of the source of the current project".

Although it should not be automatic, you can think of most things in .gitignore to fall into that category, but it's for the user to chose and configure per-repo.

/cc @bergmeister as we discussed that on slack
Please rename this issue as it's not just pester tests but exclude any file and folder in PSSA config.
I know the vscode extension may be using -ScriptDefinition and not -Script, but having an exclude in settings would open the door to the same behaviour within and outside the vscode PowerShell extension.

@gaelcolas For settings support, there is issue #1230
The way the extension works, I'd still say it is something to be implemented in the extension for your use case. Because people edit their code without always saving the file, the extension needs to use the -ScriptDefinition parameter set to analyse the current in memory text. Do you agree @TylerLeonhardt ?
This and the other issue is for cmdlet/setting files options, which is a different use case.

Yeah. The extension would need an exclusion list. Feel free to open an issue in https://github.com/PowerShell/vscode-powershell

Was this page helpful?
0 / 5 - 0 ratings