Files
PowerShell/test/powershell/Split-Path.Tests.ps1
T

111 lines
4.5 KiB
PowerShell
Raw Normal View History

2015-10-22 16:35:31 -07:00
Describe "Split-Path" {
2015-12-28 11:05:48 -08:00
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
if ($isWindows)
{
2015-12-28 11:05:48 -08:00
$qualifier = "C:"
}
else
{
2015-12-28 11:05:48 -08:00
$qualifier = "/"
}
2015-07-15 10:15:39 -07:00
It "Should return a string object when invoked" {
( Split-Path . ).GetType().Name | Should Be "String"
( Split-Path . -Leaf ).GetType().Name | Should Be "String"
( Split-Path . -Resolve ).GetType().Name | Should Be "String"
2015-07-15 10:15:39 -07:00
}
It "Should return the name of the drive when the qualifier switch is used" {
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" {
2015-08-26 10:53:36 -07:00
# ErrorAction SilentlyContinue merely suppresses the error from the console.
# Throwing exceptions still seen by Pester.
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" {
Split-Path "abadTest" -Qualifier -ErrorAction SilentlyContinue | Should Throw
}
It "Should return the path when the noqualifier switch is used on a Linux system" {
{ Split-Path ${qualifier}usr/bin -NoQualifier } | Should Not Throw
2015-12-28 11:05:48 -08:00
if ($isWindows)
{
2015-12-29 09:49:46 -08:00
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "usr/bin"
}
else
{
2015-12-29 09:49:46 -08:00
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "/usr/bin"
}
2015-07-15 10:15:39 -07:00
}
It "Should return the base name when the leaf switch is used" {
Split-Path ${qualifier}usr/bin -Leaf | Should be "bin"
Split-Path ${qualifier}usr/local/bin -Leaf | Should be "bin"
2015-08-26 11:17:52 -07:00
Split-Path usr/bin -Leaf | Should be "bin"
Split-Path ./bin -Leaf | Should be "bin"
Split-Path bin -Leaf | Should be "bin"
2015-07-15 10:15:39 -07:00
}
It "Should be able to accept regular expression input and output an array for multiple objects" {
2015-12-29 09:49:46 -08:00
$testDir = "./testDir"
$testFile1 = "testfile1.ps1"
$testFile2 = "testfile2.ps1"
2015-12-29 09:49:46 -08:00
$testFilePath1 = $testDir + "/" + $testFile1
$testFilePath2 = $testDir + "/" + $testFile2
2015-08-26 10:53:36 -07:00
2015-08-28 17:22:51 -07:00
New-Item -ItemType file -Path $testFilePath1, $testFilePath2 -Force
2015-08-26 10:53:36 -07:00
2015-08-28 17:22:51 -07:00
Test-Path $testFilePath1 | Should Be $true
Test-Path $testFilePath2 | Should Be $true
$actual = ( Split-Path $testDir/*file*.ps1 -Leaf -Resolve ) | Sort-Object
2015-08-28 16:07:11 -07:00
$actual.GetType().BaseType.Name | Should Be "Array"
2015-08-28 17:22:51 -07:00
$actual[0] | Should Be $testFile1
$actual[1] | Should Be $testFile2
2015-08-26 10:53:36 -07:00
2015-12-29 09:49:46 -08:00
Remove-Item $testDir -Recurse -ErrorAction Ignore
2015-08-28 17:22:51 -07:00
Test-Path $testDir | Should Be $false
2015-07-15 10:15:39 -07:00
}
It "Should be able to tell if a given path is an absolute path" {
( 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
2015-08-26 10:53:36 -07:00
2015-07-15 10:15:39 -07:00
}
It "Should support piping" {
$path = "${qualifier}usr/bin"
( $path | Split-Path ) | Should Be "${qualifier}usr"
2015-07-15 10:15:39 -07:00
}
It "Should return the path up to the parent of the directory when Parent switch is used" {
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 "${qualifier}usr/bin/" -Parentaoeu } | Should Throw "A parameter cannot be found that matches parameter name"
}
}