Modules and Tests
Package a tool as a module, check its style, and prove it works.
Updated
Modules
- Your Own Module (Journeyman): A module is a folder of functions that PowerShell loads by name: the functions go in a .psm1 file, Export-ModuleMember chooses which ones others can call, New-ModuleManifest writes a .psd1 that gives the module a version and a description, and a folder on $env:PSModulePath lets PowerShell find it from anywhere.
Quality
- PSScriptAnalyzer: A Second Pair of Eyes (Journeyman): Invoke-ScriptAnalyzer reads a script or a whole module without running it and lists every likely problem with its rule, severity, line and message; the rules worth knowing ask for full command names, approved verbs, singular nouns, -WhatIf on commands that change things, results instead of Write-Host and no unused variables, and a settings file or SuppressMessageAttribute sets one aside on purpose.
- Testing with Pester (Freelancer): A Pester test file, named something.Tests.ps1, groups checks in Describe blocks, each It block runs your code and checks one answer with Should, and Invoke-Pester runs them all in seconds, marks each one passed or failed and says what it expected and what it got; TestDrive: gives every test a throwaway folder for its files.
Exercise: Test Your Tool
- Exercise: Test Your Tool (Freelancer): Give the RecipeBox module a manifest with New-ModuleManifest, then write five Pester tests that work on a copy of the recipes in TestDrive:, and a check proves they're good enough by breaking RecipeBox five ways and expecting a test to fail each time.