MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "String cmdlets" -Tags "CI" {4 Context "Select-String" {5 BeforeAll {6 $sep = [io.path]::DirectorySeparatorChar7 $fileName = New-Item 'TestDrive:\selectStr[ingLi]teralPath.txt'8 "abc" | Out-File -LiteralPath $fileName.fullname9 "bcd" | Out-File -LiteralPath $fileName.fullname -Append10 "cde" | Out-File -LiteralPath $fileName.fullname -Append11 12 $fileNameWithDots = $fileName.FullName.Replace("\", "\.\")13 14 $subDirName = Join-Path $TestDrive 'selectSubDir'15 New-Item -Path $subDirName -ItemType Directory -Force > $null16 $pathWithoutWildcard = $TestDrive17 $pathWithWildcard = Join-Path $TestDrive '*'18 19 # Here Get-ChildItem adds 'PSDrive' property20 $tempFile = New-TemporaryFile | Get-Item21 "abc" | Out-File -LiteralPath $tempFile.fullname22 "bcd" | Out-File -LiteralPath $tempFile.fullname -Append23 "cde" | Out-File -LiteralPath $tempFile.fullname -Append24 $driveLetter = $tempFile.PSDrive.Name25 $fileNameAsNetworkPath = "\\localhost\$driveLetter`$" + $tempFile.FullName.SubString(2)26 27 Push-Location "$fileName\.."28 }29 30 AfterAll {31 Remove-Item $tempFile -Force -ErrorAction SilentlyContinue32 Pop-Location33 }34 35 It "Select-String does not throw on subdirectory (path without wildcard)" {36 { Select-String -Path $pathWithoutWildcard "noExists" -ErrorAction Stop } | Should -Not -Throw37 }38 39 It "Select-String does not throw on subdirectory (path with wildcard)" {40 { Select-String -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should -Not -Throw41 }42 43 It "LiteralPath with relative path" {44 (Select-String -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should -Be 245 }46 47 It "LiteralPath with absolute path" {48 (Select-String -LiteralPath $fileName "b").count | Should -Be 249 }50 51 It "LiteralPath with dots in path" {52 (Select-String -LiteralPath $fileNameWithDots "b").count | Should -Be 253 }54 55 It "Network path" -Skip:(!$IsWindows) {56 (Select-String -LiteralPath $fileNameAsNetworkPath "b").count | Should -Be 257 }58 59 It "throws error for non filesystem providers" {60 $aaa = "aaaaaaaaaa"61 Select-String -LiteralPath variable:\aaa "a" -ErrorAction SilentlyContinue -ErrorVariable selectStringError62 $selectStringError.FullyQualifiedErrorId | Should -Be 'ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand'63 }64 65 It "throws parameter binding exception for invalid context" {66 { Select-String It $PSScriptRoot -Context -1,-1 } | Should -Throw Context67 }68 69 It "match object supports RelativePath method" {70 $file = "Modules${sep}Microsoft.PowerShell.Utility${sep}Microsoft.PowerShell.Utility.psd1"71 72 $match = Select-String CmdletsToExport $PSHOME/$file73 74 $match.RelativePath($PSHOME) | Should -Be $file75 $match.RelativePath($PSHOME.ToLower()) | Should -Be $file76 $match.RelativePath($PSHOME.ToUpper()) | Should -Be $file77 }78 }79}80 