Merge branch 'dev/cross-platform-pester'

This commit is contained in:
Andrew Schwartzmeyer
2016-01-06 12:54:57 -08:00
18 changed files with 370 additions and 311 deletions
+4 -15
View File
@@ -1,20 +1,9 @@
Describe "Compare-Object" {
# First ensure the environment is set up
if (Test-Path "/tmp")
{
$testDirectory = "/tmp/testDirectory"
$nl = "`n"
$slash = "/"
$nl = [Environment]::NewLine
$slash = [System.IO.Path]::DirectorySeparatorChar
$testDirectory = $HOME + $slash + "testDirectory"
$dir = $testDirectory
}
else
{
$testDirectory = "C:\Users\v-zafolw\testdirectory"
$nl = "`r`n"
$slash = "\\"
}
$dir = $testDirectory
New-Item $testDirectory -ItemType directory -Force
$content1 = "line 1" + $nl + "line 2"
@@ -1,4 +1,6 @@
Describe "Environment-Variables" {
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
It "Should have environment variables" {
Get-Item ENV: | Should Not BeNullOrEmpty
}
@@ -8,17 +10,26 @@
}
It "Should contain /bin in the PATH" {
$ENV:PATH | Should Match "/bin"
if ($isWindows)
{
$ENV:PATH | Should Match "C:"
}
else
{
$ENV:PATH | Should Match "/bin"
}
}
It "Should have the correct HOME" {
$expected = /bin/bash -c "cd ~ && pwd"
$ENV:HOME | Should Be $expected
}
It "Should be able to access the members of the environment variable" {
$expected = /bin/bash -c "cd ~ && pwd"
(Get-Item ENV:HOME).Value | Should Be $expected
if ($isWindows)
{
$expected = "\Users\" + $ENV:USERNAME
}
else
{
$expected = /bin/bash -c "cd ~ && pwd"
}
$ENV:HOME | Should Be $expected
}
It "Should be able to set the environment variables" {
+4 -3
View File
@@ -1,4 +1,5 @@
Describe "Format-List" {
$nl = [Environment]::NewLine
BeforeEach {
$in = New-Object PSObject
Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue
@@ -22,7 +23,7 @@ Describe "Format-List" {
}
It "Should produce the expected output" {
$expected = "`n`ntestName : testValue`n`n`n`n"
$expected = "${nl}${nl}testName : testValue${nl}${nl}${nl}${nl}"
$in = New-Object PSObject
Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue
@@ -43,14 +44,14 @@ Describe "Format-List" {
It "Should be able to display a list of props when separated by a comma" {
{ Get-Process | Format-List -Property Name,BasePriority } | Should Not Throw
(Get-Process | Format-List -Property Name,BasePriority | Out-String) -Split "`n" |
(Get-Process | Format-List -Property Name,BasePriority | Out-String) -Split "${nl}" |
Where-Object { $_.trim() -ne "" } |
ForEach-Object { $_ | Should Match "(Name)|(BasePriority)" }
}
It "Should show the requested prop in every element" {
# Testing each element of format-list, using a for-each loop since the Format-List is so opaque
(Get-Process | Format-List -Property CPU | Out-String) -Split "`n" |
(Get-Process | Format-List -Property CPU | Out-String) -Split "${nl}" |
Where-Object { $_.trim() -ne "" } |
ForEach-Object { $_ | Should Match "CPU :" }
}
+13 -6
View File
@@ -1,18 +1,25 @@
Describe "Get-ChildItem" {
It "Should list the contents of the current folder" {
(Get-ChildItem .).Name.Length | Should BeGreaterThan 0
(ls .).Name.Length | Should BeGreaterThan 0
}
It "Should list the contents of the home directory" {
pushd /usr/
pushd $HOME
(Get-ChildItem .).Name.Length | Should BeGreaterThan 0
popd
}
pushd /usr/
(ls .).Name.Length | Should BeGreaterThan 0
popd
It "Should be able to use the ls alias" {
$(ls .).Name.Length | Should Be $(Get-ChildItem .).Name.Length
}
It "Should have a the proper fields and be populated" {
$var = Get-Childitem .
$var.Name.Length | Should BeGreaterThan 0
$var.Mode.Length | Should BeGreaterThan 0
$var.LastWriteTime | Should BeGreaterThan 0
$var.Length.Length | Should BeGreaterThan 0
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
Describe "Get-Content" {
$testString = "This is a test content for a file"
$nl = "`n"
$nl = [Environment]::NewLine
$firstline = "Here's a first line "
$secondline = " here's a second line"
@@ -14,8 +14,8 @@
$testPath2 = "/tmp/testfile2"
BeforeEach {
New-Item -Path $testPath -Force -Value $testString
New-Item -Path $testPath2 -Force -Value $testString2
New-Item -Path $testPath -ItemType file -Force -Value $testString
New-Item -Path $testPath2 -ItemType file -Force -Value $testString2
}
It "Should throw an error on a directory " {
+9 -7
View File
@@ -3,19 +3,21 @@
Describe "Get-ItemProperty" {
$currentDirectory = Split-Path $here -Leaf
$parentDirectory = Split-Path $here/.. -Leaf
if (Test-Path /tmp)
{
$tempDirectory = "/tmp/testfolder"
$testProvider = "/"
}
else
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
if ($isWindows)
{
$tempDirectory = "~/testfolder"
$testProvider = "C"
}
else
{
$tempDirectory = "/tmp/testfolder"
$testProvider = "/"
}
New-Item $tempDirectory -ItemType Directory -Force
$testfile = $tempDirectory + "/" + "testfile1"
$testfile = $tempDirectory + [Environment]::DirectorySeparatorChar + "testfile1"
New-Item $testfile -ItemType file -Force
+13 -3
View File
@@ -1,10 +1,12 @@
Describe "Get-PSDrive" {
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
It "Should not throw" {
Get-PSDrive | Should Not BeNullOrEmpty
}
It "Should have a name and a length property" {
(Get-PSDrive).Name | Should Not BeNullOrEmpty
(Get-PSDrive).Name | Should Not BeNullOrEmpty
(Get-PSDrive).Root.Length | Should Not BeLessThan 1
}
@@ -23,8 +25,16 @@
It "Should return drive info"{
(Get-PSDrive Env).Name | Should Be Env
(Get-PSDrive /).Root | Should Be /
(Get-PSDrive /).Provider.Name | Should Be FileSystem
(Get-PSDrive Cert).Root | Should Be \
if ($isWindows)
{
(Get-PSDrive C).Provider.Name | Should Be FileSystem
}
else
{
(Get-PSDrive /).Provider.Name | Should Be FileSystem
}
}
It "Should be able to access a drive using the PSProvider switch" {
+2 -6
View File
@@ -1,13 +1,9 @@
Describe "Get-UICulture" {
It "Should return a type of CultureInfo as the name of the type" {
(Get-UICulture).GetType().Name | Should Be CultureInfo
(Get-UICulture).GetType().Name | Should Match CultureInfo
}
It "Should have $ PsUICulture variable be equivalent to Get-UICulture object" {
(Get-UICulture).Name | Should Be $PsUICulture
(Get-UICulture).Name | Should Be $PsUICulture
}
}
+1 -7
View File
@@ -126,13 +126,7 @@
}
Context "String tests" {
if ($true) # TODO: test if Windows or Linux
{
$nl = "`n"
}
else {
$nl = "`r`n"
}
$nl = [Environment]::NewLine
$testString = "HAD I the heavens’ embroidered cloths,$nl Enwrought with golden and silver light,$nl The blue and the dim and the dark cloths$nl Of night and light and the half light,$nl I would spread the cloths under your feet:$nl But I, being poor, have only my dreams;$nl I have spread my dreams under your feet;$nl Tread softly because you tread on my dreams."
-4
View File
@@ -92,8 +92,4 @@ Describe "New-Item" {
Test-Path $FullyQualifiedFile | Should Be $false
}
It "Should produce an error when the credentials switch is thrown" {
{ New-Item -Name $testfile -Path $tmpDirectory -ItemType file -Credential domain/USER } | Should Throw "not implemented"
}
}
+3 -1
View File
@@ -1,4 +1,6 @@
Describe "New-Variable" {
$nl = [Environment]::NewLine
It "Should create a new variable with no parameters" {
{ New-Variable var1 } | Should Not Throw
}
@@ -62,7 +64,7 @@
$output = $in | Format-List -Property Description | Out-String
$output | Should Be "`n`nDescription : test description`n`n`n`n"
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
}
It "Should be able to set the value using the value switch" {
+4 -11
View File
@@ -1,18 +1,11 @@
Describe "Out-String" {
if (Test-Path /tmp)
{
$nl = "`n"
}
else
{
$nl = "`r`n"
}
$nl = [Environment]::NewLine
It "Should accumulate the strings and returns them as a single string" {
$testArray = "a", " b"
$testArray.GetType().BaseType | Should Be array
$testArray | Out-String | Should Be "a$nl b$nl"
$($testArray | Out-String).GetType() | Should Be string
@@ -41,4 +34,4 @@
$streamoutputlength | Should BeLessThan $nonstreamoutputlength
}
}
}
+26 -12
View File
@@ -1,4 +1,6 @@
Describe "Select-String" {
$nl = [Environment]::NewLine
$currentDirectory = $pwd.Path
Context "String actions" {
$testinputone = "hello","Hello","goodbye"
$testinputtwo = "hello","Hello"
@@ -85,9 +87,21 @@
}
Context "Filesytem actions" {
$testInputFile = "/tmp/testfile1.txt"
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
if ($isWindows)
{
$testDirectory = "C:\tmp\"
$testInputFile = "C:\tmp\testfile1.txt"
}
else
{
$testDirectory = "/tmp/"
$testInputFile = "/tmp/testfile1.txt"
}
BeforeEach {
New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string`nThis is the second line`nThis is the third line`nThis is the fourth line`nNo matches"
New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches"
}
It "Should return an object when a match is found is the file on only one line" {
@@ -102,7 +116,7 @@
It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" {
$expected = $testInputFile + ":1:This is a text string, and another string"
Select-String $testInputFile -Pattern "string" | Should Be $expected
Select-String $(Split-Path $testInputFile -NoQualifier) -Pattern "string" | Should Be $expected
}
It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" {
@@ -131,11 +145,11 @@
It "Should return the third line in testfile1 and the lines above and below it " {
$expectedLine = "testfile1.txt:2:This is the second line"
$expectedLineBefore = "testfile1.txt:3:This is the third line"
$expectedLineAfter = "/tmp/testfile1.txt:4:This is the fourth line"
$expectedLineAfter = "testfile1.txt:4:This is the fourth line"
Select-String third $testInputFile -Context 1 | Should Match $expectedLine
Select-String third $testInputFile -Context 1 | Should Match $expectedLineBefore
Select-String third $testInputFile -Context 1 | Should Match $expectedLineAfter
Select-String third $testInputFile -Context 1 | Should Match $expectedLineBefore
Select-String third $testInputFile -Context 1 | Should Match $expectedLineAfter
}
It "Should return the number of matches for 'is' in textfile1 " {
@@ -143,14 +157,13 @@
}
It "Should return the third line in testfile1 when a relative path is used" {
$expected = "/tmp/testfile1.txt:3:This is the third line"
$expected = "testfile1.txt:3:This is the third line"
Select-String third /tmp/../tmp/testfile1.txt | Should Match $expected
Select-String third $testDirectory/"../tmp/testfile1.txt" | Should Match $expected
}
It "Should return the fourth line in testfile1 when a relative path is used" {
$testDirectory = "/tmp/"
$expected = "/tmp/testfile1.txt:5:No matches"
$expected = "testfile1.txt:5:No matches"
pushd $testDirectory
@@ -159,15 +172,16 @@
}
It "Should return the fourth line in testfile1 when a regular expression is used" {
$expected = "/tmp/testfile1.txt:5:No matches"
$expected = "testfile1.txt:5:No matches"
Select-String 'matc*' $testInputFile -CaseSensitive | Should Match $expected
}
It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" {
$expected = "/tmp/testfile1.txt:5:No matches"
$expected = "testfile1.txt:5:No matches"
Select-String 'matc*' $testInputFile -ca | Should Match $expected
}
}
Push-Location $currentDirectory
}
+56 -49
View File
@@ -1,61 +1,68 @@
Describe "Set-Location" {
Context "Functionality testing" {
$startDirectory = Get-Location
$startDirectory = Get-Location
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
It "Should be able to be called without error" {
{ Set-Location / } | Should Not Throw
}
if ($isWindows)
{
$target = "C:\"
}
else
{
$target = "/"
}
It "Should be able to be called on different providers" {
{ Set-Location alias: } | Should Not Throw
{ Set-Location env: } | Should Not Throw
}
It "Should be able to be called without error" {
{ Set-Location $target } | Should Not Throw
}
It "Should be able use the cd alias without error" {
{ cd / } | Should Not Throw
}
It "Should be able to be called on different providers" {
{ Set-Location alias: } | Should Not Throw
{ Set-Location env: } | Should Not Throw
}
It "Should be able to use the chdir alias without error" {
{ chdir / } | Should Not Throw
}
It "Should be able use the cd alias without error" {
{ cd $target } | Should Not Throw
}
It "Should be able to use the sl alias without error" {
{ sl / } | Should Not Throw
}
It "Should be able to use the chdir alias without error" {
{ chdir $target } | Should Not Throw
}
It "Should have the correct current location when using the set-location cmdlet" {
Set-Location $startDirectory
$(Get-Location).Path | Should Be $startDirectory.Path
}
It "Should have the correct current location when using the cd alias" {
cd /
$(Get-Location).Path | Should Be /
}
It "Should have the correct current location when using the chdir alias" {
chdir /
$(Get-Location).Path | Should Be /
}
It "Should have the correct current location when using the chdir alias" {
sl /
$(Get-Location).Path | Should Be /
}
It "Should be able to use the Path switch" {
{ Set-Location -Path / } | Should Not Throw
}
It "Should generate a pathinfo object when using the Passthru switch" {
$(Set-Location / -PassThru).GetType().Name | Should Be PathInfo
}
It "Should be able to use the sl alias without error" {
{ sl $target } | Should Not Throw
}
It "Should have the correct current location when using the set-location cmdlet" {
Set-Location $startDirectory
$(Get-Location).Path | Should Be $startDirectory.Path
}
It "Should have the correct current location when using the cd alias" {
cd $target
$(Get-Location).Path | Should Be $target
}
It "Should have the correct current location when using the chdir alias" {
chdir $target
$(Get-Location).Path | Should Be $target
}
It "Should have the correct current location when using the chdir alias" {
sl $target
$(Get-Location).Path | Should Be $target
}
It "Should be able to use the Path switch" {
{ Set-Location -Path $target } | Should Not Throw
}
It "Should generate a pathinfo object when using the Passthru switch" {
$(Set-Location $target -PassThru).GetType().Name | Should Be PathInfo
}
Set-Location $startDirectory
}
+34 -34
View File
@@ -1,34 +1,34 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Describe "Set-PSBreakpoint" {
# Set up test script
$testScript = "$here/psbreakpointtestscript.ps1"
"`$var = 1 " > $testScript
It "Should be able to set a psbreakpoint on a line" {
$lineNumber = 1
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line | Should Be $lineNumber
}
It "Should throw when a string is entered for a line number" {
{
$lineNumber = "one"
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line
} | Should Throw
}
It "Should be able to set a psbreakpoint on a Command" {
$command = "theCommand"
$(Set-PSBreakpoint -Command $command -Script $testScript).Command | Should Be $command
}
It "Should be able to set a psbreakpoint on a variable" {
$var = "theVariable"
$(Set-PSBreakpoint -Command $var -Script $testScript).Command | Should Be $var
}
# clean up after ourselves
Remove-Item -Path $testScript
}
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Describe "Set-PSBreakpoint" {
# Set up test script
$testScript = "$here/psbreakpointtestscript.ps1"
"`$var = 1 " > $testScript
It "Should be able to set a psbreakpoint on a line" {
$lineNumber = 1
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line | Should Be $lineNumber
}
It "Should throw when a string is entered for a line number" {
{
$lineNumber = "one"
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line
} | Should Throw
}
It "Should be able to set a psbreakpoint on a Command" {
$command = "theCommand"
$(Set-PSBreakpoint -Command $command -Script $testScript).Command | Should Be $command
}
It "Should be able to set a psbreakpoint on a variable" {
$var = "theVariable"
$(Set-PSBreakpoint -Command $var -Script $testScript).Command | Should Be $var
}
# clean up after ourselves
Remove-Item -Path $testScript
}
+3 -1
View File
@@ -1,4 +1,6 @@
Describe "Set-Variable" {
${nl} = [Environment]::Newline
It "Should create a new variable with no parameters" {
{ Set-Variable testVar } | Should Not Throw
}
@@ -51,7 +53,7 @@
$output = $in | Format-List -Property Description | Out-String
# This will cause errors running these tests in windows
$output | Should Be "`n`nDescription : test description`n`n`n`n"
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
}
It "Should be able to set the value using the value switch" {
+52 -27
View File
@@ -1,4 +1,15 @@
Describe "Split-Path" {
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
if ($isWindows)
{
$qualifier = "C:"
}
else
{
$qualifier = "/"
}
It "Should return a string object when invoked" {
( Split-Path . ).GetType().Name | Should Be "String"
( Split-Path . -Leaf ).GetType().Name | Should Be "String"
@@ -6,15 +17,23 @@
}
It "Should return the name of the drive when the qualifier switch is used" {
Split-Path / -Qualifier | Should Be "/"
Split-Path /usr/bin -Qualifier | Should Be "/"
Split-Path $qualifier -Qualifier | Should Be "$qualifier"
Split-Path ${qualifier}usr/bin -Qualifier | Should Be "$qualifier"
}
It "Should error when using the qualifier switch for a windows path while on a nonwindows machine" {
# ErrorAction SilentlyContinue merely suppresses the error from the console.
# Throwing exceptions still seen by Pester.
Split-Path "C:\Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw
if ($qualifier -eq "/")
{
Split-Path "C:\Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw
}
else
{
Split-Path "/Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw
}
}
It "Should error when no directory separator characters are used with a qualifier" {
@@ -22,24 +41,29 @@
}
It "Should return the path when the noqualifier switch is used on a Linux system" {
{ Split-Path /usr/bin -NoQualifier } | Should Not Throw
Split-Path /usr/bin -NoQualifier | Should Be "/usr/bin"
{ Split-Path ${qualifier}usr/bin -NoQualifier } | Should Not Throw
if ($isWindows)
{
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "usr/bin"
}
else
{
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "/usr/bin"
}
}
It "Should return the base name when the leaf switch is used" {
Split-Path /usr/bin -Leaf | Should be "bin"
Split-Path /usr/local/bin -Leaf | Should be "bin"
Split-Path ${qualifier}usr/bin -Leaf | Should be "bin"
Split-Path ${qualifier}usr/local/bin -Leaf | Should be "bin"
Split-Path usr/bin -Leaf | Should be "bin"
Split-Path ./bin -Leaf | Should be "bin"
Split-Path bin -Leaf | Should be "bin"
}
It "Should be able to accept regular expression input and output an array for multiple objects" {
$testDir = "./tmp"
Remove-Item $testDir -Recurse -Force -ErrorAction Ignore
$testFile1 = "testfile1.ps1"
$testFile2 = "testfile2.ps1"
$testDir = "./testDir"
$testFile1 = "testfile1.ps1"
$testFile2 = "testfile2.ps1"
$testFilePath1 = $testDir + "/" + $testFile1
$testFilePath2 = $testDir + "/" + $testFile2
@@ -53,34 +77,35 @@
$actual[0] | Should Be $testFile1
$actual[1] | Should Be $testFile2
Remove-Item $testDir -Recurse -Force -ErrorAction Ignore
Remove-Item $testDir -Recurse -ErrorAction Ignore
Test-Path $testDir | Should Be $false
}
It "Should be able to tell if a given path is an absolute path" {
( Split-Path /usr/bin -IsAbsolute ) | Should be $true
( Split-Path .. -IsAbsolute ) | Should be $false
( Split-Path /usr/.. -IsAbsolute ) | Should be $true
( Split-Path /usr/../ -IsAbsolute ) | Should be $true
( Split-Path ../ -IsAbsolute ) | Should be $false
( Split-Path . -IsAbsolute ) | Should be $false
( Split-Path ~/ -IsAbsolute ) | Should be $false
( Split-Path ~/.. -IsAbsolute ) | Should be $false
( Split-Path ~/../.. -IsAbsolute ) | Should be $false
( Split-Path ${qualifier}usr/bin -IsAbsolute ) | Should be $true
( Split-Path .. -IsAbsolute ) | Should be $false
( Split-Path ${qualifier}usr/.. -IsAbsolute ) | Should be $true
( Split-Path ${qualifier}usr/../ -IsAbsolute ) | Should be $true
( Split-Path ../ -IsAbsolute ) | Should be $false
( Split-Path . -IsAbsolute ) | Should be $false
( Split-Path ~/ -IsAbsolute ) | Should be $false
( Split-Path ~/.. -IsAbsolute ) | Should be $false
( Split-Path ~/../.. -IsAbsolute ) | Should be $false
}
It "Should support piping" {
( "/usr/bin" | Split-Path ) | Should Be "/usr"
$path = "${qualifier}usr/bin"
( $path | Split-Path ) | Should Be "${qualifier}usr"
}
It "Should return the path up to the parent of the directory when Parent switch is used" {
Split-Path "/usr/bin" -Parent | Should Be "/usr"
Split-Path "/usr/local/bin" -Parent | Should Be "/usr/local"
Split-Path "usr/local/bin" -Parent | Should Be "usr/local"
Split-Path "${qualifier}usr/bin" -Parent | Should Be "${qualifier}usr"
Split-Path "${qualifier}usr/local/bin" -Parent | Should Be $(Join-Path "${qualifier}usr" -ChildPath "local")
Split-Path "usr/local/bin" -Parent | Should Be $(Join-Path "usr" -ChildPath "local")
}
It "Should throw if a parameterSetName is incorrect" {
{ Split-Path "/usr/bin/" -Parentaoeu } | Should Throw "A parameter cannot be found that matches parameter name"
{ Split-Path "${qualifier}usr/bin/" -Parentaoeu } | Should Throw "A parameter cannot be found that matches parameter name"
}
}
+124 -114
View File
@@ -1,114 +1,124 @@
Describe "Test-Path" {
$testdirectory = "/usr/bin"
$testfilename = "vi" # use /usr/bin/vi since that's bundled with all linux
$testfile = $testdirectory + "/" + $testfilename
It "Should be called on an existing path without error" {
{ Test-Path $testdirectory } | Should Not Throw
{ Test-Path -Path $testdirectory } | Should Not Throw
{ Test-Path -LiteralPath $testdirectory } | Should Not Throw
}
It "Should allow piping objects to it" {
{ $testdirectory | Test-Path } | Should Not Throw
$testdirectory | Test-Path | Should Be $true
"/usr/bin/totallyFakeDirectory" | Test-Path | Should Be $false
}
It "Should return a boolean data type" {
{ Test-Path -Path $testdirectory } | Should Be $true
}
It "Should be called on a nonexistant path without error" {
{ Test-Path -Path "aNonexistant/path/that/should/error" } | Should Not Throw
}
It "Should return false for a nonexistant path" {
Test-Path -Path "aNonexistant/path/that/should/error" | Should Be $false
}
It "Should return true for an existing path" {
Test-Path -Path $testdirectory | Should Be $true
}
It "Should be able to accept a regular expression" {
{ Test-Path -Path "/u*" } | Should Not Throw
{ Test-Path -Path "/u[a-z]r" } | Should Not Throw
}
It "Should be able to return the correct result when a regular expression is used" {
Test-Path -Path "/u*" | Should Be $true
Test-Path -Path "/u[a-z]*" | Should Be $true
Test-Path -Path "/aoeu*" | Should Be $false
Test-Path -Path "/u[A-Z]" | Should Be $false
}
It "Should return false when the Leaf pathtype is used on a directory" {
Test-Path -Path $testdirectory -PathType Leaf | Should Be $false
}
It "Should return true when the Leaf pathtype is used on an existing endpoint" {
Test-Path -Path $testfile -PathType Leaf | Should Be $true
}
It "Should return false when the Leaf pathtype is used on a nonexistant file" {
Test-Path -Path "aoeu" -PathType Leaf | Should Be $false
}
It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" {
Test-Path -Path $testfile -Type Leaf | Should Be $true
}
It "Should be able to search multiple regular expressions using the include switch" {
Test-Path -Path "/usr/bin/*" -Include vi* | Should Be $true
}
It "Should be able to exclude a regular expression using the exclude switch" {
Test-Path -Path "/usr/bin/" -Exclude vi* | Should Be $true
}
It "Should be able to exclude multiple regular expressions using the exclude switch" {
# tests whether there's any files in the /usr directory that don't start with 'd' or 'g'
Test-Path -Path "/usr" -Exclude d*, g* | Should Be $true
}
It "Should return true if the syntax of the path is correct when using the IsValid switch" {
Test-Path -Path /this/is/a/valid/path -IsValid | Should Be $true
}
It "Should return false if the syntax of the path is incorrect when using the IsValid switch" {
Test-Path -Path C:/usr/bin -IsValid | Should Be $false
}
It "Should return true on paths containing spaces when the path is surrounded in quotes" {
Test-Path -Path "/totally a valid/path" -IsValid | Should Be $true
}
It "Should throw on paths containing spaces when the path is not surrounded in quotes" {
{ Test-Path -Path /a path/without quotes/around/it -IsValid } | Should Throw
}
It "Should return true if a directory leads or trails with a space when surrounded by quotes" {
Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should Be $true
}
It "Should return true on a valid path when the LiteralPath switch is used" {
Test-Path -LiteralPath "/usr/bin" | Should Be $true
}
It "Should return false if regular expressions are used with the LiteralPath switch" {
Test-Path -LiteralPath /*sr/bin | Should Be $false
Test-Path -LiteralPath /[usth]sr/bin | Should Be $false
}
It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" {
Test-Path -PSPath /*sr/bin | Should Be $false
Test-Path -PSPath /[aoeu]sr/bin | Should Be $false
}
It "Should return true if used on components other than filesystem objects" {
Test-Path Alias:\gci | Should Be $true
Test-Path Env:\HOME | Should Be $true
}
}
Describe "Test-Path" {
BeforeAll {
$guid = [Guid]::NewGuid().Guid
$testdirectory = New-Item -Path $guid -ItemType Directory -Force
$testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force
$testfile = $tmp + "/" + $testfilename
# populate with additional files
New-Item -Path $testdirectory -Name datestfile -value 1 -ItemType file | Out-Null
New-Item -Path $testdirectory -Name gatestfile -value 1 -ItemType file | Out-Null
}
AfterAll {
Remove-Item -Path $testdirectory -Recurse
}
It "Should be called on an existing path without error" {
{ Test-Path $testdirectory } | Should Not Throw
{ Test-Path -Path $testdirectory } | Should Not Throw
{ Test-Path -LiteralPath $testdirectory } | Should Not Throw
}
It "Should allow piping objects to it" {
{ $testdirectory | Test-Path } | Should Not Throw
$testdirectory | Test-Path | Should Be $true
"/usr/bin/totallyFakeDirectory" | Test-Path | Should Be $false
}
It "Should be called on a nonexistant path without error" {
{ Test-Path -Path "aNonexistant/path/that/should/error" } | Should Not Throw
}
It "Should return false for a nonexistant path" {
Test-Path -Path "aNonexistant/path/that/should/error" | Should Be $false
}
It "Should return true for an existing path" {
{ Test-Path -Path $testdirectory } | Should Be $true
}
It "Should be able to accept a regular expression" {
{ Test-Path -Path "/u*" } | Should Not Throw
{ Test-Path -Path "/u[a-z]r" } | Should Not Throw
}
It "Should be able to return the correct result when a regular expression is used" {
Test-Path -Path "/u*" | Should Be $true
Test-Path -Path "/u[a-z]*" | Should Be $true
Test-Path -Path "/aoeu*" | Should Be $false
Test-Path -Path "/u[A-Z]" | Should Be $false
}
It "Should return false when the Leaf pathtype is used on a directory" {
Test-Path -Path $testdirectory -PathType Leaf | Should Be $false
}
It "Should return true when the Leaf pathtype is used on an existing endpoint" {
Test-Path -Path $testfilename -PathType Leaf | Should Be $true
}
It "Should return false when the Leaf pathtype is used on a nonexistant file" {
Test-Path -Path "aoeu" -PathType Leaf | Should Be $false
}
It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" {
Test-Path -Path $testfilename -Type Leaf | Should Be $true
}
It "Should be able to search multiple regular expressions using the include switch" {
{ Test-Path -Path "$testdirectory/*" -Include t* } | Should Be $true
}
It "Should be able to exclude a regular expression using the exclude switch" {
{ Test-Path -Path "$testdirectory/*" -Exclude v* } | Should Be $true
}
It "Should be able to exclude multiple regular expressions using the exclude switch" {
# tests whether there's any files in the /usr directory that don't start with 'd' or 'g'
{ Test-Path -Path $testfilename -Exclude d*, g* } | Should Be $true
}
It "Should return true if the syntax of the path is correct when using the IsValid switch" {
{ Test-Path -Path /this/is/a/valid/path -IsValid } | Should Be $true
}
It "Should return false if the syntax of the path is incorrect when using the IsValid switch" {
$badPath = " :;!@#$%^&*(){}?+|_-"
Test-Path -Path $badPath -IsValid | Should Be $false
}
It "Should return true on paths containing spaces when the path is surrounded in quotes" {
Test-Path -Path "/totally a valid/path" -IsValid | Should Be $true
}
It "Should throw on paths containing spaces when the path is not surrounded in quotes" {
{ Test-Path -Path /a path/without quotes/around/it -IsValid } | Should Throw
}
It "Should return true if a directory leads or trails with a space when surrounded by quotes" {
Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should Be $true
}
It "Should return true on a valid path when the LiteralPath switch is used" {
Test-Path -LiteralPath $testfilename | Should Be $true
}
It "Should return false if regular expressions are used with the LiteralPath switch" {
Test-Path -LiteralPath /*sr/bin | Should Be $false
Test-Path -LiteralPath /[usth]sr/bin | Should Be $false
}
It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" {
Test-Path -PSPath /*sr/bin | Should Be $false
Test-Path -PSPath /[aoeu]sr/bin | Should Be $false
}
It "Should return true if used on components other than filesystem objects" {
Test-Path Alias:\gci | Should Be $true
Test-Path Env:\PATH | Should Be $true
}
}