-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpre-commit.ps1
37 lines (32 loc) · 946 Bytes
/
pre-commit.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
[CmdletBinding()]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]
$TestNamesRegex = "null"
)
$TAG = "main"
docker run --rm -v ${pwd}:/lint -w /lint ghcr.io/antonbabenko/pre-commit-terraform:$TAG run -a
$tests = Get-ChildItem -Recurse -Filter '*.tftest.hcl' -File | Sort-Object
if ($TestNamesRegex) {
$tests = $tests | Where-Object { $_.Name -match $TestNamesRegex }
}
if (!$tests) {
Write-Output "No tests requested"
exit 0
}
Write-Output "Running tests: " $($tests | ForEach-Object { $_.Name })
$startingLocation = Get-Location
$tests | ForEach-Object -Parallel {
if ($_.DirectoryName -notmatch "tests") {
Push-Location -Path $_.DirectoryName
}
Write-Output "Running test: $($_.FullName)"
terraform init -upgrade
terraform validate -force
terraform test
$currentLocation = Get-Location
if ($currentLocation -ne $startingLocation) {
Pop-Location
}
}