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.
|
|
|
|
|
Describe "String cmdlets" -Tags "CI" {
|
2016-07-25 11:06:55 -07:00
|
|
|
Context "Select-String" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$sep = [io.path]::DirectorySeparatorChar
|
|
|
|
|
$fileName = New-Item 'TestDrive:\selectStr[ingLi]teralPath.txt'
|
2017-01-16 13:31:14 -08:00
|
|
|
"abc" | Out-File -LiteralPath $fileName.fullname
|
2016-07-25 11:06:55 -07:00
|
|
|
"bcd" | Out-File -LiteralPath $fileName.fullname -Append
|
|
|
|
|
"cde" | Out-File -LiteralPath $fileName.fullname -Append
|
|
|
|
|
|
|
|
|
|
$fileNameWithDots = $fileName.FullName.Replace("\", "\.\")
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-09-15 01:57:17 +04:00
|
|
|
$subDirName = Join-Path $TestDrive 'selectSubDir'
|
|
|
|
|
New-Item -Path $subDirName -ItemType Directory -Force > $null
|
|
|
|
|
$pathWithoutWildcard = $TestDrive
|
|
|
|
|
$pathWithWildcard = Join-Path $TestDrive '*'
|
|
|
|
|
|
2017-09-27 07:20:02 +04:00
|
|
|
# Here Get-ChildItem adds 'PSDrive' property
|
|
|
|
|
$tempFile = New-TemporaryFile | Get-Item
|
2017-01-16 13:31:14 -08:00
|
|
|
"abc" | Out-File -LiteralPath $tempFile.fullname
|
2016-07-25 11:06:55 -07:00
|
|
|
"bcd" | Out-File -LiteralPath $tempFile.fullname -Append
|
|
|
|
|
"cde" | Out-File -LiteralPath $tempFile.fullname -Append
|
|
|
|
|
$driveLetter = $tempFile.PSDrive.Name
|
|
|
|
|
$fileNameAsNetworkPath = "\\localhost\$driveLetter`$" + $tempFile.FullName.SubString(2)
|
|
|
|
|
|
|
|
|
|
Push-Location "$fileName\.."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
|
|
|
|
|
Pop-Location
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 01:57:17 +04:00
|
|
|
It "Select-String does not throw on subdirectory (path without wildcard)" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Select-String -Path $pathWithoutWildcard "noExists" -ErrorAction Stop } | Should -Not -Throw
|
2017-09-15 01:57:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Select-String does not throw on subdirectory (path with wildcard)" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Select-String -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should -Not -Throw
|
2017-09-15 01:57:17 +04:00
|
|
|
}
|
|
|
|
|
|
2016-07-25 11:06:55 -07:00
|
|
|
It "LiteralPath with relative path" {
|
2020-05-07 13:00:30 +01:00
|
|
|
(Select-String -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should -Be 2
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-16 13:31:14 -08:00
|
|
|
It "LiteralPath with absolute path" {
|
2020-05-07 13:00:30 +01:00
|
|
|
(Select-String -LiteralPath $fileName "b").count | Should -Be 2
|
2017-01-16 13:31:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "LiteralPath with dots in path" {
|
2020-05-07 13:00:30 +01:00
|
|
|
(Select-String -LiteralPath $fileNameWithDots "b").count | Should -Be 2
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
It "Network path" -Skip:(!$IsWindows) {
|
|
|
|
|
(Select-String -LiteralPath $fileNameAsNetworkPath "b").count | Should -Be 2
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "throws error for non filesystem providers" {
|
|
|
|
|
$aaa = "aaaaaaaaaa"
|
2020-05-07 13:00:30 +01:00
|
|
|
Select-String -LiteralPath variable:\aaa "a" -ErrorAction SilentlyContinue -ErrorVariable selectStringError
|
2018-03-23 10:26:23 -07:00
|
|
|
$selectStringError.FullyQualifiedErrorId | Should -Be 'ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand'
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "throws parameter binding exception for invalid context" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ Select-String It $PSScriptRoot -Context -1,-1 } | Should -Throw Context
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "match object supports RelativePath method" {
|
|
|
|
|
$file = "Modules${sep}Microsoft.PowerShell.Utility${sep}Microsoft.PowerShell.Utility.psd1"
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2020-01-13 19:35:33 +00:00
|
|
|
$match = Select-String CmdletsToExport $PSHOME/$file
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2020-01-13 19:35:33 +00:00
|
|
|
$match.RelativePath($PSHOME) | Should -Be $file
|
|
|
|
|
$match.RelativePath($PSHOME.ToLower()) | Should -Be $file
|
|
|
|
|
$match.RelativePath($PSHOME.ToUpper()) | Should -Be $file
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|