Summary
This chapter explored the complex topic of testing in PowerShell.
Static analysis is one part of testing and is the approach used by modules like PSScriptAnalyzer
. Static analysis makes use of the Abstract Syntax Tree and tokenizers in PowerShell.
The Abstract Syntax Tree or AST describes the content of a block of code as a tree of different elements, starting with a ScriptBlockAst
at the highest level. You can use the ParseInput
and ParseFile
methods of the Parser
type to get either an instance of the AST for a piece of code or the tokens that make up a script that includes comments.
The ShowPSAst
module can be used to visualize and explore the AST tree. ShowPSAst
is a useful tool when starting to work with AST as the tree can quickly become complex.
PSScriptAnalyzer
uses either AST or tokens to define rules. Rules can be used to test and enforce personal or organization-specific practices.
Pester is a testing framework and this chapter explored both...