Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Test-Path.Tests.ps1258 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Test-Path" -Tags "CI" {4    BeforeAll {5        $testdirectory = $TestDrive6        $testfilename = New-Item -Path $testdirectory -Name testfile.txt -ItemType file -Value 1 -Force7 8        # populate with additional files9        New-Item -Path $testdirectory -Name datestfile -Value 1 -ItemType file | Out-Null10        New-Item -Path $testdirectory -Name gatestfile -Value 1 -ItemType file | Out-Null11        New-Item -Path $testdirectory -Name usr -Value 1 -ItemType directory | Out-Null12 13        $nonExistentDir = Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin14        $nonExistentPath = Join-Path -Path (Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin) -ChildPath error15 16        $today = Get-Date17        $oneDayOld = (Get-Date).AddDays(-1)18        $twoDaysOld = (Get-Date).AddDays(-2)19 20        $oldFilePath = Join-Path -Path $testdirectory -ChildPath oldfile21        $oldFile = New-Item -Path $oldFilePath -ItemType File22        $oldFile.LastWriteTime = $oneDayOld23 24        $oldDirPath = Join-Path -Path $testdirectory -ChildPath olddir25        $oldDir = New-Item -Path $oldDirPath -ItemType Directory26        $oldDir.LastWriteTime = $oneDayOld27 28        $newFilePath = Join-Path -Path $testdirectory -ChildPath newfile29        New-Item -Path $newFilePath -ItemType File | Out-Null30 31        $newDirPath = Join-Path -Path $testdirectory -ChildPath newdir32        New-Item -Path $newDirPath -ItemType Directory | Out-Null33    }34 35    It "Should be called on an existing path without error" {36        { Test-Path $testdirectory }              | Should -Not -Throw37        { Test-Path -Path $testdirectory }        | Should -Not -Throw38        { Test-Path -LiteralPath $testdirectory } | Should -Not -Throw39    }40 41    It "Should allow piping objects to it" {42        { $testdirectory | Test-Path  } | Should -Not -Throw43 44        $testdirectory                  | Test-Path | Should -BeTrue45        $nonExistentDir                 | Test-Path | Should -BeFalse46    }47 48    It "Should be called on a nonexistent path without error" {49        { Test-Path -Path $nonExistentPath } | Should -Not -Throw50    }51 52    It "Should return false for a nonexistent path" {53        Test-Path -Path $nonExistentPath | Should -BeFalse54    }55 56    It "Should return true for an existing path" {57        { Test-Path -Path $testdirectory } | Should -BeTrue58    }59 60    It 'Should return false for an empty string' {61        Test-Path -Path '' | Should -BeFalse62    }63 64    It 'Should return false for a whitespace string' {65        Test-Path -Path '  ' | Should -BeFalse66    }67 68    It 'Should write a non-terminating error when given a null path' {69        # This ensures the error is non-terminating; a terminating error would fail the first test70        { Test-Path -Path $null -ErrorAction SilentlyContinue } | Should -Not -Throw71        { Test-Path -Path $null -ErrorAction Stop }             | Should -Throw -ErrorId 'NullPathNotPermitted,Microsoft.PowerShell.Commands.TestPathCommand'72    }73 74    It 'Should write a non-terminating error when given an array of null paths' {75        # This ensures the error is non-terminating; a terminating error would fail the first test76        { Test-Path -Path $null, $null -ErrorAction SilentlyContinue } | Should -Not -Throw77        { Test-Path -Path $null, $null -ErrorAction Stop }             | Should -Throw -ErrorId 'NullPathNotPermitted,Microsoft.PowerShell.Commands.TestPathCommand'78    }79 80    It "Should be able to accept a regular expression" {81        { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") }           | Should -Not -Throw82        { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") }      | Should -Not -Throw83    }84 85    It "Should be able to return the correct result when a regular expression is used" {86        Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*")               | Should -BeTrue87        Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]*")          | Should -BeTrue88 89        Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "aoeu*")            | Should -BeFalse90        Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[A-Z]")           | Should -BeFalse91    }92 93    It "Should return false when the Leaf pathtype is used on a directory" {94        Test-Path -Path $testdirectory -PathType Leaf | Should -BeFalse95    }96 97    It "Should return true when the Leaf pathtype is used on an existing endpoint" {98        Test-Path -Path $testfilename -PathType Leaf | Should -BeTrue99    }100 101    It "Should return false when the Leaf pathtype is used on a nonexistent file" {102        Test-Path -Path "aoeu" -PathType Leaf | Should -BeFalse103    }104 105    It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" {106        Test-Path -Path $testfilename -Type Leaf | Should -BeTrue107    }108 109    It "Should be able to search multiple regular expressions using the include switch" {110        { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Include t* } | Should -BeTrue111    }112 113    It "Should be able to exclude a regular expression using the exclude switch" {114        { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Exclude v* } | Should -BeTrue115    }116 117    It "Should be able to exclude multiple regular expressions using the exclude switch" {118        # tests whether there's any files in the usr directory that don't start with 'd' or 'g'119        { Test-Path -Path $testfilename -Exclude d*, g* } | Should -BeTrue120    }121 122    It "Should return true if the syntax of the path is correct when using the IsValid switch" {123        { Test-Path -Path $nonExistentPath -IsValid } | Should -BeTrue124    }125 126    It "Should return false if the syntax of the path is incorrect when using the IsValid switch" {127        $badPath = " :;!@#$%^&*(){}?+|_-"128        Test-Path -Path $badPath -IsValid | Should -BeFalse129    }130 131    It 'Windows paths should be valid: <path>' -skip:(!$IsWindows) -TestCases @(132        @{ path = "C:\Program Files" }133        @{ path = "C:\Program Files (x86)\" }134        @{ path = "filesystem::z:\foo" }135        @{ path = "filesystem::z:\foo\" }136        @{ path = "variable::psversiontable" }137        @{ path = "c:\windows\cmd.exe:test" }138    ) {139        param($path)140        Test-Path -Path $path -IsValid | Should -BeTrue141    }142 143    It 'Windows paths should be inavlid: <variant>' -Skip:(!$IsWindows) -TestCases @(144        @{ variant = "wildcard"; path = "C:\Program Files\foo*" }145        @{ variant = "pipe symbol"; path = "C:\Program Files|p\foo" }146        @{ variant = "null char"; path = "C:\Win`u{0000}dows\System32" }147    ) {148        param($path)149        Test-Path -Path $path -IsValid | Should -BeFalse150    }151 152    It 'Unix paths should be valid: <path>' -Skip:($IsWindows) -TestCases @(153        @{ path = "/usr/bin" }154        @{ path = "/usr/bin/" }155        @{ path = "filesystem::/usr/bin" }156        @{ path = "filesystem::/usr/bin/" }157        @{ path = "variable::psversiontable" }158        @{ path = "/usr/bi*n/test" }159        @{ path = "/usr/bin/tes*t" }160    ) {161        param($path)162        Test-Path -Path $path -IsValid | Should -BeTrue163    }164 165    It 'Unix paths should be invalid: <variant>' -Skip:($IsWindows) -TestCases @(166        @{ variant = "null in path"; path = "/usr/bi`u{0000}n/test" }167        @{ variant = "null in filename"; path = "/usr/bin/t`u{0000}est" }168    ) {169        param($path)170        Test-Path -Path $path -IsValid | Should -BeFalse171    }172 173    It "Should return true on paths containing spaces when the path is surrounded in quotes" {174        Test-Path -Path "/totally a valid/path" -IsValid | Should -BeTrue175    }176 177    It "Should throw on paths containing spaces when the path is not surrounded in quotes" {178        { Test-Path -Path /a path/without quotes/around/it -IsValid } | Should -Throw -ErrorId "PositionalParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand"179    }180 181    It "Should return true if a directory leads or trails with a space when surrounded by quotes" {182        Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should -BeTrue183    }184 185    It "Should return true on a valid path when the LiteralPath switch is used" {186        Test-Path -LiteralPath $testfilename | Should -BeTrue187    }188 189    It "Should return false if regular expressions are used with the LiteralPath switch" {190        Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u*")            | Should -BeFalse191        Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r")       | Should -BeFalse192    }193 194    It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" {195        Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u*")            | Should -BeFalse196        Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r")       | Should -BeFalse197    }198 199    It "Should return true if used on components other than filesystem objects" {200        Test-Path Alias:\gci | Should -BeTrue201        Test-Path Env:\PATH  | Should -BeTrue202    }203 204    It "Should return true if NewerThan is used and path is newer than one day" {205        Test-Path -Path $newFilePath -PathType Leaf -NewerThan $oneDayOld | Should -BeTrue206        Test-Path -Path $newDirPath -PathType Container -NewerThan $oneDayOld | Should -BeTrue207        Test-Path -Path $newFilePath -PathType Any -NewerThan $oneDayOld | Should -BeTrue208        Test-Path -Path $newDirPath -PathType Any -NewerThan $oneDayOld | Should -BeTrue209        Test-Path -Path $newFilePath -NewerThan $oneDayOld | Should -BeTrue210        Test-Path -Path $newDirPath -NewerThan $oneDayOld | Should -BeTrue211    }212 213    It "Should return false if NewerThan is used and path is not newer than today" {214        Test-Path -Path $oldFilePath -PathType Leaf -NewerThan $today | Should -BeFalse215        Test-Path -Path $oldDirPath -PathType Container -NewerThan $today | Should -BeFalse216        Test-Path -Path $oldFilePath -PathType Any -NewerThan $today | Should -BeFalse217        Test-Path -Path $oldDirPath -PathType Any -NewerThan $today | Should -BeFalse218        Test-Path -Path $oldFilePath -NewerThan $today | Should -BeFalse219        Test-Path -Path $oldDirPath -NewerThan $today | Should -BeFalse220    }221 222    It "Should return true if OlderThan is used and path is older than today" {223        Test-Path -Path $oldFilePath -PathType Leaf -OlderThan $today | Should -BeTrue224        Test-Path -Path $oldDirPath -PathType Container -OlderThan $today | Should -BeTrue225        Test-Path -Path $oldFilePath -PathType Any -OlderThan $today | Should -BeTrue226        Test-Path -Path $oldDirPath -PathType Any -OlderThan $today | Should -BeTrue227        Test-Path -Path $oldFilePath -OlderThan $today | Should -BeTrue228        Test-Path -Path $oldDirPath -OlderThan $today | Should -BeTrue229    }230 231    It "Should return false if OlderThan is used and path is not older than one day" {232        Test-Path -Path $newFilePath -PathType Leaf -OlderThan $oneDayOld | Should -BeFalse233        Test-Path -Path $newDirPath -PathType Container -OlderThan $oneDayOld | Should -BeFalse234        Test-Path -Path $newFilePath -PathType Any -OlderThan $oneDayOld | Should -BeFalse235        Test-Path -Path $newDirPath -PathType Any -OlderThan $oneDayOld | Should -BeFalse236        Test-Path -Path $newFilePath -OlderThan $oneDayOld | Should -BeFalse237        Test-Path -Path $newDirPath -OlderThan $oneDayOld | Should -BeFalse238    }239 240    It "Should return true if OlderThan and NewerThan is used together and path exists in date range" {241        Test-Path -Path $oldFilePath -PathType Leaf -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue242        Test-Path -Path $oldDirPath -PathType Container -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue243        Test-Path -Path $oldFilePath -PathType Any -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue244        Test-Path -Path $oldDirPath -PathType Any -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue245        Test-Path -Path $oldFilePath -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue246        Test-Path -Path $oldDirPath -NewerThan $twoDaysOld -OlderThan $today | Should -BeTrue247    }248 249    It "Should return false if OlderThan and NewerThan is used together and path does not exist in date range" {250        Test-Path -Path $newFilePath -PathType Leaf -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse251        Test-Path -Path $newDirPath -PathType Container -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse252        Test-Path -Path $newFilePath -PathType Any -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse253        Test-Path -Path $newDirPath -PathType Any -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse254        Test-Path -Path $newFilePath -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse255        Test-Path -Path $newDirPath -NewerThan $twoDaysOld -OlderThan $oneDayOld | Should -BeFalse256    }257}258