/
Remove-FoldersRecursively.Tests.ps1
109 lines (88 loc) · 2.71 KB
/
Remove-FoldersRecursively.Tests.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
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".").Replace(".ps1","")
$baseModulePath = "$here\..\tools\powershell\modules"
$module = Get-Module $sut
if ($module -ne $null)
{
Remove-Module $sut
}
Describe "Remove-FoldersRecursively_1" {
Context "When module is invoked with an empty deleteIncludePaths parameter" {
Import-Module "$baseModulePath\$sut"
#Calling script is the root of the OneBuild folder, where Invoke-Build invokes pester
$testBasePath = Join-Path "$here" "\.." -Resolve
$result = ""
try {
Remove-FoldersRecursively -deleteIncludePaths @()
}
#catch [System.Management.Automation.ParameterBindingValidationException] {
catch {
$result = "$_.Exception.Message"
}
finally {
Remove-Module $sut
}
It "Should throw a terminating exception" {
$result | Should Match "Cannot validate argument on parameter 'deleteIncludePaths'"
}
}
}
Describe "Remove-FoldersRecursively_2" {
Context "When module is invoked with a path that does NOT exist" {
Import-Module "$baseModulePath\$sut"
Mock -ModuleName $sut Confirm-Path { return 1 }
$testBasePath = "$TestDrive\NonExistentPath\"
$result = ""
try {
$result = Remove-FoldersRecursively -path $testBasePath -deleteIncludePaths @("bin")
}
catch {
throw
}
finally {
Remove-Module $sut
}
It "Should exit the module with code 1" {
$result | Should Be 1
}
}
}
Describe "Remove-FoldersRecursively_3" {
Context "When a valid basePath includes paths to delete" {
Import-Module "$baseModulePath\$sut"
#Here we get the TestDrive using pesters $TestDrive variable which holds the full file system location to the temporary PSDrive.
New-Item -Name "bin" -Path $TestDrive -ItemType Directory
$testBasePath = "$TestDrive"
Mock -ModuleName $sut Write-Host {} -Verifiable -ParameterFilter {
$Object -eq "Searching for paths to delete, recursively from: $testBasePath"
}
Mock -ModuleName $sut Write-Host {} -Verifiable -ParameterFilter {
$Object -eq "Cleaning: $testBasePath\bin"
}
$result = ""
try {
$result = Remove-FoldersRecursively -path $testBasePath -deleteIncludePaths @("bin")
}
catch {
throw
}
finally {
Remove-Module $sut
}
It "Should search the basePath recursively for paths to delete" {
Assert-VerifiableMocks
}
It "Should delete all matching paths" {
$pathExists = Test-Path ("$TestDrive\bin")
$pathExists | Should Be $False
}
It "Should exit the module with code 0" {
$result | Should Be 0
}
}
}
$module = Get-Module $sut
if ($module -ne $null)
{
Remove-Module $sut
}