Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

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