diff --git a/test/powershell/Language/Scripting/Requires.Tests.ps1 b/test/powershell/Language/Scripting/Requires.Tests.ps1 new file mode 100644 index 000000000..19557602d --- /dev/null +++ b/test/powershell/Language/Scripting/Requires.Tests.ps1 @@ -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 - " -TestCases $testcases { + param($command) + try + { + [scriptblock]::Create($command) + throw "'$command' should have thrown ParserError" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "ParseException" + } + } + } + +} \ No newline at end of file diff --git a/test/powershell/enginecore/CommandDiscovery.Tests.ps1 b/test/powershell/enginecore/CommandDiscovery.Tests.ps1 new file mode 100644 index 000000000..a765120e7 --- /dev/null +++ b/test/powershell/enginecore/CommandDiscovery.Tests.ps1 @@ -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 "" -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 + } +} \ No newline at end of file