-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathCommand.Type.Tests.ps1
More file actions
57 lines (49 loc) · 2.45 KB
/
Copy pathCommand.Type.Tests.ps1
File metadata and controls
57 lines (49 loc) · 2.45 KB
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
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }
BeforeAll {
if (-not $env:BHProjectPath) {
& "$PSScriptRoot\..\build.ps1" -Task 'Build'
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force
Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force
$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/Command.ps1'
}
Describe 'Command script' {
It 'Executes the Source string as PowerShell in the current session' {
$flagPath = Join-Path 'TestDrive:' 'flag.txt'
$dep = New-PSDependFixture -DependencyName 'CmdOne' -DependencyType 'Command' -Source "Set-Content -Path '$flagPath' -Value 'ran'"
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
(Get-Content $flagPath) | Should -Be 'ran'
}
It 'Iterates multiple Source entries' {
$countPath = Join-Path 'TestDrive:' 'count.txt'
$dep = New-PSDependFixture -DependencyName 'CmdMulti' -DependencyType 'Command' -Source @(
"Add-Content '$countPath' 'a'"
"Add-Content '$countPath' 'b'"
)
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
(Get-Content $countPath) | Should -Be @('a', 'b')
}
It 'Writes a non-terminating error and continues by default when the Source errors' {
$dep = New-PSDependFixture -DependencyName 'CmdSwallow' -DependencyType 'Command' -Source "throw 'boom'"
$err = $null
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath; ErrRef = [ref]$err } {
& $ScriptPath -Dependency $Dep -ErrorAction SilentlyContinue -ErrorVariable scriptErr
$ErrRef.Value = $scriptErr
}
$err | Should -Not -BeNullOrEmpty
($err | Out-String) | Should -Match 'boom'
}
It 'Throws a terminating error when -FailOnError is specified' {
$dep = New-PSDependFixture -DependencyName 'CmdFail' -DependencyType 'Command' -Source "throw 'boom'"
{
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -FailOnError
}
} | Should -Throw -ExpectedMessage '*boom*'
}
}