-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIsePester.psm1
67 lines (48 loc) · 1.87 KB
/
IsePester.psm1
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
function Add-PesterMenu {
Remove-PesterMenu
[void]$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("_Pester", {IsePester\Invoke-IsePester}, "CTRL+F5")
}
function Get-PesterMenu {
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus |
Where {$_.DisplayName -Match "pester"}
}
function Remove-PesterMenu {
$menu = Get-PesterMenu
if($menu) {
[void]$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Remove($menu)
}
}
function Invoke-IsePester
{
if(!$psISE.CurrentFile.IsUntitled) {
$psISE.CurrentFile.Save()
$fileName = $psISE.CurrentFile.FullPath
if($filename -notmatch '\.tests\.') {
$parts = $filename -split '\.'
$testFilename = "{0}.tests.ps1" -f ($parts[0..($parts.Count-2)] -join '.')
Write-debug $testFilename
if(Test-Path $testFilename) {
$filename = $testFilename
}else{
# Look for tests in a tests subfolder
$folder = join-path -path (split-path $fileName -Parent) -ChildPath Tests
$parts = (split-path $fileName -Leaf) -split '\.'
$testfile = "$($parts[0]).tests.$($parts[1])"
$testFileName = Join-Path -path $folder -ChildPath $testFile
# Check file exists
if(Test-Path $testFileName) {
$filename = $testFileName
}
}
Write-debug $filename
}
} else {
$fileName = [io.path]::GetTempFileName().Replace(".tmp", ".tests.ps1")
$psISE.CurrentFile.Editor.Text | Set-Content -Path $fileName
}
Import-Module Pester
Write-Debug $fileName
Invoke-Pester -relative_path $fileName
if(!$psISE.CurrentFile.IsSaved) { Remove-Item $fileName -Force -ErrorAction SilentlyContinue }
}
Add-PesterMenu