Powershell.Helper.Extension.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#Reference: https://www.simple-talk.com/sysadmin/powershell/practical-powershell-unit-testing-getting-started/
$srcModule = $MyInvocation.MyCommand.Path `
    -replace '\.Tests\.', '.' -replace "ps1", "psd1"
Import-Module $srcModule 

InModuleScope "Powershell.Helper.Extension" {
    Describe "Add-Path" {
        Context "Test1" {
            #Mock Test-Path{if($Path -eq ""){Write-Error "Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string."; } else{return $true;}}
            #Mock New-Item {return $Path; }
            #Mock Get-Item {return $Path;}

            #use $env:ALLUSERSPROFILE which usually points to c:\programdata
            $path = Join-Path(Join-Path($env:ALLUSERSPROFILE)$(New-Guid).ToString())$(New-Guid).ToString()
            It "After using Add-Path the path is confirmed with test-path" {
                $newpath = Add-Path $path;
                Test-Path $newpath  | Should Be $true
            }

            It "Build-Path is not a command" {
                !(Get-Command "Build-Path" -errorAction SilentlyContinue) | Should Be $false
            }
            It "Build-Path is an alias." {
                get-alias -Name Build-Path | Should Be $true
            }
            It "Build-Path works with a UNC path." {
                $pattern = "([a-zA-Z:]{2,2})*"
                $found =  $path -match $pattern
                if($found){
                 $replace = $Matches[0]
                 $givenPath = $path -replace $replace, "\\$($env:COMPUTERNAME)\$($replace.Replace(":", "`$" ))"
                                
                $testPath = Add-Path $givenPath
                $testPath | Should Be $givenPath                
                }                
            }
            BeforeEach {
                if(Test-Path $path){
                    $parentPath = (get-item $path).Parent.FullName
                    if(Test-Path $parentPath){
                        "it exists"
                        rd $parentPath -Force
                    }
                }
            }
            AfterEach {
                if(Test-Path $path){
                    $parentPath = (get-item $path).Parent.FullName
                    if(Test-Path $parentPath){
                        "it exists"
                        rd $parentPath -Force
                    }
                }
            }
        }
        Context "Test Get-Help"{
            It "Can get-help and see examples" {
                $(get-help Add-Path ).examples.example.Count -gt 0 | Should Be $true
            }
        }
    }
    Describe "Format-OrderedList" {
        Mock Read-Host { }
        Context "Mock Out-Host confirm it is called" {
            Mock Get-Service { $count = 0; while($count -lt 5 ){"" | select -property @{Expression={"Stopped"}; Label="Status"},@{Expression={"ServiceName$count"}; Label="Name"},@{Expression={"Service Diplay Name $count"}; Label="DisplayName"};                   $count++;}}
            Mock Out-Host {return 1;}
            It "It calls Get-Service returns without selecting an item " {
                Get-Service | Format-OrderedList #| Write-Verbose # | Should Be ""
                Assert-MockCalled Out-Host -Times 1 
            }
        }
        
        Context "Mock out-host return object" {
            Mock Get-Service { $count = 0; $returnObject = @(); while($count -lt 100 ){$item = "" | select -property @{Expression={"Stopped"}; Label="Status"},@{Expression={"ServiceName$count"}; Label="Name"},@{Expression={"Service Diplay Name $count"}; Label="DisplayName"}; $returnObject = $returnObject + $item; $count++;} return $returnObject;}
            
            Mock Out-Host { 
            begin{$object=@(); }
            process{
                $object = $object + $InputObject
            }
            end{
                return $object
            }
        }
            It "The object returned has the correct count " {
                $(Get-Service | Format-OrderedList).Count  | Should Be 100
            }
            It "The returned object is formatted correctly when property is provided " {
                $getService = Get-Service | Format-OrderedList -property Name
                $pattern = "[0-9]{1,} :[\t]{1}[\w\d ]{1,}[\t]{1}"
                $getService | select -First 1 | Should Match $pattern

            }
            It "The returned object is formatted correctly multiple properties" {
                $getService = Get-Service | Format-OrderedList -property Name,DisplayName
                $pattern = "[0-9]{1,} :[\t]{1}[\w\d ]{1,}[\t]{1}[\w\d ]{1,}[\t]{1}"
                $getService | select -First 1 | Should Match $pattern

            }
            It "The returned object is formatted correctly when property is not provided " {
                $getService = Get-Service | Format-OrderedList
                $pattern = "[0-9]{1,} :[\t]{1}[\w\d ]{1,}[\t]{1}"
                $getService | select -First 1 | Should Match $pattern
            }
            It "The returned array of items is formated with the Array Items" {
                $testArray = @("Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9")
                $pattern = "[0-9]{1,} :[\t]{1}Item[1-9]{1}"
                $testArray | Format-OrderedList| select -First 1 | Should Match $pattern
            }

        }
        Context "Test Get-Help"{
            It "Can get-help and see examples" {
                $(get-help Format-OrderedList ).examples.example.Count -gt 0 | Should Be $true
            }
        }
    
    }
    Describe "Limit-Job" {
        Context "Test Limit-Job"{    
            It "The method exists when calling Get-Command" {
                !(Get-Command "Limit-Job" -errorAction SilentlyContinue) | Should Be $false
            }  
            It "Can start multiple Jobs" {
                #Create a list of jobs to start
                $StartJob = @({start-job -ScriptBlock { sleep -Milliseconds 10 }},{start-job -ScriptBlock { sleep -Milliseconds 20 }},{start-job -ScriptBlock { sleep -Milliseconds 30 }}  )
                $job = Limit-Job -StartJob $StartJob
                $JobsCountAfter = get-job  
                $JobsCountAfter.Count | Should Be $StartJob.Count
            }              
            It "Can start multiple commands as Jobs" {
                #Create a list of jobs to start
                $StartJob = @({ sleep -Milliseconds 10 },{ sleep -Milliseconds 20 },{ sleep -Milliseconds 30 })
                $job = Limit-Job -StartJob $StartJob
                $JobsCountAfter = get-job  
                $JobsCountAfter.Count | Should Be $StartJob.Count
            }
            It "Can limit how many jobs running to 1 " {
                $StartJob = @({start-job -ScriptBlock { sleep -Milliseconds 101 }},{start-job -ScriptBlock { sleep -Milliseconds 1002 }},{start-job -ScriptBlock { sleep -Milliseconds 1003 }}  )
                $Limit = 1
                $job = Limit-Job -StartJob $StartJob -Limit $Limit
                $JobsCountAfter = get-job -State Running 
                $JobsCountAfter.Count | Should Be $Limit

            }
            It "Can limit how many jobs running to 2 " {
                $StartJob = @({start-job -ScriptBlock { sleep -Milliseconds 0101 }},{start-job -ScriptBlock { sleep -Milliseconds 5002 }},{start-job -ScriptBlock { sleep -Milliseconds 5003 }}  )
                $Limit = 2
                Limit-Job -StartJob $StartJob -Limit $Limit
            
                $(get-job -State Running).Count | Should Be $Limit

            }

            BeforeEach{
                #remove any jobs in this session
                get-job | Stop-Job 
                get-job | remove-job
            }
            AfterEach{
                #remove any jobs in this session
                get-job | Stop-Job 
                get-job | remove-job

            }
        }
        Context "Test Get-Help"{
            It "Can get-help and see examples" {
                $(get-help Limit-Job).examples.example.Count -gt 0 | Should Be $true
            }
        }

    }

}
Remove-Module "Powershell.Helper.Extension"