# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. Describe "Get-ChildItem" -Tags "CI" { Context 'FileSystem provider' { BeforeAll { # Create Test data $max_Path = 260 $item_a = "a3fe710a-31af-4834-bc29-d0b584589838" $item_B = "B1B691A9-B7B1-4584-AED7-5259511BEEC4" $item_c = "c283d143-2116-4809-bf11-4f7d61613f92" $item_D = "D39B4FD9-3E1D-4DD5-8718-22FE2C934CE3" $item_E = "EE150FEB-0F21-4AFF-8066-AF59E925810C" $item_F = ".F81D8514-8862-4227-B041-0529B1656A43" $item_G = "5560A62F-74F1-4FAE-9A23-F4EBD90D2676" $item_H = "5f05ebca-4859-11ec-81d3-0242ac130003" $item_I = "z" * ($max_Path - $TestDrive.FullName.Length - $item_H.Length - 2) $item_J = "32d74aae-9054-4fa7-be97-8c806d10e8b9" $null = New-Item -Path $TestDrive -Name $item_a -ItemType "File" -Force $null = New-Item -Path $TestDrive -Name $item_B -ItemType "File" -Force $null = New-Item -Path $TestDrive -Name $item_c -ItemType "File" -Force $null = New-Item -Path $TestDrive -Name $item_D -ItemType "File" -Force $null = New-Item -Path $TestDrive -Name $item_E -ItemType "Directory" -Force $null = New-Item -Path $TestDrive -Name $item_F -ItemType "File" -Force | ForEach-Object {$_.Attributes = "hidden"} $null = New-Item -Path (Join-Path -Path $TestDrive -ChildPath $item_E) -Name $item_G -ItemType "File" -Force $null = New-Item -Path $TestDrive\$item_I\$item_H -Name $item_J -ItemType "Directory" -Force $searchRoot = Join-Path $TestDrive -ChildPath "TestPS" $file1 = Join-Path $searchRoot -ChildPath "D1" -AdditionalChildPath "File1.txt" $file2 = Join-Path $searchRoot -ChildPath "File1.txt" $PathWildCardTestCases = @( @{Parameters = @{Path = $searchRoot; Recurse = $true; Directory = $true }; ExpectedCount = 1; Title = "directory without wildcard"}, @{Parameters = @{Path = (Join-Path $searchRoot '*'); Recurse = $true; Directory = $true }; ExpectedCount = 1; Title = "directory with wildcard"}, @{Parameters = @{Path = $searchRoot; Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file without wildcard"}, @{Parameters = @{Path = (Join-Path $searchRoot '*'); Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file with wildcard"}, @{Parameters = @{Path = (Join-Path $searchRoot 'F*.txt'); Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file with wildcard filename"} ) } It "Should list the contents of the current folder" { (Get-ChildItem .).Name.Length | Should -BeGreaterThan 0 } It "Should list the contents of the root folder using Drive:\ notation" { (Get-ChildItem TestDrive:\).Name.Length | Should -BeGreaterThan 0 } It "Should list the contents of the root folder using Drive:\ notation from within another folder" { try { Push-Location -Path TestDrive:\$item_E (Get-ChildItem TestDrive:\ -File).Name.Length | Should -BeExactly 4 } finally { Pop-Location } } It "Should list the contents of the current folder using Drive: notation when in the root" { (Get-ChildItem TestDrive:).Name.Length | Should -BeGreaterThan 0 } It "Should list the contents of the current folder using Drive: notation when not in the root" { try { Push-Location -Path TestDrive:\$item_E (Get-ChildItem TestDrive:).Name | Should -BeExactly $item_G } finally { Pop-Location } } It "Should list the contents of the home directory" { Push-Location $HOME (Get-ChildItem .).Name.Length | Should -BeGreaterThan 0 Pop-Location } It "Should have all the proper fields and be populated" { $var = Get-ChildItem . $var.Name.Length | Should -BeGreaterThan 0 $var.Mode.Length | Should -BeGreaterThan 0 $var.LastWriteTime | Should -BeGreaterThan 0 $var.Length.Length | Should -BeGreaterThan 0 } It "Should have mode property populated for protected files on Windows" -Skip:(!$IsWindows) { $files = Get-ChildItem -Force ~\NT* $files.Count | Should -BeGreaterThan 0 foreach ($file in $files) { $file.Mode | Should -Not -BeNullOrEmpty } } It "Should list files in sorted order" { $files = Get-ChildItem -Path $TestDrive $files[0].Name | Should -Be $item_E $files[1].Name | Should -Be $item_I $files[2].Name | Should -Be $item_a $files[3].Name | Should -Be $item_B $files[4].Name | Should -Be $item_c $files[5].Name | Should -Be $item_D } It "Should list hidden files as well when 'Force' parameter is used" { $files = Get-ChildItem -Path $TestDrive -Force $files | Should -Not -BeNullOrEmpty $files.Count | Should -Be 7 $files.Name.Contains($item_F) | Should -BeTrue } It "Should list only hidden files when 'Hidden' parameter is used" { $files = Get-ChildItem -Path $TestDrive -Hidden $files | Should -Not -BeNullOrEmpty $files.Count | Should -Be 1 $files[0].Name | Should -BeExactly $item_F } It "Should find the hidden file if specified with hidden switch" { $file = Get-ChildItem -Path (Join-Path $TestDrive $item_F) -Hidden $file | Should -Not -BeNullOrEmpty $file.Count | Should -Be 1 $file.Name | Should -BeExactly $item_F } It "Should list items in current directory only with depth set to 0" { (Get-ChildItem -Path $TestDrive -Depth 0).Count | Should -Be 6 (Get-ChildItem -Path $TestDrive -Depth 0 -Include *).Count | Should -Be 6 (Get-ChildItem -Path $TestDrive -Depth 0 -Exclude IntentionallyNonexistent).Count | Should -Be 6 } It "Should return items recursively when using 'Include' or 'Exclude' parameters" { (Get-ChildItem -Path $TestDrive -Depth 1).Count | Should -Be 8 (Get-ChildItem -Path $TestDrive -Depth 1 -Include $item_G).Count | Should -Be 1 (Get-ChildItem -Path $TestDrive -Depth 1 -Exclude $item_a).Count | Should -Be 7 } It "Should return names recursively when the path has a wildcard leaf" { $root = New-Item -Path $TestDrive -Name "NameRecurse" -ItemType Directory try { $child = New-Item -Path $root -Name "Child" -ItemType Directory $null = New-Item -Path $root -Name "root.md" -ItemType File $null = New-Item -Path $child -Name "child.md" -ItemType File $null = New-Item -Path $child -Name "ignored.txt" -ItemType File $path = Join-Path $root "*.md" $result = Get-ChildItem -Path $path -Recurse -File -Name $result | Should -HaveCount 2 $result | Should -Contain "root.md" $result | Should -Contain "Child$([IO.Path]::DirectorySeparatorChar)child.md" Get-ChildItem -Path $path -Depth 0 -File -Name | Should -BeExactly "root.md" } finally { Remove-Item -LiteralPath $root -Recurse -Force } } It "Should preserve recursive wildcard filters through the public GetNames API" { $root = New-Item -Path $TestDrive -Name "PublicGetNamesRecurse" -ItemType Directory try { $child = New-Item -Path $root -Name "Child" -ItemType Directory $null = New-Item -Path $root -Name "root.md" -ItemType File $null = New-Item -Path $child -Name "child.md" -ItemType File $null = New-Item -Path $child -Name "ignored.txt" -ItemType File $path = Join-Path $root "*.md" $result = $ExecutionContext.SessionState.InvokeProvider.ChildItem.GetNames( $path, [System.Management.Automation.ReturnContainers]::ReturnMatchingContainers, $true) $result | Should -HaveCount 2 $result | Should -Contain "root.md" $result | Should -Contain "Child$([IO.Path]::DirectorySeparatorChar)child.md" } finally { Remove-Item -LiteralPath $root -Recurse -Force } } It "Should resolve recursive leaf paths from the current directory" { $root = New-Item -Path $TestDrive -Name "CurrentDirectoryRecurse" -ItemType Directory try { $child = New-Item -Path $root -Name "Child" -ItemType Directory $rootFile = New-Item -Path $root -Name "root.md" -ItemType File $childFile = New-Item -Path $child -Name "child.md" -ItemType File Push-Location $root try { $result = Get-ChildItem *.md -Recurse -File -Name $result | Should -HaveCount 2 $result | Should -Contain $rootFile.Name $result | Should -Contain "Child$([IO.Path]::DirectorySeparatorChar)$($childFile.Name)" $result = Get-ChildItem child.md -Recurse -File -Name $result | Should -HaveCount 1 $result | Should -BeExactly "Child$([IO.Path]::DirectorySeparatorChar)$($childFile.Name)" } finally { Pop-Location } } finally { Remove-Item -LiteralPath $root -Recurse -Force } } It "Should return items recursively when using 'Include' or 'Exclude' parameters with -LiteralPath" { (Get-ChildItem -LiteralPath $TestDrive -Recurse -Exclude *).Count | Should -Be 0 (Get-ChildItem -LiteralPath $TestDrive -Recurse -Include *.dll).Count | Should -Be (Get-ChildItem $TestDrive -Recurse -Include *.dll).Count (Get-ChildItem -LiteralPath $TestDrive -Depth 1 -Include $item_G).Count | Should -Be 1 (Get-ChildItem -LiteralPath $TestDrive -Depth 1 -Exclude $item_a).Count | Should -Be 7 } It "get-childitem path wildcard -