Created
March 13, 2020 11:58
-
-
Save JPRuskin/40fc5a9dcf65edd9c9ee7d8c36d83438 to your computer and use it in GitHub Desktop.
Gets a list of changed files in a given repository directory and runs tests that match them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-ModifiedTests { | |
<# | |
.Synopsis | |
Gets a list of changed files in a repository, and run every changed test and test matching a changed function/file | |
.Description | |
This assumes that each file has a matching .Tests.ps1 file, suited for running with Pester. It also ignores Integration tagged tests. | |
.Example | |
Invoke-ModifiedTests -Repository C:\Source\Module | |
# Runs modified tests and modified-function tests within C:\Source\Module | |
#> | |
param( | |
# Path to the repository to test | |
[ValidateScript({ | |
if (-not (Test-Path (Join-Path $_ '.git') -Type Container)) { | |
throw "Directory should be a Git repository" | |
} | |
$true | |
})] | |
[Alias('PSPath')] | |
[string]$Repository, | |
# If provided, will not search for tests matching non-test files | |
[switch]$IgnoreChangedFunctions | |
) | |
process { | |
Push-Location $Repository | |
$ChangedFiles = git diff master... --name-only | Convert-Path | |
$ChangedTests = $ChangedFiles.Where{$_.EndsWith('.Tests.ps1')} | |
$ChangedFunctions = $ChangedFiles.Where{$_ -notmatch ".Tests.ps1$" -and $_.EndsWith('.ps1')} | |
Write-Host "$($ChangedTests.Count) tests have been modified. $($ChangedFunctions.Count) have been modified." | |
if (-not $IgnoreChangedFunctions) { | |
foreach ($Function in $ChangedFunctions) { | |
$TestFound = $false | |
$ChangedTests += (Get-ChildItem -Filter "$(([IO.FileInfo]$Function).BaseName).Tests.ps1" -Recurse -OutVariable TestFound).FullName | |
if (-not $TestFound) { | |
Write-Warning "No tests found for $($Function.Name)!" | |
} | |
} | |
} | |
Invoke-Pester -Script $ChangedTests -ExcludeTag Integration | |
Pop-Location | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment