mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Merge branch 'develop' into dev/262-clear-variable
This commit is contained in:
+1
-1
Submodule src/monad updated: 3a92ed01b5...a4db8beb6e
+1
-1
Submodule src/monad-ext updated: ea31eca622...8b20276170
+1
-1
Submodule src/monad-native updated: 5693da6a46...d1de7e28c5
@@ -1,26 +1,104 @@
|
||||
Describe "Test-Get-Content" {
|
||||
$testString = "This is a test content for a file"
|
||||
$nl = "`n"
|
||||
|
||||
$firstline = "Here's a first line "
|
||||
$secondline = " here's a second line"
|
||||
$thirdline = "more text"
|
||||
$fourthline = "just to make sure"
|
||||
$fifthline = "there's plenty to work with"
|
||||
|
||||
$testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline
|
||||
|
||||
$testPath = "/tmp/testfile1"
|
||||
$testPath2 = "/tmp/testfile2"
|
||||
|
||||
BeforeEach {
|
||||
New-Item -Path $testPath -Force -Value $testString
|
||||
New-Item -Path $testPath2 -Force -Value $testString2
|
||||
}
|
||||
|
||||
It "Should throw an error on a directory " {
|
||||
# also tests that -erroraction SilentlyContinue will work.
|
||||
|
||||
Get-Content . -ErrorAction SilentlyContinue | Should Throw
|
||||
cat . -ErrorAction SilentlyContinue | Should Throw
|
||||
gc . -ErrorAction SilentlyContinue | Should Throw
|
||||
type . -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
}
|
||||
|
||||
It "Should deliver an array object when listing a file" {
|
||||
(Get-Content -Path ./Test-Get-Content.Tests.ps1).GetType().BaseType.Name | Should Be "Array"
|
||||
(Get-Content -Path ./Test-Get-Content.Tests.ps1)[0] |Should be "Describe `"Test-Get-Content`" `{"
|
||||
It "Should return an Object when listing only a single line" {
|
||||
(Get-Content -Path $testPath).GetType().BaseType.Name | Should Be "Object"
|
||||
}
|
||||
|
||||
(gc -Path ./Test-Get-Content.Tests.ps1).GetType().BaseType.Name | Should Be "Array"
|
||||
(gc -Path ./Test-Get-Content.Tests.ps1)[0] |Should be "Describe `"Test-Get-Content`" `{"
|
||||
It "Should deliver an array object when listing a file with multiple lines" {
|
||||
(Get-Content -Path $testPath2).GetType().BaseType.Name | Should Be "Array"
|
||||
}
|
||||
|
||||
(type -Path ./Test-Get-Content.Tests.ps1).GetType().BaseType.Name | Should Be "Array"
|
||||
(type -Path ./Test-Get-Content.Tests.ps1)[0] |Should be "Describe `"Test-Get-Content`" `{"
|
||||
It "Should return the correct information from a file" {
|
||||
(Get-Content -Path $testPath) | Should Be $testString
|
||||
}
|
||||
|
||||
(cat -Path ./Test-Get-Content.Tests.ps1).GetType().BaseType.Name | Should Be "Array"
|
||||
(cat -Path ./Test-Get-Content.Tests.ps1)[0] |Should be "Describe `"Test-Get-Content`" `{"
|
||||
It "Should be able to call using the cat alias" {
|
||||
{ cat -Path $testPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call using the gc alias" {
|
||||
{ gc -Path $testPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call using the type alias" {
|
||||
{ type -Path $testPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return the same values for aliases" {
|
||||
$getContentAlias = Get-Content -Path $testPath
|
||||
$gcAlias = gc -Path $testPath
|
||||
$catAlias = cat -Path $testPath
|
||||
$typeAlias = type -Path $testPath
|
||||
|
||||
$getContentAlias | Should Be $gcAlias
|
||||
$getContentAlias | Should Be $catAlias
|
||||
$getContentAlias | Should Be $typeAlias
|
||||
}
|
||||
|
||||
It "Should be able to return a specific line from a file" {
|
||||
(Get-Content -Path $testPath2)[1] | Should be $secondline
|
||||
}
|
||||
|
||||
It "Should be able to specify the number of lines to get the content of using the TotalCount switch" {
|
||||
$returnArray = (cat -Path $testPath2 -TotalCount 2)
|
||||
|
||||
$returnArray[0] | Should Be $firstline
|
||||
$returnArray[1] | Should Be $secondline
|
||||
}
|
||||
|
||||
It "Should be able to specify the number of lines to get the content of using the Head switch" {
|
||||
$returnArray = (cat -Path $testPath2 -Head 2)
|
||||
|
||||
$returnArray[0] | Should Be $firstline
|
||||
$returnArray[1] | Should Be $secondline
|
||||
}
|
||||
|
||||
It "Should be able to specify the number of lines to get the content of using the First switch" {
|
||||
$returnArray = (cat -Path $testPath2 -First 2)
|
||||
|
||||
$returnArray[0] | Should Be $firstline
|
||||
$returnArray[1] | Should Be $secondline
|
||||
}
|
||||
|
||||
It "Should return the last line of a file using the Tail switch" {
|
||||
Get-Content -Path $testPath -Tail 1 | Should Be $testString
|
||||
}
|
||||
|
||||
|
||||
It "Should return the last lines of a file using the Last alias" {
|
||||
Get-Content -Path $testPath2 -Last 1 | Should Be $fifthline
|
||||
}
|
||||
|
||||
It "Should be able to get content within a different drive" {
|
||||
pushd env:
|
||||
$expectedoutput = [Environment]::GetEnvironmentVariable("PATH");
|
||||
|
||||
{ Get-Content PATH } | Should Not Throw
|
||||
Get-Content PATH | Should Be $expectedoutput
|
||||
|
||||
popd
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
Describe "Test-Get-Random" {
|
||||
It "Should return a random number greater than -1 " {
|
||||
Get-Random | Should BeGreaterThan -1
|
||||
}
|
||||
It "Should return a random number less than 100 " {
|
||||
Get-Random -Maximum 100 | Should BeLessThan 100
|
||||
Get-Random -Maximum 100 | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should return a random number less than 100 and greater than -100 " {
|
||||
$randomNumber = Get-Random -Minimum -100 -Maximum 100
|
||||
$randomNumber | Should BeLessThan 100
|
||||
$randomNumber | Should BeGreaterThan -100
|
||||
}
|
||||
|
||||
It "Should return a random number less than 20.93 and greater than 10.7 " {
|
||||
$randomNumber = Get-Random -Minimum 10.7 -Maximum 20.93
|
||||
$randomNumber | Should BeLessThan 20.93
|
||||
$randomNumber | Should BeGreaterThan 10.7
|
||||
}
|
||||
|
||||
It "Should return same number for both Get-Random when switch SetSeed is used " {
|
||||
$firstRandomNumber = Get-Random -Maximum 100 -SetSeed 23
|
||||
$secondRandomNumber = Get-Random -Maximum 100 -SetSeed 23
|
||||
$firstRandomNumber | Should be $secondRandomNumber
|
||||
}
|
||||
|
||||
It "Should return a number from 1,2,3,5,8,13 " {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13
|
||||
$randomNumber | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
}
|
||||
|
||||
It "Should return an array " {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3
|
||||
$randomNumber.GetType().BaseType | Should Be array
|
||||
}
|
||||
|
||||
It "Should return three random numbers for array of 1,2,3,5,8,13 " {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3
|
||||
$randomNumber[0] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[1] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[2] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[3] | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return all the numbers for array of 1,2,3,5,8,13 in no particular order" {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue)
|
||||
$randomNumber[0] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[1] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[2] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[3] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[4] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[5] | Should Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[6] | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return for a string collection " {
|
||||
$randomNumber = Get-Random -InputObject "red", "yellow", "blue"
|
||||
$randomNumber | Should Be ("red" -or "yellow" -or "blue")
|
||||
}
|
||||
|
||||
It "Should return a number for hexdecimal " {
|
||||
$randomNumber = Get-Random 0x07FFFFFFFFF
|
||||
$randomNumber | Should BeLessThan 549755813887
|
||||
$randomNumber | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should return false, check two random numbers are not equal when not using the SetSeed switch " {
|
||||
$firstRandomNumber = Get-Random
|
||||
$secondRandomNumber = Get-Random
|
||||
$firstRandomNumber | Should Not Be $secondRandomNumber
|
||||
}
|
||||
|
||||
It "Should return the same number for hexidemical number and regular number when the switch SetSeed it used " {
|
||||
$firstRandomNumber = Get-Random 0x07FFFFFFFF -SetSeed 20
|
||||
$secondRandomNumber = Get-Random 34359738367 -SetSeed 20
|
||||
$firstRandomNumber | Should Be @secondRandomNumber
|
||||
}
|
||||
It "Should throw an error because the hexidecial number is to large " {
|
||||
{ Get-Random 0x07FFFFFFFFFFFFFFFF } | Should Throw "Value was either too large or too small for a UInt32"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
Describe "Test-Select-String" {
|
||||
Context "String actions" {
|
||||
$testinputone = "hello","Hello","goodbye"
|
||||
$testinputtwo = "hello","Hello"
|
||||
|
||||
it "Should be called without errors" {
|
||||
{ $testinputone | Select-String -Pattern "hello" } | Should Not Throw
|
||||
}
|
||||
|
||||
it "Should be called without error using the sls alias" {
|
||||
{ $testinputone | sls -Pattern "hello" } | Should Not Throw
|
||||
}
|
||||
|
||||
it "Should return an array data type when multiple matches are found" {
|
||||
( $testinputtwo | Select-String -Pattern "hello").gettype().basetype | Should Be Array
|
||||
}
|
||||
|
||||
it "Should return the same result for the alias sls and Select-String " {
|
||||
$firstMatch = $testinputone | Select-String -Pattern "hello"
|
||||
$secondMatch = $testinputone | sls -Pattern "hello"
|
||||
|
||||
$equal = @(compare-object $firstMatch $secondMatch).Length -eq 0
|
||||
$equal | Should Be True
|
||||
}
|
||||
|
||||
it "Should return an object type when one match is found" {
|
||||
( $testinputtwo | Select-String -Pattern "hello" -CaseSensitive).gettype().basetype | Should Be System.Object
|
||||
}
|
||||
|
||||
it "Should return matchinfo type" {
|
||||
( $testinputtwo | Select-String -Pattern "hello" -CaseSensitive).gettype().name | Should Be MatchInfo
|
||||
}
|
||||
|
||||
it "Should be called without an error using ca for casesensitive " {
|
||||
{$testinputone | Select-String -Pattern "hello" -ca } | Should Not Throw
|
||||
}
|
||||
|
||||
it "Should use the ca alias for casesenstive" {
|
||||
$firstMatch = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive
|
||||
$secondMatch = $testinputtwo | Select-String -Pattern "hello" -ca
|
||||
|
||||
$equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0
|
||||
$equal | Should Be True
|
||||
}
|
||||
|
||||
it "Should only return the case sensitive match when the casesensitive switch is used" {
|
||||
$testinputtwo | Select-String -Pattern "hello" -CaseSensitive | Should Be "hello"
|
||||
}
|
||||
|
||||
it "Should accept a collection of strings from the input object" {
|
||||
{ Select-String -InputObject "some stuff", "other stuff" -Pattern "other" } | Should Not Throw
|
||||
}
|
||||
|
||||
it "Should return system.object when the input object switch is used on a collection" {
|
||||
( Select-String -InputObject "some stuff", "other stuff" -pattern "other" ).gettype().basetype | Should Be System.Object
|
||||
}
|
||||
|
||||
it "Should return null or empty when the input object switch is used on a collection and the pattern does not exist" {
|
||||
Select-String -InputObject "some stuff", "other stuff" -Pattern "neither" | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
it "Should return a bool type when the quiet switch is used" {
|
||||
($testinputtwo | Select-String -Quiet "hello" -CaseSensitive).gettype() | Should Be Bool
|
||||
}
|
||||
|
||||
it "Should be true when select string returns a positive result when the quiet switch is used" {
|
||||
($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should Be $True
|
||||
}
|
||||
|
||||
it "Should be empty when select string does not return a result when the quiet switch is used" {
|
||||
$testinputtwo | Select-String -Quiet "goodbye" | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
it "Should return an array of non matching strings when the switch of NotMatch is used and the string do not match" {
|
||||
$testinputone | Select-String -Pattern "goodbye" -NotMatch | Should Be "hello", "hello"
|
||||
}
|
||||
|
||||
it "Should return the same as NotMatch" {
|
||||
$firstMatch = $testinputone | Select-String -pattern "goodbye" -NotMatch
|
||||
$secondMatch = $testinputone | Select-String -pattern "goodbye" -n
|
||||
|
||||
$equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0
|
||||
$equal | Should Be True
|
||||
}
|
||||
}
|
||||
|
||||
Context "Filesytem actions" {
|
||||
$testInputFile = "/tmp/testfile1.txt"
|
||||
BeforeEach {
|
||||
New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string`nThis is the second line`nThis is the third line`nThis is the fourth line`nNo matches"
|
||||
}
|
||||
|
||||
It "Should return an object when a match is found is the file on only one line" {
|
||||
(Select-String $testInputFile -Pattern "string").GetType().BaseType | Should be System.Object
|
||||
}
|
||||
|
||||
It "Should return an array when a match is found is the file on several lines" {
|
||||
(Select-String $testInputFile -Pattern "in").GetType().BaseType | Should be array
|
||||
(Select-String $testInputFile -Pattern "in")[0].GetType().Name | Should Be MatchInfo
|
||||
}
|
||||
|
||||
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" {
|
||||
$expected = $testInputFile + ":1:This is a text string, and another string"
|
||||
|
||||
Select-String $testInputFile -Pattern "string" | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" {
|
||||
$expected = $testInputFile + ":2:This is the second line"
|
||||
|
||||
Select-String $testInputFile -Pattern "second"| Should Be $expected
|
||||
}
|
||||
|
||||
It "Should return all strings where 'in' is found in testfile1 pattern switch is not required" {
|
||||
$expected1 = "This is a text string, and another string"
|
||||
$expected2 = "This is the second line"
|
||||
$expected3 = "This is the third line"
|
||||
$expected4 = "This is the fourth line"
|
||||
|
||||
(Select-String in $testInputFile)[0].Line | Should Be $expected1
|
||||
(Select-String in $testInputFile)[1].Line | Should Be $expected2
|
||||
(Select-String in $testInputFile)[2].Line | Should Be $expected3
|
||||
(Select-String in $testInputFile)[3].Line | Should Be $expected4
|
||||
(Select-String in $testInputFile)[4].Line | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return empty because 'for' is not found in testfile1 " {
|
||||
Select-String for $testInputFile | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return the third line in testfile1 and the lines above and below it " {
|
||||
$expectedLine = "testfile1.txt:2:This is the second line"
|
||||
$expectedLineBefore = "testfile1.txt:3:This is the third line"
|
||||
$expectedLineAfter = "/tmp/testfile1.txt:4:This is the fourth line"
|
||||
|
||||
Select-String third $testInputFile -Context 1 | Should Match $expectedLine
|
||||
Select-String third $testInputFile -Context 1 | Should Match $expectedLineBefore
|
||||
Select-String third $testInputFile -Context 1 | Should Match $expectedLineAfter
|
||||
}
|
||||
|
||||
It "Should return the number of matches for 'is' in textfile1 " {
|
||||
(Select-String is $testInputFile -CaseSensitive).count| Should Be 4
|
||||
}
|
||||
|
||||
It "Should return the third line in testfile1 when a relative path is used" {
|
||||
$expected = "/tmp/testfile1.txt:3:This is the third line"
|
||||
|
||||
Select-String third /tmp/../tmp/testfile1.txt | Should Match $expected
|
||||
}
|
||||
|
||||
It "Should return the fourth line in testfile1 when a relative path is used" {
|
||||
$testDirectory = "/tmp/"
|
||||
$expected = "/tmp/testfile1.txt:5:No matches"
|
||||
|
||||
pushd $testDirectory
|
||||
|
||||
Select-String matches $testDirectory/testfile1.txt | Should Match $expected
|
||||
popd
|
||||
}
|
||||
|
||||
It "Should return the fourth line in testfile1 when a regular expression is used" {
|
||||
$expected = "/tmp/testfile1.txt:5:No matches"
|
||||
|
||||
Select-String 'matc*' $testInputFile -CaseSensitive | Should Match $expected
|
||||
}
|
||||
|
||||
It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" {
|
||||
$expected = "/tmp/testfile1.txt:5:No matches"
|
||||
|
||||
Select-String 'matc*' $testInputFile -ca | Should Match $expected
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user