Tests/ScriptCoverage.Test.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Start out by making en empty directory
New-Item .\TestDirectory -ItemType Directory -Force
        
# Now let's create two scripts. The first script has
# a 50/50 chance of saying "Hello World" or "Goodbye World"
'
if ((0,1 | Get-Random)) {
    "Hello World"
} else {
    "Goodbye World"
}
'
 |
    Out-File .\TestDirectory\Chance.ps1
        
# The second script will say Hello World or Goodbye World depending
# on if it was passed a parameter
'
param ($a)
if ($a) {
    "Hello World"
} else {
    "Goodbye World"
}
'
 > .\TestDirectory\Parameter.ps1

# We pipe the output of Get-ChildItem into Show-ScriptCoverage. This will run
# each of the scripts, and return a property bag containing:
# - The name of the file that is being instrumented
# - The output of the script
# - Any errors encountered running the script
# - A visual representation of what lines in the script were hit
# By Piping it into Select-Object and expanding the coverage property, we'll
# just see what lines were hit
Get-ChildItem .\TestDirectory |
    Show-ScriptCoverage | 
    Select-Object -ExpandProperty Coverage
        
# Cool. Now let's be polite and clean up the files we created
Remove-Item .\TestDirectory -Recurse -Force