Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4303,6 +4303,12 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
if (CompletionRequiresQuotes(completionText, !useLiteralPath))
{
var quoteInUse = quote == string.Empty ? "'" : quote;

if (!useLiteralPath)
{
completionText = WildcardPattern.Escape(completionText);
}

if (quoteInUse == "'")
{
completionText = completionText.Replace("'", "''");
Expand All @@ -4315,20 +4321,6 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
completionText = completionText.Replace("$", "`$");
}

if (!useLiteralPath)
{
if (quoteInUse == "'")
{
completionText = completionText.Replace("[", "`[");
completionText = completionText.Replace("]", "`]");
}
else
{
completionText = completionText.Replace("[", "``[");
completionText = completionText.Replace("]", "``]");
}
}

completionText = quoteInUse + completionText + quoteInUse;
}
else if (quote != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ internal void GetChildItems(
string originalPath = path;
path =
Globber.GetProviderPath(
path,
context.SuppressWildcardExpansion ? path : WildcardPattern.Unescape(path),
context,
out provider,
out drive);
Expand Down Expand Up @@ -2689,7 +2689,7 @@ internal void GetChildNames(

string providerPath =
Globber.GetProviderPath(
path,
context.SuppressWildcardExpansion ? path : WildcardPattern.Unescape(path),
context,
out provider,
out drive);
Expand Down Expand Up @@ -3798,7 +3798,7 @@ internal void NewItem(
if (String.IsNullOrEmpty(name))
{
string providerPath =
Globber.GetProviderPath(resolvePath, context, out provider, out driveInfo);
Globber.GetProviderPath(WildcardPattern.Unescape(resolvePath), context, out provider, out driveInfo);

providerInstance = GetProviderInstance(provider);
providerPaths.Add(providerPath);
Expand Down
5 changes: 3 additions & 2 deletions src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ internal static string Escape(string pattern, char[] charsNotToEscape)
char ch = pattern[i];

//
// if it is a wildcard char, escape it
// if it is a wildcard char or escape char, escape it
//
if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch))
if ((IsWildcardChar(ch) || ch == escapeChar) &&
!charsNotToEscape.Contains(ch))
{
temp[tempIndex++] = escapeChar;
}
Expand Down
16 changes: 9 additions & 7 deletions src/System.Management.Automation/namespaces/LocationGlobber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,13 @@ private Collection<PathInfo> ResolveDriveQualifiedPath(

s_pathResolutionTracer.WriteLine("Path is DRIVE-QUALIFIED");

string relativePath = GetDriveRootRelativePathFromPSPath(path, context, true, out drive, out providerInstance);
string relativePath =
GetDriveRootRelativePathFromPSPath(
path,
context,
!context.SuppressWildcardExpansion,
out drive,
out providerInstance);

Dbg.Diagnostics.Assert(
drive != null,
Expand Down Expand Up @@ -3630,13 +3636,11 @@ private List<string> GenerateNewPSPathsWithGlobLeaf(
StringContainsGlobCharacters(leafElement) ||
isLastLeaf)
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);

// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down Expand Up @@ -4270,13 +4274,11 @@ internal List<string> GenerateNewPathsWithGlobLeaf(
(StringContainsGlobCharacters(leafElement) ||
isLastLeaf))
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);

// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down
3 changes: 2 additions & 1 deletion src/System.Management.Automation/utils/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ internal static string ResolveFilePath(string filePath, PSCmdlet command, bool i
PSDriveInfo drive = null;
path =
command.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
filePath, cmdletProviderContext, out provider, out drive);
isLiteralPath ? filePath : WildcardPattern.Unescape(filePath),
cmdletProviderContext, out provider, out drive);
cmdletProviderContext.ThrowFirstErrorOrDoNothing();
if (!provider.NameEquals(command.Context.ProviderNames.FileSystem))
{
Expand Down
3 changes: 3 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,9 @@ dir -Recurse `
@{ inputStr = "Get-Process >'.\My ``[Path``]\'"; expected = "'.${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "Get-Process >${tempDir}\My"; expected = "'${tempDir}${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``[Path``]\'"; expected = "'${tempDir}${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "cd 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``["; expected = "'${tempDir}${separator}My ``[Path``]'" }
)

Push-Location -Path $tempDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,16 @@ Describe "Hash expression with if statement as value" -Tags "CI" {
$hash['h'] | Should -BeExactly 'h'
}
}

Describe "WildcardPattern" -Tags "CI" {
It "Unescaping escaped string '<inputStr>' should get the original" -TestCases @(
@{inputStr = '*This'}
@{inputStr = 'Is?'}
@{inputStr = 'Real[ly]'}
@{inputStr = 'Ba`sic'}
@{inputStr = 'Test `[more`]?'}
) {
param($inputStr)
[WildcardPattern]::Unescape([WildcardPattern]::Escape($inputStr)) | Should -BeExactly $inputStr
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Describe "Get-ChildItem" -Tags "CI" {
$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

$specialDirName = "Test[Dir]"
$specialDir = "Test``[Dir``]"

$searchRoot = Join-Path $TestDrive -ChildPath "TestPS"
$file1 = Join-Path $searchRoot -ChildPath "D1" -AdditionalChildPath "File1.txt"
$file2 = Join-Path $searchRoot -ChildPath "File1.txt"
Expand Down Expand Up @@ -142,6 +145,14 @@ Describe "Get-ChildItem" -Tags "CI" {
(Get-ChildItem -LiteralPath $TestDrive -Depth 1 -Exclude $item_a).Count | Should Be 5
}

It "Should list files in directory contains special char" {
$null = New-Item -Path $TestDrive -Name $specialDirName -ItemType Directory -Force
$specialPath = Join-Path $TestDrive $specialDir
$null = New-Item -Path $specialPath -Name file1.txt -ItemType File -Force
$null = New-Item -Path $specialPath -Name file2.txt -ItemType File -Force
(Get-ChildItem -Path $specialPath).Count | Should -Be 2
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ function Clean-State
Describe "New-Item" -Tags "CI" {
$tmpDirectory = $TestDrive
$testfile = "testfile.txt"
$testfileSp = "``[test``]file.txt"
$testfolder = "newDirectory"
$testsubfolder = "newSubDirectory"
$testlink = "testlink"
$FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile
$FullyQualifiedFileSp = Join-Path -Path $tmpDirectory -ChildPath $testfileSp
$FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
$FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink
$FullyQualifiedSubFolder = Join-Path -Path $FullyQualifiedFolder -ChildPath $testsubfolder
$FullyQualifiedFileInFolder = Join-Path -Path $FullyQualifiedFolder -ChildPath $testfile


BeforeEach {
Clean-State
}
Expand Down Expand Up @@ -100,6 +101,12 @@ Describe "New-Item" -Tags "CI" {
Test-Path $FullyQualifiedFile | Should -BeTrue
}

It "Should create a file with correct name when Name switch is not used and Path contains special char" {
New-Item -Path $FullyQualifiedFileSp -ItemType file

$FullyQualifiedFileSp | Should -Exist
}

It "Should be able to create a multiple items in different directories" {
$FullyQualifiedFile2 = Join-Path -Path $tmpDirectory -ChildPath test2.txt
New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2
Expand Down Expand Up @@ -193,9 +200,15 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
$testfile = "testfile.txt"
$testfolder = "newDirectory"
$testlink = "testlink"
$testlinkSrcSpName = "[test]src"
$testlinkSrcSp = "``[test``]src"
$testlinkSpName = "[test]link"
$testlinkSp = "``[test``]link"
$FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile
$FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
$FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink
$FullyQualifiedLSrcSp = Join-Path -Path $tmpDirectory -ChildPath $testlinkSrcSp
$FullyQualifiedLinkSp = Join-Path -Path $tmpDirectory -ChildPath $testlinkSp
$SymLinkMask = [System.IO.FileAttributes]::ReparsePoint
$DirLinkMask = $SymLinkMask -bor [System.IO.FileAttributes]::Directory

Expand Down Expand Up @@ -246,6 +259,21 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
Test-Path $FullyQualifiedLink | Should -BeFalse
}

It "Should create symbolic link with name contains special char" {
$null = New-Item -Path $tmpDirectory -Name $testlinkSrcSpName -ItemType File
$FullyQualifiedLSrcSp | Should -Exist

$null = New-Item -Path $FullyQualifiedLinkSp -Target $FullyQualifiedLSrcSp -ItemType SymbolicLink
$FullyQualifiedLinkSp | Should -Exist

$expectedTarget = Join-Path $tmpDirectory $testlinkSrcSpName

$fileInfo = Get-ChildItem $FullyQualifiedLinkSp
$fileInfo.Target | Should -Match ([regex]::Escape($expectedTarget))
$fileInfo.LinkType | Should -BeExactly "SymbolicLink"
$fileInfo.Attributes -band $DirLinkMask | Should -BeExactly $SymLinkMask
}

It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" {
$folderName = [System.IO.Path]::GetRandomFileName()
$symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Describe "Out-File" -Tags "CI" {
$expectedContent = "some test text"
$inObject = New-Object psobject -Property @{text=$expectedContent}
$testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt
$testfileSp = Join-Path -Path $TestDrive -ChildPath "``[outfileTest``].txt"
}

AfterEach {
Expand Down Expand Up @@ -91,6 +92,14 @@ Describe "Out-File" -Tags "CI" {
$actual[11] | Should -BeNullOrEmpty
}

It "Should create the file with correct name when FilePath contains special char" {
Out-File -FilePath $testfile
Out-File -FilePath $testfileSp

$testfile | Should -Exist
$testfileSp | Should -Exist
}

It "Should limit each line to the specified number of characters when the width switch is used on objects" {

Out-File -FilePath $testfile -Width 10 -InputObject $inObject
Expand Down