Restructure of pester test layout

This restructuring moves tests to a directory name which matches the module
in which the cmdlet resides which should improve the discoverability of a
specific test.
For tests which are not about cmdlets in a module, new directories have been
created to make those tests easier to find as well
This commit is contained in:
James Truher authored and Andrew Schwartzmeyer committed 2016-06-29 12:05:41 -07:00
1 parent ef32e2339e
commit 1e1c4b7dc0
126 files changed
+10 -6

No files matched your search

@@ -0,0 +1,118 @@
Describe "Configuration file locations" {
BeforeAll {
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
$profileName = "Microsoft.PowerShell_profile.ps1"
}
Context "Default configuration file locations" {
BeforeAll {
if ($IsWindows) {
$expectedCache = [IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft", "Windows", "PowerShell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:USERPROFILE, "Documents", "WindowsPowerShell", "Modules")
$expectedProfile = [io.path]::Combine($env:USERPROFILE, "Documents","WindowsPowerShell",$profileName)
$expectedReadline = [IO.Path]::Combine($env:AppData, "Microsoft", "Windows", "PowerShell", "PSReadline", "ConsoleHost_history.txt")
} else {
$expectedCache = [IO.Path]::Combine($env:HOME, ".cache", "powershell", "StartupProfileData-NonInteractive")
$expectedModule = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "Modules")
$expectedProfile = [io.path]::Combine($env:HOME,".config","powershell",$profileName)
$expectedReadline = [IO.Path]::Combine($env:HOME, ".local", "share", "powershell", "PSReadLine", "ConsoleHost_history.txt")
}
if ($env:TRAVIS_OS_NAME -eq "osx") {
$ItArgs = @{ pending = $true }
} else {
$ItArgs = @{}
}
}
BeforeEach {
$original_PSMODULEPATH = $env:PSMODULEPATH
}
AfterEach {
$env:PSMODULEPATH = $original_PSMODULEPATH
}
It @ItArgs "Profile location should be correct" {
& $powershell -noprofile `$PROFILE | Should Be $expectedProfile
}
It @ItArgs "PSMODULEPATH should contain the correct path" {
$env:PSMODULEPATH = ""
$actual = & $powershell -noprofile `$env:PSMODULEPATH
$actual | Should Match ([regex]::Escape($expectedModule))
}
It @ItArgs "PSReadLine history save location should be correct" {
& $powershell -noprofile { (Get-PSReadlineOption).HistorySavePath } | Should Be $expectedReadline
}
# This feature (and thus test) has been disabled because of the AssemblyLoadContext scenario
It "JIT cache should be created correctly" -Skip {
Remove-Item -ErrorAction SilentlyContinue $expectedCache
& $powershell -noprofile { exit }
$expectedCache | Should Exist
}
# The ModuleAnalysisCache cannot be forced to exist, thus we cannot test it
}
Context "XDG Base Directory Specification is supported on Linux" {
BeforeAll {
# Using It @ItArgs, we automatically skip on Windows for all these tests
if ($IsWindows) {
$ItArgs = @{ skip = $true }
} elseif ($env:TRAVIS_OS_NAME -eq "osx") {
$ItArgs = @{ pending = $true }
} else {
$ItArgs = @{}
}
}
BeforeEach {
$original_PSMODULEPATH = $env:PSMODULEPATH
$original_XDG_CONFIG_HOME = $env:XDG_CONFIG_HOME
$original_XDG_CACHE_HOME = $env:XDG_CACHE_HOME
$original_XDG_DATA_HOME = $env:XDG_DATA_HOME
}
AfterEach {
$env:PSMODULEPATH = $original_PSMODULEPATH
$env:XDG_CONFIG_HOME = $original_XDG_CONFIG_HOME
$env:XDG_CACHE_HOME = $original_XDG_CACHE_HOME
$env:XDG_DATA_HOME = $original_XDG_DATA_HOME
}
It @ItArgs "Profile should respect XDG_CONFIG_HOME" {
$env:XDG_CONFIG_HOME = $TestDrive
$expected = [IO.Path]::Combine($TestDrive, "powershell", $profileName)
& $powershell -noprofile `$PROFILE | Should Be $expected
}
It @ItArgs "PSMODULEPATH should respect XDG_DATA_HOME" {
$env:PSMODULEPATH = ""
$env:XDG_DATA_HOME = $TestDrive
$expected = [IO.Path]::Combine($TestDrive, "powershell", "Modules")
$actual = & $powershell -noprofile `$env:PSMODULEPATH
$actual | Should Match $expected
}
It @ItArgs "PSReadLine history should respect XDG_DATA_HOME" {
$env:XDG_DATA_HOME = $TestDrive
$expected = [IO.Path]::Combine($TestDrive, "powershell", "PSReadLine", "ConsoleHost_history.txt")
& $powershell -noprofile { (Get-PSReadlineOption).HistorySavePath } | Should Be $expected
}
# This feature (and thus test) has been disabled because of the AssemblyLoadContext scenario
It -Skip "JIT cache should respect XDG_CACHE_HOME" {
$env:XDG_CACHE_HOME = $TestDrive
$expected = [IO.Path]::Combine($TestDrive, "powershell", "StartupProfileData-NonInteractive")
Remove-Item -ErrorAction SilentlyContinue $expected
& $powershell -noprofile { exit }
$expected | Should Exist
}
}
}
+166
View File
@@ -0,0 +1,166 @@
using namespace System.Diagnostics
Describe "ConsoleHost unit tests" -Tag 'Slow' {
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
Context "CommandLine" {
It "simple -args" -Pending:($env:TRAVIS_OS_NAME -eq "osx") {
& $powershell -noprofile { $args[0] } -args "hello world" | Should Be "hello world"
}
It "array -args" -Pending:($env:TRAVIS_OS_NAME -eq "osx") {
& $powershell -noprofile { $args[0] } -args 1,(2,3) | Should Be 1
(& $powershell -noprofile { $args[1] } -args 1,(2,3))[1] | Should Be 3
}
foreach ($x in "--help", "-help", "-h", "-?", "--he", "-hel", "--HELP", "-hEl") {
It "Accepts '$x' as a parameter for help" {
& $powershell -noprofile $x | ?{ $_ -match "PowerShell[.exe] -Help | -? | /?" } | Should Not BeNullOrEmpty
}
}
It "Should accept a Base64 encoded command" {
$commandString = "Get-Location"
$encodedCommand = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($commandString))
# We don't compare to `Get-Location` directly because object and formatted output comparisons are difficult
$expected = & $powershell -noprofile -command $commandString
$actual = & $powershell -noprofile -EncodedCommand $encodedCommand
$actual | Should Be $expected
}
}
Context "Pipe to/from powershell" {
$p = [PSCustomObject]@{X=10;Y=20}
It "xml input" {
$p | & $powershell -noprofile { $input | Foreach-Object {$a = 0} { $a += $_.X + $_.Y } { $a } } | Should Be 30
$p | & $powershell -noprofile -inputFormat xml { $input | Foreach-Object {$a = 0} { $a += $_.X + $_.Y } { $a } } | Should Be 30
}
It "text input" -Pending:($env:TRAVIS_OS_NAME -eq "osx") {
# Join (multiple lines) and remove whitespace (we don't care about spacing) to verify we converted to string (by generating a table)
$p | & $powershell -noprofile -inputFormat text { -join ($input -replace "\s","") } | Should Be "XY--1020"
}
It "xml output" {
& $powershell -noprofile { [PSCustomObject]@{X=10;Y=20} } | Foreach-Object {$a = 0} { $a += $_.X + $_.Y } { $a } | Should Be 30
& $powershell -noprofile -outputFormat xml { [PSCustomObject]@{X=10;Y=20} } | Foreach-Object {$a = 0} { $a += $_.X + $_.Y } { $a } | Should Be 30
}
It "text output" -Pending:($env:TRAVIS_OS_NAME -eq "osx") {
# Join (multiple lines) and remove whitespace (we don't care about spacing) to verify we converted to string (by generating a table)
-join (& $powershell -noprofile -outputFormat text { [PSCustomObject]@{X=10;Y=20} }) -replace "\s","" | Should Be "XY--1020"
}
}
Context "Redirected standard handles" {
function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn)
{
return [ProcessStartInfo]@{
FileName = $powershell
Arguments = $CommandLine
RedirectStandardInput = $RedirectStdIn
RedirectStandardOutput = $true
RedirectStandardError = $true
UseShellExecute = $false
}
}
function RunPowerShell([ProcessStartInfo]$si)
{
$process = [Process]::Start($si)
return $process
}
function EnsureChildHasExited([Process]$process, [int]$WaitTimeInMS = 15000)
{
$process.WaitForExit($WaitTimeInMS)
if (!$process.HasExited)
{
$process.HasExited | Should Be $true
$process.Kill()
}
}
It "Simple redirected output" -Pending:($env:TRAVIS_OS_NAME -eq "osx") {
$si = NewProcessStartInfo "-noprofile 1+1"
$process = RunPowerShell $si
$process.StandardOutput.ReadToEnd() | Should Be 2
EnsureChildHasExited $process
}
$nl = [Environment]::Newline
# Redirected input is broken on Windows in .NET Core
It "Redirected input" -Pending:$IsWindows {
$si = NewProcessStartInfo "-noprofile ""`$function:prompt = { 'PS> ' }""" -RedirectStdIn
$process = RunPowerShell $si
$process.StandardInput.Write("1+1`n")
$process.StandardOutput.ReadLine() | Should Be "PS> 1+1"
$process.StandardOutput.ReadLine() | Should Be "2"
$process.StandardInput.Write("1+2`n")
$process.StandardOutput.ReadLine() | Should Be "PS> 1+2"
$process.StandardOutput.ReadLine() | Should Be "3"
$process.StandardInput.Close()
$process.StandardOutput.ReadToEnd() | Should Be "PS> "
EnsureChildHasExited $process
}
It "Redirected input explicit prompting" -Pending:$IsCore {
$si = NewProcessStartInfo "-noprofile -File -" -RedirectStdIn
$process = RunPowerShell $si
$process.StandardInput.Write("`$function:prompt = { 'PS> ' }`n")
$null = $process.StandardOutput.ReadLine()
$process.StandardInput.Write("1+1`n")
$process.StandardOutput.ReadLine() | Should Be "PS> 1+1"
$process.StandardOutput.ReadLine() | Should Be "2"
$process.StandardInput.Close()
$process.StandardOutput.ReadToEnd() | Should Be "PS> "
EnsureChildHasExited $process
}
It "Redirected input no prompting" -Pending:$IsCore {
$si = NewProcessStartInfo "-noprofile -" -RedirectStdIn
$process = RunPowerShell $si
$process.StandardInput.Write("1+1`n")
$process.StandardInput.Close()
$process.StandardOutput.ReadToEnd() | Should Be "2${nl}"
EnsureChildHasExited $process
}
It "Redirected input w/ nested prompt" -Pending:$IsCore {
$si = NewProcessStartInfo "-noprofile ""`$function:prompt = { 'PS' + ('>'*(`$nestedPromptLevel+1)) + ' ' }""" -RedirectStdIn
$process = RunPowerShell $si
$process.StandardInput.Write("`$host.EnterNestedPrompt()`n")
$process.StandardOutput.ReadLine() | Should Be "PS> `$host.EnterNestedPrompt()"
$process.StandardInput.Write("exit`n")
$process.StandardOutput.ReadLine() | Should Be "PS>> exit"
$process.StandardInput.Close()
$process.StandardOutput.ReadToEnd() | Should Be "PS> "
EnsureChildHasExited $process
}
}
Context "Exception handling" {
It "Should handle a CallDepthOverflow" {
# Infinite recursion
function recurse
{
recurse $args
}
try
{
recurse "args"
Throw "Incorrect exception"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CallDepthOverflow"
}
}
}
}
@@ -0,0 +1,34 @@
Describe "PSVersionTable" {
It "Should have version table entries" {
$PSVersionTable.Count | Should Be 9
}
It "Should have the right version table entries" {
$PSVersionTable.ContainsKey("PSVersion") | Should Be True
$PSVersionTable.ContainsKey("PSEdition") | Should Be True
$PSVersionTable.ContainsKey("WSManStackVersion") | Should Be True
$PSVersionTable.ContainsKey("SerializationVersion") | Should Be True
$PSVersionTable.ContainsKey("CLRVersion") | Should Be True
$PSVersionTable.ContainsKey("BuildVersion") | Should Be True
$PSVersionTable.ContainsKey("PSCompatibleVersions") | Should Be True
$PSVersionTable.ContainsKey("PSRemotingProtocolVersion") | Should Be True
$PSVersionTable.ContainsKey("GitCommitId") | Should Be True
}
It "GitCommitId property should not contain an error" {
$PSVersionTable.GitCommitId | Should not match "powershell.version"
}
It "Should have the correct edition" -Skip:(!$IsCore) {
$expected =
if ($IsLinux -Or $IsOSX) {
"Linux"
} elseif ($IsWindows) {
"Core"
} else {
throw [PlatformNotSupportedException]
}
$PSVersionTable["PSEdition"] | Should Be $expected
}
}