2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2016-10-07 10:36:13 -07:00
Describe "Basic FileSystem Provider Tests" -Tags "CI" {
2016-09-13 18:29:48 -07:00
BeforeAll {
2016-10-07 10:36:13 -07:00
$testDir = "TestDir"
$testFile = "TestFile.txt"
2016-09-13 18:29:48 -07:00
$restoreLocation = Get-Location
}
AfterAll {
#restore the previous location
Set-Location -Path $restoreLocation
}
2017-01-16 13:31:14 -08:00
2016-10-07 10:36:13 -07:00
BeforeEach {
Set-Location -Path "TestDrive:\"
}
2016-09-13 18:29:48 -07:00
2016-10-07 10:36:13 -07:00
Context "Validate basic FileSystem Cmdlets" {
BeforeAll {
$newTestDir = "NewTestDir"
$newTestFile = "NewTestFile.txt"
$testContent = "Some Content"
$testContent2 = "More Content"
2019-05-20 22:43:51 -07:00
$reservedNamesTests = @ (
@ { deviceName = 'CON' }
@ { deviceName = 'PRN' }
@ { deviceName = 'AUX' }
@ { deviceName = 'CLOCK$' }
@ { deviceName = 'NUL' }
2020-09-14 19:10:12 +01:00
@ { deviceName = 'CONIN$' }
@ { deviceName = 'CONOUT$' }
2019-05-20 22:43:51 -07:00
@ { deviceName = 'COM0' }
@ { deviceName = 'COM1' }
@ { deviceName = 'COM2' }
@ { deviceName = 'COM3' }
@ { deviceName = 'COM4' }
@ { deviceName = 'COM5' }
@ { deviceName = 'COM6' }
@ { deviceName = 'COM7' }
@ { deviceName = 'COM8' }
@ { deviceName = 'COM9' }
@ { deviceName = 'LPT0' }
@ { deviceName = 'LPT1' }
@ { deviceName = 'LPT2' }
@ { deviceName = 'LPT3' }
@ { deviceName = 'LPT4' }
@ { deviceName = 'LPT5' }
@ { deviceName = 'LPT6' }
@ { deviceName = 'LPT7' }
@ { deviceName = 'LPT8' }
@ { deviceName = 'LPT9' }
)
2016-10-07 10:36:13 -07:00
}
BeforeEach {
New-Item -Path $testDir -ItemType Directory > $null
New-Item -Path $testFile -ItemType File > $null
}
AfterEach {
Set-Location -Path "TestDrive:\"
Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue
}
It "Verify New-Item for directory" {
$newDir = New-Item -Path $newTestDir -ItemType Directory
$directoryExists = Test-Path $newTestDir
2018-03-09 15:13:15 -08:00
$directoryExists | Should -BeTrue
$newDir . Name | Should -BeExactly $newTestDir
2016-10-07 10:36:13 -07:00
}
It "Verify New-Item for file" {
$newFile = New-Item -Path $newTestFile -ItemType File
2018-03-09 15:13:15 -08:00
$newTestFile | Should -Exist
$newFile . Name | Should -BeExactly $newTestFile
2016-10-07 10:36:13 -07:00
}
It "Verify Remove-Item for directory" {
$existsBefore = Test-Path $testDir
Remove-Item -Path $testDir -Recurse -Force
$existsAfter = Test-Path $testDir
2018-03-09 15:13:15 -08:00
$existsBefore | Should -BeTrue
$existsAfter | Should -BeFalse
2016-10-07 10:36:13 -07:00
}
It "Verify Remove-Item for file" {
$existsBefore = Test-Path $testFile
Remove-Item -Path $testFile -Force
$existsAfter = Test-Path $testFile
2018-03-09 15:13:15 -08:00
$existsBefore | Should -BeTrue
$existsAfter | Should -BeFalse
2016-10-07 10:36:13 -07:00
}
2017-08-02 09:51:22 -07:00
It "Verify Rename-Item for file" {
Rename-Item -Path $testFile -NewName $newTestFile -ErrorAction Stop
2018-03-09 15:13:15 -08:00
$testFile | Should -Not -Exist
$newTestFile | Should -Exist
2017-08-02 09:51:22 -07:00
}
It "Verify Rename-Item for directory" {
Rename-Item -Path $testDir -NewName $newTestDir -ErrorAction Stop
2018-03-09 15:13:15 -08:00
$testDir | Should -Not -Exist
$newTestDir | Should -Exist
2017-08-02 09:51:22 -07:00
}
It "Verify Rename-Item will not rename to an existing name" {
2018-05-17 14:42:04 -07:00
{ Rename-Item -Path $testFile -NewName $testDir -ErrorAction Stop } | Should -Throw -ErrorId "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand"
2020-01-13 19:19:39 +00:00
$error [ 0 ]. Exception | Should -BeOfType System . IO . IOException
2018-03-09 15:13:15 -08:00
$testFile | Should -Exist
2017-08-02 09:51:22 -07:00
}
2016-10-07 10:36:13 -07:00
It "Verify Copy-Item" {
$newFile = Copy-Item -Path $testFile -Destination $newTestFile -PassThru
$fileExists = Test-Path $newTestFile
2018-03-09 15:13:15 -08:00
$fileExists | Should -BeTrue
$newFile . Name | Should -BeExactly $newTestFile
2016-10-07 10:36:13 -07:00
}
2017-08-02 09:51:22 -07:00
It "Verify Move-Item for file" {
Move-Item -Path $testFile -Destination $testDir -ErrorAction Stop
2018-03-09 15:13:15 -08:00
$testFile | Should -Not -Exist
" $testDir / $testFile " | Should -Exist
2017-08-02 09:51:22 -07:00
}
It "Verify Move-Item for directory" {
$destDir = "DestinationDirectory"
2019-01-03 07:59:14 +00:00
New-Item -Path $destDir -ItemType Directory -ErrorAction Stop > $null
2017-08-02 09:51:22 -07:00
Move-Item -Path $testFile -Destination $testDir
Move-Item -Path $testDir -Destination $destDir
2018-03-09 15:13:15 -08:00
$testDir | Should -Not -Exist
" $destDir / $testDir " | Should -Exist
" $destDir / $testDir / $testFile " | Should -Exist
2017-08-02 09:51:22 -07:00
}
2020-07-06 16:39:08 -07:00
It "Verify Move-Item across devices for directory" {
[ System.Management.Automation.Internal.InternalTestHooks ]:: SetTestHook ( 'ThrowExdevErrorOnMoveDirectory' , $true )
try
{
$dir = ( New-Item -Path TestDrive : / dir -ItemType Directory -ErrorAction Stop ). FullName
$file = ( New-Item -Path " $dir /file.txt" -Value "HELLO" -ErrorAction Stop ). Name
$destination = " $TestDrive /destination"
Move-Item -Path $dir -Destination $destination -ErrorAction Stop
$dir | Should -Not -Exist
$destination | Should -Exist
" $destination / $file " | Should -Exist
}
finally
{
[ System.Management.Automation.Internal.InternalTestHooks ]:: SetTestHook ( 'ThrowExdevErrorOnMoveDirectory' , $false )
}
}
2017-08-02 09:51:22 -07:00
It "Verify Move-Item will not move to an existing file" {
2018-05-17 14:42:04 -07:00
{ Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand"
2020-01-13 19:19:39 +00:00
$error [ 0 ]. Exception | Should -BeOfType System . IO . IOException
2018-03-09 15:13:15 -08:00
$testDir | Should -Exist
2017-08-02 09:51:22 -07:00
}
2020-07-06 16:39:08 -07:00
It "Verify Move-Item throws correct error for non-existent source" {
{ Move-Item -Path / does / not / exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'
}
2017-08-02 09:51:22 -07:00
It "Verify Move-Item as substitute for Rename-Item" {
2016-10-07 10:36:13 -07:00
$newFile = Move-Item -Path $testFile -Destination $newTestFile -PassThru
$fileExists = Test-Path $newTestFile
2018-03-09 15:13:15 -08:00
$fileExists | Should -BeTrue
$newFile . Name | Should -Be $newTestFile
2016-10-07 10:36:13 -07:00
}
It "Verify Get-ChildItem" {
$dirContents = Get-ChildItem "."
2018-03-09 15:13:15 -08:00
$dirContents . Count | Should -Be 2
2016-10-07 10:36:13 -07:00
}
2017-08-01 13:16:30 -07:00
It "Verify Get-ChildItem can get the name of a specified item." {
$fileName = Get-ChildItem $testFile -Name
$fileInfo = Get-ChildItem $testFile
2018-03-09 15:13:15 -08:00
$fileName | Should -BeExactly $fileInfo . Name
2017-08-01 13:16:30 -07:00
}
2016-10-07 10:36:13 -07:00
It "Set-Content to a file" {
$content = Set-Content -Value $testContent -Path $testFile -PassThru
2018-03-09 15:13:15 -08:00
$content | Should -BeExactly $testContent
2017-01-16 13:31:14 -08:00
}
2016-10-07 10:36:13 -07:00
It "Add-Content to a file" {
$content = Set-Content -Value $testContent -Path $testFile -PassThru
$addContent = Add-Content -Value $testContent2 -Path $testFile -PassThru
$fullContent = Get-Content -Path $testFile
2018-03-09 15:13:15 -08:00
$content | Should -Match $testContent
$addContent | Should -Match $testContent2
( $fullContent [ 0 ] + $fullContent [ 1 ]) | Should -Match ( $testContent + $testContent2 )
2016-10-07 10:36:13 -07:00
}
It "Clear-Content of a file" {
Set-Content -Value $testContent -Path $testFile
$contentBefore = Get-Content -Path $testFile
Clear-Content -Path $testFile
$contentAfter = Get-Content -Path $testFile
2018-03-09 15:13:15 -08:00
$contentBefore . Count | Should -Be 1
$contentAfter . Count | Should -Be 0
2016-10-07 10:36:13 -07:00
}
2017-03-07 10:29:20 -08:00
2019-05-20 22:43:51 -07:00
It "Copy-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip: ( -not $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
{ Copy-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"
2017-03-07 10:29:20 -08:00
}
2019-05-20 22:43:51 -07:00
It "Move-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip: ( -not $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
{ Move-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "MoveError,Microsoft.PowerShell.Commands.MoveItemCommand"
2017-03-07 10:29:20 -08:00
}
2019-05-20 22:43:51 -07:00
It "Rename-Item on Windows rejects Windows reserved device name: <deviceName>" -Skip: ( -not $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
{ Rename-Item -Path $testFile -NewName $deviceName -ErrorAction Stop } | Should -Throw -ErrorId "RenameError,Microsoft.PowerShell.Commands.RenameItemCommand"
2017-03-07 10:29:20 -08:00
}
2019-05-20 22:43:51 -07:00
It "Copy-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip: ( $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
Copy-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue
Test-Path $deviceName | Should -BeTrue
2017-03-07 10:29:20 -08:00
}
2019-05-20 22:43:51 -07:00
It "Move-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip: ( $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue
Test-Path $deviceName | Should -BeTrue
New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue
2017-03-07 10:29:20 -08:00
}
2019-05-20 22:43:51 -07:00
It "Rename-Item on Unix succeeds with Windows reserved device name: <deviceName>" -Skip: ( $IsWindows ) -TestCases $reservedNamesTests {
param ( $deviceName )
Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue
Test-Path $deviceName | Should -BeTrue
New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue
2017-03-07 10:29:20 -08:00
}
2017-10-06 11:27:15 -07:00
It "Set-Location on Unix succeeds with folder with colon: <path>" -Skip: ( $IsWindows ) -TestCases @ (
@ { path = "\hello:world" },
@ { path = "\:world" },
@ { path = "/hello:" }
) {
param ( $path )
try {
New-Item -Path " $testdrive$path " -ItemType Directory > $null
Set-Location " $testdrive "
Set-Location ". $path "
2018-03-09 15:13:15 -08:00
( Get-Location ). Path | Should -BeExactly " $testdrive / $( $path . Substring ( 1 , $path . Length - 1 )) "
2017-10-06 11:27:15 -07:00
}
finally {
Remove-Item -Path " $testdrive$path " -ErrorAction SilentlyContinue
}
}
It "Get-Content on Unix succeeds with folder and file with colon: <path>" -Skip: ( $IsWindows ) -TestCases @ (
@ { path = "\foo:bar.txt" },
@ { path = "/foo:" },
@ { path = "\:bar" }
) {
param ( $path )
try {
$testPath = " $testdrive /hello:world"
New-Item -Path " $testPath " -ItemType Directory > $null
Set-Content -Path " $testPath$path " -Value "Hello"
$files = Get-ChildItem " $testPath "
2018-03-09 15:13:15 -08:00
$files . Count | Should -Be 1
$files [ 0 ]. Name | Should -BeExactly $path . Substring ( 1 , $path . Length - 1 )
$files [ 0 ] | Get-Content | Should -BeExactly "Hello"
2017-10-06 11:27:15 -07:00
}
finally {
Remove-Item -Path $testPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
2016-10-07 10:36:13 -07:00
}
2017-08-02 09:51:22 -07:00
Context "Validate behavior when access is denied" {
BeforeAll {
2017-10-17 17:25:11 -07:00
$powershell = Join-Path $PSHOME "pwsh"
2017-08-02 09:51:22 -07:00
if ( $IsWindows )
{
$protectedPath = Join-Path ([ environment ]:: GetFolderPath ( "windows" )) "appcompat" "Programs"
$protectedPath2 = Join-Path $protectedPath "Install"
$newItemPath = Join-Path $protectedPath "foo"
2019-05-10 10:56:12 -07:00
$shouldSkip = -not ( Test-Path $protectedPath )
2017-08-02 09:51:22 -07:00
}
2021-03-08 15:01:31 -08:00
if ( $IsWindows ) {
$fqaccessdenied = "MoveDirectoryItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.MoveItemCommand"
}
else {
$fqaccessdenied = "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand"
}
2017-08-02 09:51:22 -07:00
}
2019-05-10 10:56:12 -07:00
It "Access-denied test for <cmdline>" -Skip: ( -not $IsWindows -or $shouldSkip ) -TestCases @ (
2017-09-12 11:01:32 -07:00
# NOTE: ensure the fileNameBase parameter is unique for each test case; it is used to generate a unique error and done file name.
2018-08-17 22:06:51 -07:00
# The following test does not consistently work on windows
# @{cmdline = "Get-Item $protectedPath2 -ErrorAction Stop"; expectedError = "ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetItemCommand"}
2018-12-11 14:20:22 -08:00
# @{cmdline = "New-Item -Type File -Path $newItemPath -ErrorAction Stop"; expectedError = "NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand"}
2018-03-19 15:36:56 -07:00
@ { cmdline = "Get-ChildItem $protectedPath -ErrorAction Stop" ; expectedError = "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand" }
@ { cmdline = "Rename-Item -Path $protectedPath -NewName bar -ErrorAction Stop" ; expectedError = "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand" },
2021-03-08 15:01:31 -08:00
@ { cmdline = "Move-Item -Path $protectedPath -Destination bar -ErrorAction Stop" ; expectedError = $fqaccessdenied }
2017-08-02 09:51:22 -07:00
) {
param ( $cmdline , $expectedError )
2018-03-19 15:36:56 -07:00
$scriptBlock = [ scriptblock ]:: Create ( $cmdline )
$scriptBlock | Should -Throw -ErrorId $expectedError
2017-08-02 09:51:22 -07:00
}
}
2019-11-12 08:53:33 -08:00
Context "Appx path" {
BeforeAll {
$skipTest = $true
2020-01-05 13:01:42 +05:00
if ( $IsWindows -and ( Get-Command -Name Get-AppxPackage ) ) {
$pkgDir = ( Get-AppxPackage Microsoft . WindowsCalculator -ErrorAction SilentlyContinue ). InstallLocation
$skipTest = $pkgDir -eq $null
2019-11-12 08:53:33 -08:00
}
}
It "Can get an appx package item" -Skip: $skipTest {
2020-01-24 19:00:37 +00:00
Get-Item $pkgDir \ Calculator . exe -ErrorAction Stop | Should -BeOfType System . IO . FileInfo
Get-Item -Path $pkgDir -ErrorAction Stop | Should -BeOfType System . IO . DirectoryInfo
2019-11-12 08:53:33 -08:00
Get-ChildItem -Path $pkgDir -ErrorAction Stop | Should -Not -BeNullOrEmpty
}
}
2016-10-07 10:36:13 -07:00
Context "Validate basic host navigation functionality" {
BeforeAll {
#build semi-complex directory structure to test navigation within
$level1_0 = "Level1_0"
$level2_0 = "Level2_0"
$level2_1 = "Level2_1"
$root = Join-Path "TestDrive:" "" #adds correct / or \
$level1_0Full = Join-Path $root $level1_0
$level2_0Full = Join-Path $level1_0Full $level2_0
$level2_1Full = Join-Path $level1_0Full $level2_1
New-Item -Path $level1_0Full -ItemType Directory > $null
New-Item -Path $level2_0Full -ItemType Directory > $null
New-Item -Path $level2_1Full -ItemType Directory > $null
}
It "Verify Get-Location and Set-Location" {
$currentLoc = Get-Location
Set-Location $level1_0
$level1Loc = Get-Location
Set-Location $level2_0
$level2Loc = Get-Location
2018-03-09 15:13:15 -08:00
$currentLoc . Path | Should -BeExactly $root
$level1Loc . Path | Should -BeExactly $level1_0Full
$level2Loc . Path | Should -BeExactly $level2_0Full
2016-10-07 10:36:13 -07:00
}
It "Verify Push-Location and Pop-Location" {
#push a bunch of locations
Push-Location
$push0 = Get-Location
Set-Location $level1_0
Push-Location
$push1 = Get-Location
Set-Location $level2_0
Push-Location
$push2 = Get-Location
#navigate back home to change path out of all pushed locations
Set-Location "TestDrive:\"
#pop locations off
Pop-Location
$pop0 = Get-Location
Pop-Location
$pop1 = Get-Location
Pop-Location
$pop2 = Get-Location
2018-03-09 15:13:15 -08:00
$pop0 . Path | Should -BeExactly $push2 . Path
$pop1 . Path | Should -BeExactly $push1 . Path
$pop2 . Path | Should -BeExactly $push0 . Path
2016-10-07 10:36:13 -07:00
}
}
Context "Validate Basic Path Cmdlets" {
It "Verify Convert-Path" {
$result = Convert-Path "."
2018-03-09 15:13:15 -08:00
( $result . TrimEnd ( '/\' )) | Should -BeExactly " $TESTDRIVE "
2016-10-07 10:36:13 -07:00
}
It "Verify Join-Path" {
$result = Join-Path -Path "TestDrive:" -ChildPath "temp"
if ( $IsWindows ) {
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:\temp"
2016-10-07 10:36:13 -07:00
}
else {
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:/temp"
2016-10-07 10:36:13 -07:00
}
}
It "Verify Split-Path" {
$testPath = Join-Path "TestDrive:" "MyTestFile.txt"
$result = Split-Path $testPath -Qualifier
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:"
2016-10-07 10:36:13 -07:00
}
It "Verify Test-Path" {
$result = Test-Path $HOME
2018-03-09 15:13:15 -08:00
$result | Should -BeTrue
2016-10-07 10:36:13 -07:00
}
It "Verify HOME" {
$homePath = $HOME
$tildePath = ( Resolve-Path -Path ~). Path
2018-03-09 15:13:15 -08:00
$homePath | Should -BeExactly $tildePath
2016-10-07 10:36:13 -07:00
}
}
}
2017-05-02 18:42:16 -07:00
Describe "Handling of globbing patterns" -Tags "CI" {
Context "Handling of Unix [ab] globbing patterns in literal paths" {
BeforeAll {
$filePath = Join-Path $TESTDRIVE "file[txt].txt"
$newPath = Join-Path $TESTDRIVE "file.txt.txt"
$dirPath = Join-Path $TESTDRIVE "subdir"
}
BeforeEach {
$file = New-Item -ItemType File -Path $filePath -Force
}
2018-02-05 11:58:25 -08:00
AfterEach {
2017-05-02 18:42:16 -07:00
Remove-Item -Force -Recurse -Path $dirPath -ErrorAction SilentlyContinue
Remove-Item -Force -LiteralPath $newPath -ErrorAction SilentlyContinue
}
It "Rename-Item -LiteralPath can rename a file with Unix globbing characters" {
Rename-Item -LiteralPath $file . FullName -NewName $newPath
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $file . FullName | Should -BeFalse
Test-Path -LiteralPath $newPath | Should -BeTrue
2017-05-02 18:42:16 -07:00
}
It "Remove-Item -LiteralPath can delete a file with Unix globbing characters" {
Remove-Item -LiteralPath $file . FullName
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $file . FullName | Should -BeFalse
2017-05-02 18:42:16 -07:00
}
It "Move-Item -LiteralPath can move a file with Unix globbing characters" {
$dir = New-Item -ItemType Directory -Path $dirPath
Move-Item -LiteralPath $file . FullName -Destination $dir . FullName
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $file . FullName | Should -BeFalse
2017-05-02 18:42:16 -07:00
$newPath = Join-Path $dir . FullName $file . Name
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $newPath | Should -BeTrue
2017-05-02 18:42:16 -07:00
}
It "Copy-Item -LiteralPath can copy a file with Unix globbing characters" {
Copy-Item -LiteralPath $file . FullName -Destination $newPath
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $newPath | Should -BeTrue
2017-05-02 18:42:16 -07:00
}
2017-10-09 09:33:22 -07:00
}
2017-09-25 10:15:38 -07:00
2017-10-09 09:33:22 -07:00
Context "Handle asterisks in name" {
It "Remove-Item -LiteralPath should fail if it contains asterisk and file doesn't exist" {
2018-05-17 14:42:04 -07:00
{ Remove-Item -LiteralPath ./ foo *. txt -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
2017-09-25 10:15:38 -07:00
}
2017-10-09 09:33:22 -07:00
It "Remove-Item -LiteralPath should succeed for file with asterisk in name" -Skip: ( $IsWindows ) {
$testPath = " $testdrive \foo*"
$testPath2 = " $testdrive \foo*2"
New-Item -Path $testPath -ItemType File
New-Item -Path $testPath2 -ItemType File
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $testPath | Should -BeTrue
Test-Path -LiteralPath $testPath2 | Should -BeTrue
{ Remove-Item -LiteralPath $testPath } | Should -Not -Throw
Test-Path -LiteralPath $testPath | Should -BeFalse
2017-10-09 09:33:22 -07:00
# make sure wildcard wasn't applied so this file should still exist
2018-03-09 15:13:15 -08:00
Test-Path -LiteralPath $testPath2 | Should -BeTrue
2017-10-09 09:33:22 -07:00
}
2017-05-02 18:42:16 -07:00
}
}
2017-04-27 17:47:24 -07:00
Describe "Hard link and symbolic link tests" -Tags "CI" , "RequireAdminOnWindows" {
BeforeAll {
# on macOS, the /tmp directory is a symlink, so we'll resolve it here
$TestPath = $TestDrive
2017-09-07 10:34:40 -07:00
if ( $IsMacOS )
2017-04-27 17:47:24 -07:00
{
$item = Get-Item $TestPath
$dirName = $item . BaseName
2019-05-20 22:43:51 -07:00
$item = Get-Item $item . PSParentPath -Force
2017-04-27 17:47:24 -07:00
if ( $item . LinkType -eq "SymbolicLink" )
{
$TestPath = Join-Path $item . Target $dirName
}
}
$realFile = Join-Path $TestPath "file.txt"
$nonFile = Join-Path $TestPath "not-a-file"
$fileContent = "some text"
$realDir = Join-Path $TestPath "subdir"
$nonDir = Join-Path $TestPath "not-a-dir"
$hardLinkToFile = Join-Path $TestPath "hard-to-file.txt"
$symLinkToFile = Join-Path $TestPath "sym-link-to-file.txt"
$symLinkToDir = Join-Path $TestPath "sym-link-to-dir"
$symLinkToNothing = Join-Path $TestPath "sym-link-to-nowhere"
$dirSymLinkToDir = Join-Path $TestPath "symd-link-to-dir"
$junctionToDir = Join-Path $TestPath "junction-to-dir"
2019-01-03 07:59:14 +00:00
New-Item -ItemType File -Path $realFile -Value $fileContent > $null
New-Item -ItemType Directory -Path $realDir > $null
2017-04-27 17:47:24 -07:00
}
Context "New-Item and hard/symbolic links" {
It "New-Item can create a hard link to a file" {
2019-05-20 22:43:51 -07:00
New-Item -ItemType HardLink -Path $hardLinkToFile -Value $realFile > $null
2018-03-09 15:13:15 -08:00
Test-Path $hardLinkToFile | Should -BeTrue
2017-04-27 17:47:24 -07:00
$link = Get-Item -Path $hardLinkToFile
2018-03-09 15:13:15 -08:00
$link . LinkType | Should -BeExactly "HardLink"
Get-Content -Path $hardLinkToFile | Should -Be $fileContent
2017-04-27 17:47:24 -07:00
}
It "New-Item can create symbolic link to file" {
2019-05-20 22:43:51 -07:00
New-Item -ItemType SymbolicLink -Path $symLinkToFile -Value $realFile > $null
2018-03-09 15:13:15 -08:00
Test-Path $symLinkToFile | Should -BeTrue
2017-04-27 17:47:24 -07:00
$real = Get-Item -Path $realFile
$link = Get-Item -Path $symLinkToFile
2018-03-09 15:13:15 -08:00
$link . LinkType | Should -BeExactly "SymbolicLink"
2019-05-20 22:43:51 -07:00
$link . Target | Should -BeExactly $real . ToString ()
2018-03-09 15:13:15 -08:00
Get-Content -Path $symLinkToFile | Should -Be $fileContent
2017-04-27 17:47:24 -07:00
}
It "New-Item can create a symbolic link to nothing" {
2019-05-20 22:43:51 -07:00
New-Item -ItemType SymbolicLink -Path $symLinkToNothing -Value $nonFile > $null
2018-03-09 15:13:15 -08:00
Test-Path $symLinkToNothing | Should -BeTrue
2017-04-27 17:47:24 -07:00
$link = Get-Item -Path $symLinkToNothing
2018-03-09 15:13:15 -08:00
$link . LinkType | Should -BeExactly "SymbolicLink"
2019-05-20 22:43:51 -07:00
$link . Target | Should -Be $nonFile . ToString ()
2017-04-27 17:47:24 -07:00
}
2017-05-05 10:09:34 -07:00
It "New-Item emits an error when path to symbolic link already exists." {
2018-05-17 14:42:04 -07:00
{ New-Item -ItemType SymbolicLink -Path $realDir -Value $symLinkToDir -ErrorAction Stop } | Should -Throw -ErrorId "SymLinkExists,Microsoft.PowerShell.Commands.NewItemCommand"
2017-05-05 10:09:34 -07:00
}
2017-04-27 17:47:24 -07:00
It "New-Item can create a symbolic link to a directory" -Skip: ( $IsWindows ) {
2019-05-20 22:43:51 -07:00
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null
2018-03-09 15:13:15 -08:00
Test-Path $symLinkToDir | Should -BeTrue
2017-04-27 17:47:24 -07:00
$real = Get-Item -Path $realDir
$link = Get-Item -Path $symLinkToDir
2018-03-09 15:13:15 -08:00
$link . LinkType | Should -BeExactly "SymbolicLink"
2019-05-20 22:43:51 -07:00
$link . Target | Should -BeExactly $real . ToString ()
2017-04-27 17:47:24 -07:00
}
It "New-Item can create a directory symbolic link to a directory" -Skip: ( -Not $IsWindows ) {
2019-05-20 22:43:51 -07:00
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir > $null
2018-03-09 15:13:15 -08:00
Test-Path $symLinkToDir | Should -BeTrue
2017-04-27 17:47:24 -07:00
$real = Get-Item -Path $realDir
$link = Get-Item -Path $symLinkToDir
2018-03-09 15:13:15 -08:00
$link | Should -BeOfType System . IO . DirectoryInfo
$link . LinkType | Should -BeExactly "SymbolicLink"
2019-05-20 22:43:51 -07:00
$link . Target | Should -BeExactly $real . ToString ()
2017-04-27 17:47:24 -07:00
}
It "New-Item can create a directory junction to a directory" -Skip: ( -Not $IsWindows ) {
2019-05-20 22:43:51 -07:00
New-Item -ItemType Junction -Path $junctionToDir -Value $realDir > $null
2018-03-09 15:13:15 -08:00
Test-Path $junctionToDir | Should -BeTrue
2017-04-27 17:47:24 -07:00
}
}
2017-05-04 18:00:13 -07:00
Context "Get-ChildItem and symbolic links" {
BeforeAll {
2017-05-15 14:09:46 -07:00
$alphaDir = Join-Path $TestDrive "sub-alpha"
$alphaLink = Join-Path $TestDrive "link-alpha"
$alphaFile1 = Join-Path $alphaDir "AlphaFile1.txt"
$alphaFile2 = Join-Path $alphaDir "AlphaFile2.txt"
2017-07-06 12:28:27 -07:00
$omegaDir = Join-Path $TestDrive "sub-omega"
$omegaFile1 = Join-Path $omegaDir "OmegaFile1"
$omegaFile2 = Join-Path $omegaDir "OmegaFile2"
2018-03-09 15:13:15 -08:00
$betaDir = Join-Path $alphaDir "sub-Beta"
$betaLink = Join-Path $alphaDir "link-Beta"
2017-05-15 14:09:46 -07:00
$betaFile1 = Join-Path $betaDir "BetaFile1.txt"
$betaFile2 = Join-Path $betaDir "BetaFile2.txt"
$betaFile3 = Join-Path $betaDir "BetaFile3.txt"
2017-07-06 12:28:27 -07:00
$gammaDir = Join-Path $betaDir "sub-gamma"
$uponeLink = Join-Path $gammaDir "upone-link"
$uptwoLink = Join-Path $gammaDir "uptwo-link"
$omegaLink = Join-Path $gammaDir "omegaLink"
2017-05-04 18:00:13 -07:00
2017-05-15 14:09:46 -07:00
New-Item -ItemType Directory -Path $alphaDir
New-Item -ItemType File -Path $alphaFile1
New-Item -ItemType File -Path $alphaFile2
New-Item -ItemType Directory -Path $betaDir
New-Item -ItemType File -Path $betaFile1
New-Item -ItemType File -Path $betaFile2
New-Item -ItemType File -Path $betaFile3
2017-07-06 12:28:27 -07:00
New-Item -ItemType Directory $omegaDir
New-Item -ItemType File -Path $omegaFile1
New-Item -ItemType File -Path $omegaFile2
2017-05-04 18:00:13 -07:00
}
AfterAll {
2017-05-15 14:09:46 -07:00
Remove-Item -Path $alphaLink -Force -ErrorAction SilentlyContinue
Remove-Item -Path $betaLink -Force -ErrorAction SilentlyContinue
2017-05-04 18:00:13 -07:00
}
It "Get-ChildItem gets content of linked-to directory" {
2017-05-15 14:09:46 -07:00
$filenamePattern = "AlphaFile[12]\.txt"
New-Item -ItemType SymbolicLink -Path $alphaLink -Value $alphaDir
$ci = Get-ChildItem $alphaLink
2018-03-09 15:13:15 -08:00
$ci . Count | Should -Be 3
$ci [ 1 ]. Name | Should -MatchExactly $filenamePattern
$ci [ 2 ]. Name | Should -MatchExactly $filenamePattern
2017-05-15 14:09:46 -07:00
}
2020-02-12 08:17:44 +05:00
It "Get-ChildItem -Name gets content of linked-to directory" {
# The test depends on the files created in previous test:
#$filenamePattern = "AlphaFile[12]\.txt"
#New-Item -ItemType SymbolicLink -Path $alphaLink -Value $alphaDir
$ci = Get-ChildItem $alphaLink -Name
$ci . Count | Should -Be 3
$ci [ 1 ] | Should -MatchExactly $filenamePattern
$ci [ 2 ] | Should -MatchExactly $filenamePattern
}
2017-05-15 14:09:46 -07:00
It "Get-ChildItem does not recurse into symbolic links not explicitly given on the command line" {
New-Item -ItemType SymbolicLink -Path $betaLink -Value $betaDir
$ci = Get-ChildItem $alphaLink -Recurse
2018-03-09 15:13:15 -08:00
$ci . Count | Should -BeExactly 7
2017-05-04 18:00:13 -07:00
}
2020-02-12 08:17:44 +05:00
It "Get-ChildItem -Name does not recurse into symbolic links not explicitly given on the command line" -Pending {
# The test depends on the files created in previous test:
#New-Item -ItemType SymbolicLink -Path $betaLink -Value $betaDir
$ci = Get-ChildItem $alphaLink -Recurse -Name
$ci . Count | Should -BeExactly 7 # returns 10 - unexpectly recurce in link-alpha\link-Beta. See https://github.com/PowerShell/PowerShell/issues/11614
}
2017-07-06 12:28:27 -07:00
It "Get-ChildItem will recurse into symlinks given -FollowSymlink, avoiding link loops" {
New-Item -ItemType Directory -Path $gammaDir
New-Item -ItemType SymbolicLink -Path $uponeLink -Value $betaDir
New-Item -ItemType SymbolicLink -Path $uptwoLink -Value $alphaDir
New-Item -ItemType SymbolicLink -Path $omegaLink -Value $omegaDir
$ci = Get-ChildItem -Path $alphaDir -FollowSymlink -Recurse -WarningVariable w -WarningAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$ci . Count | Should -BeExactly 13
$w . Count | Should -BeExactly 3
2017-07-06 12:28:27 -07:00
}
2020-02-12 08:17:44 +05:00
It "Get-ChildItem -Name will recurse into symlinks given -FollowSymlink, avoiding link loops" -Pending {
# The test depends on the files created in previous test:
# New-Item -ItemType Directory -Path $gammaDir
# New-Item -ItemType SymbolicLink -Path $uponeLink -Value $betaDir
# New-Item -ItemType SymbolicLink -Path $uptwoLink -Value $alphaDir
# New-Item -ItemType SymbolicLink -Path $omegaLink -Value $omegaDir
$ci = Get-ChildItem -Path $alphaDir -FollowSymlink -Recurse -WarningVariable w -WarningAction SilentlyContinue -Name # unexpectly dead cycle. See https://github.com/PowerShell/PowerShell/issues/11614
$ci . Count | Should -BeExactly 13
$w . Count | Should -BeExactly 3
}
2017-05-04 18:00:13 -07:00
}
2017-04-27 17:47:24 -07:00
Context "Remove-Item and hard/symbolic links" {
BeforeAll {
$testCases = @ (
@ {
Name = "Remove-Item can remove a hard link to a file"
Link = $hardLinkToFile
Target = $realFile
}
@ {
Name = "Remove-Item can remove a symbolic link to a file"
Link = $symLinkToFile
Target = $realFile
}
)
# New-Item on Windows will not create a "plain" symlink to a directory
$unixTestCases = @ (
@ {
Name = "Remove-Item can remove a symbolic link to a directory on Unix"
Link = $symLinkToDir
Target = $realDir
}
)
# Junctions and directory symbolic links are Windows and NTFS only
$windowsTestCases = @ (
@ {
Name = "Remove-Item can remove a symbolic link to a directory on Windows"
Link = $symLinkToDir
Target = $realDir
}
@ {
Name = "Remove-Item can remove a directory symbolic link to a directory on Windows"
Link = $dirSymLinkToDir
Target = $realDir
}
@ {
Name = "Remove-Item can remove a junction to a directory"
Link = $junctionToDir
Target = $realDir
}
)
function TestRemoveItem
{
Param (
[ string ] $Link ,
[ string ] $Target
)
2019-01-03 07:59:14 +00:00
Remove-Item -Path $Link -ErrorAction SilentlyContinue > $null
2018-03-09 15:13:15 -08:00
Test-Path -Path $Link | Should -BeFalse
Test-Path -Path $Target | Should -BeTrue
2017-04-27 17:47:24 -07:00
}
}
It "<Name>" -TestCases $testCases {
Param (
[ string ] $Name ,
[ string ] $Link ,
[ string ] $Target
)
TestRemoveItem $Link $Target
}
It "<Name>" -TestCases $unixTestCases -Skip: ( $IsWindows ) {
Param (
[ string ] $Name ,
[ string ] $Link ,
[ string ] $Target
)
TestRemoveItem $Link $Target
}
It "<Name>" -TestCases $windowsTestCases -Skip: ( -not $IsWindows ) {
Param (
[ string ] $Name ,
[ string ] $Link ,
[ string ] $Target
)
TestRemoveItem $Link $Target
}
It "Remove-Item ignores -Recurse switch when deleting symlink to directory" {
$folder = Join-Path $TestDrive "folder"
$file = Join-Path $TestDrive "folder" "file"
$link = Join-Path $TestDrive "sym-to-folder"
2019-01-03 07:59:14 +00:00
New-Item -ItemType Directory -Path $folder > $null
New-Item -ItemType File -Path $file -Value "some content" > $null
2020-05-07 13:00:30 +01:00
New-Item -ItemType SymbolicLink -Path $link -Value $folder > $null
$childA = Get-ChildItem $folder
2017-04-27 17:47:24 -07:00
Remove-Item -Path $link -Recurse
$childB = Get-ChildItem $folder
2018-03-09 15:13:15 -08:00
$childB . Count | Should -Be 1
$childB . Count | Should -BeExactly $childA . Count
$childB . Name | Should -BeExactly $childA . Name
2017-04-27 17:47:24 -07:00
}
}
}
2017-04-21 16:09:53 -07:00
Describe "Copy-Item can avoid copying an item onto itself" -Tags "CI" , "RequireAdminOnWindows" {
BeforeAll {
# For now, we'll assume the tests are running the platform's
# native filesystem, in its default mode
$isCaseSensitive = $IsLinux
# The name of the key in an exception's Data dictionary when an
# attempt is made to copy an item onto itself.
$selfCopyKey = "SelfCopy"
$subDir = " $TestDrive /sub"
$otherSubDir = " $TestDrive /other-sub"
$fileName = "file.txt"
$filePath = " $TestDrive / $fileName "
$otherFileName = "other-file"
$otherFile = " $otherSubDir / $otherFileName "
$symToOther = " $subDir /sym-to-other"
$secondSymToOther = " $subDir /another-sym-to-other"
$symToSym = " $subDir /sym-to-sym-to-other"
$symToOtherFile = " $subDir /sym-to-other-file"
$hardToOtherFile = " $subDir /hard-to-other-file"
$symdToOther = " $subDir /symd-to-other"
$junctionToOther = " $subDir /junction-to-other"
2019-01-03 07:59:14 +00:00
New-Item -ItemType File $filePath -Value "stuff" > $null
New-Item -ItemType Directory $subDir > $null
New-Item -ItemType Directory $otherSubDir > $null
New-Item -ItemType File $otherFile -Value "some text" > $null
New-Item -ItemType SymbolicLink $symToOther -Value $otherSubDir > $null
New-Item -ItemType SymbolicLink $secondSymToOther -Value $otherSubDir > $null
New-Item -ItemType SymbolicLink $symToSym -Value $symToOther > $null
New-Item -ItemType SymbolicLink $symToOtherFile -Value $otherFile > $null
New-Item -ItemType HardLink $hardToOtherFile -Value $otherFile > $null
2017-04-21 16:09:53 -07:00
if ( $IsWindows )
{
2019-01-03 07:59:14 +00:00
New-Item -ItemType Junction $junctionToOther -Value $otherSubDir > $null
New-Item -ItemType SymbolicLink $symdToOther -Value $otherSubDir > $null
2017-04-21 16:09:53 -07:00
}
}
Context "Copy-Item using different case (on case-sensitive file systems)" {
BeforeEach {
$sourcePath = $filePath
$destinationPath = " $TestDrive /" + $fileName . Toupper ()
}
AfterEach {
Remove-Item -Path $destinationPath -ErrorAction SilentlyContinue
}
It "Copy-Item can copy to file name differing only by case" {
if ( $isCaseSensitive )
{
2018-03-09 15:13:15 -08:00
Copy-Item -Path $sourcePath -Destination $destinationPath -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
Test-Path -Path $destinationPath | Should -BeTrue
2017-04-21 16:09:53 -07:00
}
else
{
2018-05-17 14:42:04 -07:00
{ Copy-Item -Path $sourcePath -Destination $destinationPath -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"
2020-01-13 19:19:39 +00:00
$error [ 0 ]. Exception | Should -BeOfType System . IO . IOException
$error [ 0 ]. Exception . Data [ $selfCopyKey ] | Should -Not -BeNullOrEmpty
2017-04-21 16:09:53 -07:00
}
}
}
Context "Copy-Item avoids copying an item onto itself" {
BeforeAll {
$testCases = @ (
@ {
Name = "Copy to same path"
Source = $otherFile
Destination = $otherFile
}
@ {
Name = "Copy hard link"
Source = $hardToOtherFile
Destination = $otherFile
}
@ {
Name = "Copy hard link, reversed"
Source = $otherFile
Destination = $hardToOtherFile
}
@ {
Name = "Copy symbolic link to target"
Source = $symToOtherFile
Destination = $otherFile
}
@ {
Name = "Copy symbolic link to symbolic link with same target"
Source = $secondSymToOther
Destination = $symToOther
}
@ {
Name = "Copy through chain of symbolic links"
Source = $symToSym
Destination = $otherSubDir
}
)
# Junctions and directory symbolic links are Windows and NTFS only
$windowsTestCases = @ (
@ {
Name = "Copy junction to target"
Source = $junctionToOther
Destination = $otherSubDir
}
@ {
Name = "Copy directory symbolic link to target"
Source = $symdToOther
Destination = $otherSubDir
}
)
function TestSelfCopy
{
Param (
[ string ] $Source ,
[ string ] $Destination
)
2018-05-17 14:42:04 -07:00
{ Copy-Item -Path $Source -Destination $Destination -ErrorAction Stop } | Should -Throw -ErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"
2020-01-13 19:19:39 +00:00
$error [ 0 ]. Exception | Should -BeOfType System . IO . IOException
$error [ 0 ]. Exception . Data [ $selfCopyKey ] | Should -Not -BeNullOrEmpty
2017-04-21 16:09:53 -07:00
}
}
It "<Name>" -TestCases $testCases {
Param (
[ string ] $Name ,
[ string ] $Source ,
[ string ] $Destination
)
TestSelfCopy $Source $Destination
}
It "<Name>" -TestCases $windowsTestCases -Skip: ( -not $IsWindows ) {
Param (
[ string ] $Name ,
[ string ] $Source ,
[ string ] $Destination
)
TestSelfCopy $Source $Destination
}
}
}
2017-06-07 17:13:51 -07:00
Describe "Handling long paths" -Tags "CI" {
BeforeAll {
$longDir = 'a' * 250
$longSubDir = 'b' * 250
$fileName = "file1.txt"
$topPath = Join-Path $TestDrive $longDir
$longDirPath = Join-Path $topPath $longSubDir
$longFilePath = Join-Path $longDirPath $fileName
$cwd = Get-Location
}
BeforeEach {
New-Item -ItemType File -Path $longFilePath -Force | Out-Null
}
AfterEach {
Remove-Item -Path $topPath -Force -Recurse -ErrorAction SilentlyContinue
Set-Location $cwd
}
It "Can remove a file via a long path" {
Remove-Item -Path $longFilePath -ErrorVariable e -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$e | Should -BeNullOrEmpty
$longFilePath | Should -Not -Exist
2017-06-07 17:13:51 -07:00
}
It "Can rename a file via a long path" {
$newFileName = "new-file.txt"
$newPath = Join-Path $longDirPath $newFileName
Rename-Item -Path $longFilePath -NewName $newFileName
2018-03-09 15:13:15 -08:00
$longFilePath | Should -Not -Exist
$newPath | Should -Exist
2017-06-07 17:13:51 -07:00
}
It "Can change into a directory via a long path" {
Set-Location -Path $longDirPath -ErrorVariable e -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$e | Should -BeNullOrEmpty
2017-06-07 17:13:51 -07:00
$c = Get-Location
2018-03-09 15:13:15 -08:00
$fileName | Should -Exist
2017-06-07 17:13:51 -07:00
}
It "Can use Test-Path to check for a file via a long path" {
2018-03-09 15:13:15 -08:00
Test-Path $longFilePath | Should -BeTrue
2017-06-07 17:13:51 -07:00
}
}
2016-10-07 10:36:13 -07:00
Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature" {
BeforeAll {
$testDir = "testDir"
$testFile = "testFile.txt"
$testFile2 = "testFile2.txt"
$testContent = "Test 1"
$testContent2 = "Test 2"
$restoreLocation = Get-Location
}
AfterAll {
#restore the previous location
Set-Location -Path $restoreLocation
}
2017-01-16 13:31:14 -08:00
2016-09-13 18:29:48 -07:00
BeforeEach {
Set-Location -Path "TestDrive:\"
New-Item -Path $testFile -ItemType File > $null
2016-10-07 10:36:13 -07:00
New-Item -Path $testFile2 -ItemType File > $null
2016-09-13 18:29:48 -07:00
}
AfterEach {
Set-Location -Path "TestDrive:\"
Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue
}
2016-10-07 10:36:13 -07:00
Context "Valdiate New-Item parameters" {
BeforeEach {
#remove every file so that New-Item can be validated
Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue
}
It "Verify Directory + Whatif" {
New-Item -Path . -ItemType Directory -Name $testDir -WhatIf > $null
2018-05-17 14:42:04 -07:00
{ Get-Item -Path $testDir -ErrorAction Stop } |
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
It "Verify Directory + Confirm bypass" {
$result = New-Item -Path . -ItemType Directory -Name $testDir -Confirm: $false
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testDir
2016-10-07 10:36:13 -07:00
}
It "Verify Directory + Force" {
New-Item -Path . -ItemType Directory -Name $testDir > $null
$result = New-Item -Path . -ItemType Directory -Name $testDir -Force #would normally fail without force
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testDir
2016-10-07 10:36:13 -07:00
}
It "Verify File + Value" {
$result = New-Item -Path . -ItemType File -Name $testFile -Value "Some String"
$content = Get-Content -Path $testFile
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile
$content | Should -BeExactly "Some String"
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Get-Item parameters" {
It "Verify Force" {
$result = Get-Item -Path $testFile -Force
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile
2016-10-07 10:36:13 -07:00
}
It "Verify Path Wildcard" {
$result = Get-Item -Path "*2.txt"
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile2
2016-10-07 10:36:13 -07:00
}
It "Verify Include" {
$result = Get-Item -Path "TestDrive:\*" -Include "*2.txt"
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile2
2016-10-07 10:36:13 -07:00
}
It "Verify Include and Exclude Intersection" {
$result = Get-Item -Path "TestDrive:\*" -Include "*.txt" -Exclude "*2*"
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile
2016-10-07 10:36:13 -07:00
}
It "Verify Filter" {
2020-05-07 13:00:30 +01:00
$result = Get-Item -Path "TestDrive:\*" -Filter "*2.txt"
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile2
2016-10-07 10:36:13 -07:00
}
2017-10-27 05:14:46 +08:00
It "Verify -LiteralPath with wildcard fails for file that doesn't exist" {
2018-05-17 14:42:04 -07:00
{ Get-Item -LiteralPath "a*b.txt" -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2017-10-27 05:14:46 +08:00
}
It "Verify -LiteralPath with wildcard succeeds for file" -Skip: ( $IsWindows ) {
New-Item -Path " $testdrive \a*b.txt" -ItemType File > $null
$file = Get-Item -LiteralPath " $testdrive \a*b.txt"
2018-03-09 15:13:15 -08:00
$file . Name | Should -BeExactly "a*b.txt"
2017-10-27 05:14:46 +08:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Move-Item parameters" {
BeforeAll {
$altTestFile = "movedFile.txt"
}
BeforeEach {
2017-10-27 05:14:46 +08:00
New-Item -Path $testFile -ItemType File -Force > $null
New-Item -Path $testFile2 -ItemType File -Force > $null
2016-10-07 10:36:13 -07:00
}
AfterEach {
Remove-Item -Path * -Recurse -Force -ErrorAction SilentlyContinue
}
2017-10-27 05:14:46 +08:00
It "Verify WhatIf" {
2016-10-07 10:36:13 -07:00
Move-Item -Path $testFile -Destination $altTestFile -WhatIf
2018-05-17 14:42:04 -07:00
{ Get-Item -Path $altTestFile -ErrorAction Stop } |
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
2017-10-27 05:14:46 +08:00
It "Verify Include and Exclude Intersection" {
New-Item -Path "TestDrive:\dest" -ItemType Directory
Move-Item -Path "TestDrive:\*" -Destination "TestDrive:\dest" -Include "*.txt" -Exclude "*2*"
$file1 = Get-Item "TestDrive:\dest\ $testFile2 " -ErrorAction SilentlyContinue
$file2 = Get-Item "TestDrive:\dest\ $testFile " -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$file1 | Should -BeNullOrEmpty
$file2 . Name | Should -BeExactly $testFile
2016-10-07 10:36:13 -07:00
}
2017-10-27 05:14:46 +08:00
It "Verify Filter" {
2016-10-07 10:36:13 -07:00
Move-Item -Path "TestDrive:\*" -Filter "*2.txt" -Destination $altTestFile
$file1 = Get-Item $testFile2 -ErrorAction SilentlyContinue
$file2 = Get-Item $altTestFile -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$file1 | Should -BeNullOrEmpty
$file2 . Name | Should -BeExactly $altTestFile
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Rename-Item parameters" {
BeforeAll {
$newFile = "NewName.txt"
}
It "Verify WhatIf" {
Rename-Item -Path $testFile -NewName $newFile -WhatIf
2018-05-17 14:42:04 -07:00
{ Get-Item -Path $newFile -ErrorAction Stop } |
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
It "Verify Confirm can be bypassed" {
Rename-Item -Path $testFile -NewName $newFile -Confirm: $false
$file1 = Get-Item -Path $testFile -ErrorAction SilentlyContinue
$file2 = Get-Item -Path $newFile -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$file1 | Should -BeNullOrEmpty
$file2 . Name | Should -BeExactly $newFile
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Remove-Item parameters" {
It "Verify WhatIf" {
Remove-Item $testFile -WhatIf
$result = Get-Item $testFile
2018-03-09 15:13:15 -08:00
$result . Name | Should -BeExactly $testFile
2016-10-07 10:36:13 -07:00
}
It "Verify Confirm can be bypassed" {
Remove-Item $testFile -Confirm: $false
2018-05-17 14:42:04 -07:00
{ Get-Item $testFile -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
It "Verify LiteralPath" {
Remove-Item -LiteralPath "TestDrive:\ $testFile " -Recurse
2018-05-17 14:42:04 -07:00
{ Get-Item $testFile -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
It "Verify Filter" {
Remove-Item "TestDrive:\*" -Filter "*.txt"
2017-10-09 09:33:22 -07:00
$result = Get-Item "TestDrive:\*.txt"
2018-03-09 15:13:15 -08:00
$result | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
It "Verify Include" {
Remove-Item "TestDrive:\*" -Include "*2.txt"
2018-05-17 14:42:04 -07:00
{ Get-Item $testFile2 -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
2016-10-07 10:36:13 -07:00
}
It "Verify Include and Exclude Intersection" {
2020-05-07 13:00:30 +01:00
Remove-Item "TestDrive:\*" -Include "*.txt" -Exclude "*2*"
2016-10-07 10:36:13 -07:00
$file1 = Get-Item $testFile -ErrorAction SilentlyContinue
$file2 = Get-Item $testFile2 -ErrorAction SilentlyContinue
2018-03-09 15:13:15 -08:00
$file1 | Should -BeNullOrEmpty
$file2 . Name | Should -BeExactly $testFile2
2016-10-07 10:36:13 -07:00
}
2017-10-09 09:33:22 -07:00
It "Verify Path can accept wildcard" {
Remove-Item "TestDrive:\*.txt" -Recurse -Force
$result = Get-ChildItem "TestDrive:\*.txt"
2018-03-09 15:13:15 -08:00
$result | Should -BeNullOrEmpty
2017-10-09 09:33:22 -07:00
}
It "Verify no error if wildcard doesn't match: <path>" -TestCases @ (
@ { path = "TestDrive:\*.foo" },
@ { path = "TestDrive:\[z]" },
@ { path = "TestDrive:\z.*" }
) {
param ( $path )
2018-03-09 15:13:15 -08:00
{ Remove-Item $path } | Should -Not -Throw
2017-10-09 09:33:22 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Set-Content parameters" {
It "Validate Array Input for Path and Value" {
Set-Content -Path @ ( $testFile , $testFile2 ) -Value @ ( $testContent , $testContent2 )
$content1 = Get-Content $testFile
$content2 = Get-Content $testFile2
2018-03-09 15:13:15 -08:00
$content1 | Should -BeExactly $content2
( $content1 [ 0 ] + $content1 [ 1 ]) | Should -BeExactly ( $testContent + $testContent2 )
2016-10-07 10:36:13 -07:00
}
It "Validate LiteralPath" {
Set-Content -LiteralPath "TestDrive:\ $testFile " -Value $testContent
$content = Get-Content $testFile
2018-03-09 15:13:15 -08:00
$content | Should -BeExactly $testContent
2016-10-07 10:36:13 -07:00
}
It "Validate Confirm can be bypassed" {
Set-Content -Path $testFile -Value $testContent -Confirm: $false
$content = Get-Content $testFile
2018-03-09 15:13:15 -08:00
$content | Should -BeExactly $testContent
2016-10-07 10:36:13 -07:00
}
It "Validate WhatIf" {
Set-Content -Path $testFile -Value $testContent -WhatIf
$content = Get-Content $testFile
2018-03-09 15:13:15 -08:00
$content | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
It "Validate Include" {
Set-Content -Path "TestDrive:\*" -Value $testContent -Include "*2.txt"
$content1 = Get-Content $testFile
$content2 = Get-Content $testFile2
2018-03-09 15:13:15 -08:00
$content1 | Should -BeNullOrEmpty
$content2 | Should -BeExactly $testContent
2016-10-07 10:36:13 -07:00
}
It "Validate Exclude" {
Set-Content -Path "TestDrive:\*" -Value $testContent -Exclude "*2.txt"
$content1 = Get-Content $testFile
$content2 = Get-Content $testFile2
2018-03-09 15:13:15 -08:00
$content1 | Should -BeExactly $testContent
$content2 | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
It "Validate Filter" {
Set-Content -Path "TestDrive:\*" -Value $testContent -Filter "*2.txt"
$content1 = Get-Content $testFile
$content2 = Get-Content $testFile2
2018-03-09 15:13:15 -08:00
$content1 | Should -BeNullOrEmpty
$content2 | Should -BeExactly $testContent
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Get-Content parameters" {
BeforeEach {
Set-Content -Path $testFile -Value $testContent
Set-Content -Path $testFile2 -Value $testContent2
}
It "Validate Array Input for Path" {
$result = Get-Content -Path @ ( $testFile , $testFile2 )
2018-03-09 15:13:15 -08:00
$result [ 0 ] | Should -BeExactly $testContent
$result [ 1 ] | Should -BeExactly $testContent2
2016-10-07 10:36:13 -07:00
}
It "Validate Include" {
$result = Get-Content -Path "TestDrive:\*" -Include "*2.txt"
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $testContent2
2016-10-07 10:36:13 -07:00
}
It "Validate Exclude" {
$result = Get-Content -Path "TestDrive:\*" -Exclude "*2.txt"
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $testContent
2016-10-07 10:36:13 -07:00
}
It "Validate Filter" {
$result = Get-Content -Path "TestDrive:\*" -Filter "*2.txt"
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $testContent2
2016-10-07 10:36:13 -07:00
}
It "Validate ReadCount" {
Set-Content -Path $testFile -Value "Test Line 1 `n Test Line 2 `n Test Line 3 `n Test Line 4 `n Test Line 5 `n Test Line 6"
$result = ( Get-Content -Path $testFile -ReadCount 2 )
2018-03-09 15:13:15 -08:00
$result [ 0 ][ 0 ] | Should -BeExactly "Test Line 1"
$result [ 0 ][ 1 ] | Should -BeExactly "Test Line 2"
$result [ 1 ][ 0 ] | Should -BeExactly "Test Line 3"
$result [ 1 ][ 1 ] | Should -BeExactly "Test Line 4"
$result [ 2 ][ 0 ] | Should -BeExactly "Test Line 5"
$result [ 2 ][ 1 ] | Should -BeExactly "Test Line 6"
2016-10-07 10:36:13 -07:00
}
It "Validate TotalCount" {
Set-Content -Path $testFile -Value "Test Line 1 `n Test Line 2 `n Test Line 3 `n Test Line 4 `n Test Line 5 `n Test Line 6"
$result = Get-Content -Path $testFile -TotalCount 4
2018-03-09 15:13:15 -08:00
$result [ 0 ] | Should -BeExactly "Test Line 1"
$result [ 1 ] | Should -BeExactly "Test Line 2"
$result [ 2 ] | Should -BeExactly "Test Line 3"
$result [ 3 ] | Should -BeExactly "Test Line 4"
$result [ 4 ] | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
It "Validate Tail" {
Set-Content -Path $testFile -Value "Test Line 1 `n Test Line 2 `n Test Line 3 `n Test Line 4 `n Test Line 5 `n Test Line 6"
$result = Get-Content -Path $testFile -Tail 2
2018-03-09 15:13:15 -08:00
$result [ 0 ] | Should -BeExactly "Test Line 5"
$result [ 1 ] | Should -BeExactly "Test Line 6"
$result [ 2 ] | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
}
2016-10-07 10:36:13 -07:00
Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Feature" {
2016-09-13 18:29:48 -07:00
BeforeAll {
2016-10-07 10:36:13 -07:00
$testDir = "testDir"
$testFile = "testFile.txt"
$testFile2 = "testFile2.txt"
$testContent = "Test 1"
$testContent2 = "Test 2"
2016-09-13 18:29:48 -07:00
$restoreLocation = Get-Location
#build semi-complex directory structure to test navigation within
2016-10-07 10:36:13 -07:00
Set-Location -Path "TestDrive:\"
2016-09-13 18:29:48 -07:00
$level1_0 = "Level1_0"
$level2_0 = "Level2_0"
$level2_1 = "Level2_1"
2017-03-11 07:50:23 +01:00
$fileExt = ".ext"
2016-10-07 10:36:13 -07:00
$root = Join-Path "TestDrive:" "" #adds correct / or \
$level1_0Full = Join-Path $root $level1_0
$level2_0Full = Join-Path $level1_0Full $level2_0
$level2_1Full = Join-Path $level1_0Full $level2_1
New-Item -Path $level1_0Full -ItemType Directory > $null
New-Item -Path $level2_0Full -ItemType Directory > $null
New-Item -Path $level2_1Full -ItemType Directory > $null
2016-09-13 18:29:48 -07:00
}
AfterAll {
#restore the previous location
Set-Location -Path $restoreLocation
}
BeforeEach {
Set-Location -Path "TestDrive:\"
}
2016-10-07 10:36:13 -07:00
Context "Validate Resolve-Path Cmdlet Parameters" {
It "Verify LiteralPath" {
$result = Resolve-Path -LiteralPath "TestDrive:\"
2018-03-09 15:13:15 -08:00
( $result . Path . TrimEnd ( '/\' )) | Should -BeExactly "TestDrive:"
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
It "Verify relative" {
$relativePath = Resolve-Path -Path . -Relative
2018-03-09 15:13:15 -08:00
$relativePath | Should -BeExactly ( Join-Path "." "" )
2016-09-13 18:29:48 -07:00
}
}
2016-10-07 10:36:13 -07:00
Context "Validate Join-Path Cmdlet Parameters" {
It "Validate Resolve" {
$result = Join-Path -Path . -ChildPath $level1_0 -Resolve
if ( $IsWindows ) {
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:\ $level1_0 "
2016-10-07 10:36:13 -07:00
}
else {
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:/ $level1_0 "
2016-10-07 10:36:13 -07:00
}
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Validate Split-Path Cmdlet Parameters" {
It "Validate Parent" {
$result = Split-Path -Path $level1_0Full -Parent -Resolve
2018-03-09 15:13:15 -08:00
( $result . TrimEnd ( '/\' )) | Should -BeExactly "TestDrive:"
2016-10-07 10:36:13 -07:00
}
It "Validate IsAbsolute" {
$resolved = Split-Path -Path . -Resolve -IsAbsolute
$unresolved = Split-Path -Path . -IsAbsolute
2018-03-09 15:13:15 -08:00
$resolved | Should -BeTrue
$unresolved | Should -BeFalse
2016-10-07 10:36:13 -07:00
}
It "Validate Leaf" {
$result = Split-Path -Path $level1_0Full -Leaf
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $level1_0
2016-10-07 10:36:13 -07:00
}
2017-04-21 16:09:53 -07:00
2017-03-11 07:50:23 +01:00
It 'Validate LeafBase' {
$result = Split-Path -Path " $level2_1Full$fileExt " -LeafBase
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $level2_1
2017-03-11 07:50:23 +01:00
}
It 'Validate LeafBase is not over-zealous' {
2017-04-21 16:09:53 -07:00
2017-03-11 07:50:23 +01:00
$result = Split-Path -Path " $level2_1Full$fileExt$fileExt " -LeafBase
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly " $level2_1$fileExt "
2017-03-11 07:50:23 +01:00
}
It 'Validate LeafBase' {
$result = Split-Path -Path " $level2_1Full$fileExt " -Extension
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly $fileExt
2017-03-11 07:50:23 +01:00
}
2016-10-07 10:36:13 -07:00
It "Validate NoQualifier" {
$result = Split-Path -Path $level1_0Full -NoQualifier
2018-03-09 15:13:15 -08:00
( $result . TrimStart ( '/\' )) | Should -BeExactly $level1_0
2016-10-07 10:36:13 -07:00
}
It "Validate Qualifier" {
$result = Split-Path -Path $level1_0Full -Qualifier
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "TestDrive:"
2016-10-07 10:36:13 -07:00
}
2016-09-13 18:29:48 -07:00
}
2016-10-07 10:36:13 -07:00
Context "Valdiate Set-Location Cmdlets Parameters" {
It "Without Passthru Doesn't Return a Path" {
$result = Set-Location -Path $level1_0
2018-03-09 15:13:15 -08:00
$result | Should -BeNullOrEmpty
2016-10-07 10:36:13 -07:00
}
It "By LiteralPath" {
$result = Set-Location -LiteralPath $level1_0Full -PassThru
2018-03-09 15:13:15 -08:00
$result . Path | Should -BeExactly $level1_0Full
2016-10-07 10:36:13 -07:00
}
It "To Default Location Stack Does Nothing" {
$beforeLoc = Get-Location
Set-Location -StackName ""
$afterLoc = Get-Location
2018-03-09 15:13:15 -08:00
$beforeLoc . Path | Should -BeExactly $afterLoc . Path
2016-10-07 10:36:13 -07:00
}
It "WhatIf is Not Supported" {
2018-05-17 14:42:04 -07:00
{ Set-Location $level1_0 -WhatIf } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand"
2016-10-07 10:36:13 -07:00
}
}
Context "Valdiate Push-Location and Pop-Location Cmdlets Parameters" {
It "Verify Push + Path" {
Push-Location -Path $level1_0
Push-Location -Path $level2_0
$location1 = Get-Location
Pop-Location
$location2 = Get-Location
Pop-Location
$location3 = Get-Location
2018-03-09 15:13:15 -08:00
$location1 . Path | Should -BeExactly $level2_0Full
$location2 . Path | Should -BeExactly $level1_0Full
$location3 . Path | Should -BeExactly $root
2016-10-07 10:36:13 -07:00
}
It "Verify Push + PassThru" {
$location1 = Get-Location
$passThru1 = Push-Location -PassThru
Set-Location $level1_0
$location2 = Get-Location
$passThru2 = Push-Location -PassThru
Set-Location $level2_0
$location3 = Get-Location
2018-03-09 15:13:15 -08:00
$location1 . Path | Should -BeExactly $passThru1 . Path
$location2 . Path | Should -BeExactly $passThru2 . Path
$location3 . Path | Should -BeExactly $level2_0Full
2016-10-07 10:36:13 -07:00
}
It "Verify Push + LiteralPath" {
Push-Location -LiteralPath $level1_0Full
Push-Location -LiteralPath $level2_0Full
$location1 = Get-Location
Pop-Location
$location2 = Get-Location
Pop-Location
$location3 = Get-Location
2018-03-09 15:13:15 -08:00
$location1 . Path | Should -BeExactly $level2_0Full
$location2 . Path | Should -BeExactly $level1_0Full
$location3 . Path | Should -BeExactly $root
2016-10-07 10:36:13 -07:00
}
It "Verify Pop + Invalid Stack Name" {
2018-05-17 14:42:04 -07:00
{ Pop-Location -StackName UnknownStackName -ErrorAction Stop } |
Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.PopLocationCommand"
2016-10-07 10:36:13 -07:00
}
}
2017-03-07 10:29:20 -08:00
}
2017-10-09 14:05:16 -07:00
Describe "UNC paths" -Tags 'CI' {
It "Can Get-ChildItems from a UNC location using <cmdlet>" -Skip: (! $IsWindows ) -TestCases @ (
@ { cmdlet = "Push-Location" },
@ { cmdlet = "Set-Location" }
) {
param ( $cmdlet )
$originalLocation = Get-Location
try {
$systemDrive = ( $env:SystemDrive ). Replace ( ":" , "$" )
$testPath = Join-Path "\\localhost" $systemDrive
& $cmdlet $testPath
2018-03-09 15:13:15 -08:00
Get-Location | Should -BeExactly "Microsoft.PowerShell.Core\FileSystem:: $testPath "
2018-02-05 11:58:25 -08:00
$children = Get-ChildItem -ErrorAction Stop
2018-03-09 15:13:15 -08:00
$children . Count | Should -BeGreaterThan 0
2017-10-09 14:05:16 -07:00
}
finally {
Set-Location $originalLocation
}
}
}
2018-05-01 17:28:57 -07:00
Describe "Remove-Item UnAuthorized Access" -Tags "CI" , "RequireAdminOnWindows" {
BeforeAll {
if ( $IsWindows ) {
$folder = Join-Path $TestDrive "UnAuthFolder"
$protectedPath = ( New-Item $folder -ItemType Directory ). FullName
}
}
It "Access-denied test for removing a folder" -Skip: ( -not $IsWindows ) {
# The expected error is returned when there is a empty directory with the user does not have authorization to is deleted.
# It cannot have 'System. 'Hidden' or 'ReadOnly' attribute as well as -Force should not be used.
$powershell = Join-Path $PSHOME "pwsh"
$errorFile = Join-Path ( Get-Item $testdrive ). FullName "RemoveItemError.txt"
$cmdline = " $powershell -c Remove-Item -Path $protectedPath -ErrorVariable err ; `$ err.FullyQualifiedErrorId | Out-File $errorFile "
## Remove inheritance
$acl = Get-Acl $protectedPath
$acl . SetAccessRuleProtection ( $true , $true )
Set-Acl $protectedPath $acl
## Remove ACL
$acl = Get-Acl $protectedPath
$acl . Access | ForEach-Object { $acl . RemoveAccessRule ( $_ ) } | Out-Null
# Add local admin
$rule = [ System.Security.AccessControl.FileSystemAccessRule ]:: new ( "BUILTIN\Administrators" , "FullControl" , "Allow" )
$acl . SetAccessRule ( $rule )
Set-Acl $protectedPath $acl
runas . exe / trustlevel : 0x20000 " $cmdline "
2019-05-02 01:03:43 +01:00
Wait-FileToBePresent -File $errorFile -TimeoutInSeconds 10 | Should -BeTrue
2018-05-01 17:28:57 -07:00
Get-Content $errorFile | Should -BeExactly 'RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand'
}
}
2018-06-11 23:06:08 +00:00
Describe "Verify sub-directory creation under root" -Tag 'CI' , 'RequireSudoOnUnix' {
BeforeAll {
$dirPath = "\TestDir- $(( New-Guid ). Guid ) "
}
AfterAll {
if ( Test-Path $dirPath )
{
Remove-Item -Path $dirPath -Force -ErrorAction SilentlyContinue
}
}
2020-02-18 14:11:22 -08:00
It "Can create a sub directory under root path" -Skip: $IsMacOs {
2018-06-11 23:06:08 +00:00
New-Item -Path $dirPath -ItemType Directory -Force > $null
$dirPath | Should -Exist
}
}