MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Import-Module HelpersCommon5 6Describe "Basic FileSystem Provider Tests" -Tags "CI" {7 BeforeAll {8 $testDir = "TestDir"9 $testFile = "TestFile.txt"10 $restoreLocation = Get-Location11 }12 13 AfterAll {14 #restore the previous location15 Set-Location -Path $restoreLocation16 }17 18 BeforeEach {19 Set-Location -Path "TestDrive:\"20 }21 22 Context "Validate basic FileSystem Cmdlets" {23 BeforeAll {24 $newTestDir = "NewTestDir"25 $newTestFile = "NewTestFile.txt"26 $testContent = "Some Content"27 $testContent2 = "More Content"28 $reservedNamesTests = @(29 @{ deviceName = 'CON' }30 @{ deviceName = 'PRN' }31 @{ deviceName = 'AUX' }32 @{ deviceName = 'CLOCK$' }33 @{ deviceName = 'NUL' }34 @{ deviceName = 'CONIN$' }35 @{ deviceName = 'CONOUT$' }36 @{ deviceName = 'COM0' }37 @{ deviceName = 'COM1' }38 @{ deviceName = 'COM2' }39 @{ deviceName = 'COM3' }40 @{ deviceName = 'COM4' }41 @{ deviceName = 'COM5' }42 @{ deviceName = 'COM6' }43 @{ deviceName = 'COM7' }44 @{ deviceName = 'COM8' }45 @{ deviceName = 'COM9' }46 @{ deviceName = 'LPT0' }47 @{ deviceName = 'LPT1' }48 @{ deviceName = 'LPT2' }49 @{ deviceName = 'LPT3' }50 @{ deviceName = 'LPT4' }51 @{ deviceName = 'LPT5' }52 @{ deviceName = 'LPT6' }53 @{ deviceName = 'LPT7' }54 @{ deviceName = 'LPT8' }55 @{ deviceName = 'LPT9' }56 )57 }58 59 BeforeEach {60 New-Item -Path $testDir -ItemType Directory > $null61 New-Item -Path $testFile -ItemType File > $null62 }63 64 AfterEach {65 Set-Location -Path "TestDrive:\"66 Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue67 }68 69 It "Verify New-Item for directory" {70 $newDir = New-Item -Path $newTestDir -ItemType Directory71 $directoryExists = Test-Path $newTestDir72 $directoryExists | Should -BeTrue73 $newDir.Name | Should -BeExactly $newTestDir74 }75 76 It "Verify New-Item for file" {77 $newFile = New-Item -Path $newTestFile -ItemType File78 $newTestFile | Should -Exist79 $newFile.Name | Should -BeExactly $newTestFile80 }81 82 It "Verify Remove-Item for directory" {83 $existsBefore = Test-Path $testDir84 Remove-Item -Path $testDir -Recurse -Force85 $existsAfter = Test-Path $testDir86 $existsBefore | Should -BeTrue87 $existsAfter | Should -BeFalse88 }89 90 It "Verify Remove-Item for file" {91 $existsBefore = Test-Path $testFile92 Remove-Item -Path $testFile -Force93 $existsAfter = Test-Path $testFile94 $existsBefore | Should -BeTrue95 $existsAfter | Should -BeFalse96 }97 98 It "Verify Remove-Item for file" {99 $longDir = 'a' * 250100 $longSubDir = 'b' * 250101 $fileName = "file1.txt"102 $topPath = Join-Path $TestDrive $longDir103 $longDirPath = Join-Path $topPath $longSubDir104 $longFilePath = Join-Path $longDirPath $fileName105 $null = New-Item -itemtype file -path $longFilePath -force106 107 $longFilePath | Should -Exist108 109 Remove-Item -Path $longFilePath -Force110 111 $longFilePath | Should -Not -Exist112 }113 114 It "Verify Rename-Item for file" {115 Rename-Item -Path $testFile -NewName $newTestFile -ErrorAction Stop116 $testFile | Should -Not -Exist117 $newTestFile | Should -Exist118 }119 120 It "Verify Rename-Item for directory" {121 Rename-Item -Path $testDir -NewName $newTestDir -ErrorAction Stop122 $testDir | Should -Not -Exist123 $newTestDir | Should -Exist124 }125 126 It "Verify Rename-Item will not rename to an existing name" {127 { Rename-Item -Path $testFile -NewName $testDir -ErrorAction Stop } | Should -Throw -ErrorId "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand"128 $error[0].Exception | Should -BeOfType System.IO.IOException129 $testFile | Should -Exist130 }131 132 It "Verify Copy-Item" {133 $newFile = Copy-Item -Path $testFile -Destination $newTestFile -PassThru134 $fileExists = Test-Path $newTestFile135 $fileExists | Should -BeTrue136 $newFile.Name | Should -BeExactly $newTestFile137 }138 139 It "Verify Move-Item for file" {140 Move-Item -Path $testFile -Destination $testDir -ErrorAction Stop141 $testFile | Should -Not -Exist142 "$testDir/$testFile" | Should -Exist143 }144 145 It "Verify Move-Item for directory" {146 $destDir = "DestinationDirectory"147 New-Item -Path $destDir -ItemType Directory -ErrorAction Stop > $null148 Move-Item -Path $testFile -Destination $testDir149 Move-Item -Path $testDir -Destination $destDir150 $testDir | Should -Not -Exist151 "$destDir/$testDir" | Should -Exist152 "$destDir/$testDir/$testFile" | Should -Exist153 }154 155 It "Verify Move-Item across devices for directory" {156 [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ThrowExdevErrorOnMoveDirectory', $true)157 try158 {159 $dir = (New-Item -Path TestDrive:/dir -ItemType Directory -ErrorAction Stop).FullName160 $file = (New-Item -Path "$dir/file.txt" -Value "HELLO" -ErrorAction Stop).Name161 $destination = "$TestDrive/destination"162 163 Move-Item -Path $dir -Destination $destination -ErrorAction Stop164 165 $dir | Should -Not -Exist166 $destination | Should -Exist167 "$destination/$file" | Should -Exist168 }169 finally170 {171 [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ThrowExdevErrorOnMoveDirectory', $false)172 }173 }174 175 It "Verify Move-Item will not move to an existing file" {176 if ($IsWindows) {177 $expectedError = 'MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand'178 }179 else {180 $expectedError = 'DirectoryExist,Microsoft.PowerShell.Commands.MoveItemCommand'181 }182 183 $e = { Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId $expectedError -PassThru184 $e.Exception | Should -BeOfType System.IO.IOException185 $testDir | Should -Exist186 }187 188 It 'Verify Move-Item fails for non-existing destination path' {189 $e = { Move-Item -Path $testDir -Destination TestDrive:/0/2/0 -ErrorAction Stop } | Should -Throw -ErrorId 'MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand' -PassThru190 $e.Exception | Should -BeOfType System.IO.IOException191 }192 193 It 'Verify Move-Item fails for destination that is subdir of source with trailing: <trailingChar>' -TestCases @(194 @{ trailingChar = [System.IO.Path]::DirectorySeparatorChar }195 @{ trailingChar = [System.IO.Path]::AltDirectorySeparatorChar }196 @{ trailingChar = '' }197 ) {198 param($trailingChar)199 200 $dest = Join-Path -Path $TestDrive -ChildPath dest201 $null = New-item -ItemType Directory -Path $dest -Force -ErrorAction Stop202 $src = "$TestDrive$trailingChar"203 204 { Move-Item -Path $src -Destination $dest -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'205 }206 207 It 'Verify Move-Item fails when destination is same as source w/wo directory separator: <source>' -TestCases @(208 @{ source = './Empty/' }209 @{ source = './Empty' }210 @{ source = '.\Empty\' }211 @{ source = '.\Empty' }212 ) {213 param($source)214 215 try {216 Push-Location $TestDrive217 New-Item -ItemType Directory -Path 'Empty'218 { Move-Item -Path $source -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'219 }220 finally {221 Pop-Location222 Remove-Item 'Empty' -Force223 }224 }225 226 It "Verify Move-Item throws correct error for non-existent source" {227 { Move-Item -Path /does/not/exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'228 }229 230 It "Verify Move-Item as substitute for Rename-Item" {231 $newFile = Move-Item -Path $testFile -Destination $newTestFile -PassThru232 $fileExists = Test-Path $newTestFile233 $fileExists | Should -BeTrue234 $newFile.Name | Should -Be $newTestFile235 }236 237 It "Verify Get-ChildItem" {238 $dirContents = Get-ChildItem "."239 $dirContents.Count | Should -Be 2240 }241 242 It "Verify Get-ChildItem can get the name of a specified item." {243 $fileName = Get-ChildItem $testFile -Name244 $fileInfo = Get-ChildItem $testFile245 $fileName | Should -BeExactly $fileInfo.Name246 }247 248 It "Set-Content to a file" {249 $content = Set-Content -Value $testContent -Path $testFile -PassThru250 $content | Should -BeExactly $testContent251 }252 253 It "Add-Content to a file" {254 $content = Set-Content -Value $testContent -Path $testFile -PassThru255 $addContent = Add-Content -Value $testContent2 -Path $testFile -PassThru256 $fullContent = Get-Content -Path $testFile257 $content | Should -Match $testContent258 $addContent | Should -Match $testContent2259 ($fullContent[0] + $fullContent[1]) | Should -Match ($testContent + $testContent2)260 }261 262 It "Clear-Content of a file" {263 Set-Content -Value $testContent -Path $testFile264 $contentBefore = Get-Content -Path $testFile265 Clear-Content -Path $testFile266 $contentAfter = Get-Content -Path $testFile267 $contentBefore.Count | Should -Be 1268 $contentAfter.Count | Should -Be 0269 }270 271 It "Copy-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip:(-not $IsWindows) -TestCases $reservedNamesTests {272 param($deviceName)273 { Copy-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"274 }275 276 It "Move-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip:(-not $IsWindows) -TestCases $reservedNamesTests {277 param($deviceName)278 { Move-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "MoveError,Microsoft.PowerShell.Commands.MoveItemCommand"279 }280 281 It "Rename-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip:(-not $IsWindows) -TestCases $reservedNamesTests {282 param($deviceName)283 { Rename-Item -Path $testFile -NewName $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "RenameError,Microsoft.PowerShell.Commands.RenameItemCommand"284 }285 286 It "Copy-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip:($IsWindows) -TestCases $reservedNamesTests {287 param($deviceName)288 Copy-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue289 Test-Path $deviceName | Should -BeTrue290 }291 292 It "Move-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip:($IsWindows) -TestCases $reservedNamesTests {293 param($deviceName)294 Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue295 Test-Path $deviceName | Should -BeTrue296 New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue297 }298 299 It "Rename-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip:($IsWindows) -TestCases $reservedNamesTests {300 param($deviceName)301 Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue302 Test-Path $deviceName | Should -BeTrue303 New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue304 }305 306 It "Set-Location on Unix succeeds with folder with colon: <path>" -Skip:($IsWindows) -TestCases @(307 @{path="\hello:world"},308 @{path="\:world"},309 @{path="/hello:"}310 ) {311 param($path)312 try {313 New-Item -Path "$testdrive$path" -ItemType Directory > $null314 Set-Location "$testdrive"315 Set-Location ".$path"316 (Get-Location).Path | Should -BeExactly "$testdrive/$($path.Substring(1,$path.Length-1))"317 }318 finally {319 Remove-Item -Path "$testdrive$path" -ErrorAction SilentlyContinue320 }321 }322 323 It "Get-Content on Unix succeeds with folder and file with colon: <path>" -Skip:($IsWindows) -TestCases @(324 @{path="\foo:bar.txt"},325 @{path="/foo:"},326 @{path="\:bar"}327 ) {328 param($path)329 try {330 $testPath = "$testdrive/hello:world"331 New-Item -Path "$testPath" -ItemType Directory > $null332 Set-Content -Path "$testPath$path" -Value "Hello"333 $files = Get-ChildItem "$testPath"334 $files.Count | Should -Be 1335 $files[0].Name | Should -BeExactly $path.Substring(1,$path.Length-1)336 $files[0] | Get-Content | Should -BeExactly "Hello"337 }338 finally {339 Remove-Item -Path $testPath -Recurse -Force -ErrorAction SilentlyContinue340 }341 }342 343 It "Copy-Item can copy 0 byte length file" {344 $zeroLengthFile = New-Item -Path (Join-Path $TestDrive "zeroLengthFile.txt") -ItemType File -Force345 Copy-Item -Path $zeroLengthFile -Destination "$TestDrive\zeroLengthFile2.txt" -Force346 "$TestDrive\zeroLengthFile2.txt" | Should -Exist347 }348 }349 350 Context "Validate behavior when access is denied" {351 BeforeAll {352 $powershell = Join-Path $PSHOME "pwsh"353 if ($IsWindows)354 {355 $protectedPath = Join-Path ([environment]::GetFolderPath("windows")) "appcompat" "Programs"356 $protectedPath2 = Join-Path $protectedPath "Install"357 $newItemPath = Join-Path $protectedPath "foo"358 $shouldSkip = -not (Test-Path $protectedPath)359 }360 361 if ($IsWindows) {362 $fqaccessdenied = "MoveDirectoryItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.MoveItemCommand"363 }364 else {365 $fqaccessdenied = "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand"366 }367 }368 369 It "Access-denied test for <cmdline>" -Skip:(-not $IsWindows -or $shouldSkip) -TestCases @(370 # NOTE: ensure the fileNameBase parameter is unique for each test case; it is used to generate a unique error and done file name.371 # The following test does not consistently work on windows372 # @{cmdline = "Get-Item $protectedPath2 -ErrorAction Stop"; expectedError = "ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetItemCommand"}373 # @{cmdline = "New-Item -Type File -Path $newItemPath -ErrorAction Stop"; expectedError = "NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand"}374 @{cmdline = "Get-ChildItem $protectedPath -ErrorAction Stop"; expectedError = "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand"}375 @{cmdline = "Rename-Item -Path $protectedPath -NewName bar -ErrorAction Stop"; expectedError = "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand"},376 @{cmdline = "Move-Item -Path $protectedPath -Destination bar -ErrorAction Stop"; expectedError = $fqaccessdenied}377 ) {378 param ($cmdline, $expectedError)379 380 if (Test-IsElevated) {381 Set-ItResult -Skipped -Because "Process must NOT be elevated"382 }383 384 $scriptBlock = [scriptblock]::Create($cmdline)385 $scriptBlock | Should -Throw -ErrorId $expectedError386 }387 }388 389 Context "Appx path" {390 BeforeAll {391 $skipTest = $true392 if ($IsWindows -and (Get-Command -Name Get-AppxPackage) ) {393 $pkgDir = (Get-AppxPackage Microsoft.WindowsCalculator -ErrorAction SilentlyContinue).InstallLocation394 $skipTest = $pkgDir -eq $null395 }396 }397 398 It "Can get an appx package item" -Skip:$skipTest {399 Get-Item $pkgDir\Calculator.exe -ErrorAction Stop | Should -BeOfType System.IO.FileInfo400 Get-Item -Path $pkgDir -ErrorAction Stop | Should -BeOfType System.IO.DirectoryInfo401 Get-ChildItem -Path $pkgDir -ErrorAction Stop | Should -Not -BeNullOrEmpty402 }403 }404 405 Context "Validate basic host navigation functionality" {406 BeforeAll {407 #build semi-complex directory structure to test navigation within408 $level1_0 = "Level1_0"409 $level2_0 = "Level2_0"410 $level2_1 = "Level2_1"411 $root = Join-Path "TestDrive:" "" #adds correct / or \412 $level1_0Full = Join-Path $root $level1_0413 $level2_0Full = Join-Path $level1_0Full $level2_0414 $level2_1Full = Join-Path $level1_0Full $level2_1415 New-Item -Path $level1_0Full -ItemType Directory > $null416 New-Item -Path $level2_0Full -ItemType Directory > $null417 New-Item -Path $level2_1Full -ItemType Directory > $null418 }419 420 It "Verify Get-Location and Set-Location" {421 $currentLoc = Get-Location422 Set-Location $level1_0423 $level1Loc = Get-Location424 Set-Location $level2_0425 $level2Loc = Get-Location426 $currentLoc.Path | Should -BeExactly $root427 $level1Loc.Path | Should -BeExactly $level1_0Full428 $level2Loc.Path | Should -BeExactly $level2_0Full429 }430 431 It "Verify Push-Location and Pop-Location" {432 #push a bunch of locations433 Push-Location434 $push0 = Get-Location435 Set-Location $level1_0436 Push-Location437 $push1 = Get-Location438 Set-Location $level2_0439 Push-Location440 $push2 = Get-Location441 442 #navigate back home to change path out of all pushed locations443 Set-Location "TestDrive:\"444 445 #pop locations off446 Pop-Location447 $pop0 = Get-Location448 Pop-Location449 $pop1 = Get-Location450 Pop-Location451 $pop2 = Get-Location452 453 $pop0.Path | Should -BeExactly $push2.Path454 $pop1.Path | Should -BeExactly $push1.Path455 $pop2.Path | Should -BeExactly $push0.Path456 }457 }458 459 Context "Validate Basic Path Cmdlets" {460 It "Verify Convert-Path" {461 $result = Convert-Path "."462 ($result.TrimEnd('/\')) | Should -BeExactly "$TESTDRIVE"463 }464 465 It "Verify Join-Path" {466 $result = Join-Path -Path "TestDrive:" -ChildPath "temp"467 468 if ($IsWindows) {469 $result | Should -BeExactly "TestDrive:\temp"470 }471 else {472 $result | Should -BeExactly "TestDrive:/temp"473 }474 }475 476 It "Verify Split-Path" {477 $testPath = Join-Path "TestDrive:" "MyTestFile.txt"478 $result = Split-Path $testPath -Qualifier479 $result | Should -BeExactly "TestDrive:"480 }481 482 It "Verify Test-Path" {483 $result = Test-Path $HOME484 $result | Should -BeTrue485 }486 487 It "Verify HOME" {488 $homePath = $HOME489 $tildePath = (Resolve-Path -Path ~).Path490 $homePath | Should -BeExactly $tildePath491 }492 }493}494 495Describe "Handling of globbing patterns" -Tags "CI" {496 497 Context "Handling of Unix [ab] globbing patterns in literal paths" {498 BeforeAll {499 $filePath = Join-Path $TESTDRIVE "file[txt].txt"500 $newPath = Join-Path $TESTDRIVE "file.txt.txt"501 $dirPath = Join-Path $TESTDRIVE "subdir"502 }503 BeforeEach {504 $file = New-Item -ItemType File -Path $filePath -Force505 }506 AfterEach {507 Remove-Item -Force -Recurse -Path $dirPath -ErrorAction SilentlyContinue508 Remove-Item -Force -LiteralPath $newPath -ErrorAction SilentlyContinue509 }510 511 It "Rename-Item -LiteralPath can rename a file with Unix globbing characters" {512 Rename-Item -LiteralPath $file.FullName -NewName $newPath513 Test-Path -LiteralPath $file.FullName | Should -BeFalse514 Test-Path -LiteralPath $newPath | Should -BeTrue515 }516 517 It "Remove-Item -LiteralPath can delete a file with Unix globbing characters" {518 Remove-Item -LiteralPath $file.FullName519 Test-Path -LiteralPath $file.FullName | Should -BeFalse520 }521 522 It "Move-Item -LiteralPath can move a file with Unix globbing characters" {523 $dir = New-Item -ItemType Directory -Path $dirPath524 Move-Item -LiteralPath $file.FullName -Destination $dir.FullName525 Test-Path -LiteralPath $file.FullName | Should -BeFalse526 $newPath = Join-Path $dir.FullName $file.Name527 Test-Path -LiteralPath $newPath | Should -BeTrue528 }529 530 It "Copy-Item -LiteralPath can copy a file with Unix globbing characters" {531 Copy-Item -LiteralPath $file.FullName -Destination $newPath532 Test-Path -LiteralPath $newPath | Should -BeTrue533 }534 }535 536 Context "Handle asterisks in name" {537 It "Remove-Item -LiteralPath should fail if it contains asterisk and file doesn't exist" {538 { Remove-Item -LiteralPath ./foo*.txt -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"539 }540 541 It "Remove-Item -LiteralPath should succeed for file with asterisk in name" -Skip:($IsWindows) {542 $testPath = "$testdrive\foo*"543 $testPath2 = "$testdrive\foo*2"544 New-Item -Path $testPath -ItemType File545 New-Item -Path $testPath2 -ItemType File546 Test-Path -LiteralPath $testPath | Should -BeTrue547 Test-Path -LiteralPath $testPath2 | Should -BeTrue548 { Remove-Item -LiteralPath $testPath } | Should -Not -Throw549 Test-Path -LiteralPath $testPath | Should -BeFalse550 # make sure wildcard wasn't applied so this file should still exist551 Test-Path -LiteralPath $testPath2 | Should -BeTrue552 }553 }554 555 Context "Device paths" {556 # The globber is overly greedy somewhere so you need to escape the escape backtick to preserve the question mark issue https://github.com/PowerShell/PowerShell/issues/19627557 It "Handle device paths: <path>" -Skip:(!$IsWindows) -TestCases @(558 @{ path = "\\.\${env:SystemDrive}\" }559 @{ path = "\\.\${env:SystemDrive}\*" }560 @{ path = "\\``?\${env:SystemDrive}\" }561 @{ path = "\\``?\${env:SystemDrive}\*" }562 ) {563 param($path)564 $expected = Get-ChildItem -Path ${env:SystemDrive}\565 $result = Get-ChildItem -Path $path566 $result.Count | Should -Be $expected.Count567 }568 569 It "Handle folders within a device path: <path>" -Skip:(!$IsWindows) -TestCases @(570 @{ path = "\\.\${env:SystemRoot}\" }571 @{ path = "\\.\${env:SystemRoot}\*" }572 @{ path = "\\``?\${env:SystemRoot}\" }573 @{ path = "\\``?\${env:SystemRoot}\*" }574 ) {575 param($path)576 $expected = Get-ChildItem -Path ${env:SystemRoot}577 $result = Get-ChildItem -Path $path578 $result.Count | Should -Be $expected.Count579 }580 581 It "Fails for invalid device path: <path>" -Skip:(!$IsWindows) -TestCases @(582 @{ path = "\\.\INVALID0\" }583 @{ path = "\\``?\INVALID0\" }584 # @{ path = "\\.\INVALID0\*" } // problem in globber where this fails but is ignored issue https://github.com/PowerShell/PowerShell/issues/19626585 # @{ path = "\\``?\INVALID0\*" }586 ) {587 param($path)588 { Get-ChildItem -Path $path -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand'589 }590 }591}592 593Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" {594 BeforeAll {595 # on macOS, the /tmp directory is a symlink, so we'll resolve it here596 $TestPath = $TestDrive597 if ($IsMacOS)598 {599 $item = Get-Item $TestPath600 $dirName = $item.BaseName601 $item = Get-Item $item.PSParentPath -Force602 if ($item.LinkType -eq "SymbolicLink")603 {604 $TestPath = Join-Path $item.Target $dirName605 }606 }607 608 # Ensure that the file link can still be successfully created when the target file/directory name contains wildcards.609 $realFile = Join-Path $TestPath "[file].txt"610 $nonFile = Join-Path $TestPath "not-a-file"611 $fileContent = "some text"612 $realDir = Join-Path $TestPath "[subdir]"613 $realDir2 = Join-Path $TestPath "[second-subdir]"614 $nonDir = Join-Path $TestPath "not-a-dir"615 $hardLinkToFile = Join-Path $TestPath "hard-to-file.txt"616 $symLinkToFile = Join-Path $TestPath "sym-link-to-file.txt"617 $symLinkToDir = Join-Path $TestPath "sym-link-to-dir"618 $symLinkToNothing = Join-Path $TestPath "sym-link-to-nowhere"619 $dirSymLinkToDir = Join-Path $TestPath "symd-link-to-dir"620 $junctionToDir = Join-Path $TestPath "junction-to-dir"621 622 New-Item -ItemType File -Path $realFile -Value $fileContent > $null623 New-Item -ItemType Directory -Path $realDir > $null624 New-Item -ItemType Directory -Path $realDir2 > $null625 }626 627 Context "New-Item and hard/symbolic links" {628 AfterEach {629 # clean up created links after each test630 Remove-Item -Exclude (Split-Path -Leaf ([WildcardPattern]::Escape($realFile)), ([WildcardPattern]::Escape($realDir)), ([WildcardPattern]::Escape($realDir2))) -Recurse $TestPath/*631 }632 633 It "New-Item can create a hard link to a file" {634 New-Item -ItemType HardLink -Path $hardLinkToFile -Value $realFile > $null635 Test-Path $hardLinkToFile | Should -BeTrue636 $link = Get-Item -Path $hardLinkToFile637 $link.LinkType | Should -BeExactly "HardLink"638 Get-Content -Path $hardLinkToFile | Should -Be $fileContent639 }640 It "New-Item can create symbolic link to file" {641 New-Item -ItemType SymbolicLink -Path $symLinkToFile -Value $realFile > $null642 Test-Path $symLinkToFile | Should -BeTrue643 $real = Get-Item -LiteralPath $realFile644 $link = Get-Item -Path $symLinkToFile645 $link.LinkType | Should -BeExactly "SymbolicLink"646 $link.Target | Should -BeExactly $real.ToString()647 Get-Content -Path $symLinkToFile | Should -Be $fileContent648 }649 It "New-Item can create a symbolic link to nothing" {650 New-Item -ItemType SymbolicLink -Path $symLinkToNothing -Value $nonFile > $null651 Test-Path $symLinkToNothing | Should -BeTrue652 $link = Get-Item -Path $symLinkToNothing653 $link.LinkType | Should -BeExactly "SymbolicLink"654 $link.Target | Should -Be $nonFile.ToString()655 }656 It "New-Item emits an error when path to symbolic link already exists." {657 { New-Item -ItemType SymbolicLink -Path $realDir -Value $symLinkToDir -ErrorAction Stop } | Should -Throw -ErrorId "SymLinkExists,Microsoft.PowerShell.Commands.NewItemCommand"658 }659 It "New-Item can create a symbolic link to a directory" -Skip:($IsWindows) {660 New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null661 Test-Path $symLinkToDir | Should -BeTrue662 $real = Get-Item -LiteralPath $realDir663 $link = Get-Item -Path $symLinkToDir664 $link.LinkType | Should -BeExactly "SymbolicLink"665 $link.Target | Should -BeExactly $real.ToString()666 }667 It "New-Item can create a directory symbolic link to a directory" -Skip:(-Not $IsWindows) {668 New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null669 Test-Path $symLinkToDir | Should -BeTrue670 $real = Get-Item -LiteralPath $realDir671 $link = Get-Item -Path $symLinkToDir672 $link | Should -BeOfType System.IO.DirectoryInfo673 $link.LinkType | Should -BeExactly "SymbolicLink"674 $link.Target | Should -BeExactly $real.ToString()675 }676 677 It "New-Item can create a directory symbolic link to a directory using a relative path" -Skip:(-Not $IsWindows) {678 $target = Split-Path -Leaf $realDir679 New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $target > $null680 Test-Path $symLinkToDir | Should -BeTrue681 $real = Get-Item -LiteralPath $realDir682 $link = Get-Item -Path $symLinkToDir683 $link | Should -BeOfType System.IO.DirectoryInfo684 $link.LinkType | Should -BeExactly "SymbolicLink"685 $link.ResolvedTarget | Should -BeExactly $real.ToString()686 $link.Target | Should -BeExactly $target687 }688 689 It "New-Item can create a directory symbolic link to a directory using a relative path with .\" -Skip:(-Not $IsWindows) {690 $target = ".\$(Split-Path -Leaf $realDir)"691 New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $target > $null692 Test-Path $symLinkToDir | Should -BeTrue693 $real = Get-Item -LiteralPath $realDir694 $link = Get-Item -Path $symLinkToDir695 $link | Should -BeOfType System.IO.DirectoryInfo696 $link.LinkType | Should -BeExactly "SymbolicLink"697 $link.ResolvedTarget | Should -BeExactly $real.ToString()698 $link.Target | Should -BeExactly $target699 }700 701 It "New-Item can create a directory junction to a directory" -Skip:(-Not $IsWindows) {702 New-Item -ItemType Junction -Path $junctionToDir -Value $realDir > $null703 Test-Path $junctionToDir | Should -BeTrue704 }705 706 It 'New-Item fails creating junction with relative path' -Skip:(!$IsWindows) {707 try {708 Push-Location $TestDrive709 1 > 1.txt710 { New-Item -ItemType Junction -Path 2.txt -Target 1.txt -ErrorAction Stop } | Should -Throw -ErrorId "NotAbsolutePath,Microsoft.PowerShell.Commands.NewItemCommand"711 }712 finally {713 Pop-Location714 }715 }716 717 It 'New-Item can create hardlink with relative path' {718 try {719 Push-Location $TestDrive720 1 > 1.txt721 New-Item -ItemType HardLink -Path 2.txt -Target 1.txt -ErrorAction Stop722 $hl = Get-Item -Path .\2.txt -ErrorAction Stop723 $hl.LinkType | Should -BeExactly "HardLink"724 }725 finally {726 Pop-Location727 }728 }729 730 It 'New-Item will fail to forcibly create hardlink to itself' {731 $i = New-Item -ItemType File -Path "$TestDrive\file.txt" -Force -ErrorAction Ignore732 { New-Item -ItemType HardLink -Path $i -Target $i -Force -ErrorAction Stop } | Should -Throw -ErrorId "TargetIsSameAsLink,Microsoft.PowerShell.Commands.NewItemCommand"733 }734 735 It "New-Item -Force can overwrite a junction" -Skip:(-Not $IsWindows){736 $rd2 = Get-Item -LiteralPath $realDir2737 New-Item -Name testfile.txt -ItemType file -Path ([WildcardPattern]::Escape($realDir))738 New-Item -ItemType Junction -Path $junctionToDir -Value $realDir > $null739 Test-Path $junctionToDir | Should -BeTrue740 { New-Item -ItemType Junction -Path $junctionToDir -Value $realDir -ErrorAction Stop > $null } | Should -Throw -ErrorId "DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand"741 New-Item -ItemType Junction -Path $junctionToDir -Value $realDir2 -Force > $null742 $Junction = Get-Item -Path $junctionToDir743 $Junction.Target | Should -BeExactly $rd2.ToString()744 }745 }746 747 Context "Get-ChildItem and symbolic links" {748 BeforeAll {749 $alphaDir = Join-Path $TestDrive "sub-alpha"750 $alphaLink = Join-Path $TestDrive "link-alpha"751 $alphaFile1 = Join-Path $alphaDir "AlphaFile1.txt"752 $alphaFile2 = Join-Path $alphaDir "AlphaFile2.txt"753 $omegaDir = Join-Path $TestDrive "sub-omega"754 $omegaFile1 = Join-Path $omegaDir "OmegaFile1"755 $omegaFile2 = Join-Path $omegaDir "OmegaFile2"756 $betaDir = Join-Path $alphaDir "sub-Beta"757 $betaLink = Join-Path $alphaDir "link-Beta" # Don't change! The name is hard-coded in PowerShell for OneDrive tests.758 $betaFile1 = Join-Path $betaDir "BetaFile1.txt"759 $betaFile2 = Join-Path $betaDir "BetaFile2.txt"760 $betaFile3 = Join-Path $betaDir "BetaFile3.txt"761 $gammaDir = Join-Path $betaDir "sub-gamma"762 $uponeLink = Join-Path $gammaDir "upone-link"763 $uptwoLink = Join-Path $gammaDir "uptwo-link"764 $omegaLink = Join-Path $gammaDir "omegaLink"765 766 New-Item -ItemType Directory -Path $alphaDir767 New-Item -ItemType File -Path $alphaFile1768 New-Item -ItemType File -Path $alphaFile2769 New-Item -ItemType Directory -Path $betaDir770 New-Item -ItemType File -Path $betaFile1771 New-Item -ItemType File -Path $betaFile2772 New-Item -ItemType File -Path $betaFile3773 New-Item -ItemType Directory $omegaDir774 New-Item -ItemType File -Path $omegaFile1775 New-Item -ItemType File -Path $omegaFile2776 }777 AfterAll {778 Remove-Item -Path $alphaLink -Force -ErrorAction SilentlyContinue779 Remove-Item -Path $betaLink -Force -ErrorAction SilentlyContinue780 }781 782 It "Get-ChildItem gets content of linked-to directory" {783 $filenamePattern = "AlphaFile[12]\.txt"784 New-Item -ItemType SymbolicLink -Path $alphaLink -Value $alphaDir785 $ci = Get-ChildItem $alphaLink786 $ci.Count | Should -Be 3787 $ci[1].Name | Should -MatchExactly $filenamePattern788 $ci[2].Name | Should -MatchExactly $filenamePattern789 }790 It "Get-ChildItem -Name gets content of linked-to directory" {791 # The test depends on the files created in previous test:792 #$filenamePattern = "AlphaFile[12]\.txt"793 #New-Item -ItemType SymbolicLink -Path $alphaLink -Value $alphaDir794 $ci = Get-ChildItem $alphaLink -Name795 $ci.Count | Should -Be 3796 $ci[1] | Should -MatchExactly $filenamePattern797 $ci[2] | Should -MatchExactly $filenamePattern798 }799 It "Get-ChildItem does not recurse into symbolic links not explicitly given on the command line" {800 New-Item -ItemType SymbolicLink -Path $betaLink -Value $betaDir801 $ci = Get-ChildItem $alphaLink -Recurse802 $ci.Count | Should -BeExactly 7803 }804 It "Get-ChildItem -Name does not recurse into symbolic links not explicitly given on the command line" -Pending {805 # The test depends on the files created in previous test:806 #New-Item -ItemType SymbolicLink -Path $betaLink -Value $betaDir807 $ci = Get-ChildItem $alphaLink -Recurse -Name808 $ci.Count | Should -BeExactly 7 # returns 10 - unexpectly recurce in link-alpha\link-Beta. See https://github.com/PowerShell/PowerShell/issues/11614809 }810 It "Get-ChildItem will recurse into symlinks given -FollowSymlink, avoiding link loops" {811 New-Item -ItemType Directory -Path $gammaDir812 New-Item -ItemType SymbolicLink -Path $uponeLink -Value $betaDir813 New-Item -ItemType SymbolicLink -Path $uptwoLink -Value $alphaDir814 New-Item -ItemType SymbolicLink -Path $omegaLink -Value $omegaDir815 $ci = Get-ChildItem -Path $alphaDir -FollowSymlink -Recurse -WarningVariable w -WarningAction SilentlyContinue816 $ci.Count | Should -BeExactly 13817 $w.Count | Should -BeExactly 3818 }819 It "Get-ChildItem -Name will recurse into symlinks given -FollowSymlink, avoiding link loops" -Pending {820 # The test depends on the files created in previous test:821 # New-Item -ItemType Directory -Path $gammaDir822 # New-Item -ItemType SymbolicLink -Path $uponeLink -Value $betaDir823 # New-Item -ItemType SymbolicLink -Path $uptwoLink -Value $alphaDir824 # New-Item -ItemType SymbolicLink -Path $omegaLink -Value $omegaDir825 $ci = Get-ChildItem -Path $alphaDir -FollowSymlink -Recurse -WarningVariable w -WarningAction SilentlyContinue -Name # unexpectly dead cycle. See https://github.com/PowerShell/PowerShell/issues/11614826 $ci.Count | Should -BeExactly 13827 $w.Count | Should -BeExactly 3828 }829 }830 831 Context "Remove-Item and hard/symbolic links" {832 BeforeAll {833 $testCases = @(834 @{835 Name = "Remove-Item can remove a hard link to a file"836 Link = $hardLinkToFile837 Target = $realFile838 }839 @{840 Name = "Remove-Item can remove a symbolic link to a file"841 Link = $symLinkToFile842 Target = $realFile843 }844 )845 846 # New-Item on Windows will not create a "plain" symlink to a directory847 $unixTestCases = @(848 @{849 Name = "Remove-Item can remove a symbolic link to a directory on Unix"850 Link = $symLinkToDir851 Target = $realDir852 }853 )854 855 # Junctions and directory symbolic links are Windows and NTFS only856 $windowsTestCases = @(857 @{858 Name = "Remove-Item can remove a symbolic link to a directory on Windows"859 Link = $symLinkToDir860 Target = $realDir861 }862 @{863 Name = "Remove-Item can remove a directory symbolic link to a directory on Windows"864 Link = $dirSymLinkToDir865 Target = $realDir866 }867 @{868 Name = "Remove-Item can remove a junction to a directory"869 Link = $junctionToDir870 Target = $realDir871 }872 )873 874 function TestRemoveItem875 {876 Param (877 [string]$Link,878 [string]$Target879 )880 881 Remove-Item -Path $Link -ErrorAction SilentlyContinue > $null882 Test-Path -Path $Link | Should -BeFalse883 Test-Path -LiteralPath $Target | Should -BeTrue884 }885 }886 887 It "<Name>" -TestCases $testCases {888 Param (889 [string]$Name,890 [string]$Link,891 [string]$Target892 )893 894 TestRemoveItem $Link $Target895 }896 897 It "<Name>" -TestCases $unixTestCases -Skip:($IsWindows) {898 Param (899 [string]$Name,900 [string]$Link,901 [string]$Target902 )903 904 TestRemoveItem $Link $Target905 }906 907 It "<Name>" -TestCases $windowsTestCases -Skip:(-not $IsWindows) {908 Param (909 [string]$Name,910 [string]$Link,911 [string]$Target912 )913 914 TestRemoveItem $Link $Target915 }916 917 It "Remove-Item ignores -Recurse switch when deleting symlink to directory" {918 $folder = Join-Path $TestDrive "folder"919 $file = Join-Path $TestDrive "folder" "file"920 $link = Join-Path $TestDrive "sym-to-folder"921 New-Item -ItemType Directory -Path $folder > $null922 New-Item -ItemType File -Path $file -Value "some content" > $null923 New-Item -ItemType SymbolicLink -Path $link -Value $folder > $null924 $childA = Get-ChildItem $folder925 Remove-Item -Path $link -Recurse926 $childB = Get-ChildItem $folder927 $childB.Count | Should -Be 1928 $childB.Count | Should -BeExactly $childA.Count929 $childB.Name | Should -BeExactly $childA.Name930 }931 }932 933 Context "Show immediate target" {934 BeforeAll {935 $testDir = Join-Path $TestDrive "immediate-target"936 New-Item -ItemType Directory $testDir > $null937 938 $testFile = Join-Path $testDir "target"939 Set-Content -Path $testFile -Value "Hello world"940 941 Push-Location $testDir942 New-Item -ItemType SymbolicLink -Path 'firstLink' -Value 'target' > $null943 New-Item -ItemType SymbolicLink -Path 'secondLink' -Value 'firstLink' > $null944 Pop-Location945 }946 947 AfterAll {948 Remove-Item $testDir -Recurse -Force949 }950 951 It "Property 'Target' should show the immediate target" {952 $firstLink = Get-Item (Join-Path $testDir 'firstLink')953 $firstLink.Target | Should -BeExactly 'target'954 $str = [Microsoft.PowerShell.Commands.FileSystemProvider]::NameString($firstLink)955 [System.Management.Automation.Internal.StringDecorated]::new($str).ToString([System.Management.Automation.OutputRendering]::PlainText) | Should -BeExactly 'firstLink -> target'956 957 $secondLink = Get-Item (Join-Path $testDir 'secondLink')958 $secondLink.Target | Should -BeExactly 'firstLink'959 $str = [Microsoft.PowerShell.Commands.FileSystemProvider]::NameString($secondLink)960 [System.Management.Automation.Internal.StringDecorated]::new($str).ToString([System.Management.Automation.OutputRendering]::PlainText) | Should -BeExactly 'secondLink -> firstLink'961 }962 963 It "Get-Content should be able to resolve the final target" {964 Get-Content (Join-Path $testDir 'firstLink') | Should -BeExactly "Hello world"965 Get-Content (Join-Path $testDir 'secondLink') | Should -BeExactly "Hello world"966 }967 }968}969 970Describe "Copy-Item can avoid copying an item onto itself" -Tags "CI", "RequireAdminOnWindows" {971 BeforeAll {972 # For now, we'll assume the tests are running the platform's973 # native filesystem, in its default mode974 $isCaseSensitive = $IsLinux975 976 # The name of the key in an exception's Data dictionary when an977 # attempt is made to copy an item onto itself.978 $selfCopyKey = "SelfCopy"979 980 $subDir = "$TestDrive/sub"981 $otherSubDir = "$TestDrive/other-sub"982 $fileName = "file.txt"983 $filePath = "$TestDrive/$fileName"984 $otherFileName = "other-file"985 $otherFile = "$otherSubDir/$otherFileName"986 $symToOther = "$subDir/sym-to-other"987 $secondSymToOther = "$subDir/another-sym-to-other"988 $symToSym = "$subDir/sym-to-sym-to-other"989 $symToOtherFile = "$subDir/sym-to-other-file"990 $hardToOtherFile = "$subDir/hard-to-other-file"991 $symdToOther = "$subDir/symd-to-other"992 $junctionToOther = "$subDir/junction-to-other"993 994 New-Item -ItemType File $filePath -Value "stuff" > $null995 New-Item -ItemType Directory $subDir > $null996 New-Item -ItemType Directory $otherSubDir > $null997 New-Item -ItemType File $otherFile -Value "some text" > $null998 New-Item -ItemType SymbolicLink $symToOther -Value $otherSubDir > $null999 New-Item -ItemType SymbolicLink $secondSymToOther -Value $otherSubDir > $null1000 New-Item -ItemType SymbolicLink $symToSym -Value $symToOther > $null1001 New-Item -ItemType SymbolicLink $symToOtherFile -Value $otherFile > $null1002 New-Item -ItemType HardLink $hardToOtherFile -Value $otherFile > $null1003 1004 if ($IsWindows)1005 {1006 New-Item -ItemType Junction $junctionToOther -Value $otherSubDir > $null1007 New-Item -ItemType SymbolicLink $symdToOther -Value $otherSubDir > $null1008 }1009 }1010 1011 Context "Copy-Item using different case (on case-sensitive file systems)" {1012 BeforeEach {1013 $sourcePath = $filePath1014 $destinationPath = "$TestDrive/" + $fileName.Toupper()1015 }1016 AfterEach {1017 Remove-Item -Path $destinationPath -ErrorAction SilentlyContinue1018 }1019 1020 It "Copy-Item can copy to file name differing only by case" {1021 if ($isCaseSensitive)1022 {1023 Copy-Item -Path $sourcePath -Destination $destinationPath -ErrorAction SilentlyContinue | Should -BeNullOrEmpty1024 Test-Path -Path $destinationPath | Should -BeTrue1025 }1026 else1027 {1028 { Copy-Item -Path $sourcePath -Destination $destinationPath -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"1029 $error[0].Exception | Should -BeOfType System.IO.IOException1030 $error[0].Exception.Data[$selfCopyKey] | Should -Not -BeNullOrEmpty1031 }1032 }1033 }1034 1035 Context "Copy-Item avoids copying an item onto itself" {1036 BeforeAll {1037 $testCases = @(1038 @{1039 Name = "Copy to same path"1040 Source = $otherFile1041 Destination = $otherFile1042 }1043 @{1044 Name = "Copy hard link"1045 Source = $hardToOtherFile1046 Destination = $otherFile1047 }1048 @{1049 Name = "Copy hard link, reversed"1050 Source = $otherFile1051 Destination = $hardToOtherFile1052 }1053 @{1054 Name = "Copy symbolic link to target"1055 Source = $symToOtherFile1056 Destination = $otherFile1057 }1058 @{1059 Name = "Copy symbolic link to symbolic link with same target"1060 Source = $secondSymToOther1061 Destination = $symToOther1062 }1063 @{1064 Name = "Copy through chain of symbolic links"1065 Source = $symToSym1066 Destination = $otherSubDir1067 }1068 )1069 1070 # Junctions and directory symbolic links are Windows and NTFS only1071 $windowsTestCases = @(1072 @{1073 Name = "Copy junction to target"1074 Source = $junctionToOther1075 Destination = $otherSubDir1076 }1077 @{1078 Name = "Copy directory symbolic link to target"1079 Source = $symdToOther1080 Destination = $otherSubDir1081 }1082 )1083 1084 function TestSelfCopy1085 {1086 Param (1087 [string]$Source,1088 [string]$Destination1089 )1090 1091 { Copy-Item -Path $Source -Destination $Destination -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"1092 $error[0].Exception | Should -BeOfType System.IO.IOException1093 $error[0].Exception.Data[$selfCopyKey] | Should -Not -BeNullOrEmpty1094 }1095 }1096 1097 It "<Name>" -TestCases $testCases {1098 Param (1099 [string]$Name,1100 [string]$Source,1101 [string]$Destination1102 )1103 1104 TestSelfCopy $Source $Destination1105 }1106 1107 It "<Name>" -TestCases $windowsTestCases -Skip:(-not $IsWindows) {1108 Param (1109 [string]$Name,1110 [string]$Source,1111 [string]$Destination1112 )1113 1114 TestSelfCopy $Source $Destination1115 }1116 }1117}1118 1119Describe "Handling long paths" -Tags "CI" {1120 BeforeAll {1121 $longDir = 'a' * 2501122 $longSubDir = 'b' * 2501123 $fileName = "file1.txt"1124 $topPath = Join-Path $TestDrive $longDir1125 $longDirPath = Join-Path $topPath $longSubDir1126 $longFilePath = Join-Path $longDirPath $fileName1127 $cwd = Get-Location1128 }1129 BeforeEach {1130 New-Item -ItemType File -Path $longFilePath -Force | Out-Null1131 }1132 AfterEach {1133 Remove-Item -Path $topPath -Force -Recurse -ErrorAction SilentlyContinue1134 Set-Location $cwd1135 }1136 1137 It "Can remove a file via a long path" {1138 Remove-Item -Path $longFilePath -ErrorVariable e -ErrorAction SilentlyContinue1139 $e | Should -BeNullOrEmpty1140 $longFilePath | Should -Not -Exist1141 }1142 It "Can rename a file via a long path" {1143 $newFileName = "new-file.txt"1144 $newPath = Join-Path $longDirPath $newFileName1145 Rename-Item -Path $longFilePath -NewName $newFileName1146 $longFilePath | Should -Not -Exist1147 $newPath | Should -Exist1148 }1149 It "Can change into a directory via a long path" {1150 Set-Location -Path $longDirPath -ErrorVariable e -ErrorAction SilentlyContinue1151 $e | Should -BeNullOrEmpty1152 $c = Get-Location1153 $fileName | Should -Exist1154 }1155 It "Can use Test-Path to check for a file via a long path" {1156 Test-Path $longFilePath | Should -BeTrue1157 }1158}1159 1160Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature" {1161 BeforeAll {1162 $testDir = "testDir"1163 $testFile = "testFile.txt"1164 $testFile2 = "testFile2.txt"1165 $testContent = "Test 1"1166 $testContent2 = "Test 2"1167 $restoreLocation = Get-Location1168 }1169 1170 AfterAll {1171 #restore the previous location1172 Set-Location -Path $restoreLocation1173 }1174 1175 BeforeEach {1176 Set-Location -Path "TestDrive:\"1177 New-Item -Path $testFile -ItemType File > $null1178 New-Item -Path $testFile2 -ItemType File > $null1179 }1180 1181 AfterEach {1182 Set-Location -Path "TestDrive:\"1183 Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue1184 }1185 1186 Context "Valdiate New-Item parameters" {1187 BeforeEach {1188 #remove every file so that New-Item can be validated1189 Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue1190 }1191 1192 It "Verify Directory + Whatif" {1193 New-Item -Path . -ItemType Directory -Name $testDir -WhatIf > $null1194 { Get-Item -Path $testDir -ErrorAction Stop } |1195 Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"1196 }1197 1198 It "Verify Directory + Confirm bypass" {1199 $result = New-Item -Path . -ItemType Directory -Name $testDir -Confirm:$false1200 $result.Name | Should -BeExactly $testDir