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 @@ -3443,7 +3443,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 Expand Up @@ -3535,9 +3535,14 @@ internal void NewItem(
throw PSTraceSource.NewInvalidOperationException(SessionStateStrings.PathNotFound, targetPath);
}

// If the original target was a relative path, we want to leave it as relative if it did not require
// globbing to resolve.
if (WildcardPattern.ContainsWildcardCharacters(targetPath))
// If the original target was a relative path, we want to leave it as relative
if (targetPath.StartsWith('.'))
{
var sessionState = ExecutionContext.EngineSessionState;
string resolvedPath = sessionState.NormalizeRelativePath(globbedTarget[0], sessionState.CurrentLocation.ProviderPath);
content = resolvedPath.StartsWith('.') ? resolvedPath : Path.Combine(".", resolvedPath);
}
else
{
content = globbedTarget[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,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 @@ -103,9 +104,15 @@ 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 > $null

$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
New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2 > $null

Test-Path $FullyQualifiedFile | Should -BeTrue
Test-Path $FullyQualifiedFile2 | Should -BeTrue
Expand Down Expand Up @@ -196,9 +203,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 @@ -249,6 +262,21 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
Test-Path $FullyQualifiedLink | Should -BeFalse
}

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

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

$expectedTarget = Join-Path -Path $tmpDirectory -ChildPath $testlinkSrcSpName

$fileInfo = Get-Item -Path $FullyQualifiedLinkSp
$fileInfo.Target | Should -BeExactly $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
@@ -1,9 +1,12 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Remove-Item" -Tags "CI" {
$testpath = $TestDrive
$testfile = "testfile.txt"
$testfilepath = Join-Path -Path $testpath -ChildPath $testfile
$testpath = $TestDrive
$testfile = "testfile.txt"
$testfileSpName = "[testfile].txt"
$testfileSp = "``[testfile``].txt"
$testfilepath = Join-Path -Path $testpath -ChildPath $testfile
$testfilepathSp = Join-Path -Path $testpath -ChildPath $testfileSp
Context "File removal Tests" {
BeforeEach {
New-Item -Name $testfile -Path $testpath -ItemType "file" -Value "lorem ipsum" -Force
Expand Down Expand Up @@ -109,6 +112,14 @@ Describe "Remove-Item" -Tags "CI" {
Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should -BeFalse
Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should -BeFalse
}

It "Should be able to remove file when path contains special char" {
New-Item -Path $testpath -Name $testfileSpName -ItemType File -Force > $null
$testfilepathSp | Should -Exist

Remove-Item -Path $testfilepathSp -Force
$testfilepathSp | Should -Not -Exist
}
}

Context "Directory Removal Tests" {
Expand Down