File size: 4,387 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Convert-Path tests" -Tag CI {
    BeforeAll {
        $hiddenFilePrefix = ($IsLinux -or $IsMacOS) ? '.' : ''

        $hiddenFilePath1 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test1.txt"
        $hiddenFilePath2 = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test2.txt"

        $hiddenFile1 = New-Item -Path $hiddenFilePath1 -ItemType File
        $hiddenFile2 = New-Item -Path $hiddenFilePath2 -ItemType File

        $relativeHiddenFilePath1 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test1.txt"
        $relativeHiddenFilePath2 = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test2.txt"

        if ($IsWindows) {
            $hiddenFile1.Attributes = "Hidden"
            $hiddenFile2.Attributes = "Hidden"
        }

        $hiddenFileWildcardPath = Join-Path -Path $TestDrive -ChildPath "$($hiddenFilePrefix)test*.txt"
        $relativeHiddenFileWildcardPath = ".$([System.IO.Path]::DirectorySeparatorChar)$($hiddenFilePrefix)test*.txt"
    }

    It "Convert-Path should handle provider qualified paths" {
        Convert-Path -Path "FileSystem::${TestDrive}" | Should -BeExactly "${TestDrive}"
    }

    It "Convert-Path should return the proper path" {
        Convert-Path -Path "$TestDrive" | Should -BeExactly "$TestDrive"
    }

    It "Convert-Path supports pipelined input" {
        "$TestDrive" | Convert-Path | Should -BeExactly "$TestDrive"
    }

    It "Convert-Path supports pipelined input by property name" {
        Get-Item -Path $TestDrive | Convert-Path | Should -BeExactly "$TestDrive"
    }

    It "Convert-Path without arguments is an error" {
        $ps = [powershell]::Create()
        { $ps.AddCommand("Convert-Path").Invoke() } | Should -Throw -ErrorId "ParameterBindingException"
    }

    It "Convert-Path with null path is an error" {
        { Convert-Path -Path "" } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertPathCommand"
    }

    It "Convert-Path with non-existing non-filesystem path is an error" {
        { Convert-Path -Path "env:thisvariableshouldnotexist" -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ConvertPathCommand"
    }

    It "Convert-Path can handle multiple directories" {
        $d1 = Setup -Dir -Path dir1 -PassThru
        $d2 = Setup -Dir -Path dir2 -PassThru
        $result = Convert-Path -Path "${TestDrive}/dir?"
        $result.count | Should -Be 2
        $result -join "," | Should -BeExactly (@("$d1","$d2") -join ",")
    }

    It "Convert-Path should return something which exists" {
        Convert-Path -Path $TestDrive | Should -Exist
    }

    It "Convert-Path -Path '<Path>' -Force:<Force> should return '<ExpectedResult>'" -TestCases @(
        @{
            Path           = $relativeHiddenFilePath1
            BasePath       = $TestDrive
            Force          = $false
            ExpectedResult = $hiddenFilePath1
        }
        @{
            Path           = $relativeHiddenFilePath2
            BasePath       = $TestDrive
            Force          = $false
            ExpectedResult = $hiddenFilePath2
        }
        @{
            Path           = $relativeHiddenFileWildcardPath
            BasePath       = $TestDrive
            Force          = $false
            ExpectedResult = $null
        }
        @{
            Path           = $relativeHiddenFilePath1
            BasePath       = $TestDrive
            Force          = $true
            ExpectedResult = $hiddenFilePath1
        }
        @{
            Path           = $relativeHiddenFilePath2
            BasePath       = $TestDrive
            Force          = $true
            ExpectedResult = $hiddenFilePath2
        }
        @{
            Path           = $relativeHiddenFileWildcardPath
            BasePath       = $TestDrive
            Force          = $true
            ExpectedResult = @($hiddenFilePath1, $hiddenFilePath2)
        }
    ) {
        param($Path, $BasePath, $Force, $ExpectedResult)
        try {
            Push-Location -Path $BasePath
            Convert-Path -Path $Path -Force:$Force | Should -BeExactly $ExpectedResult
        }
        finally {
            Pop-Location
        }
    }
}