File size: 7,153 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Join-Path cmdlet tests" -Tags "CI" {
$SepChar=[io.path]::DirectorySeparatorChar
BeforeAll {
$StartingLocation = Get-Location
}
AfterEach {
Set-Location $StartingLocation
}
It "should output multiple paths when called with multiple -Path targets" {
Setup -Dir SubDir1
(Join-Path -Path TestDrive:,$TestDrive -ChildPath "SubDir1" -Resolve).Length | Should -Be 2
}
It "should throw 'DriveNotFound' when called with -Resolve and drive does not exist" {
{ Join-Path bogusdrive:\\somedir otherdir -Resolve -ErrorAction Stop; Throw "Previous statement unexpectedly succeeded..." } |
Should -Throw -ErrorId "DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
}
It "should throw 'PathNotFound' when called with -Resolve and item does not exist" {
{ Join-Path "Bogus" "Path" -Resolve -ErrorAction Stop; Throw "Previous statement unexpectedly succeeded..." } |
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 905237)] Note: Result should be the same on non-Windows platforms too
It "should return one object when called with a Windows FileSystem::Redirector" {
Set-Location ("env:"+$SepChar)
$result=Join-Path FileSystem::windir system32
$result.Count | Should -Be 1
$result | Should -BeExactly ("FileSystem::windir"+$SepChar+"system32")
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
It "should be able to join-path special string 'Variable:' with 'foo'" {
$result=Join-Path "Variable:" "foo"
$result.Count | Should -Be 1
$result | Should -BeExactly ("Variable:"+$SepChar+"foo")
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
It "should be able to join-path special string 'Alias:' with 'foo'" {
$result=Join-Path "Alias:" "foo"
$result.Count | Should -Be 1
$result | Should -BeExactly ("Alias:"+$SepChar+"foo")
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
It "should be able to join-path special string 'Env:' with 'foo'" {
$result=Join-Path "Env:" "foo"
$result.Count | Should -Be 1
$result | Should -BeExactly ("Env:"+$SepChar+"foo")
}
It "should be able to join multiple child paths passed by position with remaining arguments" {
$result = Join-Path one two three four five
$result.Count | Should -Be 1
$result | Should -BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five"
}
It "Join-Path -Path <Path> -ChildPath <ChildPath> should return '<ExpectedResult>'" -TestCases @(
@{
Path = 'one'
ChildPath = 'two', 'three'
ExpectedResult = "one${sepChar}two${sepChar}three"
}
@{
Path = 'one', 'two'
ChildPath = 'three', 'four'
ExpectedResult = @(
"one${sepChar}three${sepChar}four"
"two${sepChar}three${sepChar}four"
)
}
@{
Path = 'one'
ChildPath = @()
ExpectedResult = "one${sepChar}"
}
@{
Path = 'one'
ChildPath = $null
ExpectedResult = "one${sepChar}"
}
@{
Path = 'one'
ChildPath = [string]::Empty
ExpectedResult = "one${sepChar}"
}
) {
param($Path, $ChildPath, $ExpectedResult)
$result = Join-Path -Path $Path -ChildPath $ChildPath
$result | Should -BeExactly $ExpectedResult
}
It "should handle extension parameter: <TestName>" -TestCases @(
@{
TestName = "change extension"
ChildPath = "file.txt"
Extension = ".log"
ExpectedChildPath = "file.log"
}
@{
TestName = "add extension to file without extension"
ChildPath = "file"
Extension = ".txt"
ExpectedChildPath = "file.txt"
}
@{
TestName = "extension without leading dot"
ChildPath = "file.txt"
Extension = "log"
ExpectedChildPath = "file.log"
}
@{
TestName = "double extension with dot"
ChildPath = "file.txt"
Extension = ".tar.gz"
ExpectedChildPath = "file.tar.gz"
}
@{
TestName = "double extension without dot"
ChildPath = "file.txt"
Extension = "tar.gz"
ExpectedChildPath = "file.tar.gz"
}
@{
TestName = "remove extension with empty string"
ChildPath = "file.txt"
Extension = ""
ExpectedChildPath = "file"
}
@{
TestName = "preserve dots in base name when removing extension with empty string"
ChildPath = "file...txt"
Extension = ""
ExpectedChildPath = "file.."
}
@{
TestName = "replace only the last extension for files with multiple dots"
ChildPath = "file.backup.txt"
Extension = ".log"
ExpectedChildPath = "file.backup.log"
}
@{
TestName = "preserve dots in base name when changing extension"
ChildPath = "file...txt"
Extension = ".md"
ExpectedChildPath = "file...md"
}
@{
TestName = "add extension to directory-like path"
ChildPath = "subfolder"
Extension = ".log"
ExpectedChildPath = "subfolder.log"
}
) {
param($TestName, $ChildPath, $Extension, $ExpectedChildPath)
$result = Join-Path -Path "folder" -ChildPath $ChildPath -Extension $Extension
$result | Should -BeExactly "folder${SepChar}${ExpectedChildPath}"
}
It "should handle extension parameter with multiple child path segments: <TestName>" -TestCases @(
@{
TestName = "change extension when joining multiple child path segments"
ChildPaths = @("subfolder", "file.txt")
Extension = ".log"
ExpectedPath = "folder${SepChar}subfolder${SepChar}file.log"
}
) {
param($TestName, $ChildPaths, $Extension, $ExpectedPath)
$result = Join-Path -Path "folder" -ChildPath $ChildPaths -Extension $Extension
$result | Should -BeExactly $ExpectedPath
}
It "should change extension for multiple paths" {
$result = Join-Path -Path "folder1", "folder2" -ChildPath "file.txt" -Extension ".log"
$result.Count | Should -Be 2
$result[0] | Should -BeExactly "folder1${SepChar}file.log"
$result[1] | Should -BeExactly "folder2${SepChar}file.log"
}
It "should resolve path when -Extension changes to existing file" {
New-Item -Path TestDrive:\testfile.log -ItemType File -Force | Out-Null
$result = Join-Path -Path TestDrive: -ChildPath "testfile.txt" -Extension ".log" -Resolve
$result | Should -BeLike "*testfile.log"
}
It "should throw error when -Extension changes to non-existing file with -Resolve" {
{ Join-Path -Path TestDrive: -ChildPath "testfile.txt" -Extension ".nonexistent" -Resolve -ErrorAction Stop; Throw "Previous statement unexpectedly succeeded..." } |
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
}
It "should accept Extension from pipeline by property name" {
$obj = [PSCustomObject]@{ Path = "folder"; ChildPath = "file.txt"; Extension = ".log" }
$result = $obj | Join-Path
$result | Should -BeExactly "folder${SepChar}file.log"
}
}
|