-
Notifications
You must be signed in to change notification settings - Fork 4
/
add-beforeall.ps1
120 lines (96 loc) · 3.32 KB
/
add-beforeall.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting
# all code into Pester controlled blocks.
# DO this:
# BeforeAll {
# . $PSScriptRoot/Code.ps1
# }
# DO this:
# BeforeAll {
# . $PSCommandPath.Replace('.Tests.', '')
# }
# DON'T do this:
# . $PSScriptRoot/Code.ps1
# DON'T do this:
# . $here/$sut
# DON'T do this:
# BeforeAll {
# . ($MyInvocation.MyCommand.Path | Split-Path)/Code.ps1
# }
# 🔥 it is highly encouraged to backup your solution first, or use git.
param (
[Parameter(Mandatory=$false)]
# Path to be recursively searched for *.Tests.ps1
[String[]] $Path = ".",
# Excluded paths using -like wildcards
[String[]] $Exclude = @(),
[String] $Filter = "*.Tests.ps1",
[String] $Margin = " " * 4,
[String] $Encoding = "UTF8"
)
$files = Get-ChildItem $Path -Recurse -Filter *.Tests.ps1 |
where { $fullName = $_.FullName; -not ($Exclude | where { $_ -like $fullName })}
foreach ($f in $files) {
$fullName = $f.FullName
$beforeAllFound = $false
$describeFound = $false
$setupFound = $false
$lines = (Get-Content $fullName -Encoding UTF8)
$i = 0
foreach ($line in $lines) {
if ($line -match "BeforeAll\s*{") {
$beforeAllFound = $true
break
}
if ($line -match "^\s*(Describe|Context)\s*[-""'{]") {
$describeFound = $true
break
}
if (-not [string]::IsNullOrWhiteSpace($line) -and $line -notmatch "^\s*#") {
$setupFound = $true
}
$i++
}
if ($beforeAllFound) {
Write-Verbose "Found BeforeAll at the start of file, skipping. '$fullName'"
continue
}
if (-not $describeFound -or -not $setupFound) {
Write-Verbose "Found Describe or Context but no code at the start of file, skipping. '$fullName'"
continue
}
Write-Verbose "There are $i lines of setup before Describe or Context. '$fullName'"
$setupLines = $lines[0..($i - 1)]
for ($c = 0; $c -lt $setupLines.Length; $c++) {
if (-not [string]::IsNullOrWhiteSpace($setupLines[$c]) -and $setupLines[$c] -notmatch "^\s*#") {
break
}
Write-Verbose "'$($setupLines[$c])' is a comment or empty line. '$fullName'"
}
$start = 0
$commentLines = @()
if ($c -gt 0) {
$commentLines = @($setupLines[0..($c-1)])
$start = $c
}
$setupLines = @($lines[$start..($i - 1)])
for ($e = $setupLines.Length - 1; $e -ge 0; $e--) {
if (-not [string]::IsNullOrWhiteSpace($setupLines[$e])) {
break
}
}
$setupLinesWithoutEmptyEnd = $setupLines[0..$e]
$formatted = foreach ($line in $setupLinesWithoutEmptyEnd) {
if ($line -like '"@*' -or $line -like "'@*") {
# don't move here-strings terminators
$line
}
elseif (-not [string]::IsNullOrWhiteSpace($line)) {
"$Margin$line"
}
else {
""
}
}
$linesWithBeforeAll = $($commentLines) + @("BeforeAll {") + @($formatted) + @("}", "") + @($lines[$i..($lines.Length - 1)])
$linesWithBeforeAll -join [Environment]::NewLine | Set-Content $f.FullName -Encoding UTF8 -NoNewLine
}