Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Remove-Item.Tests.ps1196 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Remove-Item" -Tags "CI" {5    BeforeAll {6        $testpath = $TestDrive7        $testfile = "testfile.txt"8        $testfilepath = Join-Path -Path $testpath -ChildPath $testfile9    }10 11    Context "File removal Tests" {12        BeforeEach {13            New-Item -Name $testfile -Path $testpath -ItemType "file" -Value "lorem ipsum" -Force14            Test-Path $testfilepath | Should -BeTrue15        }16 17        It "Should be able to be called on a regular file without error using the Path parameter" {18            { Remove-Item -Path $testfilepath } | Should -Not -Throw19 20            Test-Path $testfilepath | Should -BeFalse21        }22 23        It "Should be able to be called on a file without the Path parameter" {24            { Remove-Item $testfilepath } | Should -Not -Throw25 26            Test-Path $testfilepath | Should -BeFalse27        }28 29        It "Should be able to call the rm alias" {30            { rm $testfilepath } | Should -Not -Throw31 32            Test-Path $testfilepath | Should -BeFalse33        }34 35        It "Should be able to call the del alias" {36            { del $testfilepath } | Should -Not -Throw37 38            Test-Path $testfilepath | Should -BeFalse39        }40 41        It "Should be able to call the erase alias" {42            { erase $testfilepath } | Should -Not -Throw43 44            Test-Path $testfilepath | Should -BeFalse45        }46 47        It "Should be able to call the ri alias" {48            { ri $testfilepath } | Should -Not -Throw49 50            Test-Path $testfilepath | Should -BeFalse51        }52 53        It "Should not be able to remove a read-only document without using the force switch" {54            # Set to read only55            Set-ItemProperty -Path $testfilepath -Name IsReadOnly -Value $true56 57            # attempt to remove the file58            { Remove-Item $testfilepath -ErrorAction SilentlyContinue } | Should -Not -Throw59 60            # validate61            Test-Path $testfilepath | Should -BeTrue62 63            # remove using the -force switch on the readonly object64            Remove-Item  $testfilepath -Force65 66            # Validate67            Test-Path $testfilepath | Should -BeFalse68        }69 70        It "Should be able to remove all files matching a regular expression with the include parameter" {71            # Create multiple files with specific string72            New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"73            New-Item -Name file2.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"74            New-Item -Name file3.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"75            # Create a single file that does not match that string - already done in BeforeEach76 77            # Delete the specific string78            Remove-Item (Join-Path -Path $testpath -ChildPath "*") -Include file*.txt79            # validate that the string under test was deleted, and the nonmatching strings still exist80            Test-Path (Join-Path -Path $testpath -ChildPath file1.txt) | Should -BeFalse81            Test-Path (Join-Path -Path $testpath -ChildPath file2.txt) | Should -BeFalse82            Test-Path (Join-Path -Path $testpath -ChildPath file3.txt) | Should -BeFalse83            Test-Path $testfilepath  | Should -BeTrue84 85            # Delete the non-matching strings86            Remove-Item $testfilepath87 88            Test-Path $testfilepath  | Should -BeFalse89        }90 91        It "Should be able to not remove any files matching a regular expression with the exclude parameter" {92            # Create multiple files with specific string93            New-Item -Name file1.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"94            New-Item -Name file2.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"95 96            # Create a single file that does not match that string97            New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"98 99            # Delete the specific string100            Remove-Item (Join-Path -Path $testpath -ChildPath "file*") -Exclude *.wav -Include *.txt101 102            # validate that the string under test was deleted, and the nonmatching strings still exist103            Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should -BeTrue104            Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should -BeTrue105            Test-Path (Join-Path -Path $testpath -ChildPath file1.txt) | Should -BeFalse106 107            # Delete the non-matching strings108            Remove-Item (Join-Path -Path $testpath -ChildPath file1.wav)109            Remove-Item (Join-Path -Path $testpath -ChildPath file2.wav)110 111            Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should -BeFalse112            Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should -BeFalse113        }114    }115 116    Context "Directory Removal Tests" {117        BeforeAll {118            $testdirectory = Join-Path -Path $testpath -ChildPath testdir119            $testsubdirectory = Join-Path -Path $testdirectory -ChildPath subd120        }121 122        BeforeEach {123            New-Item -Name "testdir" -Path $testpath -ItemType "directory" -Force124 125            Test-Path $testdirectory | Should -BeTrue126        }127 128        It "Should be able to remove a directory" {129            { Remove-Item $testdirectory -ErrorAction Stop } | Should -Not -Throw130 131            Test-Path $testdirectory | Should -BeFalse132        }133 134        It "Should be able to recursively delete subfolders" {135            New-Item -Name "subd" -Path $testdirectory -ItemType "directory"136            New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum"137 138            $complexDirectory = Join-Path -Path $testsubdirectory -ChildPath $testfile139            Test-Path $complexDirectory | Should -BeTrue140 141            { Remove-Item $testdirectory -Recurse -ErrorAction Stop } | Should -Not -Throw142 143            Test-Path $testdirectory | Should -BeFalse144        }145 146        It "Should be able to recursively delete a directory with a trailing backslash" {147            New-Item -Name "subd" -Path $testdirectory -ItemType "directory"148            New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum"149 150            $complexDirectory = Join-Path -Path $testsubdirectory -ChildPath $testfile151            Test-Path $complexDirectory | Should -BeTrue152 153            $testdirectoryWithBackSlash = Join-Path -Path $testdirectory -ChildPath ([IO.Path]::DirectorySeparatorChar)154            Test-Path $testdirectoryWithBackSlash | Should -BeTrue155 156            { Remove-Item $testdirectoryWithBackSlash -Recurse -ErrorAction Stop } | Should -Not -Throw157 158            Test-Path $testdirectoryWithBackSlash | Should -BeFalse159            Test-Path $testdirectory | Should -BeFalse160        }161    }162 163    Context "Alternate Data Streams should be supported on Windows" {164        BeforeAll {165            if (!$IsWindows) {166                return167            }168            $fileName = "ADStest.txt"169            $streamName = "teststream"170            $dirName = "ADStestdir"171            $fileContent =" This is file content."172            $streamContent = "datastream content here"173            $streamfile = Join-Path -Path $testpath -ChildPath $fileName174            $streamdir = Join-Path -Path $testpath -ChildPath $dirName175 176            $null = New-Item -Path $streamfile -ItemType "File" -force177            Add-Content -Path $streamfile -Value $fileContent178            Add-Content -Path $streamfile -Stream $streamName -Value $streamContent179            $null = New-Item -Path $streamdir -ItemType "Directory" -Force180            Add-Content -Path $streamdir -Stream $streamName -Value $streamContent181        }182 183        It "Should completely remove a datastream from a file" -Skip:(!$IsWindows) {184            Get-Item -Path $streamfile -Stream $streamName | Should -Not -BeNullOrEmpty185            Remove-Item -Path $streamfile -Stream $streamName186            Get-Item -Path $streamfile -Stream $streamName -ErrorAction SilentlyContinue | Should -BeNullOrEmpty187        }188 189        It "Should completely remove a datastream from a directory" -Skip:(!$IsWindows) {190            Get-Item -Path $streamdir -Stream $streamName | Should -Not -BeNullOrEmpty191            Remove-Item -Path $streamdir -Stream $streamName192            Get-Item -Path $streamdir -Stream $streamname -ErrorAction SilentlyContinue | Should -BeNullOrEmpty193        }194    }195}196