MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Convert-Path tests" -Tag CI {4 BeforeAll {5 $hiddenFilePrefix = ($IsLinux -or $IsMacOS) ? '.' : ''6 7 $hiddenFilePath1 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test1.txt"8 $hiddenFilePath2 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test2.txt"9 10 $hiddenFile1 = New-Item -Path $hiddenFilePath1 -ItemType File11 $hiddenFile2 = New-Item -Path $hiddenFilePath2 -ItemType File12 13 $relativeHiddenFilePath1 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test1.txt"14 $relativeHiddenFilePath2 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test2.txt"15 16 if ($IsWindows) {17 $hiddenFile1.Attributes = "Hidden"18 $hiddenFile2.Attributes = "Hidden"19 }20 21 $hiddenFileWildcardPath = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test*.txt"22 $relativeHiddenFileWildcardPath = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test*.txt"23 }24 25 It "Convert-Path should handle provider qualified paths" {26 Convert-Path -Path "FileSystem::${TestDrive}" | Should -BeExactly "${TestDrive}"27 }28 29 It "Convert-Path should return the proper path" {30 Convert-Path -Path "$TestDrive" | Should -BeExactly "$TestDrive"31 }32 33 It "Convert-Path supports pipelined input" {34 "$TestDrive" | Convert-Path | Should -BeExactly "$TestDrive"35 }36 37 It "Convert-Path supports pipelined input by property name" {38 Get-Item -Path $TestDrive | Convert-Path | Should -BeExactly "$TestDrive"39 }40 41 It "Convert-Path without arguments is an error" {42 $ps = [powershell]::Create()43 { $ps.AddCommand("Convert-Path").Invoke() } | Should -Throw -ErrorId "ParameterBindingException"44 }45 46 It "Convert-Path with null path is an error" {47 { Convert-Path -Path "" } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertPathCommand"48 }49 50 It "Convert-Path with non-existing non-filesystem path is an error" {51 { Convert-Path -Path "env:thisvariableshouldnotexist" -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ConvertPathCommand"52 }53 54 It "Convert-Path can handle multiple directories" {55 $d1 = Setup -Dir -Path dir1 -PassThru56 $d2 = Setup -Dir -Path dir2 -PassThru57 $result = Convert-Path -Path "${TestDrive}/dir?"58 $result.count | Should -Be 259 $result -join "," | Should -BeExactly (@("$d1","$d2") -join ",")60 }61 62 It "Convert-Path should return something which exists" {63 Convert-Path -Path $TestDrive | Should -Exist64 }65 66 It "Convert-Path -Path '<Path>' -Force:<Force> should return '<ExpectedResult>'" -TestCases @(67 @{68 Path = $relativeHiddenFilePath169 BasePath = $TestDrive70 Force = $false71 ExpectedResult = $hiddenFilePath172 }73 @{74 Path = $relativeHiddenFilePath275 BasePath = $TestDrive76 Force = $false77 ExpectedResult = $hiddenFilePath278 }79 @{80 Path = $relativeHiddenFileWildcardPath81 BasePath = $TestDrive82 Force = $false83 ExpectedResult = $null84 }85 @{86 Path = $relativeHiddenFilePath187 BasePath = $TestDrive88 Force = $true89 ExpectedResult = $hiddenFilePath190 }91 @{92 Path = $relativeHiddenFilePath293 BasePath = $TestDrive94 Force = $true95 ExpectedResult = $hiddenFilePath296 }97 @{98 Path = $relativeHiddenFileWildcardPath99 BasePath = $TestDrive100 Force = $true101 ExpectedResult = @($hiddenFilePath1, $hiddenFilePath2)102 }103 ) {104 param($Path, $BasePath, $Force, $ExpectedResult)105 try {106 Push-Location -Path $BasePath107 Convert-Path -Path $Path -Force:$Force | Should -BeExactly $ExpectedResult108 }109 finally {110 Pop-Location111 }112 }113}114 