Add test coverage for Command discovery and requires (#2285)
This commit is contained in:
parent
5d2f52e4e9
commit
a27b2303bb
2 changed files with 133 additions and 0 deletions
28
test/powershell/Language/Scripting/Requires.Tests.ps1
Normal file
28
test/powershell/Language/Scripting/Requires.Tests.ps1
Normal file
|
@ -0,0 +1,28 @@
|
|||
Describe "Requires tests" -Tags "CI" {
|
||||
Context "Parser error" {
|
||||
|
||||
$testcases = @(
|
||||
@{command = "#requiresappID`r`n$foo = 1; $foo" ; testname = "appId with newline"}
|
||||
@{command = "#requires -version A `r`n$foo = 1; $foo" ; testname = "version as character"}
|
||||
@{command = "#requires -version 2b `r`n$foo = 1; $foo" ; testname = "alphanumeric version"}
|
||||
@{command = "#requires -version 1. `r`n$foo = 1; $foo" ; testname = "version with dot"}
|
||||
@{command = "#requires -version '' `r`n$foo = 1; $foo" ; testname = "empty version"}
|
||||
@{command = "#requires -version 1.0. `r`n$foo = 1; $foo" ; testname = "version with two dots"}
|
||||
@{command = "#requires -version 1.A `r`n$foo = 1; $foo" ; testname = "alphanumeric version with dots"}
|
||||
)
|
||||
|
||||
It "throws ParserException - <testname>" -TestCases $testcases {
|
||||
param($command)
|
||||
try
|
||||
{
|
||||
[scriptblock]::Create($command)
|
||||
throw "'$command' should have thrown ParserError"
|
||||
}
|
||||
catch
|
||||
{
|
||||
$_.FullyQualifiedErrorId | Should Be "ParseException"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
105
test/powershell/enginecore/CommandDiscovery.Tests.ps1
Normal file
105
test/powershell/enginecore/CommandDiscovery.Tests.ps1
Normal file
|
@ -0,0 +1,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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue