forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.Tests.ps1
More file actions
111 lines (103 loc) · 4.19 KB
/
Copy pathHistory.Tests.ps1
File metadata and controls
111 lines (103 loc) · 4.19 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
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
Describe "History cmdlet test cases" -Tags "CI" {
Context "Simple History Tests" {
BeforeEach {
$setting = [system.management.automation.psinvocationsettings]::New()
$setting.AddToHistory = $true
$ps = [PowerShell]::Create("NewRunspace")
# we need to be sure that history is added, so use the proper
# Invoke variant
$null = $ps.addcommand("Get-Date").Invoke($null,$setting)
$ps.commands.clear()
$null = $ps.addscript("1+1").Invoke($null,$setting)
$ps.commands.clear()
$null = $ps.addcommand("Get-Location").Invoke($null,$setting)
$ps.commands.clear()
}
AfterEach {
$ps.Dispose()
}
It "Get-History returns proper history" {
# for this case, we'll *not* add to history
$result = $ps.AddCommand("Get-History").Invoke()
$result.Count | should be 3
$result[0].CommandLine | should be "Get-Date"
$result[1].CommandLine | should be "1+1"
$result[2].CommandLine | should be "Get-Location"
}
It "Invoke-History invokes proper command" {
$result = $ps.AddScript("Invoke-History 2").Invoke()
$result | Should be 2
}
It "Clear-History removes history" {
$ps.AddCommand("Clear-History").Invoke()
$ps.commands.clear()
$result = $ps.AddCommand("Get-History").Invoke()
$result | should BeNullOrEmpty
}
It "Add-History actually adds to history" {
# add this invocation to history
$ps.AddScript("Get-History|Add-History").Invoke($null,$setting)
# that's 4 history lines * 2
$ps.Commands.Clear()
$result = $ps.AddCommand("Get-History").Invoke()
$result.Count | Should be 8
for($i = 0; $i -lt 4; $i++) {
$result[$i+4].CommandLine | Should be $result[$i].CommandLine
}
}
}
It "Tests Invoke-History on a cmdlet that generates output on all streams" {
$streamSpammer = '
function StreamSpammer
{
[CmdletBinding()]
param()
Write-Debug "Debug"
Write-Error "Error"
Write-Information "Information"
Write-Progress "Progress"
Write-Verbose "Verbose"
Write-Warning "Warning"
"Output"
}
$informationPreference = "Continue"
$debugPreference = "Continue"
$verbosePreference = "Continue"
'
$invocationSettings = New-Object System.Management.Automation.PSInvocationSettings
$invocationSettings.AddToHistory = $true
$ps = [PowerShell]::Create()
$null = $ps.AddScript($streamSpammer).Invoke()
$ps.Commands.Clear()
$null = $ps.AddScript("StreamSpammer");
$null = $ps.Invoke($null, $invocationSettings)
$ps.Commands.Clear()
$null = $ps.AddScript("Invoke-History -id 1")
$result = $ps.Invoke($null, $invocationSettings)
$outputCount = $(
$ps.Streams.Error;
$ps.Streams.Progress;
$ps.Streams.Verbose;
$ps.Streams.Debug;
$ps.Streams.Warning;
$ps.Streams.Information).Count
$ps.Dispose()
## Twice per stream - once for the original invocation, and once for the re-invocation
$outputCount | Should be 12
}
It "Tests Invoke-History on a private command" {
$invocationSettings = New-Object System.Management.Automation.PSInvocationSettings
$invocationSettings.AddToHistory = $true
$ps = [PowerShell]::Create()
$null = $ps.AddScript("(Get-Command Get-Process).Visibility = 'Private'").Invoke()
$ps.Commands.Clear()
$null = $ps.AddScript("Get-Process -id $pid")
$null = $ps.Invoke($null, $invocationSettings)
$ps.Commands.Clear()
$null = $ps.AddScript("Invoke-History -id 1")
$result = $ps.Invoke($null, $invocationSettings)
$errorResult = $ps.Streams.Error[0].FullyQualifiedErrorId
$ps.Dispose()
$errorResult | Should be CommandNotFoundException
}
}