File size: 8,510 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Remove-Item" -Tags "CI" {
BeforeAll {
$testpath = $TestDrive
$testfile = "testfile.txt"
$testfilepath = Join-Path -Path $testpath -ChildPath $testfile
}
Context "File removal Tests" {
BeforeEach {
New-Item -Name $testfile -Path $testpath -ItemType "file" -Value "lorem ipsum" -Force
Test-Path $testfilepath | Should -BeTrue
}
It "Should be able to be called on a regular file without error using the Path parameter" {
{ Remove-Item -Path $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to be called on a file without the Path parameter" {
{ Remove-Item $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to call the rm alias" {
{ rm $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to call the del alias" {
{ del $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to call the erase alias" {
{ erase $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to call the ri alias" {
{ ri $testfilepath } | Should -Not -Throw
Test-Path $testfilepath | Should -BeFalse
}
It "Should not be able to remove a read-only document without using the force switch" {
# Set to read only
Set-ItemProperty -Path $testfilepath -Name IsReadOnly -Value $true
# attempt to remove the file
{ Remove-Item $testfilepath -ErrorAction SilentlyContinue } | Should -Not -Throw
# validate
Test-Path $testfilepath | Should -BeTrue
# remove using the -force switch on the readonly object
Remove-Item $testfilepath -Force
# Validate
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to remove all files matching a regular expression with the include parameter" {
# Create multiple files with specific string
New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
New-Item -Name file2.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
New-Item -Name file3.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
# Create a single file that does not match that string - already done in BeforeEach
# Delete the specific string
Remove-Item (Join-Path -Path $testpath -ChildPath "*") -Include file*.txt
# validate that the string under test was deleted, and the nonmatching strings still exist
Test-Path (Join-Path -Path $testpath -ChildPath file1.txt) | Should -BeFalse
Test-Path (Join-Path -Path $testpath -ChildPath file2.txt) | Should -BeFalse
Test-Path (Join-Path -Path $testpath -ChildPath file3.txt) | Should -BeFalse
Test-Path $testfilepath | Should -BeTrue
# Delete the non-matching strings
Remove-Item $testfilepath
Test-Path $testfilepath | Should -BeFalse
}
It "Should be able to not remove any files matching a regular expression with the exclude parameter" {
# Create multiple files with specific string
New-Item -Name file1.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"
New-Item -Name file2.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"
# Create a single file that does not match that string
New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
# Delete the specific string
Remove-Item (Join-Path -Path $testpath -ChildPath "file*") -Exclude *.wav -Include *.txt
# validate that the string under test was deleted, and the nonmatching strings still exist
Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should -BeTrue
Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should -BeTrue
Test-Path (Join-Path -Path $testpath -ChildPath file1.txt) | Should -BeFalse
# Delete the non-matching strings
Remove-Item (Join-Path -Path $testpath -ChildPath file1.wav)
Remove-Item (Join-Path -Path $testpath -ChildPath file2.wav)
Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should -BeFalse
Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should -BeFalse
}
}
Context "Directory Removal Tests" {
BeforeAll {
$testdirectory = Join-Path -Path $testpath -ChildPath testdir
$testsubdirectory = Join-Path -Path $testdirectory -ChildPath subd
}
BeforeEach {
New-Item -Name "testdir" -Path $testpath -ItemType "directory" -Force
Test-Path $testdirectory | Should -BeTrue
}
It "Should be able to remove a directory" {
{ Remove-Item $testdirectory -ErrorAction Stop } | Should -Not -Throw
Test-Path $testdirectory | Should -BeFalse
}
It "Should be able to recursively delete subfolders" {
New-Item -Name "subd" -Path $testdirectory -ItemType "directory"
New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum"
$complexDirectory = Join-Path -Path $testsubdirectory -ChildPath $testfile
Test-Path $complexDirectory | Should -BeTrue
{ Remove-Item $testdirectory -Recurse -ErrorAction Stop } | Should -Not -Throw
Test-Path $testdirectory | Should -BeFalse
}
It "Should be able to recursively delete a directory with a trailing backslash" {
New-Item -Name "subd" -Path $testdirectory -ItemType "directory"
New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum"
$complexDirectory = Join-Path -Path $testsubdirectory -ChildPath $testfile
Test-Path $complexDirectory | Should -BeTrue
$testdirectoryWithBackSlash = Join-Path -Path $testdirectory -ChildPath ([IO.Path]::DirectorySeparatorChar)
Test-Path $testdirectoryWithBackSlash | Should -BeTrue
{ Remove-Item $testdirectoryWithBackSlash -Recurse -ErrorAction Stop } | Should -Not -Throw
Test-Path $testdirectoryWithBackSlash | Should -BeFalse
Test-Path $testdirectory | Should -BeFalse
}
}
Context "Alternate Data Streams should be supported on Windows" {
BeforeAll {
if (!$IsWindows) {
return
}
$fileName = "ADStest.txt"
$streamName = "teststream"
$dirName = "ADStestdir"
$fileContent =" This is file content."
$streamContent = "datastream content here"
$streamfile = Join-Path -Path $testpath -ChildPath $fileName
$streamdir = Join-Path -Path $testpath -ChildPath $dirName
$null = New-Item -Path $streamfile -ItemType "File" -force
Add-Content -Path $streamfile -Value $fileContent
Add-Content -Path $streamfile -Stream $streamName -Value $streamContent
$null = New-Item -Path $streamdir -ItemType "Directory" -Force
Add-Content -Path $streamdir -Stream $streamName -Value $streamContent
}
It "Should completely remove a datastream from a file" -Skip:(!$IsWindows) {
Get-Item -Path $streamfile -Stream $streamName | Should -Not -BeNullOrEmpty
Remove-Item -Path $streamfile -Stream $streamName
Get-Item -Path $streamfile -Stream $streamName -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It "Should completely remove a datastream from a directory" -Skip:(!$IsWindows) {
Get-Item -Path $streamdir -Stream $streamName | Should -Not -BeNullOrEmpty
Remove-Item -Path $streamdir -Stream $streamName
Get-Item -Path $streamdir -Stream $streamname -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
}
}
|