mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Reorganize tests
- Pester source code moved to `test/Pester`, deleted `ext-src`. - Pester tests (.ps1 files) moved to `test/powershell` - xUnit tests (.cs files) moved to `test/csharp` - Third-party script test moved to `test/shebang`
This commit is contained in:
1 parent
ec08d9f46c
commit
bbebf2f76a
105 files changed
+1
-1
No files matched your search
@@ -0,0 +1,19 @@
|
||||
Describe "Add-Member" {
|
||||
|
||||
It "should be able to see a newly added member of an object" {
|
||||
$o = New-Object psobject
|
||||
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
|
||||
|
||||
$o.proppy | Should Not BeNullOrEmpty
|
||||
$o.proppy | Should Be "superVal"
|
||||
}
|
||||
|
||||
It "Should be able to add a member to an object that already has a member in it" {
|
||||
$o = New-Object psobject
|
||||
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
|
||||
Add-Member -InputObject $o -MemberType NoteProperty -Name AnotherMember -Value "AnotherValue"
|
||||
|
||||
$o.AnotherMember | Should Not BeNullOrEmpty
|
||||
$o.AnotherMember | Should Be "AnotherValue"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
Describe "Clear-Variable" {
|
||||
BeforeEach {
|
||||
$var1 = 3
|
||||
}
|
||||
|
||||
It "Should be able to clear a variable using the Name switch" {
|
||||
Clear-Variable -Name var1
|
||||
$var1 | Should BeNullOrEmpty
|
||||
{ Get-Variable var1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to clear a variable without using the Name switch" {
|
||||
Clear-Variable var1
|
||||
$var1 | Should BeNullOrEmpty
|
||||
{ Get-Variable var1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have a null value after clearing the variable" {
|
||||
Clear-Variable var1
|
||||
$var1 | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should call without error using the clv alias" {
|
||||
{ clv -Name var1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to include a set of variables to clear" {
|
||||
$var1 = 2
|
||||
$var2 = 3
|
||||
$var3 = 4
|
||||
$variable1 = 23
|
||||
$variable2 = 4
|
||||
$variable3 = 2
|
||||
$w = 3
|
||||
|
||||
Clear-Variable -Name w, vari* -Include w, vari*
|
||||
|
||||
$variable1 | Should BeNullOrEmpty
|
||||
$variable2 | Should BeNullOrEmpty
|
||||
$variable3 | Should BeNullOrEmpty
|
||||
$w | Should BeNullOrEmpty
|
||||
|
||||
$var1 | Should Not BeNullOrEmpty
|
||||
$var2 | Should Not BeNullOrEmpty
|
||||
$var3 | Should Not BeNullOrEmpty
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to exclude a set of variables to clear" {
|
||||
$var1 = 2
|
||||
$var2 = 3
|
||||
$var3 = 4
|
||||
$variable1 = 23
|
||||
$variable2 = 4
|
||||
$variable3 = 2
|
||||
$w = 3
|
||||
|
||||
Clear-Variable -Name w, vari* -Exclude var*
|
||||
|
||||
$variable1 | Should Not BeNullOrEmpty
|
||||
$variable2 | Should Not BeNullOrEmpty
|
||||
$variable3 | Should Not BeNullOrEmpty
|
||||
$var1 | Should Not BeNullOrEmpty
|
||||
$var2 | Should Not BeNullOrEmpty
|
||||
$var3 | Should Not BeNullOrEmpty
|
||||
|
||||
$w | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to pass the cleared object through the pipeline using the passthru switch" {
|
||||
{ Clear-Variable -Name var1 -PassThru | Format-Wide -Property Value } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not clear environment variables" {
|
||||
$env:TEMPVARIABLE = "test data"
|
||||
|
||||
Clear-Variable -Name env:TEMPVARIABLE -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should clear variable even if it is read-only using the Force parameter" {
|
||||
New-Variable -Name var2 -Option ReadOnly -Value 100
|
||||
|
||||
Clear-Variable -Name var1
|
||||
Clear-Variable -Name var2 -Force
|
||||
|
||||
$var1 | Should BeNullOrEmpty
|
||||
$var2 | Should BeNullOrEmpty
|
||||
|
||||
Remove-Variable -Name var2 -Force
|
||||
}
|
||||
|
||||
It "Should throw error when trying to clear variable that is read-only without using the Force parameter" {
|
||||
New-Variable -Name var2 -Option ReadOnly -Value 100
|
||||
|
||||
Clear-Variable -Name var2 -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var2 | Should Not BeNullOrEmpty
|
||||
|
||||
Remove-Variable -Name var2 -Force
|
||||
}
|
||||
|
||||
Context "Scope Tests" {
|
||||
# This will violate the DRY principle. Tread softly.
|
||||
|
||||
It "Should be able to clear a global scope variable using the global switch" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
|
||||
Clear-Variable -Name globalVar -Scope global
|
||||
|
||||
$globalVar | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should not be able to clear a global scope variable using the local switch" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
|
||||
Clear-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should not be able to clear a global variable using the script scope switch" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
|
||||
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able to clear an item locally using the local switch" {
|
||||
New-Variable localVar -Value 2 -Scope local -Force
|
||||
|
||||
Clear-Variable -Name localVar -Scope local
|
||||
|
||||
$localVar | Should BeNullOrEmpty
|
||||
|
||||
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should not be able to clear an item locally using the global switch" {
|
||||
New-Variable localVar -Value 2 -Scope local -Force
|
||||
|
||||
Clear-Variable -Name localVar -Scope global -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should not be able to clear a local variable using the script scope switch" {
|
||||
New-Variable localVar -Value 2 -Scope local -Force
|
||||
|
||||
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able to clear a script variable created using the script switch" {
|
||||
{
|
||||
New-Variable -Name derp2 -Value 3 -Scope script -Force
|
||||
|
||||
Clear-Variable -Name derp2 -Scope script
|
||||
}| Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to clear a global script variable that was created using the script scope switch" {
|
||||
{
|
||||
New-Variable -Name derpx -Value 4 -Scope script -Force
|
||||
|
||||
Clear-Variable -Name derpx -Scope script
|
||||
} | Should Not Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
Describe "Compare-Object" {
|
||||
$nl = [Environment]::NewLine
|
||||
$slash = [System.IO.Path]::DirectorySeparatorChar
|
||||
$testDirectory = $HOME + $slash + "testDirectory"
|
||||
$dir = $testDirectory
|
||||
|
||||
New-Item $testDirectory -ItemType directory -Force
|
||||
|
||||
$content1 = "line 1" + $nl + "line 2"
|
||||
$content2 = "line 1" + $nl + "line 2.1"
|
||||
$content3 = "line 1" + $nl + "line 2" + $nl + "line 3"
|
||||
$content4 = "line 1" + $nl + "line 2.1" + $nl + "Line 3"
|
||||
|
||||
$file1 = $testDirectory + $slash + "test1.txt"
|
||||
$file2 = $testDirectory + $slash + "test2.txt"
|
||||
$file3 = $testDirectory + $slash + "test3.txt"
|
||||
$file4 = $testDirectory + $slash + "test4.txt"
|
||||
|
||||
New-Item $file1 -ItemType file -Value $content1 -Force
|
||||
New-Item $file2 -ItemType file -Value $content2 -Force
|
||||
New-Item $file3 -ItemType file -Value $content3 -Force
|
||||
New-Item $file4 -ItemType file -Value $content4 -Force
|
||||
|
||||
Test-Path $testDirectory | Should Be $true
|
||||
|
||||
It "Should be able to compare the same object using the referenceObject and differenceObject switches" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2) } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not throw when referenceobject switch is not used" {
|
||||
{ Compare-Object $(Get-Content $file1) -DifferenceObject $(Get-Content $file2) } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not throw when differenceobject switch is not used" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file1) $(Get-Content $file2) } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to execute compare object using the compare alias" {
|
||||
{ compare -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2) } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should produce the same output when the compare alias is used" {
|
||||
$alias = compare -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2)
|
||||
$fullname = Compare-Object -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2)
|
||||
|
||||
$alias[0].InputObject | Should Be $fullname[0].InputObject
|
||||
$alias[0].SideIndicator | Should Be $fullname[0].SideIndicator
|
||||
$alias[1].InputObject | Should Be $fullname[1].InputObject
|
||||
$alias[1].SideIndicator | Should Be $fullname[1].SideIndicator
|
||||
|
||||
$alias.Length | Should Be 2 # There should be no other elements to test
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to execute compare object using the diff alias" {
|
||||
$alias = diff -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2)
|
||||
$fullname = Compare-Object -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2)
|
||||
|
||||
$alias[0].InputObject | Should Be $fullname[0].InputObject
|
||||
$alias[0].SideIndicator | Should Be $fullname[0].SideIndicator
|
||||
$alias[1].InputObject | Should Be $fullname[1].InputObject
|
||||
$alias[1].SideIndicator | Should Be $fullname[1].SideIndicator
|
||||
|
||||
$alias.Length | Should Be 2 # There should be no other elements to test
|
||||
}
|
||||
|
||||
It "Should indicate data that exists only in the reference dataset" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)
|
||||
|
||||
$actualOutput[1].SideIndicator | Should Be "<="
|
||||
}
|
||||
|
||||
It "Should indicate data that exists only in the difference dataset" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)
|
||||
|
||||
$actualOutput[1].SideIndicator | Should Be "<="
|
||||
}
|
||||
|
||||
It "Should indicate data that exists in both datasets when the includeEqual switch is used" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -IncludeEqual
|
||||
|
||||
$actualOutput.Length | Should Be 4
|
||||
$actualOutput[0].SideIndicator | Should Be "=="
|
||||
$actualOutput[1].SideIndicator | Should Be "=="
|
||||
}
|
||||
|
||||
It "Should be able to use the casesensitive switch" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -CaseSensitive } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should correctly indicate that different cases are different when the casesensitive switch is used" {
|
||||
$caOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -CaseSensitive
|
||||
$ncaOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)
|
||||
|
||||
$caOutput.Length | Should Be 4
|
||||
|
||||
$ncaOutput[1].SideIndicator | Should Not Be $caOutput[1].SideIndicator
|
||||
$ncaOutput[2].SideIndicator | Should Not Be $caOutput[2].SideIndicator
|
||||
$ncaOutput[3].SideIndicator | Should Not Be $caOutput[3].SideIndicator
|
||||
|
||||
}
|
||||
|
||||
It "Should throw when reference set is null" {
|
||||
{ Compare-Object -ReferenceObject $anonexistentvariable -DifferenceObject $(Get-Content $file4) } | Should Throw
|
||||
}
|
||||
|
||||
It "Should throw when difference set is null" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $anonexistentvariable } | Should Throw
|
||||
}
|
||||
|
||||
It "Should give a 0 array when using excludedifferent switch without also using the includeequal switch" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -ExcludeDifferent
|
||||
|
||||
$actualOutput.Length | Should Be 0
|
||||
}
|
||||
|
||||
It "Should only display equal lines when excludeDifferent switch is used alongside the includeequal switch" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -IncludeEqual -ExcludeDifferent
|
||||
|
||||
$actualOutput.Length | Should Be 2
|
||||
}
|
||||
|
||||
It "Should be able to pass objects to pipeline using the passthru switch" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -Passthru | Format-Wide } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to specify the property of two objects to compare" {
|
||||
$actualOutput = Compare-Object -ReferenceObject $file3 -DifferenceObject $testDirectory -Property Length
|
||||
|
||||
$actualOutput[0].Length | Should BeGreaterThan 0
|
||||
$actualOutput[1].Length | Should BeGreaterThan 0
|
||||
$actualOutput[0].Length | Should Not Be $actualOutput[1].Length
|
||||
}
|
||||
|
||||
It "Should be able to specify the syncwindow without error" {
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -syncWindow 5 } | Should Not Throw
|
||||
{ Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -syncWindow 8 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the expected output when changing the syncwindow" {
|
||||
$var1 = 1..15
|
||||
$var2 = 15..1
|
||||
|
||||
$actualOutput = Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -syncWindow 6
|
||||
|
||||
$actualOutput[0].InputObject | Should Be 15
|
||||
$actualOutput[1].InputObject | Should Be 1
|
||||
$actualOutput[2].InputObject | Should Be 1
|
||||
$actualOutput[3].InputObject | Should Be 15
|
||||
|
||||
$actualOutput[0].SideIndicator | Should be "=>"
|
||||
$actualOutput[1].SideIndicator | Should be "<="
|
||||
$actualOutput[2].SideIndicator | Should be "=>"
|
||||
$actualOutput[3].SideIndicator | Should be "<="
|
||||
}
|
||||
|
||||
# Clean up after yourself
|
||||
Remove-Item $testDirectory -Recurse -Force
|
||||
Test-Path $testDirectory | Should Be $false
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "ConvertFrom-Csv" {
|
||||
$testObject = "a", "1"
|
||||
$testcsv = "$here/assets/TestCsv2.csv"
|
||||
$testName = "Zaphod BeebleBrox"
|
||||
$testColumns = @"
|
||||
a,b,c
|
||||
1,2,3
|
||||
"@
|
||||
It "Should be able to be called" {
|
||||
{ ConvertFrom-Csv -InputObject $testObject } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to pipe" {
|
||||
{ $testObject | ConvertFrom-Csv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have expected results when using piped inputs" {
|
||||
$csvContent = Get-Content $testcsv
|
||||
$actualresult = $csvContent | ConvertFrom-Csv
|
||||
|
||||
$actualresult.GetType().BaseType.Name | Should Be "Array"
|
||||
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
|
||||
|
||||
#Should have a name property in the result
|
||||
$actualresult[0].Name | Should Be $testName
|
||||
}
|
||||
|
||||
It "Should be able to set a delimiter" {
|
||||
{ $testcsv | ConvertFrom-Csv -Delimiter ";" } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should actually delimit the output" {
|
||||
$csvContent = Get-Content $testcsv
|
||||
$actualresult = $csvContent | ConvertFrom-Csv -Delimiter ";"
|
||||
|
||||
$actualresult.GetType().BaseType.Name | Should Be "Array"
|
||||
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
|
||||
|
||||
# ConvertFrom-Csv takes the first line of the input as a header by default
|
||||
$actualresult.Length | Should Be $($csvContent.Length - 1)
|
||||
}
|
||||
|
||||
It "Should be able to have multiple columns" {
|
||||
$actualData = $testColumns | ConvertFrom-Csv
|
||||
|
||||
$actualLength = $($( $actualData | gm) | Where-Object {$_.MemberType -eq "NoteProperty" }).Length
|
||||
|
||||
$actualLength | Should Be 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
Describe "ConvertFrom-StringData" {
|
||||
$sampleData = @'
|
||||
foo = 0
|
||||
bar = 1
|
||||
bazz = 2
|
||||
'@
|
||||
|
||||
It "Should not throw when called with just the stringdata switch" {
|
||||
{ ConvertFrom-StringData -StringData 'a=b' } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return a hashtable" {
|
||||
$(ConvertFrom-StringData -StringData 'a=b').GetType().Name | Should Be "Hashtable"
|
||||
}
|
||||
|
||||
It "Should throw if not in x=y format" {
|
||||
{ ConvertFrom-StringData -StringData 'ab' } | Should Throw
|
||||
{ ConvertFrom-StringData -StringData 'a,b' } | Should Throw
|
||||
{ ConvertFrom-StringData -StringData 'a b' } | Should Throw
|
||||
{ ConvertFrom-StringData -StringData 'a\tb' } | Should Throw
|
||||
{ ConvertFrom-StringData -StringData 'a:b' } | Should Throw
|
||||
}
|
||||
|
||||
It "Should return the data on the left side in the key" {
|
||||
$actualValue = ConvertFrom-StringData -StringData 'a=b'
|
||||
|
||||
$actualValue.Keys | Should Be "a"
|
||||
}
|
||||
|
||||
It "Should return the data on the right side in the value" {
|
||||
$actualValue = ConvertFrom-StringData -StringData 'a=b'
|
||||
|
||||
$actualValue.Values | Should Be "b"
|
||||
}
|
||||
|
||||
It "Should return a keycollection for the keys" {
|
||||
$(ConvertFrom-StringData -StringData 'a=b').Keys.gettype().Name | Should Be "KeyCollection"
|
||||
}
|
||||
|
||||
It "Should return a valuecollection for the values" {
|
||||
$(ConvertFrom-StringData -StringData 'a=b').Values.gettype().Name | Should Be "ValueCollection"
|
||||
}
|
||||
|
||||
It "Should work for multiple lines" {
|
||||
{ ConvertFrom-StringData -StringData $sampleData } | Should Not Throw
|
||||
|
||||
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should Be "foo", "bar", "bazz"
|
||||
|
||||
$(ConvertFrom-StringData -StringData $sampleData).Values | Should Be "0","1","2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
Describe "ConvertTo-Csv" {
|
||||
$Name = "Hello"; $Data = "World";
|
||||
$testObject = New-Object psobject -Property @{ FirstColumn = $Name; SecondColumn = $Data }
|
||||
|
||||
|
||||
It "Should Be able to be called without error" {
|
||||
{ $testObject | ConvertTo-Csv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should output an array of objects" {
|
||||
$result = $testObject | ConvertTo-Csv
|
||||
|
||||
$result.GetType().BaseType.Name | Should Be "Array"
|
||||
}
|
||||
|
||||
It "Should return the type of data in the first element of the output array" {
|
||||
$result = $testObject | ConvertTo-Csv
|
||||
|
||||
$result[0] | Should Be "#TYPE System.Management.Automation.PSCustomObject"
|
||||
}
|
||||
|
||||
It "Should return the column info in the second element of the output array" {
|
||||
$result = $testObject | ConvertTo-Csv
|
||||
|
||||
$result[1] | Should Match "`"FirstColumn`""
|
||||
$result[1] | Should Match "`"SecondColumn`""
|
||||
}
|
||||
|
||||
It "Should return the data as a comma-separated list in the third element of the output array" {
|
||||
$result = $testObject | ConvertTo-Csv
|
||||
$result[2] | Should Match "`"Hello`""
|
||||
$result[2] | Should Match "`"World`""
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
Describe "ConvertTo-SecureString" {
|
||||
|
||||
Context "Checking return types of ConvertTo-SecureString" {
|
||||
|
||||
It "Should return System.Security.SecureString after converting plaintext variable"{
|
||||
$PesterTestConvert = (ConvertTo-SecureString "plaintextpester" -AsPlainText -force)
|
||||
($PesterTestConvert).GetType() | Should Be securestring
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
Describe "DotNetAPI" {
|
||||
$posh_E = 2.718281828459045
|
||||
$posh_pi = 3.14159265358979
|
||||
|
||||
It "Should be able to use static .NET classes and get a constant" {
|
||||
[System.Math]::E | Should Match $posh_E.ToString()
|
||||
[System.Math]::PI | Should Match $posh_pi.ToString()
|
||||
}
|
||||
|
||||
It "Should be able to invoke a method" {
|
||||
[System.Environment]::GetEnvironmentVariable("PATH") | Should Be $env:PATH
|
||||
}
|
||||
|
||||
It "Should not require 'system' in front of static classes" {
|
||||
[Environment]::CommandLine | Should Be ([System.Environment]::CommandLine)
|
||||
|
||||
[Math]::E | Should Be ([System.Math]::E)
|
||||
}
|
||||
|
||||
It "Should be able to create a new instance of a .Net object" {
|
||||
[System.Guid]$guidVal = [System.Guid]::NewGuid()
|
||||
|
||||
$guidVal.GetType().Name | Should Be "Guid"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
Describe "Environment-Variables" {
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
It "Should have environment variables" {
|
||||
Get-Item ENV: | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should have a nonempty PATH" {
|
||||
$ENV:PATH | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should contain /bin in the PATH" {
|
||||
if ($isWindows)
|
||||
{
|
||||
$ENV:PATH | Should Match "C:"
|
||||
}
|
||||
else
|
||||
{
|
||||
$ENV:PATH | Should Match "/bin"
|
||||
}
|
||||
}
|
||||
|
||||
It "Should have the correct HOME" {
|
||||
if ($isWindows)
|
||||
{
|
||||
$expected = "\Users\" + $ENV:USERNAME
|
||||
}
|
||||
else
|
||||
{
|
||||
$expected = /bin/bash -c "cd ~ && pwd"
|
||||
}
|
||||
$ENV:HOME | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to set the environment variables" {
|
||||
$expected = "this is a test environment variable"
|
||||
{ $ENV:TESTENVIRONMENTVARIABLE = $expected } | Should Not Throw
|
||||
|
||||
$ENV:TESTENVIRONMENTVARIABLE | Should Not BeNullOrEmpty
|
||||
$ENV:TESTENVIRONMENTVARIABLE | Should Be $expected
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
. $here/Test-Common.ps1
|
||||
|
||||
Describe "Export-Alias" {
|
||||
$testAliasDirectory = "$(GetTempDir)/ExportAliasTestDirectory"
|
||||
$testAliases = "TestAliases"
|
||||
$fulltestpath = "$($testAliasDirectory)/$($testAliases)"
|
||||
|
||||
BeforeEach {
|
||||
New-Item -Path $testAliasDirectory -ItemType Directory -Force
|
||||
}
|
||||
|
||||
It "Should be able to create a file in the specified location"{
|
||||
Export-Alias $fulltestpath
|
||||
|
||||
|
||||
Test-Path $fulltestpath | Should be $true
|
||||
}
|
||||
|
||||
It "Should create a file with the list of aliases that match the expected list" {
|
||||
Export-Alias $fulltestpath
|
||||
|
||||
Test-Path $fulltestpath | Should Be $true
|
||||
|
||||
$actual = Get-Content $fulltestpath | Sort
|
||||
$expected = $(Get-Command -CommandType Alias)
|
||||
|
||||
for ( $i=0; $i -lt $expected.Length; $i++)
|
||||
{
|
||||
# We loop through the expected list and not the other because the output writes some comments to the file.
|
||||
$expected[$i] | Should Match $actual[$i].Name
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Remove-Item -Path $testAliasDirectory -Recurse -Force
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
Describe "Export-Csv" {
|
||||
$testObject = @("test","object","array")
|
||||
$testCsv = "output.csv"
|
||||
|
||||
AfterEach {
|
||||
Remove-Item $testCsv -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should be able to be called without error" {
|
||||
{ $testObject | Export-Csv $testCsv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should throw if an output file isn't specified" {
|
||||
{ $testObject | Export-Csv -ErrorAction SilentlyContinue } | Should Throw
|
||||
}
|
||||
|
||||
It "Should be a string when exporting via pipe" {
|
||||
$testObject | Export-Csv $testCsv
|
||||
|
||||
$piped = Get-Content $testCsv
|
||||
|
||||
$piped[0] | Should Match ".String"
|
||||
}
|
||||
|
||||
It "Should be an object when exporting via the inputObject switch" {
|
||||
Export-Csv -InputObject $testObject -Path $testCsv
|
||||
|
||||
$switch = Get-Content $testCsv
|
||||
|
||||
$switch[0] | Should Match ".Object"
|
||||
}
|
||||
|
||||
It "Should output a csv file containing a string of all the lengths of each element when piped input is used" {
|
||||
$testObject | Export-Csv -Path $testCsv
|
||||
|
||||
$first = "`"" + $testObject[0].Length.ToString() + "`""
|
||||
$second = "`"" + $testObject[1].Length.ToString() + "`""
|
||||
$third = "`"" + $testObject[2].Length.ToString() + "`""
|
||||
$expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third)
|
||||
|
||||
for ( $i = 0; $i -lt $testCsv.Length; $i++)
|
||||
{
|
||||
$(Get-Content $testCsv)[$i] | Should Be $expected[$i]
|
||||
}
|
||||
}
|
||||
|
||||
It "Should be able to use the epcsv alias without error" {
|
||||
{ $testObject | Export-Csv -Path $testCsv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same information when using the alias vs the cmdlet" {
|
||||
$testObject | Export-Csv -Path $testCsv
|
||||
|
||||
$aliasObject = "alias.csv"
|
||||
|
||||
$testObject | epcsv -Path $aliasObject
|
||||
|
||||
for ( $i = 0; $i -lt $testCsv.Length; $i++)
|
||||
{
|
||||
$(Get-Content $testCsv)[$i] | Should Be $(Get-Content $aliasObject)[$i]
|
||||
}
|
||||
|
||||
# Clean up after yourself
|
||||
Remove-Item $aliasObject -Force
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
Describe "Export-FormatData" {
|
||||
|
||||
Context "Check Export-FormatData can be called validly." {
|
||||
It "Should be able to be called without error" {
|
||||
{ Get-FormatData | Export-FormatData -Path "outputfile" } | Should Not Throw
|
||||
Remove-Item "outputfile" -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check that the output is in the correct format" {
|
||||
It "Should not return an empty xml file" {
|
||||
Get-FormatData | Export-FormatData -Path "outputfile"
|
||||
$piped = Get-Content "outputfile"
|
||||
$piped | Should Not Be ""
|
||||
Remove-Item "outputfile" -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should have a valid xml tag at the start of the file" {
|
||||
Get-FormatData | Export-FormatData -Path "outputfile"
|
||||
$piped = Get-Content "outputfile"
|
||||
$piped[0] | Should Be "<"
|
||||
Remove-Item "outputfile" -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
Describe "Format-Custom" {
|
||||
|
||||
Context "Check Format-Custom can be called validly." {
|
||||
It "Should be able to be called without error" {
|
||||
{ Get-FormatData | Format-Custom } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call the fc alias without error" {
|
||||
{ Get-FormatData | fc } | Should Not Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check Format-Custom aliases" {
|
||||
|
||||
It "Should have the same output between the alias and the unaliased function" {
|
||||
$nonaliased = Get-FormatData | Format-Custom
|
||||
$aliased = Get-FormatData | fc
|
||||
$($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should Be 0
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check specific flags on Format-Custom" {
|
||||
|
||||
It "Should be able to specify the depth in output" {
|
||||
$getprocesspester = Get-FormatData | Format-Custom -depth 1
|
||||
($getprocesspester).Count | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should be able to use the Property flag to select properties" {
|
||||
$ProcessName = Get-Process | Format-Custom -Property "Name"
|
||||
$ProcessName | Should Not Match "Handle"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
Describe "Format-List" {
|
||||
$nl = [Environment]::NewLine
|
||||
BeforeEach {
|
||||
$in = New-Object PSObject
|
||||
Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue
|
||||
}
|
||||
|
||||
It "Should call format list without error" {
|
||||
{ $in | Format-List } | Should Not Throw
|
||||
{ $in | Format-List } | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to call the alias" {
|
||||
{ $in | fl } | Should Not Throw
|
||||
{ $in | fl } | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should have the same output whether choosing alias or not" {
|
||||
$expected = $in | Format-List | Out-String
|
||||
$actual = $in | fl | Out-String
|
||||
|
||||
$actual | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should produce the expected output" {
|
||||
$expected = "${nl}${nl}testName : testValue${nl}${nl}${nl}${nl}"
|
||||
$in = New-Object PSObject
|
||||
Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue
|
||||
|
||||
$in | Format-List | Should Not BeNullOrEmpty
|
||||
$in | Format-List | Out-String | Should Not BeNullOrEmpty
|
||||
$in | Format-List | Out-String | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to call a property of the piped input" {
|
||||
# Tested on two input commands to verify functionality.
|
||||
{ Get-Process | Format-List -Property Name } | Should Not Throw
|
||||
{ Get-Process | Format-List -Property Name } | Should Not BeNullOrEmpty
|
||||
|
||||
{ Get-Date | Format-List -Property DisplayName } | Should Not Throw
|
||||
{ Get-Date | Format-List -Property DisplayName } | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to display a list of props when separated by a comma" {
|
||||
{ Get-Process | Format-List -Property Name,BasePriority } | Should Not Throw
|
||||
|
||||
(Get-Process | Format-List -Property Name,BasePriority | Out-String) -Split "${nl}" |
|
||||
Where-Object { $_.trim() -ne "" } |
|
||||
ForEach-Object { $_ | Should Match "(Name)|(BasePriority)" }
|
||||
}
|
||||
|
||||
It "Should show the requested prop in every element" {
|
||||
# Testing each element of format-list, using a for-each loop since the Format-List is so opaque
|
||||
(Get-Process | Format-List -Property CPU | Out-String) -Split "${nl}" |
|
||||
Where-Object { $_.trim() -ne "" } |
|
||||
ForEach-Object { $_ | Should Match "CPU :" }
|
||||
}
|
||||
|
||||
It "Should not show anything other than the requested props" {
|
||||
$output = Get-Process | Format-List -Property Name | Out-String
|
||||
|
||||
$output | Should Not Match "CPU :"
|
||||
$output | Should Not Match "Id :"
|
||||
$output | Should Not Match "Handle :"
|
||||
}
|
||||
|
||||
It "Should be able to take input without piping objects to it" {
|
||||
$output = { Format-List -InputObject $in }
|
||||
|
||||
$output | Should Not Throw
|
||||
$output | Should Not BeNullOrEmpty
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
Describe "Format-Table" {
|
||||
It "Should call format table on piped input without error" {
|
||||
{ Get-Process | Format-Table } | Should Not Throw
|
||||
|
||||
{ Get-Process | ft } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return a format object data type" {
|
||||
$val = (Get-Process | Format-Table | gm )
|
||||
|
||||
$val2 = (Get-Process | Format-Table | gm )
|
||||
|
||||
$val.TypeName | Should Match "Microsoft.Powershell.Commands.Internal.Format"
|
||||
|
||||
$val2.TypeName | Should Match "Microsoft.Powershell.Commands.Internal.Format"
|
||||
}
|
||||
|
||||
It "Should be able to be called with optional parameters" {
|
||||
$v1 = (Get-Process | Format-Table *)
|
||||
$v2 = (Get-Process | Format-Table -Property ProcessName)
|
||||
$v3 = (Get-Process | Format-Table -GroupBy ProcessName)
|
||||
$v4 = (Get-Process | Format-Table -View StartTime)
|
||||
|
||||
$v12 = (Get-Process | ft *)
|
||||
$v22 = (Get-Process | ft -Property ProcessName)
|
||||
$v32 = (Get-Process | ft -GroupBy ProcessName)
|
||||
$v42 = (Get-Process | ft -View StartTime)
|
||||
|
||||
{ $v1 } | Should Not Throw
|
||||
{ $v2 } | Should Not Throw
|
||||
{ $v3 } | Should Not Throw
|
||||
{ $v4 } | Should Not Throw
|
||||
|
||||
{ $v12 } | Should Not Throw
|
||||
{ $v22 } | Should Not Throw
|
||||
{ $v32 } | Should Not Throw
|
||||
{ $v42 } | Should Not Throw
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
Describe "Format-Wide" {
|
||||
It "Should be able to call format wide without error" {
|
||||
{ Get-Process | Format-Wide } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use the fw alias without error" {
|
||||
{ Get-Process | fw } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same output between the alias and the unaliased function" {
|
||||
$nonaliased = Get-ChildItem | Format-Wide
|
||||
$aliased = Get-ChildItem | fw
|
||||
|
||||
$($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should Be 0
|
||||
}
|
||||
|
||||
It "Should be able to specify the columns in output using the column switch" {
|
||||
{ ls | Format-Wide -Column 3 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use the autosize switch" {
|
||||
{ ls | Format-Wide -Autosize } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to take inputobject instead of pipe" {
|
||||
{ Format-Wide -InputObject $(ls) } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use the property switch" {
|
||||
{ Format-Wide -InputObject $(ls) -Property Mode } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should throw an error when property switch and view switch are used together" {
|
||||
{ Format-Wide -InputObject $(ls) -Property CreationTime -View aoeu } | Should Throw "Found invalid data"
|
||||
}
|
||||
|
||||
It "Should throw and suggest proper input when view is used with invalid input without the property switch" {
|
||||
{ Format-Wide -InputObject $(gps) -View aoeu } | Should Throw
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
Describe "Get-Alias" {
|
||||
It "Should have a return type of System.Array when gal returns more than one object" {
|
||||
$val1=(Get-Alias a*)
|
||||
$val2=(Get-Alias c*)
|
||||
$i=0
|
||||
|
||||
$val1 | ForEach-Object{ $i++};
|
||||
if($i -lt 2) {
|
||||
$val1.GetType().BaseType.FullName | Should Be "System.Management.Automation.CommandInfo"
|
||||
}
|
||||
else
|
||||
{
|
||||
$val1.GetType().BaseType.FullName | Should Be "System.Array"
|
||||
}
|
||||
|
||||
$val2 | ForEach-Object{ $i++};
|
||||
if($i -lt 2) {
|
||||
$val2.GetType().BaseType.FullName | Should Be "System.Management.Automation.CommandInfo"
|
||||
}
|
||||
else
|
||||
{
|
||||
$val2.GetType().BaseType.FullName | Should Be "System.Array"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
It "should return an array of 3 objects" {
|
||||
$val = Get-Alias a*
|
||||
$alias = gal a*
|
||||
|
||||
$val.CommandType | Should Not BeNullOrEmpty
|
||||
$val.Name | Should Not BeNullOrEmpty
|
||||
$val.ModuleName | Should BeNullOrEmpty
|
||||
|
||||
$alias.CommandType | Should Not BeNullOrEmpty
|
||||
$alias.Name | Should Not BeNullOrEmpty
|
||||
$alias.ModuleName | Should BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
Describe "Get-ChildItem" {
|
||||
It "Should list the contents of the current folder" {
|
||||
(Get-ChildItem .).Name.Length | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should list the contents of the home directory" {
|
||||
pushd $HOME
|
||||
(Get-ChildItem .).Name.Length | Should BeGreaterThan 0
|
||||
popd
|
||||
}
|
||||
|
||||
It "Should be able to use the ls alias" {
|
||||
$(ls .).Name.Length | Should Be $(Get-ChildItem .).Name.Length
|
||||
}
|
||||
|
||||
It "Should have a the proper fields and be populated" {
|
||||
$var = Get-Childitem .
|
||||
|
||||
$var.Name.Length | Should BeGreaterThan 0
|
||||
$var.Mode.Length | Should BeGreaterThan 0
|
||||
$var.LastWriteTime | Should BeGreaterThan 0
|
||||
$var.Length.Length | Should BeGreaterThan 0
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
Describe "Get-Content" {
|
||||
$testString = "This is a test content for a file"
|
||||
$nl = [Environment]::NewLine
|
||||
|
||||
$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 -ItemType file -Force -Value $testString
|
||||
New-Item -Path $testPath2 -ItemType file -Force -Value $testString2
|
||||
}
|
||||
|
||||
It "Should throw an error on a directory " {
|
||||
# also tests that -erroraction SilentlyContinue will work.
|
||||
Get-Content . -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should return an Object when listing only a single line" {
|
||||
(Get-Content -Path $testPath).GetType().BaseType.Name | Should Be "Object"
|
||||
}
|
||||
|
||||
It "Should deliver an array object when listing a file with multiple lines" {
|
||||
(Get-Content -Path $testPath2).GetType().BaseType.Name | Should Be "Array"
|
||||
}
|
||||
|
||||
It "Should return the correct information from a file" {
|
||||
(Get-Content -Path $testPath) | Should Be $testString
|
||||
}
|
||||
|
||||
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,16 @@
|
||||
Describe "Get-Culture" {
|
||||
|
||||
It "Should return a type of CultureInfo for Get-Culture cmdlet" {
|
||||
|
||||
(Get-Culture).GetType() | Should Be CultureInfo
|
||||
|
||||
}
|
||||
|
||||
It "Should have $ culture variable be equivalent to (Get-Culture).Name" {
|
||||
|
||||
(Get-Culture).Name | Should Be $PsCulture
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
Describe "Get-Date" {
|
||||
It "Should return a DateTime object upon being called" {
|
||||
(Get-Date).GetType().Name.Equals('DateTime') | Should Be $true
|
||||
}
|
||||
|
||||
It "Should have colons when ToString method is used" {
|
||||
(Get-Date).ToString().Contains(":") | Should be $true
|
||||
(Get-Date -DisplayHint Time).ToString().Contains(":") | Should be $true
|
||||
(Get-Date -DisplayHint Date).ToString().Contains(":") | Should be $true
|
||||
}
|
||||
|
||||
It "Should be able to use the format flag" {
|
||||
# You would think that one could use simple loops here, but apparently powershell in windows returns different values in loops
|
||||
|
||||
(Get-Date -Format d).Contains("/") | Should be $true
|
||||
(Get-Date -Format D).Contains(",") | Should be $true
|
||||
(Get-Date -Format f).Contains(",") -and (Get-Date -Format f).Contains(":") | Should be $true
|
||||
(Get-Date -Format F).Contains(",") -and (Get-Date -Format F).Contains(":") | Should be $true
|
||||
(Get-Date -Format g).Contains("/") -and (Get-Date -Format g).Contains(":") | Should be $true
|
||||
(Get-Date -Format G).Contains("/") -and (Get-Date -Format G).Contains(":") | Should be $true
|
||||
(Get-Date -Format m).Contains(",") -or `
|
||||
(Get-Date -Format m).Contains(":") -or `
|
||||
(Get-Date -Format m).Contains("/") | Should be $false
|
||||
}
|
||||
|
||||
It "Should check that Get-Date can return the correct datetime from the system time" {
|
||||
$timeDifference = $(Get-Date).Subtract([System.DateTime]::Now)
|
||||
|
||||
$timeDifference.Days | Should Be 0
|
||||
$timeDifference.Hours | Should Be 0
|
||||
$timeDifference.Minutes | Should Be 0
|
||||
$timeDifference.Milliseconds | Should BeLessThan 1
|
||||
$timeDifference.Ticks | Should BeLessThan 10000
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
Describe "Get-Event" {
|
||||
|
||||
BeforeEach {
|
||||
( New-Event -SourceIdentifier PesterTestEvent -sender windows.timer -messagedata "PesterTestMessage" )
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
( Remove-Event -SourceIdentifier PesterTestEvent -ErrorAction SilentlyContinue )
|
||||
}
|
||||
|
||||
Context "Check return type of Get-Event" {
|
||||
|
||||
It "Should return PSEventArgs as return type of Get-Event" {
|
||||
{ ( Get-Event -SourceIdentifier PesterTestEvent ).GetType() | Should Be System.Management.Automation.PSEventArgs }
|
||||
}
|
||||
}
|
||||
|
||||
Context "Validate Get-Event can retrieve event" {
|
||||
|
||||
It "Should return at least 1 event" {
|
||||
{ (Get-Event).Count | Should BeGreaterThan 0 }
|
||||
}
|
||||
|
||||
It "Should return PesterTestMessage as the MessageData" {
|
||||
{ (Get-Event -SourceIdentifier PesterTimer).MessageData | Should Be "PesterTestMessage" }
|
||||
}
|
||||
|
||||
It "Should return Sender as windows.timer" {
|
||||
{ (Get-Event -SourceIdentifier PesterTimer).Sender | Should be windows.timer }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
Describe "Get-EventSubscriber" {
|
||||
|
||||
AfterEach {
|
||||
Unregister-Event -SourceIdentifier PesterTestRegister -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Context "Check return type of Get-EventSubscriber" {
|
||||
|
||||
It "Should return System.Management.Automation.PSEventSubscriber as return type of New-Event" {
|
||||
$pesterobject = (New-Object System.Collections.ObjectModel.ObservableCollection[object])
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber).GetType() | Should Be System.Management.Automation.PSEventSubscriber
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check Get-EventSubscriber can validly register events"{
|
||||
It "Should return source identifier of PesterTimer " {
|
||||
$pesterobject = (New-Object System.Collections.ObjectModel.ObservableCollection[object])
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber -SourceIdentifier PesterTestRegister).SourceIdentifier | Should Be "PesterTestRegister"
|
||||
}
|
||||
|
||||
It "Should return an integer greater than 0 for the SubscriptionId" {
|
||||
$pesterobject = (New-Object System.Collections.ObjectModel.ObservableCollection[object])
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber -SourceIdentifier PesterTestRegister).SubscriptionId | Should BeGreaterThan 0
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
Describe "Get-ExecutionPolicy"{
|
||||
|
||||
Context "Check return type of Get-ExecutionPolicy"{
|
||||
|
||||
It "Should return Microsoft.Powershell.ExecutionPolicy PSObject"{
|
||||
(Get-ExecutionPolicy).GetType() | Should Be Microsoft.Powershell.ExecutionPolicy
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
Describe "Get-FileHash" {
|
||||
New-Variable testDocument -Value $PSScriptRoot/assets/testablescript.ps1 -Scope Global -Force
|
||||
# The MACTripleDES and RIPEMD160 algorithms are unsupported at the moment
|
||||
$algorithms = @{"SHA1" ="01B865D143E07ECC875AB0EFC0A4429387FD0CF7";
|
||||
"SHA256" = "4A6DA9F1C0827143BB19FC4B0F2A8057BC1DF55F6D1F62FA3B917BA458E8F570";
|
||||
"SHA384" = "656215B6A07011E625206F43E57873F49AD7B36DFCABB70F6CDCE2303D7A603E55D052774D26F339A6D80A264340CB8C";
|
||||
"SHA512" = "C688C33027D89ACAC920545471C8053D8F64A54E21D0415F1E03766DDCDA215420E74FAFD1DC399864C6B6B5723A3358BD337339906797A39090B02229BF31FE";
|
||||
"MD5" = "7B09811D1631C9FD46B39D1D35522F0A";
|
||||
}
|
||||
|
||||
Context "Cmdlet result tests" {
|
||||
It "Should default to correct hash algorithm" {
|
||||
$result = Get-FileHash $testDocument
|
||||
|
||||
$result.Algorithm | Should Be "SHA256"
|
||||
}
|
||||
|
||||
It "Should be able to set the default hash algorithm" {
|
||||
$PSDefaultParameterValues.add("Get-FileHash:Algorithm","MD5")
|
||||
|
||||
$result = Get-FileHash $testDocument
|
||||
|
||||
$result.Algorithm | Should Be "MD5"
|
||||
|
||||
$PSDefaultParameterValues.Remove("Get-FileHash:Algorithm")
|
||||
}
|
||||
|
||||
It "Should list the path of the file under test" {
|
||||
$result = Get-FileHash $testDocument
|
||||
|
||||
# regular expression for the forward/backslashes for linux/windows compatability
|
||||
$result.Path | Should Match "assets[/|\\]testablescript.ps1"
|
||||
}
|
||||
}
|
||||
|
||||
Context "Algorithm tests" {
|
||||
It "Should be able to get the correct hash from each algorithm" {
|
||||
foreach ( $algorithm in $algorithms.Keys)
|
||||
{
|
||||
$algorithmResult = Get-FileHash $testDocument -Algorithm $algorithm
|
||||
|
||||
$algorithmResult.Hash | Should Be $algorithms[$algorithm]
|
||||
}
|
||||
|
||||
#MACTripleDES and RIPEMD160 are unsupported in the existing Pester tests from windows team
|
||||
{ Get-FileHash $testDocument -Algorithm MACTripleDES } | Should Throw "Algorithm 'MACTripleDES' is not supported in this system."
|
||||
{ Get-FileHash $testDocument -Algorithm RIPEMD160 } | Should Throw "Algorithm 'RIPEMD160' is not supported in this system."
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
Describe "Get-FormatData" {
|
||||
|
||||
Context "Check return type of Get-FormatData" {
|
||||
|
||||
It "Should return an object[] as the return type" {
|
||||
(Get-FormatData).GetType() | Should be System.Object[]
|
||||
}
|
||||
|
||||
It "Should return a System.Object[] with a count greater than 0" {
|
||||
(Get-formatData).Count | Should BeGreaterThan 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
Describe "Get-Host" {
|
||||
It "Should return a type of InternalHost for Get-Host cmdlet" {
|
||||
|
||||
(Get-Host).GetType().Name | Should Be InternalHost
|
||||
|
||||
}
|
||||
|
||||
It "Should have $ host variable be equivalent to Get-Host object" {
|
||||
|
||||
Get-Host | Should Be $host
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Get-Item" {
|
||||
It "Should list all the items in the current working directory when asterisk is used" {
|
||||
(Get-Item $here/*).GetType().BaseType | Should Be 'array'
|
||||
(Get-Item $here/*).GetType().Name | Should Be 'Object[]'
|
||||
}
|
||||
|
||||
It "Should return the name of the current working directory when a dot is used" {
|
||||
(Get-Item $here).GetType().BaseType | Should Be 'System.IO.FileSystemInfo'
|
||||
(Get-Item $here).Name | Should Be 'pester-tests'
|
||||
}
|
||||
|
||||
It "Should return the proper Name and BaseType for directory objects vs file system objects" {
|
||||
(Get-Item $here).GetType().Name | Should Be 'DirectoryInfo'
|
||||
(Get-Item $here/Get-Item.Tests.ps1).GetType().Name | Should Be 'FileInfo'
|
||||
}
|
||||
|
||||
It "Should have mode flags set" {
|
||||
ls $here | foreach-object { $_.Mode | Should Not BeNullOrEmpty }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Get-ItemProperty" {
|
||||
$currentDirectory = Split-Path $here -Leaf
|
||||
$parentDirectory = Split-Path $here/.. -Leaf
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
if ($isWindows)
|
||||
{
|
||||
$tempDirectory = "~/testfolder"
|
||||
$testProvider = "C"
|
||||
}
|
||||
else
|
||||
{
|
||||
$tempDirectory = "/tmp/testfolder"
|
||||
$testProvider = "/"
|
||||
}
|
||||
|
||||
New-Item $tempDirectory -ItemType Directory -Force
|
||||
$testfile = $tempDirectory + [Environment]::DirectorySeparatorChar + "testfile1"
|
||||
|
||||
New-Item $testfile -ItemType file -Force
|
||||
|
||||
It "Should be able to be called on in the current directory" {
|
||||
$(Get-ItemProperty $here).Name | Should Be $currentDirectory
|
||||
}
|
||||
|
||||
It "Should be able to be called on a parent directory" {
|
||||
(Get-ItemProperty $here/..).Name | Should Be $parentDirectory
|
||||
}
|
||||
|
||||
It "Should be able to be called on a directory using the path switch" {
|
||||
{ Get-ItemProperty -Path $tempDirectory } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to be called on a file using the path switch" {
|
||||
{ Get-ItemProperty -Path $testfile } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to access a property using the Path and name switches" {
|
||||
{ Get-ItemProperty -Path $testfile -Name fullname } | Should Not Throw
|
||||
|
||||
$output = Get-ItemProperty -Path $testfile -Name fullname
|
||||
|
||||
$output.PSPath | Should Not BeNullOrEmpty
|
||||
|
||||
$output.PSDrive | Should Be $testprovider
|
||||
|
||||
$output.PSProvider.Name | Should Be "FileSystem"
|
||||
}
|
||||
|
||||
It "Should be able to use the gp alias without error" {
|
||||
{ gp . } | Should Not Throw
|
||||
{ gp .. } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same results between alias and cmdlet" {
|
||||
$alias = gp -Path $testfile -Name fullname
|
||||
$cmdlet = Get-ItemProperty -Path $testfile -Name fullname
|
||||
|
||||
$alias.PSPath | Should Be $cmdlet.PSPath
|
||||
$alias.PSDrive | Should Be $cmdlet.PSDrive
|
||||
$alias.PSProvider.Name | Should Be $cmdlet.PSProvider.Name
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
Describe "Get-Location aka pwd" {
|
||||
<#Dependencies:
|
||||
pushd
|
||||
popd
|
||||
$HOME
|
||||
|
||||
#>
|
||||
$winHome = 'C:\Users\v-zafolw'
|
||||
$nixHome = '/home/zafolw'
|
||||
BeforeEach {
|
||||
pushd $HOME #on windows, this is c:\Users\XXXXX; for *nix, it's /home/XXXXX
|
||||
}
|
||||
|
||||
AfterEach { popd }
|
||||
|
||||
It "Should list the output of the current working directory" {
|
||||
(Get-Location).Path | Should Not BeNullOrEmpty
|
||||
(Get-Location).Path | Should Be ($winHome -or $nixHome)
|
||||
}
|
||||
|
||||
It "Should be able to use pwd the same way" {
|
||||
(pwd).Path | Should Not BeNullOrEmpty
|
||||
(pwd).Path | Should Be ($winHome -or $nixHome)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
Describe "Get-Member" {
|
||||
It "Should be able to be called on string objects, ints, arrays, etc" {
|
||||
$a = 1 #test numbers
|
||||
$b = 1.3
|
||||
$c = $false #test bools
|
||||
$d = @(1,3) # test arrays
|
||||
$e = "anoeduntodeu" #test strings
|
||||
$f = 'asntoheusth' #test strings
|
||||
|
||||
Get-Member -InputObject $a | Should Not BeNullOrEmpty
|
||||
Get-Member -InputObject $b | Should Not BeNullOrEmpty
|
||||
Get-Member -InputObject $c | Should Not BeNullOrEmpty
|
||||
Get-Member -InputObject $d | Should Not BeNullOrEmpty
|
||||
Get-Member -InputObject $e | Should Not BeNullOrEmpty
|
||||
Get-Member -InputObject $f | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to extract a field from string objects, ints, arrays, etc" {
|
||||
$a = 1 #test numbers
|
||||
$b = 1.3
|
||||
$c = $false #test bools
|
||||
$d = @(1,3) # test arrays
|
||||
$e = "anoeduntodeu" #test strings
|
||||
$f = 'asntoheusth' #test strings
|
||||
|
||||
$a.GetType().Name | Should Be 'Int32'
|
||||
$b.GetType().Name | Should Be 'Double'
|
||||
$c.GetType().Name | Should Be 'Boolean'
|
||||
$d.GetType().Name | Should Be 'Object[]'
|
||||
$e.GetType().Name | Should Be 'String'
|
||||
$f.GetType().Name | Should Be 'String'
|
||||
}
|
||||
|
||||
It "Should be able to be called on a newly created PSObject" {
|
||||
$o = New-Object psobject
|
||||
# this creates a dependency on the Add-Member cmdlet.
|
||||
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
|
||||
|
||||
Get-Member -InputObject $o | Should Not BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
Describe "Get-PSDrive" {
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
It "Should not throw" {
|
||||
Get-PSDrive | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should have a name and a length property" {
|
||||
(Get-PSDrive).Name | Should Not BeNullOrEmpty
|
||||
(Get-PSDrive).Root.Length | Should Not BeLessThan 1
|
||||
}
|
||||
|
||||
It "Should be able to be called with the gdr alias" {
|
||||
{ gdr } | Should Not Throw
|
||||
|
||||
gdr | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be the same output between Get-PSDrive and gdr" {
|
||||
$alias = gdr
|
||||
$actual = Get-PSDrive
|
||||
|
||||
$alias | Should Be $actual
|
||||
}
|
||||
|
||||
It "Should return drive info"{
|
||||
(Get-PSDrive Env).Name | Should Be Env
|
||||
(Get-PSDrive Cert).Root | Should Be \
|
||||
|
||||
if ($isWindows)
|
||||
{
|
||||
(Get-PSDrive C).Provider.Name | Should Be FileSystem
|
||||
}
|
||||
else
|
||||
{
|
||||
(Get-PSDrive /).Provider.Name | Should Be FileSystem
|
||||
}
|
||||
}
|
||||
|
||||
It "Should be able to access a drive using the PSProvider switch" {
|
||||
(Get-PSDrive -PSProvider FileSystem).Name.Length | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should return true that a drive that does not exist"{
|
||||
!(Get-PSDrive fake -ErrorAction SilentlyContinue) | Should Be $True
|
||||
Get-PSDrive fake -ErrorAction SilentlyContinue | Should BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
Describe "Get-PSProvider" {
|
||||
It "Should be able to call with no parameters without error" {
|
||||
{ Get-PSProvider } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call the filesystem provider" {
|
||||
{ Get-PSProvider FileSystem } | Should Not Throw
|
||||
|
||||
$actual = Get-PSProvider FileSystem
|
||||
|
||||
$actual.Name | Should Be "FileSystem"
|
||||
|
||||
$actual.Capabilities | Should Be "Filter, ShouldProcess, Credentials"
|
||||
}
|
||||
|
||||
It "Should be able to call a provider with a wildcard expression" {
|
||||
{ Get-PSProvider File*m } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to pipe the output" {
|
||||
$actual = Get-PSProvider
|
||||
|
||||
{ $actual | Format-List } | Should Not Throw
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
Describe "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 -1
|
||||
}
|
||||
|
||||
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 -101
|
||||
}
|
||||
|
||||
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,9 @@
|
||||
Describe "Get-RunspaceDebug" {
|
||||
|
||||
Context "Check return types of RunspaceDebug" {
|
||||
|
||||
It "Should return Microsoft.Powershell.Commands.PSRunspaceDebug as the return type" {
|
||||
(Get-RunspaceDebug).GetType() | Should Be Microsoft.Powershell.Commands.PSRunspaceDebug
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
Describe "Get-UICulture" {
|
||||
It "Should return a type of CultureInfo as the name of the type" {
|
||||
(Get-UICulture).GetType().Name | Should Match CultureInfo
|
||||
}
|
||||
|
||||
It "Should have $ PsUICulture variable be equivalent to Get-UICulture object" {
|
||||
(Get-UICulture).Name | Should Be $PsUICulture
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
Describe "Get-Unique" {
|
||||
$sortedList1 = 1,2,2,3,3,4,5
|
||||
It "Should be able to use the Get-Unique cmdlet without error with inputObject switch" {
|
||||
{ Get-Unique -InputObject $sortedList1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to uset the gu alias without error" {
|
||||
{ gu -InputObject $sortedList1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should output an array" {
|
||||
$(Get-Unique -InputObject $sortedList1).GetType().BaseType | Should Be Array
|
||||
}
|
||||
|
||||
It "Should output an array of unchanged items when the InputObject switch is used" {
|
||||
$actual = Get-Unique -InputObject $sortedList1
|
||||
|
||||
$actual[0] | Should Be $sortedList1[0]
|
||||
$actual[1] | Should Be $sortedList1[1]
|
||||
$actual[2] | Should Be $sortedList1[2]
|
||||
$actual[3] | Should Be $sortedList1[3]
|
||||
$actual[4] | Should Be $sortedList1[4]
|
||||
$actual[5] | Should Be $sortedList1[5]
|
||||
$actual[6] | Should Be $sortedList1[6]
|
||||
|
||||
$actual.Length | Should Be 7
|
||||
}
|
||||
|
||||
It "Should accept piped input" {
|
||||
{ $actualOutput = $sortedList1 | Get-Unique } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the expected output when piped input is used" {
|
||||
$actualOutput = $sortedList1 | Get-Unique
|
||||
$expectedOutput = 1,2,3,4,5
|
||||
|
||||
$actualOutput.Length | Should Be $expectedOutput.Length
|
||||
|
||||
$actualOutput[0] | Should Be $expectedOutput[0]
|
||||
$actualOutput[1] | Should Be $expectedOutput[1]
|
||||
$actualOutput[2] | Should Be $expectedOutput[2]
|
||||
$actualOutput[3] | Should Be $expectedOutput[3]
|
||||
$actualOutput[4] | Should Be $expectedOutput[4]
|
||||
}
|
||||
|
||||
It "Should be able to input a collection in the inputObject switch" {
|
||||
$collection = "a", "b", "b", "d"
|
||||
$actual = Get-Unique -InputObject $collection
|
||||
|
||||
$actual.Length | Should Be $collection.Length
|
||||
|
||||
$actual[0] | Should Be $collection[0]
|
||||
$actual[1] | Should Be $collection[1]
|
||||
$actual[2] | Should Be $collection[2]
|
||||
$actual[3] | Should Be $collection[3]
|
||||
}
|
||||
|
||||
It "Should get the unique items when piped collection input is used" {
|
||||
$collection = "a", "b", "b", "d"
|
||||
$expectedOutput = "a", "b", "d"
|
||||
|
||||
$actual = Get-Unique -InputObject $collection
|
||||
|
||||
$actual.Length | Should Be $collection.Length
|
||||
|
||||
$actual[0] | Should Be $collection[0]
|
||||
$actual[1] | Should Be $collection[1]
|
||||
$actual[2] | Should Be $collection[2]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
Describe "Get-Variable" {
|
||||
It "Should be able to call with no parameters without error" {
|
||||
{ Get-Variable } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return environment variables when called with no parameters" {
|
||||
(Get-Variable).Name -contains "$" | Should Be $true
|
||||
(Get-Variable).Name -contains "?" | Should Be $true
|
||||
(Get-Variable).Name -contains "HOST" | Should Be $true
|
||||
(Get-Variable).Name -contains "PWD" | Should Be $true
|
||||
(Get-Variable).Name -contains "PID" | Should Be $true
|
||||
(Get-Variable).Name -contains "^" | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return the value of an object" {
|
||||
New-Variable -Name tempVar -Value 1
|
||||
(Get-Variable tempVar).Value | Should Be (1)
|
||||
}
|
||||
|
||||
It "Should be able to call using the gv alias" {
|
||||
{ gv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call using the Name switch" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
|
||||
{ Get-Variable -Name var1 } | Should Not Throw
|
||||
|
||||
(Get-Variable -Name var1).Value | Should Be 4
|
||||
|
||||
Remove-Variable var1
|
||||
}
|
||||
|
||||
It "Should be able to use wildcard characters in the Name field" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
New-Variable -Name var2 -Value "test"
|
||||
|
||||
(Get-Variable -Name var*).Value[0] | Should be 4
|
||||
(Get-Variable -Name var*).Value[1] | Should be "test"
|
||||
|
||||
Remove-Variable var1
|
||||
Remove-Variable var2
|
||||
}
|
||||
|
||||
It "Should return only the value if the value switch is used" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
|
||||
Get-Variable -Name var1 -ValueOnly | Should be 4
|
||||
|
||||
Remove-Variable var1
|
||||
}
|
||||
|
||||
It "Should pipe string to the name field without the Name field being specified"{
|
||||
New-Variable -Name var1 -Value 3
|
||||
|
||||
("var1" | Get-Variable ).Value | Should Be 3
|
||||
|
||||
Remove-Variable var1
|
||||
}
|
||||
|
||||
It "Should be able to include a set of variables to get" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
New-Variable -Name var2 -Value 2
|
||||
|
||||
$actual = Get-Variable -Include var1, var2
|
||||
|
||||
$actual[0].Name | Should Be var1
|
||||
$actual[1].Name | Should Be var2
|
||||
|
||||
$actual[0].Value | Should Be 4
|
||||
$actual[1].Value | Should Be 2
|
||||
|
||||
Remove-Variable var1
|
||||
Remove-Variable var2
|
||||
}
|
||||
|
||||
It "Should be able to exclude a set of variables to get" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
New-Variable -Name var2 -Value 2
|
||||
New-Variable -Name var3 -Value "test"
|
||||
|
||||
$actual = Get-Variable -Exclude var1, var2
|
||||
|
||||
$actual | Where-Object { $_.Name -eq "var3" } | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
Context "Scope Tests" {
|
||||
# This will violate the DRY principle. Tread softly.
|
||||
It "Should be able to get a global scope variable using the global switch" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
|
||||
(Get-Variable -Name globalVar -Scope global)[0].Value | Should Be 1
|
||||
}
|
||||
|
||||
It "Should not be able to clear a global scope variable using the local switch" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
|
||||
Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able to get a global variable when there's one in the script scope" {
|
||||
New-Variable globalVar -Value 1 -Scope global -Force
|
||||
{ New-Variable globalVar -Value 2 -Scope script -Force }
|
||||
|
||||
$(Get-Variable -Name globalVar).Value | Should Be 1
|
||||
}
|
||||
|
||||
It "Should be able to get an item locally using the local switch" {
|
||||
{
|
||||
New-Variable localVar -Value 1 -Scope local -Force
|
||||
|
||||
Get-Variable -Name localVar -Scope local
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to get a variable created in the global scope when there's one in local scope" {
|
||||
New-Variable localVar -Value 1 -Scope local -Force
|
||||
|
||||
New-Variable localVar -Value 2 -Scope global -Force
|
||||
|
||||
$(Get-Variable -Name localVar -Scope global).Value | Should Be 2
|
||||
}
|
||||
|
||||
It "Should be able to get a script variable created using the script switch" {
|
||||
{
|
||||
New-Variable scriptVar -Value 1 -Scope script -Force
|
||||
|
||||
Get-Variable -Name scriptVar -Scope script
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to clear a global script variable that was created using the script scope switch" {
|
||||
{
|
||||
New-Variable scriptVar -Value 1 -Scope script -Force
|
||||
|
||||
Get-Variable -Name scriptVar -Scope script
|
||||
} | Should Not Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
Describe "Group-Object" {
|
||||
$testObject = Get-ChildItem
|
||||
|
||||
It "Should be called using an object as piped without error with no switches" {
|
||||
{$testObject | Group-Object } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be called using the InputObject without error with no other switches" {
|
||||
{ Group-Object -InputObject $testObject } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return three columns- count, name, and group" {
|
||||
$actual = Group-Object -InputObject $testObject
|
||||
|
||||
$actual.Count | Should BeGreaterThan 0
|
||||
$actual.Name.Count | Should BeGreaterThan 0
|
||||
$actual.Group.Count | Should BeGreaterThan 0
|
||||
|
||||
}
|
||||
|
||||
It "Should use the group alias" {
|
||||
{ group -InputObject $testObject } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should create a collection when the inputObject parameter is used" {
|
||||
$actualParam = Group-Object -InputObject $testObject
|
||||
|
||||
$actualParam.Group.GetType().Name | Should Be "Collection``1"
|
||||
}
|
||||
|
||||
It "Should output an array when piped input is used" {
|
||||
$actual = $testObject | Group-Object
|
||||
|
||||
$actual.Group.GetType().BaseType | Should Be Array
|
||||
}
|
||||
|
||||
It "Should have the same output between the group alias and the group-object cmdlet" {
|
||||
$actualAlias = group -InputObject $testObject
|
||||
$actualCmdlet = Group-Object -InputObject $testObject
|
||||
|
||||
$actualAlias.Name[0] | Should Be $actualCmdlet.Name[0]
|
||||
$actualAlias.Group[0] | Should Be $actualCmdlet.Group[0]
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to use the property switch without error" {
|
||||
{ $testObject | Group-Object -Property Attributes } | Should Not Throw
|
||||
|
||||
$actual = $testObject | Group-Object -Property Attributes
|
||||
|
||||
$actual.Group.Count | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should be able to use the property switch on multiple properties without error" {
|
||||
{ $testObject | Group-Object -Property Attributes, Length }
|
||||
|
||||
$actual = $testObject | Group-Object -Property Attributes, Length
|
||||
|
||||
$actual.Group.Count | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should be able to omit members of a group using the NoElement switch without error" {
|
||||
{ $testObject | Group-Object -NoElement } | Should Not Throw
|
||||
|
||||
($testObject | Group-Object -NoElement).Group | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to output a hashtable datatype" {
|
||||
$actual = $testObject | Group-Object -AsHashTable
|
||||
|
||||
$actual.GetType().Name | Should be "Hashtable"
|
||||
}
|
||||
|
||||
It "Should be able to access when output as hash table" {
|
||||
$actual = $testObject | Group-Object -AsHashTable
|
||||
|
||||
$actual.Keys | Should Not BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should throw when attempting to use AsString without AsHashTable" {
|
||||
{ $testObject | Group-Object -AsString } | Should Throw
|
||||
}
|
||||
|
||||
It "Should not throw error when using AsString when the AsHashTable was added" {
|
||||
{ $testObject | Group-Object -AsHashTable -AsString } | Should Not Throw
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Import-Alias" {
|
||||
$pesteraliasfile = "$here/assets/pesteralias.txt"
|
||||
|
||||
Context "Validate ability to import alias file" {
|
||||
|
||||
It "Should be able to import an alias file successfully" {
|
||||
{ Import-Alias $pesteraliasfile } | Should Not throw
|
||||
}
|
||||
|
||||
It "Should be able to import file via the Import-Alias alias of ipal" {
|
||||
{ ipal $pesteraliasfile } | Should Not throw
|
||||
}
|
||||
|
||||
It "Should be able to import an alias file and perform imported aliased echo cmd" {
|
||||
(Import-Alias $pesteraliasfile)
|
||||
(pesterecho pestertesting) | Should Be "pestertesting"
|
||||
}
|
||||
|
||||
It "Should be able to use ipal alias to import an alias file and perform cmd" {
|
||||
(ipal $pesteraliasfile)
|
||||
(pesterecho pestertesting) | Should be "pestertesting"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Import-Csv" {
|
||||
$testCsv = "$here/assets/TestCsv.csv"
|
||||
|
||||
It "Should be able to call without error" {
|
||||
{ Import-Csv $testCsv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to assign to a variable" {
|
||||
$actual = Import-Csv $testCsv
|
||||
|
||||
$actual | Should Not BeNullOrEmpty
|
||||
$actual.GetType().BaseType | Should Be array
|
||||
}
|
||||
|
||||
It "Should have the data from the csv file" {
|
||||
$actualContent = $(Get-Content $testCsv)[0]
|
||||
$testContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
|
||||
|
||||
$actualContent.IndexOf($testContent) | Should BeGreaterThan -1
|
||||
}
|
||||
|
||||
It "Should be able to prepend a custom header" {
|
||||
$header = "test1","test2","test3"
|
||||
|
||||
$originalContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
|
||||
|
||||
$testContent = $($(Import-Csv $testCsv -Header $header) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 3
|
||||
|
||||
# the original csv file doesn't contain the headers
|
||||
$originalContent.IndexOf($header[0]) | Should Be -1
|
||||
|
||||
# but it does with the -Header switch!
|
||||
$testContent[0] | Should Be $header[0]
|
||||
$testContent[1] | Should Be $header[1]
|
||||
$testContent[2] | Should Be $header[2]
|
||||
}
|
||||
|
||||
It "Should be able to use the alias without error" {
|
||||
{ Import-Csv $testCsv } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same output between the alias and the full cmdlet name" {
|
||||
$alias = $($(ipcsv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
|
||||
$cmdlet = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
|
||||
|
||||
$alias[0] | Should Be $cmdlet[0]
|
||||
$alias[1] | Should Be $cmdlet[1]
|
||||
$alias[2] | Should Be $cmdlet[2]
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Invoke-Expression" {
|
||||
|
||||
Context "Should execute the invoked command validly" {
|
||||
|
||||
It "Should return the echoed text" {
|
||||
(Invoke-Expression -command "echo pestertest1") | Should be "pestertest1"
|
||||
}
|
||||
|
||||
It "Should return the echoed text for the alias" {
|
||||
(iex -command "echo pestertest2") | Should Be "pestertest2"
|
||||
}
|
||||
|
||||
It "Should return the echoed text from a script" {
|
||||
$testfile = "$here/assets/echoscript.ps1"
|
||||
$testcommand = "echo pestertestscript"
|
||||
$testcommand | Add-Content -Path $testfile
|
||||
(Invoke-Expression $testfile) | Should Be "pestertestscript"
|
||||
Remove-Item $testfile
|
||||
}
|
||||
|
||||
It "Should return the echoed text from a script from the alias" {
|
||||
$testfile = "$here/assets/echoscript.ps1"
|
||||
$testcommand = "echo pestertestscript"
|
||||
$testcommand | Add-Content -Path $testfile
|
||||
(iex $testfile) | Should Be "pestertestscript"
|
||||
Remove-Item $testfile
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
# While Core PowerShell does not support the JSON cmdlets, a third
|
||||
# party C# library, [Json.NET](http://www.newtonsoft.com/json), can be
|
||||
# loaded into PowerShell and used directly.
|
||||
|
||||
# http://www.newtonsoft.com/json/help/html/ParsingLINQtoJSON.htm
|
||||
|
||||
Describe "Json.NET LINQ Parsing" {
|
||||
# load third party Json.NET library
|
||||
$base = [System.AppContext]::BaseDirectory
|
||||
$path = Join-Path $base Newtonsoft.Json.dll
|
||||
[Microsoft.PowerShell.CoreCLR.AssemblyExtensions]::LoadFrom($path)
|
||||
|
||||
BeforeEach {
|
||||
$jsonFile = "$here/assets/TestJson.json"
|
||||
$jsonData = (Get-Content $jsonFile | Out-String)
|
||||
$json = [Newtonsoft.Json.Linq.JObject]::Parse($jsonData)
|
||||
}
|
||||
|
||||
It "Should return data via Item()" {
|
||||
[string]$json.Item("Name") | Should Be "Zaphod Beeblebrox"
|
||||
}
|
||||
|
||||
It "Should return data via []" {
|
||||
[string]$json["Planet"] | Should Be "Betelgeuse"
|
||||
}
|
||||
|
||||
It "Should return nested data via Item().Item()" {
|
||||
[int]$json.Item("Appendages").Item("Heads") | Should Be 2
|
||||
}
|
||||
|
||||
It "Should return nested data via [][]" {
|
||||
[int]$json["Appendages"]["Arms"] | Should Be 3
|
||||
}
|
||||
|
||||
It "Should return correct array count" {
|
||||
$json["Achievements"].Count | Should Be 4
|
||||
}
|
||||
|
||||
It "Should return array data via [n]" {
|
||||
[string]$json["Achievements"][3] | Should Be "One hoopy frood"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Measure-Command" {
|
||||
|
||||
Context "Validate return types for Measure-Command" {
|
||||
|
||||
It "Should return TimeSpan as the return type" {
|
||||
(Measure-Command { Get-Date }).GetType() | Should Be timespan
|
||||
}
|
||||
}
|
||||
|
||||
Context "Validate that it is executing commands correctly" {
|
||||
|
||||
It "Should return TimeSpan after executing a script" {
|
||||
(Measure-Command { echo hi }).GetType() | Should Be timespan
|
||||
}
|
||||
|
||||
It "Should return TimeSpan after executing a cmdlet" {
|
||||
$pesterscript = "$here/assets/echoscript.ps1"
|
||||
$testfile = "$here/assets/echoscript.ps1"
|
||||
$testcommand = "echo pestertestscript"
|
||||
$testcommand | Add-Content -Path $testfile
|
||||
|
||||
(Measure-Command { $pesterscript }).GetType() | Should Be timespan
|
||||
Remove-Item $testfile
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
Describe "Measure-Object" {
|
||||
$testObject = 1,3,4
|
||||
|
||||
It "Should be able to be called without error" {
|
||||
{ Measure-Object | Out-Null } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call on piped input" {
|
||||
{ $testObject | Measure-Object } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to count the number of objects input to it" {
|
||||
$($testObject | Measure-Object).Count | Should Be $testObject.Length
|
||||
}
|
||||
|
||||
It "Should be able to count using the Property switch" {
|
||||
$expected = $(Get-ChildItem).Length
|
||||
$actual = $(Get-ChildItem | Measure-Object -Property Length).Count
|
||||
|
||||
$actual | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to get additional stats" {
|
||||
$actual = Get-Process | Measure-Object -Property workingset64 -Minimum -Maximum -Average
|
||||
|
||||
$actual.Average | Should BeGreaterThan 0
|
||||
$actual.Characters | Should BeNullOrEmpty
|
||||
$actual.Lines | Should BeNullOrEmpty
|
||||
$actual.Maximum | Should Not BeNullOrEmpty
|
||||
$actual.Minimum | Should Not BeNullOrEmpty
|
||||
$actual.Sum | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
Context "Numeric tests" {
|
||||
It "Should be able to sum" {
|
||||
$actual = $testObject | Measure-Object -Sum
|
||||
$expected = 0
|
||||
|
||||
foreach ( $obj in $testObject )
|
||||
{
|
||||
$expected += $obj
|
||||
}
|
||||
|
||||
$actual.Sum | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to average" {
|
||||
$actual = $testObject | Measure-Object -Average
|
||||
$expected = 0
|
||||
|
||||
foreach ( $obj in $testObject )
|
||||
{
|
||||
$expected += $obj
|
||||
}
|
||||
|
||||
$expected /= $testObject.length
|
||||
|
||||
$actual.Average | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to return a minimum" {
|
||||
$actual = $testObject | Measure-Object -Minimum
|
||||
$expected = $testObject[0]
|
||||
|
||||
for ($i=0; $i -lt $testObject.length; $i++)
|
||||
{
|
||||
if ( $testObject[$i] -lt $expected )
|
||||
{
|
||||
|
||||
$expected = $testObject[$i]
|
||||
}
|
||||
}
|
||||
|
||||
$actual.Minimum | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to return a minimum when multiple objects are the minimum" {
|
||||
$testMinimum = 1,1,2,4
|
||||
$actual = $testMinimum | Measure-Object -Minimum
|
||||
$expected = $testMinimum[0]
|
||||
|
||||
for ($i=1; $i -lt $testMinimum.length; $i++)
|
||||
{
|
||||
if ( $testMinimum[$i] -lt $expected )
|
||||
{
|
||||
|
||||
$expected = $testMinimum[$i]
|
||||
}
|
||||
}
|
||||
|
||||
$actual.Minimum | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to return a maximum" {
|
||||
$actual = $testObject | Measure-Object -Maximum
|
||||
$expected = $testObject[0]
|
||||
|
||||
for ($i=1; $i -lt $testObject.length; $i++)
|
||||
{
|
||||
if ( $testObject[$i] -gt $expected )
|
||||
{
|
||||
|
||||
$expected = $testObject[$i]
|
||||
}
|
||||
}
|
||||
|
||||
$actual.Maximum | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to return a maximum when multiple objects are the maximum" {
|
||||
$testMaximum = 1,3,5,5
|
||||
$actual = $testMaximum | Measure-Object -Maximum
|
||||
$expected = $testMaximum[0]
|
||||
|
||||
for ($i=1; $i -lt $testMaximum.length; $i++)
|
||||
{
|
||||
if ( $testMaximum[$i] -gt $expected )
|
||||
{
|
||||
|
||||
$expected = $testMaximum[$i]
|
||||
}
|
||||
}
|
||||
|
||||
$actual.Maximum | Should Be $expected
|
||||
}
|
||||
}
|
||||
|
||||
Context "String tests" {
|
||||
$nl = [Environment]::NewLine
|
||||
|
||||
$testString = "HAD I the heavens’ embroidered cloths,$nl Enwrought with golden and silver light,$nl The blue and the dim and the dark cloths$nl Of night and light and the half light,$nl I would spread the cloths under your feet:$nl But I, being poor, have only my dreams;$nl I have spread my dreams under your feet;$nl Tread softly because you tread on my dreams."
|
||||
|
||||
It "Should be able to count the number of words in a string" {
|
||||
$expectedLength = $testString.Replace($nl,"").Split().length
|
||||
$actualLength = $testString | Measure-Object -Word
|
||||
|
||||
$actualLength.Words | Should Be $expectedLength
|
||||
}
|
||||
|
||||
It "Should be able to count the number of characters in a string" {
|
||||
$expectedLength = $testString.length
|
||||
$actualLength = $testString | Measure-Object -Character
|
||||
|
||||
$actualLength.Characters | Should Be $expectedLength
|
||||
}
|
||||
|
||||
It "Should be able to count the number of lines in a string" {
|
||||
$expectedLength = $testString.Split($nl).length
|
||||
$actualLength = $testString | Measure-Object -Line
|
||||
|
||||
$actualLength.Lines | Should Be $expectedLength
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
Describe "New-Alias" {
|
||||
It "Should be able to be called using the name and value parameters without error" {
|
||||
{ New-Alias -Name testAlias -Value 100 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same output between the alias and the original cmdlet" {
|
||||
New-Alias -Name testAlias -Value Get-Process
|
||||
|
||||
$aliasId = $(testAlias).Id
|
||||
$cmdletId = $(Get-Process).Id
|
||||
foreach ($IdNumber in $aliasId)
|
||||
{
|
||||
$aliasId[$IdNumber] | Should Be $cmdletId[$IdNumber]
|
||||
}
|
||||
}
|
||||
|
||||
It "Should be able to call the New-Alias cmdlet using the nal alias without error" {
|
||||
{ nal -Name testAlias -Value 100 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same output between the nal alias and the New-Alias cmdlet" {
|
||||
nal -Name testAlias -Value Get-Process
|
||||
|
||||
New-Alias -Name testalias2 -Value Get-Process
|
||||
|
||||
$aliasData = $(testAlias).Id
|
||||
$cmdletData = $(testAlias2).Id
|
||||
|
||||
foreach ($IdNumber in $aliasData)
|
||||
{
|
||||
$aliasData[$IdNumber] | Should Be $cmdletData[$IdNumber]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
Describe "New-Event" {
|
||||
|
||||
Context "Check return type of New-Event" {
|
||||
|
||||
It "Should return PSEventArgs as return type of New-Event" {
|
||||
(New-Event -SourceIdentifier a).GetType() | Should Be System.Management.Automation.PSEventArgs
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check New-Event can register an event"{
|
||||
It "Should return PesterTestMessage as the MessageData" {
|
||||
(New-Event -sourceidentifier PesterTimer -sender windows.timer -messagedata "PesterTestMessage")
|
||||
(Get-Event -SourceIdentifier PesterTimer).MessageData | Should Be "PesterTestMessage"
|
||||
Remove-Event -sourceidentifier PesterTimer
|
||||
}
|
||||
|
||||
It "Should return Sender as windows.timer" {
|
||||
(New-Event -sourceidentifier PesterTimer -sender windows.timer -messagedata "PesterTestMessage")
|
||||
(Get-Event -SourceIdentifier PesterTimer).Sender | Should be windows.timer
|
||||
Remove-Event -sourceIdentifier PesterTimer
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
function Clean-State
|
||||
{
|
||||
if (Test-Path $FullyQualifiedFile)
|
||||
{
|
||||
Remove-Item $FullyQualifiedFile -Force
|
||||
}
|
||||
|
||||
if (Test-Path $FullyQualifiedFolder)
|
||||
{
|
||||
Remove-Item $FullyQualifiedFolder -Recurse -Force
|
||||
}
|
||||
}
|
||||
|
||||
Describe "New-Item" {
|
||||
$tmpDirectory = "/tmp"
|
||||
$testfile = "testfile.txt"
|
||||
$testfolder = "newDirectory"
|
||||
$FullyQualifiedFile = $tmpDirectory + "/" + $testfile
|
||||
$FullyQualifiedFolder = $tmpDirectory + "/" + $testfolder
|
||||
|
||||
BeforeEach {
|
||||
Clean-State
|
||||
}
|
||||
|
||||
It "should call the function without error" {
|
||||
{ New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should create a file without error" {
|
||||
New-Item -Name $testfile -Path $tmpDirectory -ItemType file
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
}
|
||||
|
||||
It "Should create a folder without an error" {
|
||||
New-Item -Name newDirectory -Path $tmpDirectory -ItemType directory
|
||||
|
||||
Test-Path $FullyQualifiedFolder | Should Be $true
|
||||
}
|
||||
|
||||
It "Should create a file using the ni alias" {
|
||||
ni -Name $testfile -Path $tmpDirectory -ItemType file
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
}
|
||||
|
||||
It "Should create a file using the Type alias instead of ItemType" {
|
||||
New-Item -Name $testfile -Path $tmpDirectory -Type file
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
}
|
||||
|
||||
It "Should create a file with sample text inside the file using the Value switch" {
|
||||
$expected = "This is test string"
|
||||
New-Item -Name $testfile -Path $tmpDirectory -ItemType file -Value $expected
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
|
||||
Get-Content $FullyQualifiedFile | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should not create a file when the Name switch is not used and only a directory specified" {
|
||||
#errorAction used because permissions issue in windows
|
||||
New-Item -Path $tmpDirectory -ItemType file -ErrorAction SilentlyContinue
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $false
|
||||
|
||||
}
|
||||
|
||||
It "Should create a file when the Name switch is not used but a fully qualified path is specified" {
|
||||
New-Item -Path $FullyQualifiedFile -ItemType file
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to create a multiple items in different directories" {
|
||||
$FullyQualifiedFile2 = $tmpDirectory + "/" + "test2.txt"
|
||||
New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $true
|
||||
Test-Path $FullyQualifiedFile2 | Should Be $true
|
||||
|
||||
Remove-Item $FullyQualifiedFile2
|
||||
}
|
||||
|
||||
It "Should be able to call the whatif switch without error" {
|
||||
{ New-Item -Name testfile.txt -Path /tmp -ItemType file -WhatIf } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not create a new file when the whatif switch is used" {
|
||||
New-Item -Name $testfile -Path $tmpDirectory -ItemType file -WhatIf
|
||||
|
||||
Test-Path $FullyQualifiedFile | Should Be $false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
Describe "New-Object" {
|
||||
It "should create an object with 4 fields" {
|
||||
$o = New-Object psobject
|
||||
$val = $o.GetType()
|
||||
|
||||
$val.IsPublic | Should Not BeNullOrEmpty
|
||||
$val.Name | Should Not BeNullOrEmpty
|
||||
$val.IsSerializable | Should Not BeNullOrEmpty
|
||||
$val.BaseType | Should Not BeNullOrEmpty
|
||||
|
||||
$val.IsPublic | Should Be $true
|
||||
$val.IsSerializable | Should Be $false
|
||||
$val.Name | Should Be 'PSCustomObject'
|
||||
$val.BaseType | Should Be 'System.Object'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
Describe "New-TimeSpan" {
|
||||
It "Should be able to create a new timespan object" {
|
||||
New-Variable -Name testObject -Value $(New-TimeSpan)
|
||||
|
||||
$testObject.GetType() | Should Be timespan
|
||||
}
|
||||
|
||||
Context "Core Functionality Tests" {
|
||||
New-Variable -Name testObject -Value $(New-TimeSpan -Days 2 -Hours 23 -Minutes 4 -Seconds 3) -Force
|
||||
|
||||
$expectedOutput = @{ "Days" = "2";
|
||||
"Hours" = "23";
|
||||
"Minutes" = "4";
|
||||
"Seconds" = "3";
|
||||
"Milliseconds" = "0";
|
||||
"Ticks" = "2558430000000";
|
||||
"TotalDays" = "2.96114583333333";
|
||||
"TotalHours" = "71.0675";
|
||||
"TotalMinutes" = "4264.05";
|
||||
"TotalSeconds" = "255843";
|
||||
"TotalMilliseconds" = "255843000"
|
||||
}
|
||||
|
||||
$TEN_MILLION = 10000000
|
||||
|
||||
It "Should have expected values for time properties set during creation" {
|
||||
$testObject.Days | Should Be $expectedOutput["Days"]
|
||||
$testObject.Hours | Should Be $expectedOutput["Hours"]
|
||||
$testObject.Minutes | Should Be $expectedOutput["Minutes"]
|
||||
$testObject.Seconds | Should Be $expectedOutput["Seconds"]
|
||||
$testObject.Ticks | Should Be $expectedOutput["Ticks"]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
It "Should have matching output when using the Start switch vs piping from another cmdlet" {
|
||||
# this file is guarenteed to exist
|
||||
$inputObject = (Get-ChildItem $PSScriptRoot/New-TimeSpan.Tests.ps1)
|
||||
$inputParameter = New-TimeSpan -Start $inputObject.lastwritetime
|
||||
$pipedInput = $inputObject | New-TimeSpan
|
||||
|
||||
$difference = [math]::Abs($inputParameter.Milliseconds - $pipedInput.Milliseconds)
|
||||
# The difference between commands should be minimal
|
||||
$difference | Should BeLessThan 10
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
Describe "New-Variable" {
|
||||
$nl = [Environment]::NewLine
|
||||
|
||||
It "Should create a new variable with no parameters" {
|
||||
{ New-Variable var1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to set variable name using the Name parameter" {
|
||||
{ New-Variable -Name var1 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to assign a value to a variable using the value switch" {
|
||||
New-Variable var1 -Value 4
|
||||
|
||||
$var1 | Should Be 4
|
||||
}
|
||||
|
||||
It "Should be able to assign a value to a new variable without using the value switch" {
|
||||
New-Variable var1 "test"
|
||||
|
||||
$var1 | Should Be "test"
|
||||
}
|
||||
|
||||
It "Should assign a description to a new variable using the description switch" {
|
||||
New-Variable var1 100 -Description "Test Description"
|
||||
|
||||
(Get-Variable var1).Description | Should Be "Test Description"
|
||||
}
|
||||
|
||||
|
||||
It "Should be able to be called with the nv alias" {
|
||||
{ nv var1 } | Should Not Throw
|
||||
{ nv var1 2 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not be able to set the name of a new variable to that of an old variable within same scope when the Force switch is missing" {
|
||||
New-Variable var1
|
||||
(New-Variable var1 -ErrorAction SilentlyContinue) | Should Throw
|
||||
}
|
||||
|
||||
It "Should change the value of an already existing variable using the Force switch" {
|
||||
New-Variable var1 -Value 1
|
||||
|
||||
$var1 | Should Be 1
|
||||
|
||||
New-Variable var1 -Value 2 -Force
|
||||
|
||||
$var1 | Should Be 2
|
||||
$var1 | Should Not Be 1
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to set the value of a variable by piped input" {
|
||||
$in = "value"
|
||||
|
||||
$in | New-Variable -Name var1
|
||||
|
||||
$var1 | Should Be $in
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to pipe object properties to output using the PassThru switch" {
|
||||
$in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru
|
||||
|
||||
$output = $in | Format-List -Property Description | Out-String
|
||||
|
||||
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
|
||||
}
|
||||
|
||||
It "Should be able to set the value using the value switch" {
|
||||
New-Variable -Name var1 -Value 2
|
||||
|
||||
$var1 | Should Be 2
|
||||
}
|
||||
|
||||
Context "Option tests" {
|
||||
BeforeEach {
|
||||
# verify that the test variable doesn't exist
|
||||
!!$var1 | Should Be $false
|
||||
}
|
||||
It "Should be able to use the options switch without error" {
|
||||
{ New-Variable -Name var1 -Value 2 -Option Unspecified } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should default to none as the value for options" {
|
||||
New-Variable -Name var2 -Value 4 -PassThru| Format-List | Out-String | Should Match "Options : None"
|
||||
}
|
||||
|
||||
It "Should be able to set ReadOnly option" {
|
||||
{ New-Variable -Name var1 -Value 2 -Option ReadOnly } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not be able to change variable created using the ReadOnly option when the Force switch is not used" {
|
||||
New-Variable -Name var1 -Value 1 -Option ReadOnly
|
||||
|
||||
Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue
|
||||
|
||||
$var1 | Should Not Be 2
|
||||
}
|
||||
|
||||
It "Should be able to set a new variable to constant" {
|
||||
{ New-Variable -Name var1 -Option Constant } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not be able to change an existing variable to constant" {
|
||||
New-Variable -Name var1 -Value 1 -PassThru
|
||||
|
||||
Set-Variable -Name var1 -Option Constant -ErrorAction SilentlyContinue
|
||||
|
||||
$var1 | Format-List | Out-String | Should Not Match "Options : None"
|
||||
|
||||
}
|
||||
|
||||
It "Should not be able to delete a constant variable" {
|
||||
New-Variable -Name var1 -Value 2 -Option Constant
|
||||
|
||||
Remove-Variable -Name var1 -ErrorAction SilentlyContinue
|
||||
|
||||
!!$var1 | Should Be $true
|
||||
|
||||
$var1 | Should Be 2
|
||||
}
|
||||
|
||||
It "Should not be able to change a constant variable" {
|
||||
New-Variable -Name var1 -Value 1 -Option Constant
|
||||
|
||||
Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue
|
||||
|
||||
$var1 | Should Not Be 2
|
||||
}
|
||||
|
||||
It "Should be able to create a variable as private without error" {
|
||||
{ New-Variable -Name var1 -Option Private } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to see the value of a private variable when within scope" {
|
||||
|
||||
New-Variable -Name var1 -Value 100 -Option Private
|
||||
|
||||
$var1 | Should Be 100
|
||||
|
||||
}
|
||||
|
||||
It "Should not be able to see the value of a private variable when out of scope" {
|
||||
{
|
||||
New-Variable -Name var1 -Value 1 -Option Private
|
||||
}
|
||||
|
||||
$var1 | Should Be # Nothing, since it's not defined in scope
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to use the AllScope switch without error" {
|
||||
{ New-Variable -Name var1 -Option AllScope } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to see variable created using the AllScope switch in a child scope" {
|
||||
New-Variable -Name var1 -Value 1 -Option AllScope
|
||||
function myFunction {
|
||||
if ( $var1 -eq 1 ) {
|
||||
$var1 = 2
|
||||
}
|
||||
}
|
||||
myFunction
|
||||
|
||||
$var1 | Should Be 2
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Context "Scope Tests" {
|
||||
It "Should be able to create a global scope variable using the global switch" {
|
||||
{ New-Variable globalVar -Value 1 -Scope global -Force } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to create a local scope variable using the local switch" {
|
||||
{ New-Variable localVar -Value 1 -Scope local -Force } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to create a script scope variable using the script switch" {
|
||||
{ New-Variable scriptVar -Value 1 -Scope script -Force } | Should Not Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
Describe "Out-File" {
|
||||
$expectedContent = "some test text"
|
||||
$inObject = New-Object psobject -Property @{text=$expectedContent}
|
||||
$testfile = "/tmp/outfileTest.txt"
|
||||
|
||||
AfterEach {
|
||||
Remove-Item -Path $testfile -Force
|
||||
}
|
||||
|
||||
It "Should be able to be called without error" {
|
||||
{ Out-File -FilePath $testfile } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to accept string input via piping" {
|
||||
{ $expectedContent | Out-File -FilePath $testfile } | Should Not Throw
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual | Should Be $expectedContent
|
||||
}
|
||||
|
||||
It "Should be able to accept string input via the InputObject swictch" {
|
||||
{ Out-File -FilePath $testfile -InputObject $expectedContent } | Should Not Throw
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual | Should Be $expectedContent
|
||||
}
|
||||
|
||||
It "Should be able to accept object input" {
|
||||
{ $inObject | Out-File -FilePath $testfile } | Should Not Throw
|
||||
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not overwrite when the noclobber switch is used" {
|
||||
|
||||
Out-File -FilePath $testfile -InputObject $inObject
|
||||
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should Throw "already exists."
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should Throw "already exists."
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual[0] | Should Be ""
|
||||
$actual[1] | Should Match "text"
|
||||
$actual[2] | Should Match "----"
|
||||
$actual[3] | Should Match "some test text"
|
||||
}
|
||||
|
||||
It "Should Append a new line when the append switch is used" {
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject -Append } | Should Not Throw
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual[0] | Should Be ""
|
||||
$actual[1] | Should Match "text"
|
||||
$actual[2] | Should Match "----"
|
||||
$actual[3] | Should Match "some test text"
|
||||
$actual[4] | Should Be ""
|
||||
$actual[5] | Should Be ""
|
||||
$actual[6] | Should Be ""
|
||||
$actual[7] | Should Match "text"
|
||||
$actual[8] | Should Match "----"
|
||||
$actual[9] | Should Match "some test text"
|
||||
$actual[10] | Should Be ""
|
||||
$actual[11] | Should Be ""
|
||||
|
||||
}
|
||||
|
||||
It "Should limit each line to the specified number of characters when the width switch is used on objects" {
|
||||
|
||||
Out-File -FilePath $testfile -Width 10 -InputObject $inObject
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual[0] | Should Be ""
|
||||
$actual[1] | Should Be "text "
|
||||
$actual[2] | Should Be "---- "
|
||||
$actual[3] | Should Be "some te..."
|
||||
|
||||
}
|
||||
|
||||
It "Should allow the cmdlet to overwrite an existing read-only file" {
|
||||
# create a read-only text file
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
||||
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true
|
||||
|
||||
# write information to the RO file
|
||||
{ Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should Not Throw
|
||||
|
||||
$actual = Get-Content $testfile
|
||||
|
||||
$actual[0] | Should Be ""
|
||||
$actual[1] | Should Match "text"
|
||||
$actual[2] | Should Match "----"
|
||||
$actual[3] | Should Match "some test text"
|
||||
$actual[4] | Should Be ""
|
||||
$actual[5] | Should Be ""
|
||||
$actual[6] | Should Be ""
|
||||
$actual[7] | Should Match "text"
|
||||
$actual[8] | Should Match "----"
|
||||
$actual[9] | Should Match "some test text"
|
||||
$actual[10] | Should Be ""
|
||||
$actual[11] | Should Be ""
|
||||
|
||||
# reset to not read only so it can be deleted
|
||||
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
Describe "Out-String" {
|
||||
$nl = [Environment]::NewLine
|
||||
|
||||
It "Should accumulate the strings and returns them as a single string" {
|
||||
$testArray = "a", " b"
|
||||
|
||||
$testArray.GetType().BaseType | Should Be array
|
||||
|
||||
$testArray | Out-String | Should Be "a$nl b$nl"
|
||||
|
||||
$($testArray | Out-String).GetType() | Should Be string
|
||||
}
|
||||
|
||||
It "Should be able to return an array of strings using the stream switch" {
|
||||
$testInput = "a", "b"
|
||||
|
||||
$($testInput | Out-String).GetType() | Should Be string
|
||||
|
||||
$($testInput | Out-String -Stream).GetType().BaseType.Name | Should Be array
|
||||
}
|
||||
|
||||
It "Should send all objects through a pipeline when not using the stream switch" {
|
||||
$testInput = "a", "b"
|
||||
$streamoutputlength = $($testInput | Out-String -Stream).Length
|
||||
$nonstreamoutputlength = $($testInput | Out-String).Length
|
||||
|
||||
$nonstreamoutputlength| Should BeGreaterThan $streamoutputlength
|
||||
}
|
||||
|
||||
It "Should send a single object through a pipeline when the stream switch is used" {
|
||||
$testInput = "a", "b"
|
||||
$streamoutputlength = $($testInput | Out-String -Stream).Length
|
||||
$nonstreamoutputlength = $($testInput | Out-String).Length
|
||||
|
||||
$streamoutputlength | Should BeLessThan $nonstreamoutputlength
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
Describe "PSVersionTable" {
|
||||
It "Should have version table entries" {
|
||||
$PSVersionTable.Count | Should BeGreaterThan 6
|
||||
}
|
||||
|
||||
It "Should have the right version table entries" {
|
||||
|
||||
$PSVersionTable.ContainsKey("PSVersion") | Should Be True
|
||||
$PSVersionTable.ContainsKey("WSManStackVersion") | Should Be True
|
||||
$PSVersionTable.ContainsKey("SerializationVersion") | Should Be True
|
||||
$PSVersionTable.ContainsKey("CLRVersion") | Should Be True
|
||||
$PSVersionTable.ContainsKey("BuildVersion") | Should Be True
|
||||
$PSVersionTable.ContainsKey("PSCompatibleVersions") | Should Be True
|
||||
$PSVersionTable.ContainsKey("PSRemotingProtocolVersion") | Should Be True
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
Describe "Pop-Location" {
|
||||
$startDirectory = $(Get-Location).Path
|
||||
|
||||
BeforeEach { Set-Location $startDirectory }
|
||||
|
||||
It "Should be able to be called without error" {
|
||||
{ Pop-Location } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not take a parameter" {
|
||||
{ Pop-Location .. } | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able pop multiple times" {
|
||||
Push-Location ..
|
||||
Push-Location ..
|
||||
Push-Location ..
|
||||
|
||||
Pop-Location
|
||||
Pop-Location
|
||||
Pop-Location
|
||||
|
||||
$(Get-Location).Path | Should Be $startDirectory
|
||||
|
||||
}
|
||||
|
||||
Set-Location $startDirectory
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
Describe "Test-Push-Location" {
|
||||
New-Variable -Name startDirectory -Value $(Get-Location).Path -Scope Global -Force
|
||||
|
||||
BeforeEach { cd $startDirectory }
|
||||
|
||||
It "Should be called without error" {
|
||||
{ Push-Location } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to push to the root directory" {
|
||||
# this works cross-platform
|
||||
{ Push-Location / } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use relative path to parent" {
|
||||
{ Push-Location .. } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use relative path to grandparent" {
|
||||
Test-Path ../.. | Should Be $true
|
||||
|
||||
{ Push-Location ../.. } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to push twice" {
|
||||
{ Push-Location .. } | Should Not Throw
|
||||
{ Push-Location .. } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to take a piped variable" {
|
||||
{ ".." | Push-Location } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to call the pushd alias" {
|
||||
{ pushd } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to push to the same location between the alias and the cmdlet" {
|
||||
pushd ..
|
||||
$aliasDirectory = $(Get-Location).Path
|
||||
|
||||
cd $startDirectory
|
||||
Push-Location ..
|
||||
$cmdletDirectory = $(Get-Location).Path
|
||||
|
||||
$aliasDirectory | Should Be $cmdletDirectory
|
||||
}
|
||||
|
||||
It "Should produce a pathinfo object when the passthru parameter is used" {
|
||||
Push-Location .. -PassThru | ForEach-Object { $_.GetType().Name | Should Be PathInfo }
|
||||
}
|
||||
|
||||
# final cleanup
|
||||
cd $startDirectory
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
Describe "Register-EngineEvent" {
|
||||
|
||||
Context "Check return type of Register-ObjectEvent" {
|
||||
It "Should return System.Management.Automation.PSEventJob as return type of Register-EngineEvent" {
|
||||
( Register-EngineEvent -SourceIdentifier PesterTestRegister -Action {echo registerengineevent} ).GetType() | Should Be System.Management.Automation.PSEventJob
|
||||
Unregister-Event -sourceidentifier PesterTestRegister
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
Describe "Register-ObjectEvent" {
|
||||
|
||||
BeforeEach {
|
||||
$pesterobject = (New-Object System.Collections.ObjectModel.ObservableCollection[object])
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
Unregister-Event -SourceIdentifier PesterTestRegister -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Context "Check return type of Register-ObjectEvent" {
|
||||
|
||||
It "Should return System.Management.Automation.PSEventSubscriber as return type of New-Event with the registered sourceidentifier" {
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber -SourceIdentifier PesterTestRegister).GetType() | Should Be System.Management.Automation.PSEventSubscriber
|
||||
}
|
||||
}
|
||||
|
||||
Context "Check Register-ObjectEvent can validly register events"{
|
||||
It "Should return source identifier of PesterTimer " {
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber -SourceIdentifier PesterTestRegister).SourceIdentifier | Should Be "PesterTestRegister"
|
||||
}
|
||||
|
||||
It "Should return an integer greater than 0 for the SubscriptionId" {
|
||||
Register-ObjectEvent -InputObject $pesterobject -EventName CollectionChanged -SourceIdentifier PesterTestRegister
|
||||
(Get-EventSubscriber -SourceIdentifier PesterTestRegister).SubscriptionId | Should BeGreaterThan 0
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
Describe "Remove-Event" {
|
||||
|
||||
BeforeEach{
|
||||
New-Event -sourceidentifier PesterTimer -sender windows.timer -messagedata "PesterTestMessage"
|
||||
}
|
||||
|
||||
AfterEach{
|
||||
Remove-Event -sourceidentifier PesterTimer -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Context "Check Remove-Event can validly remove events" {
|
||||
|
||||
It "Should remove an event given a sourceidentifier" {
|
||||
{ Remove-Event -sourceidentifier PesterTimer }
|
||||
{ Get-Event -ErrorAction SilentlyContinue | Should Not Contain PesterTimer }
|
||||
}
|
||||
|
||||
It "Should remove an event given an event identifier" {
|
||||
{ $events = Get-Event -sourceidentifier PesterTimer }
|
||||
{ $events = $events.EventIdentifier }
|
||||
{ Remove-Event -EventIdentifier $events }
|
||||
{ $events = Get-Event -ErrorAction SilentlyContinue}
|
||||
{ $events.SourceIdentifier | Should Not Contain "PesterTimer" }
|
||||
}
|
||||
|
||||
It "Should be able to remove an event given a pipe from Get-Event" {
|
||||
{ Get-Event -sourceidentifier PesterTimer | Remove-Event }
|
||||
{ Get-Event -ErrorAction SilentlyContinue | Should Not Contain "PesterTimer" }
|
||||
|
||||
}
|
||||
|
||||
It "Should NOT remove an event given the whatif flag" {
|
||||
{ Remove-Event -sourceidentifier PesterTimer -whatif }
|
||||
{ $events = Get-Event }
|
||||
{ $events.SourceIdentifier | Should Contain "PesterTimer" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
Describe "Remove-Item" {
|
||||
$testpath = "/tmp/"
|
||||
$testfile = "testfile.txt"
|
||||
$testfilepath = $testpath + $testfile
|
||||
Context "File removal Tests" {
|
||||
BeforeEach {
|
||||
New-Item -Name $testfile -Path $testpath -ItemType "file" -Value "lorem ipsum" -Force
|
||||
|
||||
Test-Path $testfilepath | Should Be $true
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to be called on a regular file without error using the Path switch" {
|
||||
{ Remove-Item -Path $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to be called on a file without the Path switch" {
|
||||
{ Remove-Item $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to call the rm alias" {
|
||||
{ rm $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to call the del alias" {
|
||||
{ del $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to call the erase alias" {
|
||||
{ erase $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to call the ri alias" {
|
||||
{ ri $testfilepath } | Should Not Throw
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should not be able to remove a read-only document without using the force switch" {
|
||||
# Set to read only
|
||||
Set-ItemProperty -Path $testfilepath -Name IsReadOnly -Value $true
|
||||
|
||||
# attempt to remove the file
|
||||
{ Remove-Item $testfilepath -ErrorAction SilentlyContinue } | Should Not Throw
|
||||
|
||||
# validate
|
||||
Test-Path $testfilepath | Should Be $true
|
||||
|
||||
# remove using the -force switch on the readonly object
|
||||
Remove-Item $testfilepath -Force
|
||||
|
||||
# Validate
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to remove all files matching a regular expression with the include switch" {
|
||||
# Create multiple files with specific string
|
||||
New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
New-Item -Name file2.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
New-Item -Name file3.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
# Create a single file that does not match that string - already done in BeforeEach
|
||||
|
||||
# Delete the specific string
|
||||
Remove-Item /tmp/* -Include file*.txt
|
||||
# validate that the string under test was deleted, and the nonmatching strings still exist
|
||||
Test-path /tmp/file1.txt | Should Be $false
|
||||
Test-path /tmp/file2.txt | Should Be $false
|
||||
Test-path /tmp/file3.txt | Should Be $false
|
||||
Test-Path $testfilepath | Should Be $true
|
||||
|
||||
# Delete the non-matching strings
|
||||
Remove-Item $testfilepath
|
||||
|
||||
Test-Path $testfilepath | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to not remove any files matching a regular expression with the exclude switch" {
|
||||
# Create multiple files with specific string
|
||||
New-Item -Name file1.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
New-Item -Name file2.wav -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
|
||||
# Create a single file that does not match that string
|
||||
New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum"
|
||||
|
||||
# Delete the specific string
|
||||
Remove-Item /tmp/file* -Exclude *.wav -Include *.txt
|
||||
|
||||
# validate that the string under test was deleted, and the nonmatching strings still exist
|
||||
Test-Path /tmp/file1.wav | Should Be $true
|
||||
Test-Path /tmp/file2.wav | Should Be $true
|
||||
Test-Path /tmp/file1.txt | Should Be $false
|
||||
|
||||
# Delete the non-matching strings
|
||||
Remove-Item /tmp/file1.wav
|
||||
Remove-Item /tmp/file2.wav
|
||||
|
||||
Test-Path /tmp/file1.wav | Should Be $false
|
||||
Test-Path /tmp/file2.wav | Should Be $false
|
||||
}
|
||||
}
|
||||
|
||||
Context "Directory Removal Tests" {
|
||||
$testdirectory = "/tmp/testdir"
|
||||
$testsubdirectory = $testdirectory + "/subd"
|
||||
BeforeEach {
|
||||
New-Item -Name "testdir" -Path "/tmp/" -ItemType "directory" -Force
|
||||
|
||||
Test-Path $testdirectory | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to remove a directory" {
|
||||
{ Remove-Item $testdirectory } | Should Not Throw
|
||||
|
||||
Test-Path $testdirectory | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to recursively delete subfolders" {
|
||||
New-Item -Name "subd" -Path $testdirectory -ItemType "directory"
|
||||
New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum"
|
||||
|
||||
$complexDirectory = $testsubdirectory + "/" + $testfile
|
||||
test-path $complexDirectory | Should Be $true
|
||||
|
||||
{ Remove-Item $testdirectory -Recurse} | Should Not Throw
|
||||
|
||||
Test-Path $testdirectory | Should Be $false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Remove-PSBreakpoint" {
|
||||
# Set up test script
|
||||
$testScript = "$here/assets/psbreakpointtestscript.ps1"
|
||||
|
||||
$script = "`$var = 1
|
||||
`$var2 = Get-Process
|
||||
|
||||
# this is a comment
|
||||
Get-Date
|
||||
"
|
||||
|
||||
$script > $testScript
|
||||
|
||||
BeforeEach {
|
||||
# set some breakpoints
|
||||
$line = Set-PSBreakpoint -Line 1,2,3 -Script $testScript
|
||||
$command = Set-PSBreakpoint -Command "Get-Date" -Script $testScript
|
||||
$variable = Set-PSBreakpoint -Variable var2 -Script $testScript
|
||||
}
|
||||
|
||||
Context "Basic Removal Methods Tests" {
|
||||
It "Should be able to remove a breakpoint by breakpoint Id" {
|
||||
$NumberOfBreakpoints = $(Get-PSBreakpoint).Id.length
|
||||
$BreakID = $(Get-PSBreakpoint).Id[0]
|
||||
Remove-PSBreakpoint -Id $BreakID
|
||||
|
||||
$(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1)
|
||||
}
|
||||
|
||||
It "Should be able to remove a breakpoint by variable" {
|
||||
$NumberOfBreakpoints = $(Get-PSBreakpoint).Id.length
|
||||
Remove-PSBreakpoint -Breakpoint $variable
|
||||
|
||||
$(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1)
|
||||
}
|
||||
|
||||
It "Should be able to remove a breakpoint by command" {
|
||||
$NumberOfBreakpoints = $(Get-PSBreakpoint).Id.length
|
||||
Remove-PSBreakpoint -Breakpoint $command
|
||||
|
||||
$(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1)
|
||||
}
|
||||
|
||||
It "Should be able to pipe breakpoint objects to Remove-PSBreakpoint" {
|
||||
$NumberOfBreakpoints = $(Get-PSBreakpoint).Id.length
|
||||
$variable | Remove-PSBreakpoint
|
||||
|
||||
$(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1)
|
||||
}
|
||||
}
|
||||
|
||||
Context "Alias Tests" {
|
||||
It "Should remove a breakpoint using the rbp alias" {
|
||||
$NumberOfBreakpoints = $(Get-PSBreakpoint).Id.length
|
||||
$BreakID = $(Get-PSBreakpoint).Id[0]
|
||||
rbp -Id $BreakID
|
||||
|
||||
$(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1)
|
||||
}
|
||||
}
|
||||
|
||||
It "Should Remove all breakpoints" {
|
||||
$(Get-PSBreakpoint).Id.Length | Should Not BeNullOrEmpty
|
||||
|
||||
Get-PSBreakpoint | Remove-PSBreakpoint
|
||||
|
||||
$(Get-PSBreakpoint).Id.Length | Should Be 0
|
||||
}
|
||||
|
||||
|
||||
#Clean up after ourselves
|
||||
|
||||
Remove-Item $testScript
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
# ensure the machine is in a clean state from the outset.
|
||||
Remove-Variable -Name var1 -ErrorAction SilentlyContinue -Force
|
||||
|
||||
Describe "Remove-Variable" {
|
||||
It "Should throw an error when a dollar sign is used in the variable name place" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
|
||||
Remove-Variable $var1 -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {
|
||||
New-Variable -Name var1 -Value 4
|
||||
|
||||
Remove-Variable var1
|
||||
|
||||
$var1 | Should Be #nothing. it should be Nothing at all.
|
||||
}
|
||||
|
||||
It "Should not throw error when used with the Name field, and named variable is specified and exists" {
|
||||
New-Variable -Name var1 -Value 2
|
||||
|
||||
Remove-Variable -Name var1
|
||||
|
||||
$var1 | Should Be #nothing. it should be Nothing at all.
|
||||
}
|
||||
|
||||
It "Should throw error when used with Name field, and named variable does not exist" {
|
||||
Remove-Variable -Name nonexistantVariable -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able to remove a variable using the rv alias" {
|
||||
New-Variable var1 -Value 2
|
||||
|
||||
$var1 | Should Be 2
|
||||
|
||||
rv -Name var1
|
||||
|
||||
$var1 | Should Be #nothing. it should be Nothing at all.
|
||||
|
||||
}
|
||||
|
||||
It "Should be able to remove a set of variables using wildcard characters" {
|
||||
New-Variable tmpvar1 -Value "tempvalue"
|
||||
New-Variable tmpvar2 -Value 2
|
||||
New-Variable tmpmyvar1 -Value 234
|
||||
|
||||
$tmpvar1 | Should Be "tempvalue"
|
||||
$tmpvar2 | Should Be 2
|
||||
$tmpmyvar1 | Should Be 234
|
||||
|
||||
Remove-Variable -Name tmp*
|
||||
|
||||
$tmpvar1 | Should Be #nothing. it should be Nothing at all.
|
||||
$tmpvar2 | Should Be #nothing. it should be Nothing at all.
|
||||
$tmpmyvar1 | Should Be #nothing. it should be Nothing at all.
|
||||
}
|
||||
|
||||
It "Should be able to exclude a set of variables to remove using the Exclude switch" {
|
||||
New-Variable tmpvar1 -Value "tempvalue"
|
||||
New-Variable tmpvar2 -Value 2
|
||||
New-Variable tmpmyvar1 -Value 234
|
||||
|
||||
$tmpvar1 | Should Be "tempvalue"
|
||||
$tmpvar2 | Should Be 2
|
||||
$tmpmyvar1 | Should Be 234
|
||||
|
||||
Remove-Variable -Name tmp* -Exclude *my*
|
||||
|
||||
$tmpvar1 | Should Be #nothing. it should be Nothing at all.
|
||||
$tmpvar2 | Should Be #nothing. it should be Nothing at all.
|
||||
$tmpmyvar1 | Should Be 234
|
||||
}
|
||||
|
||||
It "Should be able to include a set of variables to remove using the Include switch" {
|
||||
New-Variable tmpvar1 -Value "tempvalue"
|
||||
New-Variable tmpvar2 -Value 2
|
||||
New-Variable tmpmyvar1 -Value 234
|
||||
New-Variable thevar -Value 1
|
||||
|
||||
$tmpvar1 | Should Be "tempvalue"
|
||||
$tmpvar2 | Should Be 2
|
||||
$tmpmyvar1 | Should Be 234
|
||||
$thevar | Should Be 1
|
||||
|
||||
Remove-Variable -Name tmp* -Include *my*
|
||||
|
||||
$tmpvar1 | Should Be "tempvalue"
|
||||
$tmpvar2 | Should Be 2
|
||||
$tmpmyvar1 | Should Be #nothing. it should be Nothing at all.
|
||||
$thevar | should Be 1
|
||||
|
||||
Remove-Variable tmpvar1
|
||||
Remove-Variable tmpvar2
|
||||
Remove-Variable thevar
|
||||
|
||||
}
|
||||
|
||||
It "Should throw an error when attempting to remove a read-only variable and the Force switch is not used" {
|
||||
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
||||
|
||||
Remove-Variable -Name var1 -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be 2
|
||||
|
||||
Remove-Variable -Name var1 -Force
|
||||
}
|
||||
|
||||
It "Should not throw an error when attempting to remove a read-only variable and the Force switch is used" {
|
||||
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
||||
|
||||
Remove-Variable -Name var1 -Force
|
||||
|
||||
$var1 | Should Be # Nothing. It should be nothing at all.
|
||||
}
|
||||
|
||||
Context "Scope Tests" {
|
||||
It "Should be able to remove a global variable using the global switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope global
|
||||
|
||||
Remove-Variable -Name var1 -Scope global
|
||||
|
||||
$var1 | Should Be #Nothing.
|
||||
}
|
||||
|
||||
It "Should not be able to clear a global variable using the local switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope global
|
||||
|
||||
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be "context"
|
||||
|
||||
Remove-Variable -Name var1 -Scope global
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should not be able to clear a global variable using the script switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope global
|
||||
|
||||
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be "context"
|
||||
|
||||
Remove-Variable -Name var1 -Scope global
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should be able to remove an item locally using the local switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope local
|
||||
|
||||
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should be able to remove an item locally using the global switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope local
|
||||
|
||||
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be "context"
|
||||
|
||||
Remove-Variable -Name var1 -Scope local
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should be able to remove a local variable using the script scope switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope local
|
||||
|
||||
Remove-Variable -Name var1 -Scope script -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be "context"
|
||||
|
||||
Remove-Variable -Name var1 -Scope local
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should be able to remove a script variable created using the script switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope script
|
||||
|
||||
Remove-Variable -Name var1 -Scope script | Should Throw
|
||||
|
||||
$var1 | Should Be # Nothing
|
||||
}
|
||||
|
||||
It "Should not be able to remove a global script variable that was created using the script scope switch" {
|
||||
New-Variable -Name var1 -Value "context" -Scope script
|
||||
|
||||
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
$var1 | Should Be "context"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
. "$here/Test-Mocks.ps1"
|
||||
|
||||
Describe "Select-Object" {
|
||||
BeforeEach {
|
||||
$dirObject = GetFileMock
|
||||
$TestLength = 3
|
||||
}
|
||||
|
||||
It "Handle piped input without error" {
|
||||
{ $dirObject | Select-Object } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should treat input as a single object with the inputObject parameter" {
|
||||
$result = $(Select-Object -inputObject $dirObject -last $TestLength).Length
|
||||
$expected = $dirObject.Length
|
||||
|
||||
$result | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should be able to use the alias" {
|
||||
{ $dirObject | select } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have same result when using alias" {
|
||||
$result = $dirObject | select
|
||||
$expected = $dirObject | Select-Object
|
||||
|
||||
$result | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should return correct object with First parameter" {
|
||||
$result = $dirObject | Select-Object -First $TestLength
|
||||
|
||||
$result.Length | Should Be $TestLength
|
||||
|
||||
for ($i=0; $i -lt $TestLength; $i++)
|
||||
{
|
||||
$result[$i].Name | Should Be $dirObject[$i].Name
|
||||
}
|
||||
}
|
||||
|
||||
It "Should return correct object with Last parameter" {
|
||||
$result = $dirObject | Select-Object -Last $TestLength
|
||||
|
||||
$result.Length | Should Be $TestLength
|
||||
|
||||
for ($i=0; $i -lt $TestLength; $i++)
|
||||
{
|
||||
$result[$i].Name | Should Be $dirObject[$dirObject.Length - $TestLength + $i].Name
|
||||
}
|
||||
}
|
||||
|
||||
It "Should work correctly with Unique parameter" {
|
||||
$result = ("a","b","c","a","a","a" | Select-Object -Unique).Length
|
||||
$expected = 3
|
||||
|
||||
$result | Should Be $expected
|
||||
}
|
||||
|
||||
It "Should return correct object with Skip parameter" {
|
||||
$result = $dirObject | Select-Object -Skip $TestLength
|
||||
|
||||
$result.Length | Should Be ($dirObject.Length - $TestLength)
|
||||
|
||||
for ($i=0; $i -lt $TestLength; $i++)
|
||||
{
|
||||
$result[$i].Name | Should Be $dirObject[$TestLength + $i].Name
|
||||
}
|
||||
}
|
||||
|
||||
It "Should return an object with selected columns" {
|
||||
$result = $dirObject | Select-Object -Property Name, Size
|
||||
|
||||
$result.Length | Should Be $dirObject.Length
|
||||
$result[0].Name | Should Be $dirObject[0].Name
|
||||
$result[0].Size | Should Be $dirObject[0].Size
|
||||
$result[0].Mode | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should send output to pipe properly" {
|
||||
{$dirObject | Select-Object -Unique | pipelineConsume} | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should select array indices with Index parameter" {
|
||||
$firstIndex = 2
|
||||
$secondIndex = 4
|
||||
$result = $dirObject | Select-Object -Index $firstIndex, $secondIndex
|
||||
|
||||
$result[0].Name | Should Be $dirObject[$firstIndex].Name
|
||||
$result[1].Name | Should Be $dirObject[$secondIndex].Name
|
||||
}
|
||||
|
||||
# Note that these two tests will modify original values of $dirObject
|
||||
|
||||
It "Should not wait when used without -Wait option" {
|
||||
$orig1 = $dirObject[0].Size
|
||||
$orig2 = $dirObject[$TestLength].Size
|
||||
$result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength
|
||||
|
||||
$result[0].Size | Should Be ($orig1 + 1)
|
||||
$dirObject[0].Size | Should Be ($orig1 + 1)
|
||||
$dirObject[$TestLength].Size | Should Be $orig2
|
||||
}
|
||||
|
||||
It "Should wait when used with -Wait option" {
|
||||
$orig1 = $dirObject[0].Size
|
||||
$orig2 = $dirObject[$TestLength].Size
|
||||
$result = $dirObject | addOneToSizeProperty | Select-Object -First $TestLength -Wait
|
||||
|
||||
$result[0].Size | Should Be ($orig1 + 1)
|
||||
$dirObject[0].Size | Should Be ($orig1 + 1)
|
||||
$dirObject[$TestLength].Size | Should Be ($orig2 + 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
Describe "Select-String" {
|
||||
$nl = [Environment]::NewLine
|
||||
$currentDirectory = $pwd.Path
|
||||
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" {
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
if ($isWindows)
|
||||
{
|
||||
$testDirectory = "C:\tmp\"
|
||||
$testInputFile = "C:\tmp\testfile1.txt"
|
||||
}
|
||||
else
|
||||
{
|
||||
$testDirectory = "/tmp/"
|
||||
$testInputFile = "/tmp/testfile1.txt"
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
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"
|
||||
}
|
||||
|
||||
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 $(Split-Path $testInputFile -NoQualifier) -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 = "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 = "testfile1.txt:3:This is the third line"
|
||||
|
||||
Select-String third $testDirectory/"../tmp/testfile1.txt" | Should Match $expected
|
||||
}
|
||||
|
||||
It "Should return the fourth line in testfile1 when a relative path is used" {
|
||||
$expected = "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 = "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 = "testfile1.txt:5:No matches"
|
||||
|
||||
Select-String 'matc*' $testInputFile -ca | Should Match $expected
|
||||
}
|
||||
}
|
||||
Push-Location $currentDirectory
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
Describe "Set-Alias" {
|
||||
Mock Get-Date { return "Friday, October 30, 2015 3:38:08 PM" }
|
||||
It "Should be able to set alias without error" {
|
||||
|
||||
{ set-alias -Name gd -Value Get-Date } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to have the same output between set-alias and the output of the function being aliased" {
|
||||
set-alias -Name gd -Value Get-Date
|
||||
gd | Should Be $(Get-Date)
|
||||
}
|
||||
|
||||
It "Should be able to use the sal alias" {
|
||||
{ sal gd Get-Date } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the same output between the sal alias and the original set-alias cmdlet" {
|
||||
sal -Name gd -Value Get-Date
|
||||
|
||||
Set-Alias -Name gd2 -Value Get-Date
|
||||
|
||||
gd2 | Should Be $(gd)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
Describe "Set-Location" {
|
||||
$startDirectory = Get-Location
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
if ($isWindows)
|
||||
{
|
||||
$target = "C:\"
|
||||
}
|
||||
else
|
||||
{
|
||||
$target = "/"
|
||||
}
|
||||
|
||||
It "Should be able to be called without error" {
|
||||
{ Set-Location $target } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to be called on different providers" {
|
||||
{ Set-Location alias: } | Should Not Throw
|
||||
{ Set-Location env: } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able use the cd alias without error" {
|
||||
{ cd $target } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use the chdir alias without error" {
|
||||
{ chdir $target } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to use the sl alias without error" {
|
||||
{ sl $target } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should have the correct current location when using the set-location cmdlet" {
|
||||
Set-Location $startDirectory
|
||||
|
||||
$(Get-Location).Path | Should Be $startDirectory.Path
|
||||
}
|
||||
|
||||
It "Should have the correct current location when using the cd alias" {
|
||||
cd $target
|
||||
|
||||
$(Get-Location).Path | Should Be $target
|
||||
}
|
||||
|
||||
It "Should have the correct current location when using the chdir alias" {
|
||||
chdir $target
|
||||
|
||||
$(Get-Location).Path | Should Be $target
|
||||
}
|
||||
|
||||
It "Should have the correct current location when using the chdir alias" {
|
||||
sl $target
|
||||
|
||||
$(Get-Location).Path | Should Be $target
|
||||
}
|
||||
|
||||
It "Should be able to use the Path switch" {
|
||||
{ Set-Location -Path $target } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should generate a pathinfo object when using the Passthru switch" {
|
||||
$(Set-Location $target -PassThru).GetType().Name | Should Be PathInfo
|
||||
}
|
||||
|
||||
Set-Location $startDirectory
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Set-PSBreakpoint" {
|
||||
# Set up test script
|
||||
$testScript = "$here/psbreakpointtestscript.ps1"
|
||||
|
||||
"`$var = 1 " > $testScript
|
||||
|
||||
It "Should be able to set a psbreakpoint on a line" {
|
||||
$lineNumber = 1
|
||||
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line | Should Be $lineNumber
|
||||
}
|
||||
|
||||
It "Should throw when a string is entered for a line number" {
|
||||
{
|
||||
$lineNumber = "one"
|
||||
$(Set-PSBreakpoint -Line $lineNumber -Script $testScript).Line
|
||||
|
||||
} | Should Throw
|
||||
}
|
||||
|
||||
It "Should be able to set a psbreakpoint on a Command" {
|
||||
$command = "theCommand"
|
||||
$(Set-PSBreakpoint -Command $command -Script $testScript).Command | Should Be $command
|
||||
}
|
||||
|
||||
It "Should be able to set a psbreakpoint on a variable" {
|
||||
$var = "theVariable"
|
||||
$(Set-PSBreakpoint -Command $var -Script $testScript).Command | Should Be $var
|
||||
}
|
||||
|
||||
# clean up after ourselves
|
||||
Remove-Item -Path $testScript
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
Describe "Set-PSDebug" {
|
||||
# Because it is running through pester, no functions need to be called. Pester should provide plenty
|
||||
# of output.
|
||||
It "Should be able to be called without error" {
|
||||
{ Set-PSDebug -Trace 0 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to be turned off without error" {
|
||||
{ Set-PSDebug -Off } | Should Not Throw
|
||||
}
|
||||
|
||||
Context "Validate functionality" {
|
||||
BeforeEach {
|
||||
Set-PSDebug -Off
|
||||
}
|
||||
|
||||
It "Should be able to go through the tracing options" {
|
||||
{ Set-PSDebug -Trace 0 } | Should Not Throw
|
||||
{ Set-PSDebug -Trace 1 } | Should Not Throw
|
||||
{ Set-PSDebug -Trace 2 } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to set strict" {
|
||||
{ Set-PSDebug -Strict } | Should Not Throw
|
||||
}
|
||||
}
|
||||
|
||||
# final cleanup
|
||||
|
||||
Set-PSDebug -Off
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
Describe "Set-Variable" {
|
||||
${nl} = [Environment]::Newline
|
||||
|
||||
It "Should create a new variable with no parameters" {
|
||||
{ Set-Variable testVar } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should assign a value to a variable it has to create" {
|
||||
Set-Variable -Name testVar -Value 4
|
||||
|
||||
Get-Variable testVar -ValueOnly | Should Be 4
|
||||
}
|
||||
|
||||
It "Should change the value of an already existing variable" {
|
||||
$testVar=1
|
||||
|
||||
$testVar | Should Not Be 2
|
||||
|
||||
Set-Variable testVar -Value 2
|
||||
|
||||
$testVar | Should Be 2
|
||||
}
|
||||
|
||||
It "Should be able to be called with the set alias" {
|
||||
set testVar -Value 1
|
||||
|
||||
$testVar | Should Be 1
|
||||
}
|
||||
|
||||
It "Should be able to be called with the sv alias" {
|
||||
sv testVar -Value 2
|
||||
|
||||
$testVar | Should Be 2
|
||||
}
|
||||
|
||||
It "Should be able to set variable name using the Name parameter" {
|
||||
Set-Variable -Name testVar -Value 1
|
||||
|
||||
$testVar | Should Be 1
|
||||
}
|
||||
|
||||
It "Should be able to set the value of a variable by piped input" {
|
||||
$testValue = "piped input"
|
||||
|
||||
$testValue | Set-Variable -Name testVar
|
||||
|
||||
$testVar | Should Be $testValue
|
||||
}
|
||||
|
||||
It "Should be able to pipe object properties to output using the PassThru switch" {
|
||||
$in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru
|
||||
|
||||
$output = $in | Format-List -Property Description | Out-String
|
||||
|
||||
# This will cause errors running these tests in windows
|
||||
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
|
||||
}
|
||||
|
||||
It "Should be able to set the value using the value switch" {
|
||||
Set-Variable -Name testVar -Value 4
|
||||
|
||||
$testVar | Should Be 4
|
||||
|
||||
Set-Variable -Name testVar -Value "test"
|
||||
|
||||
$testVar | Should Be "test"
|
||||
}
|
||||
|
||||
Context "Scope Tests" {
|
||||
It "Should be able to set a global scope variable using the global switch" {
|
||||
{ Set-Variable globalVar -Value 1 -Scope global -Force } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to set a global variable using the script scope switch" {
|
||||
{ Set-Variable globalVar -Value 1 -Scope script -Force } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to set an item locally using the local switch" {
|
||||
{ Set-Variable globalVar -Value 1 -Scope local -Force } | Should Not Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
Describe "Split-Path" {
|
||||
$isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
|
||||
|
||||
if ($isWindows)
|
||||
{
|
||||
$qualifier = "C:"
|
||||
}
|
||||
else
|
||||
{
|
||||
$qualifier = "/"
|
||||
}
|
||||
|
||||
It "Should return a string object when invoked" {
|
||||
( Split-Path . ).GetType().Name | Should Be "String"
|
||||
( Split-Path . -Leaf ).GetType().Name | Should Be "String"
|
||||
( Split-Path . -Resolve ).GetType().Name | Should Be "String"
|
||||
}
|
||||
|
||||
It "Should return the name of the drive when the qualifier switch is used" {
|
||||
Split-Path $qualifier -Qualifier | Should Be "$qualifier"
|
||||
Split-Path ${qualifier}usr/bin -Qualifier | Should Be "$qualifier"
|
||||
}
|
||||
|
||||
It "Should error when using the qualifier switch for a windows path while on a nonwindows machine" {
|
||||
# ErrorAction SilentlyContinue merely suppresses the error from the console.
|
||||
# Throwing exceptions still seen by Pester.
|
||||
|
||||
if ($qualifier -eq "/")
|
||||
{
|
||||
Split-Path "C:\Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
else
|
||||
{
|
||||
Split-Path "/Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
It "Should error when no directory separator characters are used with a qualifier" {
|
||||
Split-Path "abadTest" -Qualifier -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should return the path when the noqualifier switch is used on a Linux system" {
|
||||
{ Split-Path ${qualifier}usr/bin -NoQualifier } | Should Not Throw
|
||||
if ($isWindows)
|
||||
{
|
||||
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "usr/bin"
|
||||
}
|
||||
else
|
||||
{
|
||||
Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "/usr/bin"
|
||||
}
|
||||
}
|
||||
|
||||
It "Should return the base name when the leaf switch is used" {
|
||||
Split-Path ${qualifier}usr/bin -Leaf | Should be "bin"
|
||||
Split-Path ${qualifier}usr/local/bin -Leaf | Should be "bin"
|
||||
Split-Path usr/bin -Leaf | Should be "bin"
|
||||
Split-Path ./bin -Leaf | Should be "bin"
|
||||
Split-Path bin -Leaf | Should be "bin"
|
||||
}
|
||||
|
||||
It "Should be able to accept regular expression input and output an array for multiple objects" {
|
||||
$testDir = "./testDir"
|
||||
$testFile1 = "testfile1.ps1"
|
||||
$testFile2 = "testfile2.ps1"
|
||||
$testFilePath1 = $testDir + "/" + $testFile1
|
||||
$testFilePath2 = $testDir + "/" + $testFile2
|
||||
|
||||
New-Item -ItemType file -Path $testFilePath1, $testFilePath2 -Force
|
||||
|
||||
Test-Path $testFilePath1 | Should Be $true
|
||||
Test-Path $testFilePath2 | Should Be $true
|
||||
|
||||
$actual = ( Split-Path $testDir/*file*.ps1 -Leaf -Resolve ) | Sort-Object
|
||||
$actual.GetType().BaseType.Name | Should Be "Array"
|
||||
$actual[0] | Should Be $testFile1
|
||||
$actual[1] | Should Be $testFile2
|
||||
|
||||
Remove-Item $testDir -Recurse -ErrorAction Ignore
|
||||
Test-Path $testDir | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be able to tell if a given path is an absolute path" {
|
||||
( Split-Path ${qualifier}usr/bin -IsAbsolute ) | Should be $true
|
||||
( Split-Path .. -IsAbsolute ) | Should be $false
|
||||
( Split-Path ${qualifier}usr/.. -IsAbsolute ) | Should be $true
|
||||
( Split-Path ${qualifier}usr/../ -IsAbsolute ) | Should be $true
|
||||
( Split-Path ../ -IsAbsolute ) | Should be $false
|
||||
( Split-Path . -IsAbsolute ) | Should be $false
|
||||
( Split-Path ~/ -IsAbsolute ) | Should be $false
|
||||
( Split-Path ~/.. -IsAbsolute ) | Should be $false
|
||||
( Split-Path ~/../.. -IsAbsolute ) | Should be $false
|
||||
|
||||
}
|
||||
|
||||
It "Should support piping" {
|
||||
$path = "${qualifier}usr/bin"
|
||||
( $path | Split-Path ) | Should Be "${qualifier}usr"
|
||||
}
|
||||
|
||||
It "Should return the path up to the parent of the directory when Parent switch is used" {
|
||||
Split-Path "${qualifier}usr/bin" -Parent | Should Be "${qualifier}usr"
|
||||
Split-Path "${qualifier}usr/local/bin" -Parent | Should Be $(Join-Path "${qualifier}usr" -ChildPath "local")
|
||||
Split-Path "usr/local/bin" -Parent | Should Be $(Join-Path "usr" -ChildPath "local")
|
||||
}
|
||||
|
||||
It "Should throw if a parameterSetName is incorrect" {
|
||||
{ Split-Path "${qualifier}usr/bin/" -Parentaoeu } | Should Throw "A parameter cannot be found that matches parameter name"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
. $here/Test-Common.ps1
|
||||
|
||||
Describe "Start-Process" {
|
||||
$pingCommand = (Get-Command -CommandType Application ping)[0].Definition
|
||||
$pingDirectory = Split-Path $pingCommand -Parent
|
||||
$tempFile = "$(GetTempDir)/PSTest"
|
||||
$assetsFile = $here + "/assets/SortTest.txt"
|
||||
$windows = IsWindows
|
||||
if ($windows)
|
||||
{
|
||||
$pingParam = "localhost -n 4"
|
||||
}
|
||||
else
|
||||
{
|
||||
$pingParam = "localhost -c 4"
|
||||
}
|
||||
|
||||
It "Should process arguments without error" {
|
||||
$process = Start-Process ping -ArgumentList $pingParam -PassThru
|
||||
|
||||
$process.Length | Should Be 1
|
||||
$process.Id | Should BeGreaterThan 1
|
||||
$process.ProcessName | Should Be "ping"
|
||||
}
|
||||
|
||||
It "Should work correctly when used with full path name" {
|
||||
$process = Start-Process $pingCommand -ArgumentList $pingParam -PassThru
|
||||
|
||||
$process.Length | Should Be 1
|
||||
$process.Id | Should BeGreaterThan 1
|
||||
$process.ProcessName | Should Be "ping"
|
||||
}
|
||||
|
||||
It "Should invoke correct path when used with FilePath argument" {
|
||||
$process = Start-Process -FilePath $pingCommand -ArgumentList $pingParam -PassThru
|
||||
|
||||
$process.Length | Should Be 1
|
||||
$process.Id | Should BeGreaterThan 1
|
||||
$process.ProcessName | Should Be "ping"
|
||||
}
|
||||
|
||||
It "Should wait for command completion if used with Wait argument" {
|
||||
$process = Start-Process ping -ArgumentList $pingParam -Wait -PassThru
|
||||
}
|
||||
|
||||
It "Should work correctly with WorkingDirectory argument" {
|
||||
$process = Start-Process ping -WorkingDirectory $pingDirectory -ArgumentList $pingParam -PassThru
|
||||
|
||||
$process.Length | Should Be 1
|
||||
$process.Id | Should BeGreaterThan 1
|
||||
$process.ProcessName | Should Be "ping"
|
||||
}
|
||||
|
||||
It "Should should handle stderr redirection without error" {
|
||||
$process = Start-Process ping -ArgumentList $pingParam -PassThru -RedirectStandardError $tempFile
|
||||
|
||||
$process.Length | Should Be 1
|
||||
$process.Id | Should BeGreaterThan 1
|
||||
$process.ProcessName | Should Be "ping"
|
||||
}
|
||||
|
||||
It "Should should handle stdout redirection without error" {
|
||||
$process = Start-Process ping -ArgumentList $pingParam -Wait -RedirectStandardOutput $tempFile
|
||||
$dirEntry = dir $tempFile
|
||||
$dirEntry.Length | Should BeGreaterThan 0
|
||||
}
|
||||
|
||||
It "Should should handle stdin redirection without error" {
|
||||
$process = Start-Process sort -Wait -RedirectStandardOutput $tempFile -RedirectStandardInput $assetsFile
|
||||
$dirEntry = dir $tempFile
|
||||
$dirEntry.Length | Should BeGreaterThan 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
Describe "Start-Sleep" {
|
||||
|
||||
Context "Validate Start-Sleep works properly" {
|
||||
It "Should only sleep for at least 3 seconds"{
|
||||
$result = Measure-Command { Start-Sleep -s 3 }
|
||||
$result.Seconds | Should BeGreaterThan 2
|
||||
}
|
||||
|
||||
It "Should sleep for at least 3 seconds using the alias" {
|
||||
$result = Measure-Command { sleep -s 3 }
|
||||
$result.Seconds | Should BeGreaterThan 2
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Describe "Tee-Object" {
|
||||
|
||||
Context "Validate Tee-Object is correctly forking output" {
|
||||
|
||||
It "Should return the output to the screen and to the variable" {
|
||||
$teefile = "$here/assets/testfile.txt"
|
||||
echo teeobjecttest1 | Tee-Object -variable teeresults
|
||||
$teeresults | Should Be "teeobjecttest1"
|
||||
Remove-Item $teefile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should return the output to the screen and to the variable from the alias" {
|
||||
$teefile = "$here/assets/testfile.txt"
|
||||
echo teeobjecttest2 | tee -variable teeresults
|
||||
$teeresults | Should Be "teeobjecttest2"
|
||||
Remove-Item $teefile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should tee the output to a file" {
|
||||
$teefile = "$here/assets/testfile.txt"
|
||||
echo teeobjecttest3 | Tee-Object $teefile
|
||||
Get-Content $teefile | Should Be "teeobjecttest3"
|
||||
Remove-Item $teefile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should tee the output to a file using the alias" {
|
||||
$teefile = "$here/assets/testfile.txt"
|
||||
echo teeobjecttest4 | tee $teefile
|
||||
Get-Content $teefile | Should Be "teeobjecttest4"
|
||||
Remove-item $teefile -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
Function IsWindows
|
||||
{
|
||||
$pingCommand = Get-Command -CommandType Application ping
|
||||
if ($pingCommand.Definition.IndexOf("\\") -ne -1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Function GetTempDir
|
||||
{
|
||||
if (IsWindows)
|
||||
{
|
||||
return $env:TEMP
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/tmp"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
Function GetFileMock () {
|
||||
$objs = @( [pscustomobject]@{ Size=4533816; Mode="-a---l"; LastWriteTime="9/1/2015 11:15 PM"; Name="explorer.exe" },
|
||||
[pscustomobject]@{ Size=994816; Mode="-a---l"; LastWriteTime="9/1/2015 11:13 PM"; Name="HelpPane.exe" },
|
||||
[pscustomobject]@{ Size=316640; Mode="-a---l"; LastWriteTime="9/1/2015 11:17 PM"; Name="WMSysPr9.prx" },
|
||||
[pscustomobject]@{ Size=215040; Mode="-a---l"; LastWriteTime="9/1/2015 11:20 PM"; Name="notepad.exe" },
|
||||
[pscustomobject]@{ Size=207239; Mode="-a----"; LastWriteTime="10/7/2015 2:37 PM"; Name="setupact.log" },
|
||||
[pscustomobject]@{ Size=181064; Mode="-a----"; LastWriteTime="9/9/2015 11:54 PM"; Name="PSEXESVC.EXE" })
|
||||
return $objs
|
||||
}
|
||||
|
||||
filter addOneToSizeProperty() {
|
||||
$_.Size += 1
|
||||
$_
|
||||
}
|
||||
|
||||
filter pipelineConsume() {
|
||||
$_
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
Describe "Test-Path" {
|
||||
BeforeAll {
|
||||
$guid = [Guid]::NewGuid().Guid
|
||||
$testdirectory = New-Item -Path $guid -ItemType Directory -Force
|
||||
$testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force
|
||||
$testfile = $tmp + "/" + $testfilename
|
||||
|
||||
# populate with additional files
|
||||
New-Item -Path $testdirectory -Name datestfile -value 1 -ItemType file | Out-Null
|
||||
New-Item -Path $testdirectory -Name gatestfile -value 1 -ItemType file | Out-Null
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Remove-Item -Path $testdirectory -Recurse
|
||||
}
|
||||
|
||||
It "Should be called on an existing path without error" {
|
||||
{ Test-Path $testdirectory } | Should Not Throw
|
||||
{ Test-Path -Path $testdirectory } | Should Not Throw
|
||||
{ Test-Path -LiteralPath $testdirectory } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should allow piping objects to it" {
|
||||
{ $testdirectory | Test-Path } | Should Not Throw
|
||||
|
||||
$testdirectory | Test-Path | Should Be $true
|
||||
"/usr/bin/totallyFakeDirectory" | Test-Path | Should Be $false
|
||||
}
|
||||
|
||||
It "Should be called on a nonexistant path without error" {
|
||||
{ Test-Path -Path "aNonexistant/path/that/should/error" } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should return false for a nonexistant path" {
|
||||
Test-Path -Path "aNonexistant/path/that/should/error" | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return true for an existing path" {
|
||||
{ Test-Path -Path $testdirectory } | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to accept a regular expression" {
|
||||
{ Test-Path -Path "/u*" } | Should Not Throw
|
||||
{ Test-Path -Path "/u[a-z]r" } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should be able to return the correct result when a regular expression is used" {
|
||||
Test-Path -Path "/u*" | Should Be $true
|
||||
Test-Path -Path "/u[a-z]*" | Should Be $true
|
||||
|
||||
Test-Path -Path "/aoeu*" | Should Be $false
|
||||
Test-Path -Path "/u[A-Z]" | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return false when the Leaf pathtype is used on a directory" {
|
||||
Test-Path -Path $testdirectory -PathType Leaf | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return true when the Leaf pathtype is used on an existing endpoint" {
|
||||
Test-Path -Path $testfilename -PathType Leaf | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return false when the Leaf pathtype is used on a nonexistant file" {
|
||||
Test-Path -Path "aoeu" -PathType Leaf | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" {
|
||||
Test-Path -Path $testfilename -Type Leaf | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to search multiple regular expressions using the include switch" {
|
||||
{ Test-Path -Path "$testdirectory/*" -Include t* } | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to exclude a regular expression using the exclude switch" {
|
||||
{ Test-Path -Path "$testdirectory/*" -Exclude v* } | Should Be $true
|
||||
}
|
||||
|
||||
It "Should be able to exclude multiple regular expressions using the exclude switch" {
|
||||
# tests whether there's any files in the /usr directory that don't start with 'd' or 'g'
|
||||
{ Test-Path -Path $testfilename -Exclude d*, g* } | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return true if the syntax of the path is correct when using the IsValid switch" {
|
||||
{ Test-Path -Path /this/is/a/valid/path -IsValid } | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return false if the syntax of the path is incorrect when using the IsValid switch" {
|
||||
$badPath = " :;!@#$%^&*(){}?+|_-"
|
||||
Test-Path -Path $badPath -IsValid | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return true on paths containing spaces when the path is surrounded in quotes" {
|
||||
Test-Path -Path "/totally a valid/path" -IsValid | Should Be $true
|
||||
}
|
||||
|
||||
It "Should throw on paths containing spaces when the path is not surrounded in quotes" {
|
||||
{ Test-Path -Path /a path/without quotes/around/it -IsValid } | Should Throw
|
||||
}
|
||||
|
||||
It "Should return true if a directory leads or trails with a space when surrounded by quotes" {
|
||||
Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return true on a valid path when the LiteralPath switch is used" {
|
||||
Test-Path -LiteralPath $testfilename | Should Be $true
|
||||
}
|
||||
|
||||
It "Should return false if regular expressions are used with the LiteralPath switch" {
|
||||
Test-Path -LiteralPath /*sr/bin | Should Be $false
|
||||
Test-Path -LiteralPath /[usth]sr/bin | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" {
|
||||
Test-Path -PSPath /*sr/bin | Should Be $false
|
||||
Test-Path -PSPath /[aoeu]sr/bin | Should Be $false
|
||||
}
|
||||
|
||||
It "Should return true if used on components other than filesystem objects" {
|
||||
Test-Path Alias:\gci | Should Be $true
|
||||
Test-Path Env:\PATH | Should Be $true
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
Describe "Update-FormatData" {
|
||||
|
||||
Context "Validate Update-FormatData update correctly" {
|
||||
|
||||
It "Should not throw upon reloading previous formatting file" {
|
||||
{ Update-FormatData } | Should Not throw
|
||||
}
|
||||
|
||||
It "Should validly load formatting data" {
|
||||
{ Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path "outputfile.ps1xml" }
|
||||
{ Update-FormatData -prependPath "outputfile.ps1xml" | Should Not throw }
|
||||
{ Remove-Item "outputfile.ps1xml" -ErrorAction SilentlyContinue }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
Describe "Update-TypeData" {
|
||||
|
||||
Context "Validate Update-Type updates correctly" {
|
||||
|
||||
It "Should not throw upon reloading previous formatting file" {
|
||||
{ Update-TypeData } | Should Not throw
|
||||
}
|
||||
|
||||
It "Should validly load formatting data" {
|
||||
{ Get-TypeData -typename System.Diagnostics.Process | Export-TypeData -Path "outputfile.ps1xml" }
|
||||
{ Update-TypeData -prependPath "outputfile.ps1xml" | Should Not throw }
|
||||
{ Remove-Item "outputfile.ps1xml" -ErrorAction SilentlyContinue }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
Describe "Wait-Event" {
|
||||
|
||||
Context "Validate Wait-Event is waiting for events" {
|
||||
It "Should time out when it does not receive a FakeEvent" {
|
||||
# Don't depend on Measure-Command
|
||||
$stopwatch = [System.Diagnostics.Stopwatch]::startNew()
|
||||
# Testing the the timeout, so wait for an event that will never be
|
||||
# raised because it is fake
|
||||
Wait-Event -Timeout 1 -SourceIdentifier "FakeEvent"
|
||||
$stopwatch.Stop()
|
||||
$stopwatch.ElapsedMilliseconds | Should BeGreaterThan 1000
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
Describe "Write-Error" {
|
||||
It "Should be able to throw" {
|
||||
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw
|
||||
}
|
||||
|
||||
It "Should throw a non-terminating error" {
|
||||
Write-Error "test throw" -ErrorAction SilentlyContinue
|
||||
|
||||
1 + 1 | Should Be 2
|
||||
}
|
||||
|
||||
It "Should trip an exception using the exception switch" {
|
||||
$var = 0
|
||||
try
|
||||
{
|
||||
Write-Error -Exception -Message "test throw"
|
||||
}
|
||||
catch [System.Exception]
|
||||
{
|
||||
|
||||
$var++
|
||||
}
|
||||
finally
|
||||
{
|
||||
$var | Should Be 1
|
||||
}
|
||||
}
|
||||
|
||||
It "Should output the error message to the `$error automatic variable" {
|
||||
$theError = "Error: Too many input values."
|
||||
write-error -message $theError -category InvalidArgument -ErrorAction SilentlyContinue
|
||||
|
||||
$error[0]| Should Be $theError
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
Describe "Write-Output" {
|
||||
$testString = $testString
|
||||
Context "Input Tests" {
|
||||
It "Should allow piped input" {
|
||||
{ $testString | Write-Output } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should write output to the output stream when using piped input" {
|
||||
$testString | Write-Output | Should Be $testString
|
||||
}
|
||||
|
||||
It "Should use inputobject switch" {
|
||||
{ Write-Output -InputObject $testString } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should write output to the output stream when using inputobject switch" {
|
||||
Write-Output -InputObject $testString | Should Be $testString
|
||||
}
|
||||
|
||||
It "Should be able to write to a variable" {
|
||||
Write-Output -InputObject $testString -OutVariable var
|
||||
$var | Should Be $testString
|
||||
}
|
||||
}
|
||||
|
||||
Context "Pipeline Command Tests" {
|
||||
It "Should send object to the next command in the pipeline" {
|
||||
Write-Output -InputObject (1+1) | Should Be 2
|
||||
}
|
||||
|
||||
It "Should have the same result between inputobject switch and piped input" {
|
||||
Write-Output -InputObject (1+1) | Should Be 2
|
||||
|
||||
1+1 | Write-Output | Should Be 2
|
||||
}
|
||||
}
|
||||
|
||||
Context "Alias Tests" {
|
||||
It "Should have the same result between the echo alias and the cmdlet" {
|
||||
$alias = echo -InputObject $testString
|
||||
$cmdlet = Write-Output -InputObject $testString
|
||||
|
||||
$alias | Should Be $cmdlet
|
||||
}
|
||||
|
||||
It "Should have the same result between the write alias and the cmdlet" {
|
||||
$alias = write -InputObject $testString
|
||||
$cmdlet = Write-Output -InputObject $testString
|
||||
|
||||
$alias | Should Be $cmdlet
|
||||
}
|
||||
}
|
||||
|
||||
Context "Enumerate Objects" {
|
||||
$enumerationObject = @(1,2,3)
|
||||
It "Should see individual objects when not using the NoEnumerate switch" {
|
||||
$singleCollection = $(Write-Output $enumerationObject| Measure-Object).Count
|
||||
|
||||
$singleCollection | Should Be $enumerationObject.length
|
||||
}
|
||||
|
||||
It "Should be able to treat a collection as a single object using the NoEnumerate switch" {
|
||||
$singleCollection = $(Write-Output $enumerationObject -NoEnumerate | Measure-Object).Count
|
||||
|
||||
$singleCollection | Should Be 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
Describe "Stream writer tests" {
|
||||
$targetfile = "writeoutput.txt"
|
||||
|
||||
# A custom function is defined here do handle the debug stream dealing with the confirm prompt
|
||||
# that would normally
|
||||
function Write-Messages
|
||||
{
|
||||
[CmdletBinding()]
|
||||
|
||||
param()
|
||||
If ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
|
||||
Write-Verbose "Verbose message"
|
||||
|
||||
Write-Debug "Debug message"
|
||||
|
||||
}
|
||||
|
||||
function Get-OutputResults
|
||||
{
|
||||
# Get the contents of the targetfile.
|
||||
# Make the array a string for less brittle testing
|
||||
$output = $(Get-Content $args[0])
|
||||
[String]::Join([Environment]::NewLine, $output )
|
||||
|
||||
return $output
|
||||
}
|
||||
Context "Redirect Stream Tests" {
|
||||
# These tests validate that a stream is actually being written to by redirecting the output of that stream
|
||||
|
||||
AfterEach { Remove-Item $targetfile }
|
||||
It "Should write warnings to the warning stream" {
|
||||
Write-Warning "Test Warning" 3>&1 > $targetfile
|
||||
|
||||
Get-Content $targetfile | Should Be "Test Warning"
|
||||
}
|
||||
|
||||
It "Should write error messages to the error stream" {
|
||||
Write-Error "Testing Error" 2>&1 > $targetfile
|
||||
|
||||
$result = Get-OutputResults $targetfile
|
||||
# The contents of the error stream should contain the expected text
|
||||
$result -match ": Testing Error" | Should Be $true
|
||||
}
|
||||
|
||||
It "Should write debug messages to the debug stream" {
|
||||
Write-Messages -Debug -EA SilentlyContinue 5>&1 > $targetfile
|
||||
|
||||
$result = Get-OutputResults $targetfile
|
||||
|
||||
# The contents of the debug stream should contain the expected text
|
||||
$result -match "Debug Message" | Should Be $true
|
||||
}
|
||||
|
||||
It "Should write messages to the verbose stream" {
|
||||
Write-Messages -Verbose 4>&1 > $targetfile
|
||||
|
||||
$result = Get-OutputResults $targetfile
|
||||
|
||||
# The contents of the debug stream should contain the expected text
|
||||
$result -match "Verbose Message" | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context "Error automatic variable" {
|
||||
It "Should write error messages to the `$Error automatic variable" {
|
||||
Write-Error "Test Error Message" -ErrorAction SilentlyContinue
|
||||
|
||||
$Error[0] | Should Match "Test Error Message"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
Describe "Write-Verbose" {
|
||||
It "Should be able to call cmdlet without error" {
|
||||
{ Write-Verbose -Message "test" -ErrorAction SilentlyContinue } | Should Not Throw
|
||||
}
|
||||
|
||||
It "Should not display verbose output by default" {
|
||||
$VerbosePreference | Should Be SilentlyContinue
|
||||
|
||||
Write-Verbose -Message "test" | Should BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should be able to set verbose output to display by changing the `$VerbosePreference automatic variable" {
|
||||
$VerbosePreference = "Continue"
|
||||
|
||||
Write-Verbose -Message "test" 4>&1 | Should Not BeNullOrEmpty
|
||||
|
||||
$VerbosePreference = "SilentlyContinue"
|
||||
}
|
||||
|
||||
It "Should be able to set verbose output to display by using the verbose switch" {
|
||||
Write-Verbose -Message "test" -Verbose 4>&1 | Should Be "test"
|
||||
}
|
||||
|
||||
It "Should be able to set verbose switch using a colon and boolean" {
|
||||
{ Write-Verbose -Message "test" -Verbose:$false } | Should Not Throw
|
||||
|
||||
$(Write-Verbose -Message "test" -Verbose:$true) 4>&1 | Should Be "test"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
5
|
||||
10
|
||||
3
|
||||
4
|
||||
8
|
||||
@@ -0,0 +1,5 @@
|
||||
Column1,Column2,Column 3
|
||||
data1,1,A
|
||||
data2,2,B
|
||||
data3,3,C
|
||||
data4,4,D
|
||||
|
@@ -0,0 +1,3 @@
|
||||
Name,Planet,Heads,Arms,Brains,Assessment
|
||||
Zaphod BeebleBrox,Beetlgeuse,2,3,Indeterminant,Dangerous
|
||||
Arthur Dent,Earth,1,2,Some,Harmless (Mostly)
|
||||
|
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"Name": "Zaphod Beeblebrox",
|
||||
"Planet": "Betelgeuse",
|
||||
"Appendages": {
|
||||
"Heads": 2,
|
||||
"Arms": 3
|
||||
},
|
||||
"Achievements": [
|
||||
"Invented the Pan Galactic Gargle Blaster",
|
||||
"Worst Dressed Sentient Being in the Known Universe",
|
||||
"The best Bang since the Big One",
|
||||
"One hoopy frood"
|
||||
]
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Get-Module
|
||||
Reference in new issue
Block a user