forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandDiscovery.Tests.ps1
More file actions
105 lines (89 loc) · 3.78 KB
/
Copy pathCommandDiscovery.Tests.ps1
File metadata and controls
105 lines (89 loc) · 3.78 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
Describe "Command Discovery tests" -Tags "CI" {
BeforeAll {
setup -f testscript.ps1 -content "'This script should not run. Running from testscript.ps1'"
setup -f testscripp.ps1 -content "'This script should not run. Running from testscripp.ps1'"
$TestCasesCommandNotFound = @(
@{command = 'CommandThatDoesnotExist' ; testName = 'Non-existent command'}
@{command = 'testscrip?.ps1' ; testName = 'Multiple matches for filename'}
@{command = "demo" + [System.IO.Path]::DirectorySeparatorChar; testName = 'Non existent command with directory separator'}
@{command = [System.IO.Path]::DirectorySeparatorChar; testName = 'Directory separator'}
@{command = 'environment::\path'; testName = 'Provider qualified path'}
)
}
It "<testName>" -TestCases $TestCasesCommandNotFound {
param($command)
try
{
& $command
throw "Should not have found command: '$command'"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'CommandNotFoundException'
}
}
It "Command lookup with duplicate paths" {
$previousPath = $env:PSMODULEPATH
try
{
New-Item -Path "$TestDrive\TestFunctionA" -ItemType Directory
New-Item -Path "$TestDrive\\TestFunctionA\TestFunctionA.psm1" -Value "function TestFunctionA {}" | Out-Null
$env:PSMODULEPATH = "$TestDrive" + [System.IO.Path]::PathSeparator + "$TestDrive"
(Get-command 'TestFunctionA').count | Should Be 1
}
finally
{
$env:PSMODULEPATH = $previousPath
}
}
It "Alias can be set for a cmdlet" {
Set-Alias 'AliasCommandDiscoveryTest' Get-ChildItem
$commands = (Get-Command 'AliasCommandDiscoveryTest')
$commands.Count | Should Be 1
$aliasResult = $commands -as [System.Management.Automation.AliasInfo]
$aliasResult | Should BeOfType [System.Management.Automation.AliasInfo]
$aliasResult.Name | Should Be 'AliasCommandDiscoveryTest'
}
It "Cyclic aliases - direct" {
try
{
Set-Alias CyclicAliasA CyclicAliasB -Force
Set-Alias CyclicAliasB CyclicAliasA -Force
& CyclicAliasA
throw "Execution should not reach here. '& CyclicAliasA' should have thrown."
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'CommandNotFoundException'
}
}
It "Cyclic aliases - indirect" {
try
{
Set-Alias CyclicAliasA CyclicAliasB -Force
Set-Alias CyclicAliasB CyclicAliasC -Force
Set-Alias CyclicAliasC CyclicAliasA -Force
& CyclicAliasA
throw "Execution should not reach here. '& CyclicAliasA' should have thrown."
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'CommandNotFoundException'
}
}
It "Get-Command should return only CmdletInfo, FunctionInfo, AliasInfo or FilterInfo" {
$commands = Get-Command
$commands.Count | Should BeGreaterThan 0
foreach($command in $commands)
{
$command.GetType().Name | should be @("AliasInfo","FunctionInfo","CmdletInfo","FilterInfo")
}
}
It "Non-existent commands with wildcard should not write errors" {
Get-Command "CommandDoesNotExist*" -ErrorVariable ev -ErrorAction SilentlyContinue
$ev | Should BeNullOrEmpty
}
It "Get- is prepended to commands" {
(& 'location').Path | Should Be (get-location).Path
}
}