Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Select-String.Tests.ps1301 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Select-String" -Tags "CI" {5    BeforeAll {6        $nl = [Environment]::NewLine7        $currentDirectory = $PWD.Path8        $originalRendering = $PSStyle.OutputRendering9        $PSStyle.OutputRendering = 'Ansi'10    }11 12    AfterAll {13        $PSStyle.OutputRendering = $originalRendering14        Push-Location $currentDirectory15    }16 17    Context "String actions" {18        BeforeAll {19            $testinputone = "hello","Hello","goodbye"20            $testinputtwo = "hello","Hello"21        }22 23        It "Should be called without errors" {24            { $testinputone | Select-String -Pattern "hello" } | Should -Not -Throw25        }26 27        It "Should return an array data type when multiple matches are found" {28            $result = $testinputtwo | Select-String -Pattern "hello"29            ,$result | Should -BeOfType System.Array30        }31 32        It "Should return an object type when one match is found" {33            $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive34            ,$result | Should -BeOfType System.Object35        }36 37        It "Should return matchinfo type" {38            $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive39            ,$result | Should -BeOfType Microsoft.PowerShell.Commands.MatchInfo40        }41 42        It "Should be called without an error using ca for casesensitive " {43            {$testinputone | Select-String -Pattern "hello" -ca } | Should -Not -Throw44        }45 46        It "Should use the ca alias for casesensitive" {47            $firstMatch = $testinputtwo  | Select-String -Pattern "hello" -CaseSensitive48            $secondMatch = $testinputtwo | Select-String -Pattern "hello" -ca49 50            $equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 051            $equal | Should -BeTrue52        }53 54        It "Should only return the case sensitive match when the casesensitive switch is used" {55            $testinputtwo | Select-String -Pattern "hello" -CaseSensitive | Should -Be "hello"56        }57 58        It "Should accept a collection of strings from the input object" {59            { Select-String -InputObject "some stuff", "other stuff" -Pattern "other" } | Should -Not -Throw60        }61 62        It "Should return system.object when the input object switch is used on a collection" {63            $result = Select-String -InputObject "some stuff", "other stuff" -Pattern "other"64            ,$result | Should -BeOfType System.Object65        }66 67        It "Should return null or empty when the input object switch is used on a collection and the pattern does not exist" {68            Select-String -InputObject "some stuff", "other stuff" -Pattern "neither" | Should -BeNullOrEmpty69        }70 71        It "Should return a bool type when the quiet switch is used" {72            ,($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeOfType System.Boolean73        }74 75        It "Should be true when select string returns a positive result when the quiet switch is used" {76            ($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeTrue77        }78 79        It "Should be empty when select string does not return a result when the quiet switch is used" {80            $testinputtwo | Select-String -Quiet "goodbye"  | Should -BeNullOrEmpty81        }82 83        It "Should return an array of non matching strings when the switch of NotMatch is used and the string do not match" {84            $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello"85        }86 87        It "Should output a string with the first match highlighted" {88            if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))89            {90                $result = $testinputone | Select-String -Pattern "l" | Out-String91                $result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"92            }93            else94            {95                $result = $testinputone | Select-String -Pattern "l" | Out-String96                $result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"97            }98        }99 100        It "Should output a string with all matches highlighted when AllMatch is used" {101            if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))102            {103                $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String104                $result | Should -Be "${nl}he`e[7ml`e[0m`e[7ml`e[0mo${nl}He`e[7ml`e[0m`e[7ml`e[0mo${nl}${nl}"105            }106            else107            {108                $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String109                $result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"110            }111        }112 113        It "Should output a string with the first match highlighted when SimpleMatch is used" {114            if ($Host.UI.SupportsVirtualTerminal -and !(Test-Path env:__SuppressAnsiEscapeSequences))115            {116                $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String117                $result | Should -Be "${nl}he`e[7ml`e[0mlo${nl}He`e[7ml`e[0mlo${nl}${nl}"118            }119            else120            {121                $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String122                $result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"123            }124        }125 126        It "Should output a string without highlighting when NoEmphasis is used" {127            $result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String128            $result | Should -Be "${nl}hello${nl}Hello${nl}${nl}"129        }130 131        It "Should return an array of matching strings without virtual terminal sequences" {132            $testinputone | Select-String -Pattern "l" | Should -Be "hello", "hello"133        }134 135        It "Should return a string type when -Raw is used" {136            $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive -Raw137            $result | Should -BeOfType System.String138        }139 140        It "Should return ParameterBindingException when -Raw and -Quiet are used together" {141            { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException])142        }143    }144 145    Context "Filesystem actions" {146        $testDirectory = $TestDrive147        $testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt148 149        BeforeEach {150            New-Item $testInputFile -ItemType "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches"151        }152 153        AfterEach {154            Remove-Item $testInputFile -Force155        }156 157        It "Should return an object when a match is found is the file on only one line" {158            $result = Select-String $testInputFile -Pattern "string"159            ,$result | Should -BeOfType System.Object160        }161 162        It "Should return an array when a match is found is the file on several lines" {163            $result = Select-String $testInputFile -Pattern "in"164            ,$result | Should -BeOfType System.Array165            $result[0] | Should -BeOfType Microsoft.PowerShell.Commands.MatchInfo166        }167 168        It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" {169            $expected = $testInputFile + ":1:This is a text string, and another string"170 171            Select-String $testInputFile -Pattern "string" | Should -BeExactly $expected172        }173 174        It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" {175            $expected = $testInputFile + ":2:This is the second line"176 177            Select-String $testInputFile  -Pattern "second" | Should -BeExactly $expected178        }179 180        It "Should return all strings where 'in' is found in testfile1 pattern switch is not required" {181            $expected1 = "This is a text string, and another string"182            $expected2 = "This is the second line"183            $expected3 = "This is the third line"184            $expected4 = "This is the fourth line"185 186            (Select-String in $testInputFile)[0].Line | Should -BeExactly $expected1187            (Select-String in $testInputFile)[1].Line | Should -BeExactly $expected2188            (Select-String in $testInputFile)[2].Line | Should -BeExactly $expected3189            (Select-String in $testInputFile)[3].Line | Should -BeExactly $expected4190            (Select-String in $testInputFile)[4].Line | Should -BeNullOrEmpty191        }192 193        It "Should return empty because 'for' is not found in testfile1 " {194            Select-String for $testInputFile | Should -BeNullOrEmpty195        }196 197        It "Should return the third line in testfile1 and the lines above and below it " {198            $expectedLine       = "testfile1.txt:2:This is the second line"199            $expectedLineBefore = "testfile1.txt:3:This is the third line"200            $expectedLineAfter  = "testfile1.txt:4:This is the fourth line"201 202            Select-String third $testInputFile -Context 1 | Should -Match $expectedLine203            Select-String third $testInputFile -Context 1 | Should -Match $expectedLineBefore204            Select-String third $testInputFile -Context 1 | Should -Match $expectedLineAfter205        }206 207        It "Should return the number of matches for 'is' in textfile1 " {208            (Select-String is $testInputFile -CaseSensitive).count | Should -Be 4209        }210 211        It "Should return the third line in testfile1 when a relative path is used" {212            $expected  = "testfile1.txt:3:This is the third line"213 214            $relativePath = Join-Path -Path $testDirectory -ChildPath ".."215            $relativePath = Join-Path -Path $relativePath -ChildPath $TestDirectory.Name216            $relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt217            Select-String third $relativePath  | Should -Match $expected218        }219 220        It "Should return the fourth line in testfile1 when a relative path is used" {221            $expected = "testfile1.txt:5:No matches"222 223            Push-Location $testDirectory224 225            Select-String matches (Join-Path -Path $testDirectory -ChildPath testfile1.txt)  | Should -Match $expected226            Pop-Location227        }228 229        It "Should return the fourth line in testfile1 when a regular expression is used" {230            $expected  = "testfile1.txt:5:No matches"231 232            Select-String 'matc*' $testInputFile -CaseSensitive | Should -Match $expected233        }234 235        It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" {236            $expected  = "testfile1.txt:5:No matches"237 238            Select-String 'matc*' $testInputFile -ca | Should -Match $expected239        }240 241        It "Should return all strings where 'in' is found in testfile1, when -Raw is used." {242            $expected1 = "This is a text string, and another string"243            $expected2 = "This is the second line"244            $expected3 = "This is the third line"245            $expected4 = "This is the fourth line"246 247            (Select-String in $testInputFile -Raw)[0] | Should -BeExactly $expected1248            (Select-String in $testInputFile -Raw)[1] | Should -BeExactly $expected2249            (Select-String in $testInputFile -Raw)[2] | Should -BeExactly $expected3250            (Select-String in $testInputFile -Raw)[3] | Should -BeExactly $expected4251            (Select-String in $testInputFile -Raw)[4] | Should -BeNullOrEmpty252        }253 254        It "Should ignore -Context parameter when -Raw is used." {255            $expected = "This is the second line"256            Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected257        }258    }259 260    Context "Culture parameter" {261        It "Should throw if -Culture parameter is used without -SimpleMatch parameter" {262            { "1" | Select-String -Pattern "hello" -Culture "ru-RU" } | Should -Throw -ErrorId "CannotSpecifyCultureWithoutSimpleMatch,Microsoft.PowerShell.Commands.SelectStringCommand"263        }264 265        It "Should accept a culture: '<culture>'" -TestCases: @(266            @{ culture = "Ordinal"},267            @{ culture = "Invariant"},268            @{ culture = "Current"},269            @{ culture = "ru-RU"}270        ) {271            param ($culture)272            { "1" | Select-String -Pattern "hello" -Culture $culture -SimpleMatch } | Should -Not -Throw273        }274 275        It "Should works if -Culture parameter is a culture name: '<culture>'-'<pattern>'-'CaseSensitive:<casesensitive>'" -TestCases: @(276            @{pattern = 'file'; culture = 'tr-TR';       expected = 'file'; casesensitive = $false }277            @{pattern = 'fIle'; culture = 'tr-TR';       expected = $null;  casesensitive = $false }278            @{pattern = 'fIle'; culture = 'tr-TR';       expected = $null;  casesensitive = $true }279            @{pattern = "f`u{0130}le"; culture = 'tr-TR';expected = 'file'; casesensitive = $false }280            @{pattern = 'file'; culture = 'en-US';       expected = 'file'; casesensitive = $false }281            @{pattern = 'fIle'; culture = 'en-US';       expected = 'file'; casesensitive = $false }282            @{pattern = 'fIle'; culture = 'en-US';       expected = $null;  casesensitive = $true }283            @{pattern = 'file'; culture = 'Ordinal';     expected = 'file'; casesensitive = $false }284            @{pattern = 'fIle'; culture = 'Ordinal';     expected = 'file'; casesensitive = $false }285            @{pattern = 'fIle'; culture = 'Ordinal';     expected = $null;  casesensitive = $true }286            @{pattern = 'file'; culture = 'Invariant';   expected = 'file'; casesensitive = $false }287            @{pattern = 'fIle'; culture = 'Invariant';   expected = 'file'; casesensitive = $false }288            @{pattern = 'fIle'; culture = 'Invariant';   expected = $null;  casesensitive = $true }289            @{pattern = 'file'; culture = 'Current';     expected = 'file'; casesensitive = $false }290            @{pattern = 'fIle'; culture = 'Current';     expected = 'file'; casesensitive = $false }291            @{pattern = 'fIle'; culture = 'Current';     expected = $null;  casesensitive = $true }292        ) {293            param ($pattern, $culture, $expected, $casesensitive)294 295            if ($culture -ne 'Current' -or [CultureInfo]::CurrentCulture.Name -ne "tr-TR") {296                'file' | Select-String -Pattern $pattern -Culture $culture -SimpleMatch -CaseSensitive:$casesensitive | Should -BeExactly $expected297            }298       }299    }300}301