2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Join-Path cmdlet tests" -Tags "CI" {
|
2016-05-04 17:09:01 -07:00
|
|
|
$SepChar=[io.path]::DirectorySeparatorChar
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$StartingLocation = Get-Location
|
|
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
AfterEach {
|
2016-05-04 17:09:01 -07:00
|
|
|
Set-Location $StartingLocation
|
|
|
|
|
}
|
|
|
|
|
It "should output multiple paths when called with multiple -Path targets" {
|
|
|
|
|
Setup -Dir SubDir1
|
2020-05-07 13:00:30 +01:00
|
|
|
(Join-Path -Path TestDrive:,$TestDrive -ChildPath "SubDir1" -Resolve).Length | Should -Be 2
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
It "should throw 'DriveNotFound' when called with -Resolve and drive does not exist" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Join-Path bogusdrive:\\somedir otherdir -Resolve -ErrorAction Stop; Throw "Previous statement unexpectedly succeeded..." } |
|
2018-05-17 14:42:04 -07:00
|
|
|
Should -Throw -ErrorId "DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
It "should throw 'PathNotFound' when called with -Resolve and item does not exist" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Join-Path "Bogus" "Path" -Resolve -ErrorAction Stop; Throw "Previous statement unexpectedly succeeded..." } |
|
2018-05-17 14:42:04 -07:00
|
|
|
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
#[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" {
|
2020-05-07 13:00:30 +01:00
|
|
|
Set-Location ("env:"+$SepChar)
|
|
|
|
|
$result=Join-Path FileSystem::windir system32
|
2018-03-09 15:13:15 -08:00
|
|
|
$result.Count | Should -Be 1
|
|
|
|
|
$result | Should -BeExactly ("FileSystem::windir"+$SepChar+"system32")
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
|
|
|
|
|
It "should be able to join-path special string 'Variable:' with 'foo'" {
|
|
|
|
|
$result=Join-Path "Variable:" "foo"
|
2018-03-09 15:13:15 -08:00
|
|
|
$result.Count | Should -Be 1
|
|
|
|
|
$result | Should -BeExactly ("Variable:"+$SepChar+"foo")
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
|
|
|
|
|
It "should be able to join-path special string 'Alias:' with 'foo'" {
|
|
|
|
|
$result=Join-Path "Alias:" "foo"
|
2018-03-09 15:13:15 -08:00
|
|
|
$result.Count | Should -Be 1
|
|
|
|
|
$result | Should -BeExactly ("Alias:"+$SepChar+"foo")
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
|
|
|
|
#[BugId(BugDatabase.WindowsOutOfBandReleases, 913084)]
|
|
|
|
|
It "should be able to join-path special string 'Env:' with 'foo'" {
|
|
|
|
|
$result=Join-Path "Env:" "foo"
|
2018-03-09 15:13:15 -08:00
|
|
|
$result.Count | Should -Be 1
|
|
|
|
|
$result | Should -BeExactly ("Env:"+$SepChar+"foo")
|
2016-05-04 17:09:01 -07:00
|
|
|
}
|
2016-08-22 10:52:09 -04:00
|
|
|
It "should be able to join multiple child paths passed by position with remaining arguments" {
|
|
|
|
|
$result = Join-Path one two three four five
|
2018-03-09 15:13:15 -08:00
|
|
|
$result.Count | Should -Be 1
|
|
|
|
|
$result | Should -BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five"
|
2016-08-22 10:52:09 -04:00
|
|
|
}
|
2025-03-18 15:44:06 +11:00
|
|
|
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
|
|
|
|
|
}
|
2025-11-22 22:28:27 +09:00
|
|
|
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"
|
|
|
|
|
}
|
2016-07-25 14:01:41 -07:00
|
|
|
}
|