|
| 1 | +function Get-TestInstanceUsage { |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Reports which $TestConfig instances each test file uses. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + tests\pester.groups.ps1 assigns a test file to a CI scenario by autodetecting which |
| 8 | + $TestConfig.Instance* variable the file references, so this is what decides which lane a test |
| 9 | + runs in - and the lanes are not equally available. Only InstanceSingle, InstanceMulti1 and |
| 10 | + InstanceMulti2 exist on GitHub Actions; a test written against InstanceCopy, InstanceHadr or |
| 11 | + InstanceRestart runs on the Azure runners only. |
| 12 | +
|
| 13 | + Use it to see which lane a change lands in before writing the test, or to find every test that |
| 14 | + a given instance would affect. |
| 15 | +
|
| 16 | + Commented out code is ignored, so a reference left behind in a comment does not move a file |
| 17 | + into a scenario it does not belong to. |
| 18 | +
|
| 19 | +.PARAMETER Path |
| 20 | + The folder holding the test files. Defaults to the tests folder of this working copy. |
| 21 | +
|
| 22 | +.PARAMETER Command |
| 23 | + Only report the test files of these commands. Wildcards are supported. Defaults to all of them. |
| 24 | +
|
| 25 | +.EXAMPLE |
| 26 | + Get-TestInstanceUsage |
| 27 | +
|
| 28 | + Reports the instances used by every test file. |
| 29 | +
|
| 30 | +.EXAMPLE |
| 31 | + Get-TestInstanceUsage -Command Get-DbaDb* |
| 32 | +
|
| 33 | + Reports the instances used by the test files of every command starting with Get-DbaDb. |
| 34 | +
|
| 35 | +.EXAMPLE |
| 36 | + Get-TestInstanceUsage | Group-Object -Property InstanceList -NoElement | Sort-Object -Property Count -Descending |
| 37 | +
|
| 38 | + Shows how many test files use each combination of instances. |
| 39 | +
|
| 40 | +.EXAMPLE |
| 41 | + Get-TestInstanceUsage | Where-Object Instances -contains "Hadr" |
| 42 | +
|
| 43 | + Lists every test that needs the availability group instance, which only the Azure runners have. |
| 44 | +#> |
| 45 | + [CmdletBinding()] |
| 46 | + param ( |
| 47 | + [string]$Path = (Join-Path (Split-Path -Path $PSScriptRoot -Parent | Split-Path -Parent) "tests"), |
| 48 | + [string[]]$Command = "*" |
| 49 | + ) |
| 50 | + |
| 51 | + foreach ($commandName in $Command) { |
| 52 | + $testFiles = Get-ChildItem -Path "$Path\$commandName.Tests.ps1" -ErrorAction SilentlyContinue | Sort-Object -Property Name |
| 53 | + if (-not $testFiles) { |
| 54 | + Write-Warning -Message "No test file found for [$commandName]" |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + foreach ($testFile in $testFiles) { |
| 59 | + $content = Get-Content -Path $testFile.FullName |
| 60 | + # This matches the current names ($TestConfig.InstanceSingle, $TestConfig.InstanceMulti1 |
| 61 | + # and so on) as well as the legacy $TestConfig.instance1, because both spellings still |
| 62 | + # appear in the autodetection in pester.groups.ps1. |
| 63 | + $instanceNames = foreach ($line in $content) { |
| 64 | + $code = $line -replace "#.*$", "" |
| 65 | + [regex]::Matches($code, "\`$TestConfig\.Instance(\w+)", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) | |
| 66 | + ForEach-Object { $PSItem.Groups[1].Value } |
| 67 | + } |
| 68 | + $instanceNames = @($instanceNames | Sort-Object -Unique) |
| 69 | + |
| 70 | + [PSCustomObject]@{ |
| 71 | + Command = $testFile.Name -replace "\.Tests\.ps1$", "" |
| 72 | + TestFileName = $testFile.Name |
| 73 | + Instances = $instanceNames |
| 74 | + # A single string as well, so that the result can be grouped on it directly |
| 75 | + InstanceList = $instanceNames -join " " |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments