MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-ChildItem" -Tags "CI" {4 5 Context 'FileSystem provider' {6 7 BeforeAll {8 # Create Test data9 $max_Path = 26010 $item_a = "a3fe710a-31af-4834-bc29-d0b584589838"11 $item_B = "B1B691A9-B7B1-4584-AED7-5259511BEEC4"12 $item_c = "c283d143-2116-4809-bf11-4f7d61613f92"13 $item_D = "D39B4FD9-3E1D-4DD5-8718-22FE2C934CE3"14 $item_E = "EE150FEB-0F21-4AFF-8066-AF59E925810C"15 $item_F = ".F81D8514-8862-4227-B041-0529B1656A43"16 $item_G = "5560A62F-74F1-4FAE-9A23-F4EBD90D2676"17 $item_H = "5f05ebca-4859-11ec-81d3-0242ac130003"18 $item_I = "z" * ($max_Path - $TestDrive.FullName.Length - $item_H.Length - 2)19 $item_J = "32d74aae-9054-4fa7-be97-8c806d10e8b9"20 $null = New-Item -Path $TestDrive -Name $item_a -ItemType "File" -Force21 $null = New-Item -Path $TestDrive -Name $item_B -ItemType "File" -Force22 $null = New-Item -Path $TestDrive -Name $item_c -ItemType "File" -Force23 $null = New-Item -Path $TestDrive -Name $item_D -ItemType "File" -Force24 $null = New-Item -Path $TestDrive -Name $item_E -ItemType "Directory" -Force25 $null = New-Item -Path $TestDrive -Name $item_F -ItemType "File" -Force | ForEach-Object {$_.Attributes = "hidden"}26 $null = New-Item -Path (Join-Path -Path $TestDrive -ChildPath $item_E) -Name $item_G -ItemType "File" -Force27 $null = New-Item -Path $TestDrive\$item_I\$item_H -Name $item_J -ItemType "Directory" -Force28 29 $searchRoot = Join-Path $TestDrive -ChildPath "TestPS"30 $file1 = Join-Path $searchRoot -ChildPath "D1" -AdditionalChildPath "File1.txt"31 $file2 = Join-Path $searchRoot -ChildPath "File1.txt"32 33 $PathWildCardTestCases = @(34 @{Parameters = @{Path = $searchRoot; Recurse = $true; Directory = $true }; ExpectedCount = 1; Title = "directory without wildcard"},35 @{Parameters = @{Path = (Join-Path $searchRoot '*'); Recurse = $true; Directory = $true }; ExpectedCount = 1; Title = "directory with wildcard"},36 @{Parameters = @{Path = $searchRoot; Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file without wildcard"},37 @{Parameters = @{Path = (Join-Path $searchRoot '*'); Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file with wildcard"},38 @{Parameters = @{Path = (Join-Path $searchRoot 'F*.txt'); Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file with wildcard filename"}39 )40 41 }42 43 It "Should list the contents of the current folder" {44 (Get-ChildItem .).Name.Length | Should -BeGreaterThan 045 }46 47 It "Should list the contents of the root folder using Drive:\ notation" {48 (Get-ChildItem TestDrive:\).Name.Length | Should -BeGreaterThan 049 }50 51 It "Should list the contents of the root folder using Drive:\ notation from within another folder" {52 try53 {54 Push-Location -Path TestDrive:\$item_E55 (Get-ChildItem TestDrive:\ -File).Name.Length | Should -BeExactly 456 }57 finally58 {59 Pop-Location60 }61 }62 63 It "Should list the contents of the current folder using Drive: notation when in the root" {64 (Get-ChildItem TestDrive:).Name.Length | Should -BeGreaterThan 065 }66 67 It "Should list the contents of the current folder using Drive: notation when not in the root" {68 try69 {70 Push-Location -Path TestDrive:\$item_E71 (Get-ChildItem TestDrive:).Name | Should -BeExactly $item_G72 }73 finally74 {75 Pop-Location76 }77 }78 79 It "Should list the contents of the home directory" {80 Push-Location $HOME81 (Get-ChildItem .).Name.Length | Should -BeGreaterThan 082 Pop-Location83 }84 85 It "Should have all the proper fields and be populated" {86 $var = Get-ChildItem .87 88 $var.Name.Length | Should -BeGreaterThan 089 $var.Mode.Length | Should -BeGreaterThan 090 $var.LastWriteTime | Should -BeGreaterThan 091 $var.Length.Length | Should -BeGreaterThan 092 }93 94 It "Should have mode property populated for protected files on Windows" -Skip:(!$IsWindows) {95 $files = Get-ChildItem -Force ~\NT*96 $files.Count | Should -BeGreaterThan 097 foreach ($file in $files)98 {99 $file.Mode | Should -Not -BeNullOrEmpty100 }101 }102 103 It "Should list files in sorted order" {104 $files = Get-ChildItem -Path $TestDrive105 $files[0].Name | Should -Be $item_E106 $files[1].Name | Should -Be $item_I107 $files[2].Name | Should -Be $item_a108 $files[3].Name | Should -Be $item_B109 $files[4].Name | Should -Be $item_c110 $files[5].Name | Should -Be $item_D111 }112 113 It "Should list hidden files as well when 'Force' parameter is used" {114 $files = Get-ChildItem -Path $TestDrive -Force115 $files | Should -Not -BeNullOrEmpty116 $files.Count | Should -Be 7117 $files.Name.Contains($item_F) | Should -BeTrue118 }119 120 It "Should list only hidden files when 'Hidden' parameter is used" {121 $files = Get-ChildItem -Path $TestDrive -Hidden122 $files | Should -Not -BeNullOrEmpty123 $files.Count | Should -Be 1124 $files[0].Name | Should -BeExactly $item_F125 }126 It "Should find the hidden file if specified with hidden switch" {127 $file = Get-ChildItem -Path (Join-Path $TestDrive $item_F) -Hidden128 $file | Should -Not -BeNullOrEmpty129 $file.Count | Should -Be 1130 $file.Name | Should -BeExactly $item_F131 }132 133 It "Should list items in current directory only with depth set to 0" {134 (Get-ChildItem -Path $TestDrive -Depth 0).Count | Should -Be 6135 (Get-ChildItem -Path $TestDrive -Depth 0 -Include *).Count | Should -Be 6136 (Get-ChildItem -Path $TestDrive -Depth 0 -Exclude IntentionallyNonexistent).Count | Should -Be 6137 }138 139 It "Should return items recursively when using 'Include' or 'Exclude' parameters" {140 (Get-ChildItem -Path $TestDrive -Depth 1).Count | Should -Be 8141 (Get-ChildItem -Path $TestDrive -Depth 1 -Include $item_G).Count | Should -Be 1142 (Get-ChildItem -Path $TestDrive -Depth 1 -Exclude $item_a).Count | Should -Be 7143 }144 145 It "Should return items recursively when using 'Include' or 'Exclude' parameters with -LiteralPath" {146 (Get-ChildItem -LiteralPath $TestDrive -Recurse -Exclude *).Count | Should -Be 0147 (Get-ChildItem -LiteralPath $TestDrive -Recurse -Include *.dll).Count | Should -Be (Get-ChildItem $TestDrive -Recurse -Include *.dll).Count148 (Get-ChildItem -LiteralPath $TestDrive -Depth 1 -Include $item_G).Count | Should -Be 1149 (Get-ChildItem -LiteralPath $TestDrive -Depth 1 -Exclude $item_a).Count | Should -Be 7150 }151 152 It "get-childitem path wildcard - <title>" -TestCases $PathWildCardTestCases {153 param($Parameters, $ExpectedCount)154 155 $null = New-Item $file1 -Force -ItemType File156 157 (Get-ChildItem @Parameters).Count | Should -Be $ExpectedCount158 }159 160 It "get-childitem with and without file in search root" {161 $null = New-Item $file2 -Force -ItemType File162 163 (Get-ChildItem -Path $searchRoot -File -Recurse).Count | Should -Be 2164 (Get-ChildItem -Path $searchRoot -Directory -Recurse).Count | Should -Be 1165 166 Remove-Item $file2 -ErrorAction SilentlyContinue -Force167 (Get-ChildItem -Path $searchRoot -File -Recurse).Count | Should -Be 1168 (Get-ChildItem -Path $searchRoot -Directory -Recurse).Count | Should -Be 1169 }170 171 # VSTS machines don't have a page file172 It "Should give .sys file if the fullpath is specified with hidden and force parameter" -Pending {173 # Don't remove!!! It is special test for hidden and opened file with exclusive lock.174 $file = Get-ChildItem -Path "$env:SystemDrive\\pagefile.sys" -Hidden175 $file | Should -Not -Be $null176 $file.Count | Should -Be 1177 $file.Name | Should -Be "pagefile.sys"178 }179 180 It "-Filter *. finds extension-less files" {181 $null = New-Item -Path TestDrive:/noextension -ItemType File182 (Get-ChildItem -File -LiteralPath TestDrive:/ -Filter noext*.*).Name | Should -BeExactly 'noextension'183 }184 185 # Because "dotnet API is not available, see PR 16044186 It "Understand APPEXECLINKs" -Skip {187 $app = Get-ChildItem -Path ~\appdata\local\microsoft\windowsapps\*.exe | Select-Object -First 1188 $app.Target | Should -Not -Be $app.FullName189 $app.LinkType | Should -BeExactly 'AppExeCLink'190 }191 192 It "Wildcard matching behavior is the same between -Path and -Include parameters when using escape characters" {193 $oldLocation = Get-Location194 195 try {196 Set-Location $TestDrive197 198 $expectedPath = 'a`[b]'199 $escapedPath = 'a```[b`]'200 New-Item -Type File $expectedPath -ErrorAction SilentlyContinue > $null201 202 $WithInclude = Get-ChildItem * -Include $escapedPath203 $WithPath = Get-ChildItem -Path $escapedPath204 205 $WithInclude.Name | Should -BeExactly $expectedPath206 $WithPath.Name | Should -BeExactly $expectedPath207 208 } finally {209 Set-Location $oldLocation210 }211 }212 213 It "Should list the folder present when path length equal to MAX_PATH" {214 (Get-ChildItem -Path TestDrive:\$item_I -Recurse -Force).Name.Length | Should -BeGreaterThan 0215 }216 217 It 'Trailing slash for -Path should treat it as a folder only when used with -recurse' {218 $foo = New-Item -ItemType Directory -Path TestDrive:\foo219 $foo2 = New-Item -ItemType Directory -Path TestDrive:\foo\foo220 $bar = New-Item -ItemType File -Path TestDrive:\foo\bar221 $bar2 = New-Item -ItemType File -Path TestDrive:\foo\foo\bar222 223 { Get-ChildItem -Path testdrive:/foo/bar/ -Recurse -ErrorAction Stop } | Should -Throw -ErrorId 'ItemNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand'224 $barFiles = Get-ChildItem -Path testdrive:/foo/bar -Recurse225 $barFiles.Count | Should -Be 2226 }227 228 It 'Works with Windows volume paths' -Skip:(!$IsWindows) {229 $winPath = $env:windir230 if (! $winPath) {231 Set-ItResult -Skipped -Because "windir is null"232 return233 }234 235 $driveLetter = $winPath[0]236 $winPartialPath = $winPath.SubString(3) # skip the drive letter, colon, and backslash237 Write-Verbose -Verbose "Partial path is '$winPartialPath'"238 $volume = (Get-Volume -DriveLetter $driveLetter).Path239 if (! $volume) {240 Set-ItResult -Skipped -Because "Get-Volume returned no volume for system drive '$driveLetter'"241 return242 }243 244 $items = Get-ChildItem -LiteralPath "${volume}${winPartialPath}"245 Write-Verbose -Verbose "Trying files in '${volume}${winPartialPath}'"246 if ($items.Count -eq 0) {247 Write-Verbose -Verbose "`$items is null!!"248 }249 250 $items[0].Parent.FullName | Should -BeExactly "${volume}${winPartialPath}"251 $items | Should -HaveCount (Get-ChildItem $winPath).Count252 }253 254 It 'Works with Windows pipes' -Skip:(!$IsWindows) {255 $out = pwsh -noprofile -custompipename myTestPipe { Get-ChildItem \\.\pipe\myTestPipe }256 $out.Name | Should -BeExactly 'myTestPipe'257 }258 }259 260 Context 'Env: Provider' {261 262 It 'can handle mixed case in Env variables' {263 try264 {265 $env:__FOODBAR = 'food'266 $env:__foodbar = 'bar'267 268 $foodbar = Get-ChildItem env: | Where-Object {$_.Name -eq '__foodbar'}269 $count = if ($IsWindows) { 1 } else { 2 }270 ($foodbar | Measure-Object).Count | Should -Be $count271 }272 catch273 {274 Get-ChildItem env: | Where-Object {$_.Name -eq '__foodbar'} | Remove-Item -ErrorAction SilentlyContinue275 }276 }277 }278}279 280Describe "Get-ChildItem with special path" -Tags "CI" {281 282 BeforeAll {283 $bracketDirName = "Test[Dir]"284 $bracketDir = "Test``[Dir``]"285 $bracketPath = Join-Path $TestDrive $bracketDir286 $null = New-Item -Path $TestDrive -Name $bracketDirName -ItemType Directory -Force287 }288 289 It "Should list files in directory with name containing bracket char" {290 $null = New-Item -Path $bracketPath -Name file1.txt -ItemType File291 $null = New-Item -Path $bracketPath -Name file2.txt -ItemType File292 Get-ChildItem -Path $bracketPath | Should -HaveCount 2293 }294}295 296Describe 'FileSystem Provider Formatting' -Tag "CI","RequireAdminOnWindows" {297 298 BeforeAll {299 $modeTestDir = New-Item -Path "$TestDrive/testmodedirectory" -ItemType Directory -Force300 $targetFile1 = New-Item -Path "$TestDrive/targetFile1" -ItemType File -Force301 $targetFile2 = New-Item -Path "$TestDrive/targetFile2" -ItemType File -Force302 $targetDir1 = New-Item -Path "$TestDrive/targetDir1" -ItemType Directory -Force303 $targetDir2 = New-Item -Path "$TestDrive/targetDir2" -ItemType Directory -Force304 305 $testcases = @(306 @{ expectedMode = "d----"; expectedModeWithoutHardlink = "d----"; itemType = "Directory"; itemName = "Directory"; fileAttributes = [System.IO.FileAttributes] "Directory"; target = $null }307 @{ expectedMode = "l----"; expectedModeWithoutHardlink = "l----"; itemType = "SymbolicLink"; itemName = "SymbolicLink-Directory"; fileAttributes = [System.IO.FileAttributes]::Directory -bor [System.IO.FileAttributes]::ReparsePoint; target = $targetDir2.FullName }308 )309 310 if ($IsWindows)311 {312 # arm64 adds the archive attribute313 $junctionMode = (Test-IsWindowsArm64) ? "la---" : "l----"314 $armFileAttributes = (Test-IsWindowsArm64) ? [System.IO.FileAttributes]"Directory,Archive,ReparsePoint" : [System.IO.FileAttributes]"Directory,ReparsePoint"315 $testcases += @{ expectedMode = $junctionMode; expectedModeWithoutHardlink = $junctionMode; itemType = "Junction"; itemName = "Junction-Directory"; fileAttributes = $armFileAttributes; target = $targetDir1.FullName }316 $testcases += @{ expectedMode = "-a---"; expectedModeWithoutHardlink = "-a---"; itemType = "File"; itemName = "ArchiveFile"; fileAttributes = [System.IO.FileAttributes] "Archive"; target = $null }317 $testcases += @{ expectedMode = "la---"; expectedModeWithoutHardlink = "la---"; itemType = "SymbolicLink"; itemName = "SymbolicLink-File"; fileAttributes = [System.IO.FileAttributes]::Archive -bor [System.IO.FileAttributes]::ReparsePoint; target = $targetFile1.FullName }318 $testcases += @{ expectedMode = "la---"; expectedModeWithoutHardlink = "-a---"; itemType = "HardLink"; itemName = "HardLink"; fileAttributes = [System.IO.FileAttributes] "Archive"; target = $targetFile2.FullName }319 }320 }321 322 It 'Validate Mode property - <itemName>' -TestCases $testcases {323 324 param($expectedMode, $expectedModeWithoutHardlink, $itemType, $itemName, $fileAttributes, $target)325 326 $item = if ($target)327 {328 New-Item -Path $modeTestDir -Name $itemName -ItemType $itemType -Target $target329 }330 else331 {332 New-Item -Path $modeTestDir -Name $itemName -ItemType $itemType333 }334 335 $item | Should -BeOfType System.IO.FileSystemInfo336 337 $actualMode = [Microsoft.PowerShell.Commands.FileSystemProvider]::Mode($item)338 $actualMode | Should -BeExactly $expectedMode339 340 $actualModeWithoutHardlink = [Microsoft.PowerShell.Commands.FileSystemProvider]::ModeWithoutHardlink($item)341 $actualModeWithoutHardlink | Should -BeExactly $expectedModeWithoutHardlink342 343 $item.Attributes | Should -Be $fileAttributes344 }345}346 