Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/System.Management.Automation/engine/SessionStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,22 @@ internal void GetChildItems(
// expectations:
if (recurse)
{
string childName = GetChildName(path, context);

// If -File or -Directory is specified and path is ended with '*', we should include the parent path as search path

bool isFileOrDirectoryPresent = false;

if(context.DynamicParameters is Microsoft.PowerShell.Commands.GetChildDynamicParameters dynParam)
{
isFileOrDirectoryPresent = dynParam.File.IsPresent || dynParam.Directory.IsPresent;
}

if (String.Equals(childName, "*", StringComparison.OrdinalIgnoreCase) && isFileOrDirectoryPresent)
{
string parentName = path.Substring(0, path.Length - childName.Length);
path = parentName;
}
// dir c:\tem* -include *.ps1 -rec => No change
if ((context.Include == null) || (context.Include.Count == 0))
{
Expand All @@ -1509,9 +1525,8 @@ internal void GetChildItems(
// Should glob paths and files that match tem*, but then
// recurse into all subdirectories and do the same for
// those directories.
if ((!String.IsNullOrEmpty(path)) && (!IsItemContainer(path)))
if (!String.IsNullOrEmpty(path) && !IsItemContainer(path))
{
string childName = GetChildName(path, context);
if (!String.Equals(childName, "*", StringComparison.OrdinalIgnoreCase))
{
if (context.Include != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ Describe "Get-ChildItem" -Tags "CI" {
$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_G = "5560A62F-74F1-4FAE-9A23-F4EBD90D2676"
$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

$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" {
Expand Down Expand Up @@ -80,6 +92,25 @@ Describe "Get-ChildItem" -Tags "CI" {
(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 5
}

It "get-childitem path wildcard - <title>" -TestCases $PathWildCardTestCases {
param($Parameters, $ExpectedCount)

$null = New-Item $file1 -Force -ItemType File

(Get-ChildItem @Parameters).Count | Should Be $ExpectedCount
}

It "get-childitem with and without file in search root" {
$null = New-Item $file2 -Force -ItemType File

(Get-ChildItem -Path $searchRoot -File -Recurse).Count | Should be 2
(Get-ChildItem -Path $searchRoot -Directory -Recurse).Count | Should be 1

Remove-Item $file2 -ErrorAction SilentlyContinue -Force
(Get-ChildItem -Path $searchRoot -File -Recurse).Count | Should be 1
(Get-ChildItem -Path $searchRoot -Directory -Recurse).Count | Should be 1
}
}

Context 'Env: Provider' {
Expand Down