MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Resolve-Path returns proper path" -Tag "CI" {4 BeforeAll {5 $driveName = "RvpaTest"6 $root = Join-Path $TestDrive "fakeroot"7 $file = Join-Path $root "file.txt"8 $null = New-Item -Path $root -ItemType Directory -Force9 $null = New-Item -Path $file -ItemType File -Force10 $null = New-PSDrive -Name $driveName -PSProvider FileSystem -Root $root11 12 $testRoot = Join-Path $TestDrive ""13 $fakeRoot = Join-Path "$driveName`:" ""14 15 $relCases = @(16 @{ wd = $fakeRoot; target = $testRoot; expected = $testRoot }17 @{ wd = $testRoot; target = Join-Path $fakeRoot "file.txt"; expected = Join-Path "." "fakeroot" "file.txt" }18 )19 20 $hiddenFilePrefix = ($IsLinux -or $IsMacOS) ? '.' : ''21 22 $hiddenFilePath1 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test1.txt"23 $hiddenFilePath2 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test2.txt"24 25 $hiddenFile1 = New-Item -Path $hiddenFilePath1 -ItemType File26 $hiddenFile2 = New-Item -Path $hiddenFilePath2 -ItemType File27 28 $relativeHiddenFilePath1 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test1.txt"29 $relativeHiddenFilePath2 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test2.txt"30 31 if ($IsWindows) {32 $hiddenFile1.Attributes = "Hidden"33 $hiddenFile2.Attributes = "Hidden"34 }35 36 $hiddenFileWildcardPath = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test*.txt"37 $relativeHiddenFileWildcardPath = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test*.txt"38 }39 AfterAll {40 Remove-PSDrive -Name $driveName -Force41 }42 It "Resolve-Path returns resolved paths" {43 Resolve-Path $TESTDRIVE | Should -BeExactly "$TESTDRIVE"44 }45 It "Resolve-Path handles provider qualified paths" {46 $result = Resolve-Path Filesystem::$TESTDRIVE47 $result.providerpath | Should -BeExactly "$TESTDRIVE"48 }49 It "Resolve-Path provides proper error on invalid location" {50 { Resolve-Path $TESTDRIVE/this.directory.is.invalid -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"51 }52 It "Resolve-Path -Path should return correct drive path" {53 $result = Resolve-Path -Path "TestDrive:\\\\\"54 ($result.Path.TrimEnd('/\')) | Should -BeExactly "TestDrive:"55 }56 It "Resolve-Path -LiteralPath should return correct drive path" {57 $result = Resolve-Path -LiteralPath "TestDrive:\\\\\"58 ($result.Path.TrimEnd('/\')) | Should -BeExactly "TestDrive:"59 }60 It "Resolve-Path -Relative '<target>' should return correct path on '<wd>'" -TestCases $relCases {61 param($wd, $target, $expected)62 try {63 Push-Location -Path $wd64 Resolve-Path -Path $target -Relative | Should -BeExactly $expected65 }66 finally {67 Pop-Location68 }69 }70 It 'Resolve-Path RelativeBasePath should handle <Scenario>' -TestCases @(71 @{72 Scenario = "Absolute Path, Absolute ReleativeBasePath"73 Path = $root74 Basepath = $testRoot75 Expected = $root, ".$([System.IO.Path]::DirectorySeparatorChar)fakeroot"76 CD = $null77 }78 @{79 Scenario = "Relative Path, Absolute ReleativeBasePath"80 Path = ".$([System.IO.Path]::DirectorySeparatorChar)fakeroot"81 Basepath = $testRoot82 Expected = $root, ".$([System.IO.Path]::DirectorySeparatorChar)fakeroot"83 CD = $null84 }85 @{86 Scenario = "Relative Path, Relative ReleativeBasePath"87 Path = ".$([System.IO.Path]::DirectorySeparatorChar)fakeroot"88 Basepath = ".$([System.IO.Path]::DirectorySeparatorChar)"89 Expected = $root, ".$([System.IO.Path]::DirectorySeparatorChar)fakeroot"90 CD = $testRoot91 }92 @{93 Scenario = "Invalid Path, Absolute ReleativeBasePath"94 Path = Join-Path $testRoot ThisPathDoesNotExist95 Basepath = $root96 Expected = $null97 CD = $null98 }99 @{100 Scenario = "Invalid Path, Invalid ReleativeBasePath"101 Path = Join-Path $testRoot ThisPathDoesNotExist102 Basepath = Join-Path $testRoot ThisPathDoesNotExist103 Expected = $null104 CD = $null105 }106 ) -Test {107 param($Path, $BasePath, $Expected, $CD)108 109 if ($null -eq $Expected)110 {111 {Resolve-Path -Path $Path -RelativeBasePath $BasePath -ErrorAction Stop} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"112 {Resolve-Path -Path $Path -RelativeBasePath $BasePath -ErrorAction Stop -Relative} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"113 }114 else115 {116 try117 {118 $OldLocation = if ($null -ne $CD)119 {120 $PWD121 Set-Location $CD122 }123 124 (Resolve-Path -Path $Path -RelativeBasePath $BasePath).ProviderPath | Should -BeExactly $Expected[0]125 Resolve-Path -Path $Path -RelativeBasePath $BasePath -Relative | Should -BeExactly $Expected[1]126 }127 finally128 {129 if ($null -ne $OldLocation)130 {131 Set-Location $OldLocation132 }133 }134 }135 }136 137 It "Resolve-Path -Path '<Path>' -RelativeBasePath '<BasePath>' -Force:<Force> should return '<ExpectedResult>'" -TestCases @(138 @{139 Path = $relativeHiddenFilePath1140 BasePath = $TestDrive141 Force = $false142 ExpectedResult = $hiddenFilePath1143 }144 @{145 Path = $relativeHiddenFilePath2146 BasePath = $TestDrive147 Force = $false148 ExpectedResult = $hiddenFilePath2149 }150 @{151 Path = $relativeHiddenFileWildcardPath152 BasePath = $TestDrive153 Force = $false154 ExpectedResult = $null155 }156 @{157 Path = $relativeHiddenFilePath1158 BasePath = $TestDrive159 Force = $true160 ExpectedResult = $hiddenFilePath1161 }162 @{163 Path = $relativeHiddenFilePath2164 BasePath = $TestDrive165 Force = $true166 ExpectedResult = $hiddenFilePath2167 }168 @{169 Path = $relativeHiddenFileWildcardPath170 BasePath = $TestDrive171 Force = $true172 ExpectedResult = @($hiddenFilePath1, $hiddenFilePath2)173 }174 ) {175 param($Path, $BasePath, $Force, $ExpectedResult)176 (Resolve-Path -Path $Path -RelativeBasePath $BasePath -Force:$Force).Path | Should -BeExactly $ExpectedResult177 }178}179 